package user import ( "errors" "strconv" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/repository" ) const ( tplInvitation base.TplName = "user/settings/invite" ) func GetInvitaionCode(ctx *context.Context) { page := ctx.QueryInt("page") if page <= 0 { page = 1 } pageSize := ctx.QueryInt("pageSize") if pageSize <= 0 { pageSize = setting.UI.IssuePagingNum } url := setting.RecommentRepoAddr + "invitaion_page" result, err := repository.RecommendFromPromote(url) resultJsonMap := make(map[string]interface{}, 0) if err == nil { for _, strLine := range result { tmpIndex := strings.Index(strLine, "=") if tmpIndex != -1 { key := strLine[0:tmpIndex] value := strLine[tmpIndex+1:] resultJsonMap[key] = value } } } if ctx.IsSigned { resultJsonMap["invitation_code"] = getInvitaionCode(ctx) re, count := models.QueryInvitaionBySrcUserId(ctx.User.ID, (page-1)*pageSize, pageSize) for _, record := range re { tmpUser, err := models.GetUserByID(record.UserID) if err == nil { record.Avatar = strings.TrimRight(setting.AppSubURL, "/") + "/user/avatar/" + tmpUser.Name + "/" + strconv.Itoa(-1) record.IsActive = tmpUser.IsActive record.Name = tmpUser.Name } } resultJsonMap["invitation_users"] = re resultJsonMap["invitation_users_count"] = count } ctx.JSON(200, resultJsonMap) } func InviationTpl(ctx *context.Context) { ctx.HTML(200, tplInvitation) } func RegisteUserByInvitaionCode(invitationcode string, newUserId int64, newPhoneNumber string, email string) error { user := parseInvitaionCode(invitationcode) if user == nil { return errors.New("The invitated user not existed.") } if newPhoneNumber != "" { re := models.QueryInvitaionByPhone(newPhoneNumber) if re != nil { if len(re) > 0 { log.Info("The phone has been invitated. so ingore it.") return errors.New("The phone has been invitated.") } } } else { log.Info("the phone number is null. user name=" + user.Name) } invitation := &models.Invitation{ SrcUserID: user.ID, UserID: newUserId, Phone: newPhoneNumber, Email: email, } err := models.InsertInvitaion(invitation) if err != nil { log.Info("insert error," + err.Error()) } return err } func getInvitaionCode(ctx *context.Context) string { return ctx.User.Name } func parseInvitaionCode(invitationcode string) *models.User { user, err := models.GetUserByName(invitationcode) if err == nil { return user } return nil }