diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-22 23:21:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-22 23:21:55 +0800 |
commit | baed01f24753afb600a2984dcb9bcda0bb8502b6 (patch) | |
tree | 5621ef980b6b0067a21c86be7e4808d83c0538ab /routers | |
parent | c2ab19888f92fbdec4276a16d224e8de80d1d1dd (diff) | |
download | gitea-baed01f24753afb600a2984dcb9bcda0bb8502b6.tar.gz gitea-baed01f24753afb600a2984dcb9bcda0bb8502b6.zip |
Remove unnecessary attributes of User struct (#17745)
* Remove unnecessary functions of User struct
* Move more database methods out of user struct
* Move more database methods out of user struct
* Fix template failure
* Fix bug
* Remove finished FIXME
* remove unnecessary code
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/org/member.go | 7 | ||||
-rw-r--r-- | routers/api/v1/org/org.go | 3 | ||||
-rw-r--r-- | routers/api/v1/repo/pull.go | 6 | ||||
-rw-r--r-- | routers/api/v1/repo/star.go | 3 | ||||
-rw-r--r-- | routers/api/v1/user/follower.go | 8 | ||||
-rw-r--r-- | routers/api/v1/user/user.go | 2 | ||||
-rw-r--r-- | routers/web/admin/users.go | 12 | ||||
-rw-r--r-- | routers/web/org/home.go | 1 | ||||
-rw-r--r-- | routers/web/org/setting.go | 7 | ||||
-rw-r--r-- | routers/web/repo/branch.go | 3 | ||||
-rw-r--r-- | routers/web/repo/compare.go | 10 | ||||
-rw-r--r-- | routers/web/repo/issue.go | 2 | ||||
-rw-r--r-- | routers/web/repo/middlewares.go | 3 | ||||
-rw-r--r-- | routers/web/repo/pull.go | 18 | ||||
-rw-r--r-- | routers/web/repo/repo.go | 2 | ||||
-rw-r--r-- | routers/web/repo/view.go | 4 | ||||
-rw-r--r-- | routers/web/user/auth.go | 17 | ||||
-rw-r--r-- | routers/web/user/home.go | 6 | ||||
-rw-r--r-- | routers/web/user/profile.go | 16 | ||||
-rw-r--r-- | routers/web/user/setting/account.go | 5 | ||||
-rw-r--r-- | routers/web/user/setting/profile.go | 34 |
21 files changed, 99 insertions, 70 deletions
diff --git a/routers/api/v1/org/member.go b/routers/api/v1/org/member.go index 4530349f2c..d818321790 100644 --- a/routers/api/v1/org/member.go +++ b/routers/api/v1/org/member.go @@ -190,7 +190,12 @@ func IsPublicMember(ctx *context.APIContext) { if ctx.Written() { return } - if userToCheck.IsPublicMember(ctx.Org.Organization.ID) { + is, err := models.IsPublicMembership(ctx.Org.Organization.ID, userToCheck.ID) + if err != nil { + ctx.Error(http.StatusInternalServerError, "IsPublicMembership", err) + return + } + if is { ctx.Status(http.StatusNoContent) } else { ctx.NotFound() diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index f539662d62..952e29fba9 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -9,6 +9,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -343,7 +344,7 @@ func Edit(ctx *context.APIContext) { if form.RepoAdminChangeTeamAccess != nil { org.RepoAdminChangeTeamAccess = *form.RepoAdminChangeTeamAccess } - if err := models.UpdateUserCols(org.AsUser(), + if err := models.UpdateUserCols(db.DefaultContext, org.AsUser(), "full_name", "description", "website", "location", "visibility", "repo_admin_change_team_access", ); err != nil { diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 7f377edc3f..efe72e1502 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -953,10 +953,10 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) } // Check if current user has fork of repository or in the same repository. - headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID) - if !has && !isSameRepo { + headRepo := models.GetForkedRepo(headUser.ID, baseRepo.ID) + if headRepo == nil && !isSameRepo { log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID) - ctx.NotFound("HasForkedRepo") + ctx.NotFound("GetForkedRepo") return nil, nil, nil, nil, "", "" } diff --git a/routers/api/v1/repo/star.go b/routers/api/v1/repo/star.go index 5fa42c3244..b8a54a6bda 100644 --- a/routers/api/v1/repo/star.go +++ b/routers/api/v1/repo/star.go @@ -7,6 +7,7 @@ package repo import ( "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -43,7 +44,7 @@ func ListStargazers(ctx *context.APIContext) { // "200": // "$ref": "#/responses/UserList" - stargazers, err := ctx.Repo.Repository.GetStargazers(utils.GetListOptions(ctx)) + stargazers, err := models.GetStargazers(ctx.Repo.Repository, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetStargazers", err) return diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go index 5d66f3bcc3..edf3cea3ea 100644 --- a/routers/api/v1/user/follower.go +++ b/routers/api/v1/user/follower.go @@ -25,7 +25,7 @@ func responseAPIUsers(ctx *context.APIContext, users []*models.User) { } func listUserFollowers(ctx *context.APIContext, u *models.User) { - users, err := u.GetFollowers(utils.GetListOptions(ctx)) + users, err := models.GetUserFollowers(u, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err) return @@ -91,9 +91,9 @@ func ListFollowers(ctx *context.APIContext) { } func listUserFollowing(ctx *context.APIContext, u *models.User) { - users, err := u.GetFollowing(utils.GetListOptions(ctx)) + users, err := models.GetUserFollowing(u, utils.GetListOptions(ctx)) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetFollowing", err) + ctx.Error(http.StatusInternalServerError, "GetUserFollowing", err) return } @@ -157,7 +157,7 @@ func ListFollowing(ctx *context.APIContext) { } func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) { - if u.IsFollowing(followID) { + if user_model.IsFollowing(u.ID, followID) { ctx.Status(http.StatusNoContent) } else { ctx.NotFound() diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 535a49d766..8c57b1f099 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -103,7 +103,7 @@ func GetInfo(ctx *context.APIContext) { return } - if !u.IsVisibleToUser(ctx.User) { + if !models.IsUserVisibleToViewer(u, ctx.User) { // fake ErrUserNotExist error message to not leak information about existence ctx.NotFound("GetUserByName", models.ErrUserNotExist{Name: ctx.Params(":username")}) return diff --git a/routers/web/admin/users.go b/routers/web/admin/users.go index b23e4cf39b..077cf02f15 100644 --- a/routers/web/admin/users.go +++ b/routers/web/admin/users.go @@ -23,10 +23,10 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/web/explore" - router_user_setting "code.gitea.io/gitea/routers/web/user/setting" + user_setting "code.gitea.io/gitea/routers/web/user/setting" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/mailer" - "code.gitea.io/gitea/services/user" + user_service "code.gitea.io/gitea/services/user" ) const ( @@ -309,7 +309,7 @@ func EditUserPost(ctx *context.Context) { } if len(form.UserName) != 0 && u.Name != form.UserName { - if err := router_user_setting.HandleUsernameChange(ctx, u, form.UserName); err != nil { + if err := user_setting.HandleUsernameChange(ctx, u, form.UserName); err != nil { ctx.Redirect(setting.AppSubURL + "/admin/users") return } @@ -378,7 +378,7 @@ func DeleteUser(ctx *context.Context) { return } - if err = user.DeleteUser(u); err != nil { + if err = user_service.DeleteUser(u); err != nil { switch { case models.IsErrUserOwnRepos(err): ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo")) @@ -411,7 +411,7 @@ func AvatarPost(ctx *context.Context) { } form := web.GetForm(ctx).(*forms.AvatarForm) - if err := router_user_setting.UpdateAvatarSetting(ctx, form, u); err != nil { + if err := user_setting.UpdateAvatarSetting(ctx, form, u); err != nil { ctx.Flash.Error(err.Error()) } else { ctx.Flash.Success(ctx.Tr("settings.update_user_avatar_success")) @@ -427,7 +427,7 @@ func DeleteAvatar(ctx *context.Context) { return } - if err := u.DeleteAvatar(); err != nil { + if err := user_service.DeleteAvatar(u); err != nil { ctx.Flash.Error(err.Error()) } diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 0752fa12cb..6e81257135 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -141,7 +141,6 @@ func Home(ctx *context.Context) { ctx.Data["MembersTotal"] = membersCount ctx.Data["Members"] = members ctx.Data["Teams"] = ctx.Org.Teams - ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5) diff --git a/routers/web/org/setting.go b/routers/web/org/setting.go index f05dbd8bc1..7a6f4fec60 100644 --- a/routers/web/org/setting.go +++ b/routers/web/org/setting.go @@ -18,9 +18,10 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" - userSetting "code.gitea.io/gitea/routers/web/user/setting" + user_setting "code.gitea.io/gitea/routers/web/user/setting" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/org" + user_service "code.gitea.io/gitea/services/user" ) const ( @@ -136,7 +137,7 @@ func SettingsPost(ctx *context.Context) { func SettingsAvatar(ctx *context.Context) { form := web.GetForm(ctx).(*forms.AvatarForm) form.Source = forms.AvatarLocal - if err := userSetting.UpdateAvatarSetting(ctx, form, ctx.Org.Organization.AsUser()); err != nil { + if err := user_setting.UpdateAvatarSetting(ctx, form, ctx.Org.Organization.AsUser()); err != nil { ctx.Flash.Error(err.Error()) } else { ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success")) @@ -147,7 +148,7 @@ func SettingsAvatar(ctx *context.Context) { // SettingsDeleteAvatar response for delete avatar on settings page func SettingsDeleteAvatar(ctx *context.Context) { - if err := ctx.Org.Organization.AsUser().DeleteAvatar(); err != nil { + if err := user_service.DeleteAvatar(ctx.Org.Organization.AsUser()); err != nil { ctx.Flash.Error(err.Error()) } diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 27cd06023d..a6ad3eff5a 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -54,7 +54,8 @@ func Branches(ctx *context.Context) { ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls() ctx.Data["IsWriter"] = ctx.Repo.CanWrite(unit.TypeCode) ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror - ctx.Data["CanPull"] = ctx.Repo.CanWrite(unit.TypeCode) || (ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID)) + ctx.Data["CanPull"] = ctx.Repo.CanWrite(unit.TypeCode) || + (ctx.IsSigned && models.HasForkedRepo(ctx.User.ID, ctx.Repo.Repository.ID)) ctx.Data["PageIsViewCode"] = true ctx.Data["PageIsBranches"] = true diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index fdaf6fc6c2..706009820a 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -331,8 +331,8 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { // "OwnForkRepo" var ownForkRepo *models.Repository if ctx.User != nil && baseRepo.OwnerID != ctx.User.ID { - repo, has := models.HasForkedRepo(ctx.User.ID, baseRepo.ID) - if has { + repo := models.GetForkedRepo(ctx.User.ID, baseRepo.ID) + if repo != nil { ownForkRepo = repo ctx.Data["OwnForkRepo"] = ownForkRepo } @@ -355,12 +355,14 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { // 5. If the headOwner has a fork of the baseRepo - use that if !has { - ci.HeadRepo, has = models.HasForkedRepo(ci.HeadUser.ID, baseRepo.ID) + ci.HeadRepo = models.GetForkedRepo(ci.HeadUser.ID, baseRepo.ID) + has = ci.HeadRepo != nil } // 6. If the baseRepo is a fork and the headUser has a fork of that use that if !has && baseRepo.IsFork { - ci.HeadRepo, has = models.HasForkedRepo(ci.HeadUser.ID, baseRepo.ForkID) + ci.HeadRepo = models.GetForkedRepo(ci.HeadUser.ID, baseRepo.ForkID) + has = ci.HeadRepo != nil } // 7. Otherwise if we're not the same repo and haven't found a repo give up diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 6cc9419763..ac6240e282 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -106,7 +106,7 @@ func MustAllowPulls(ctx *context.Context) { } // User can send pull request if owns a forked repository. - if ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID) { + if ctx.IsSigned && models.HasForkedRepo(ctx.User.ID, ctx.Repo.Repository.ID) { ctx.Repo.PullRequest.Allowed = true ctx.Repo.PullRequest.HeadInfoSubURL = url.PathEscape(ctx.User.Name) + ":" + util.PathEscapeSegments(ctx.Repo.BranchName) } diff --git a/routers/web/repo/middlewares.go b/routers/web/repo/middlewares.go index a5d478dd73..0dc6e1cb72 100644 --- a/routers/web/repo/middlewares.go +++ b/routers/web/repo/middlewares.go @@ -7,6 +7,7 @@ package repo import ( "fmt" + "code.gitea.io/gitea/models" admin_model "code.gitea.io/gitea/models/admin" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" @@ -55,7 +56,7 @@ func SetDiffViewStyle(ctx *context.Context) { } ctx.Data["IsSplitStyle"] = style == "split" - if err := ctx.User.UpdateDiffViewStyle(style); err != nil { + if err := models.UpdateUserDiffViewStyle(ctx.User, style); err != nil { ctx.ServerError("ErrUpdateDiffViewStyle", err) } } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index a7afc3a05c..83e353833f 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -108,23 +108,23 @@ func getForkRepository(ctx *context.Context) *models.Repository { ctx.Data["repo_name"] = forkRepo.Name ctx.Data["description"] = forkRepo.Description ctx.Data["IsPrivate"] = forkRepo.IsPrivate || forkRepo.Owner.Visibility == structs.VisibleTypePrivate - canForkToUser := forkRepo.OwnerID != ctx.User.ID && !ctx.User.HasForkedRepo(forkRepo.ID) + canForkToUser := forkRepo.OwnerID != ctx.User.ID && !models.HasForkedRepo(ctx.User.ID, forkRepo.ID) ctx.Data["ForkRepo"] = forkRepo - if err := ctx.User.GetOwnedOrganizations(); err != nil { - ctx.ServerError("GetOwnedOrganizations", err) + ownedOrgs, err := models.GetOwnedOrgsByUserID(ctx.User.ID) + if err != nil { + ctx.ServerError("GetOwnedOrgsByUserID", err) return nil } - var orgs []*models.User - for _, org := range ctx.User.OwnedOrgs { - if forkRepo.OwnerID != org.ID && !org.HasForkedRepo(forkRepo.ID) { + var orgs []*models.Organization + for _, org := range ownedOrgs { + if forkRepo.OwnerID != org.ID && !models.HasForkedRepo(org.ID, forkRepo.ID) { orgs = append(orgs, org) } } var traverseParentRepo = forkRepo - var err error for { if ctx.User.ID == traverseParentRepo.OwnerID { canForkToUser = false @@ -200,8 +200,8 @@ func ForkPost(ctx *context.Context) { ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form) return } - repo, has := models.HasForkedRepo(ctxUser.ID, traverseParentRepo.ID) - if has { + repo := models.GetForkedRepo(ctxUser.ID, traverseParentRepo.ID) + if repo != nil { ctx.Redirect(ctxUser.HomeLink() + "/" + url.PathEscape(repo.Name)) return } diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index e8fdb99ff4..9463fc4c5f 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -63,7 +63,7 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User { } if !ctx.User.IsAdmin { - orgsAvailable := []*models.User{} + orgsAvailable := []*models.Organization{} for i := 0; i < len(orgs); i++ { if orgs[i].CanCreateRepo() { orgsAvailable = append(orgsAvailable, orgs[i]) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 72726f0545..1293882cc5 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -928,7 +928,9 @@ func Stars(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.stargazers") ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers") ctx.Data["PageIsStargazers"] = true - RenderUserCards(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers, tplWatchers) + RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*models.User, error) { + return models.GetStargazers(ctx.Repo.Repository, opts) + }, tplWatchers) } // Forks render repository's forked users diff --git a/routers/web/user/auth.go b/routers/web/user/auth.go index 1b1c70c8fe..c5164c4956 100644 --- a/routers/web/user/auth.go +++ b/routers/web/user/auth.go @@ -33,6 +33,7 @@ import ( "code.gitea.io/gitea/services/externalaccount" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/mailer" + user_service "code.gitea.io/gitea/services/user" "github.com/markbates/goth" "github.com/tstranex/u2f" @@ -564,7 +565,7 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR // If the user does not have a locale set, we save the current one. if len(u.Language) == 0 { u.Language = ctx.Locale.Language() - if err := models.UpdateUserCols(u, "language"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, u, "language"); err != nil { log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", u.ID, u.Language)) return setting.AppSubURL + "/" } @@ -581,7 +582,7 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR // Register last login u.SetLastLogin() - if err := models.UpdateUserCols(u, "last_login_unix"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, u, "last_login_unix"); err != nil { ctx.ServerError("UpdateUserCols", err) return setting.AppSubURL + "/" } @@ -736,7 +737,7 @@ func updateAvatarIfNeed(url string, u *models.User) { if err == nil && resp.StatusCode == http.StatusOK { data, err := io.ReadAll(io.LimitReader(resp.Body, setting.Avatar.MaxFileSize+1)) if err == nil && int64(len(data)) <= setting.Avatar.MaxFileSize { - _ = u.UploadAvatar(data) + _ = user_service.UploadAvatar(u, data) } } } @@ -773,7 +774,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *models.Us // Register last login u.SetLastLogin() - if err := models.UpdateUserCols(u, "last_login_unix"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, u, "last_login_unix"); err != nil { ctx.ServerError("UpdateUserCols", err) return } @@ -1345,7 +1346,7 @@ func handleUserCreated(ctx *context.Context, u *models.User, gothUser *goth.User u.IsAdmin = true u.IsActive = true u.SetLastLogin() - if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, u, "is_admin", "is_active", "last_login_unix"); err != nil { ctx.ServerError("UpdateUser", err) return } @@ -1466,7 +1467,7 @@ func handleAccountActivation(ctx *context.Context, user *models.User) { ctx.ServerError("UpdateUser", err) return } - if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, user, "is_active", "rands"); err != nil { if models.IsErrUserNotExist(err) { ctx.NotFound("UpdateUserCols", err) } else { @@ -1726,7 +1727,7 @@ func ResetPasswdPost(ctx *context.Context) { return } u.MustChangePassword = false - if err := models.UpdateUserCols(u, "must_change_password", "passwd", "passwd_hash_algo", "rands", "salt"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, u, "must_change_password", "passwd", "passwd_hash_algo", "rands", "salt"); err != nil { ctx.ServerError("UpdateUser", err) return } @@ -1802,7 +1803,7 @@ func MustChangePasswordPost(ctx *context.Context) { u.MustChangePassword = false - if err := models.UpdateUserCols(u, "must_change_password", "passwd", "passwd_hash_algo", "salt"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, u, "must_change_password", "passwd", "passwd_hash_algo", "salt"); err != nil { ctx.ServerError("UpdateUser", err) return } diff --git a/routers/web/user/home.go b/routers/web/user/home.go index c0ecd0c2a0..9efbe755d9 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -72,6 +72,8 @@ func Dashboard(ctx *context.Context) { ctx.Data["Title"] = ctxUser.DisplayName() + " - " + ctx.Tr("dashboard") ctx.Data["PageIsDashboard"] = true ctx.Data["PageIsNews"] = true + cnt, _ := models.GetOrganizationCount(db.DefaultContext, ctxUser) + ctx.Data["UserOrgsCount"] = cnt var uid int64 if ctxUser != nil { @@ -111,9 +113,9 @@ func Dashboard(ctx *context.Context) { return } } else { - mirrors, err = ctxUser.GetMirrorRepositories() + mirrors, err = models.GetUserMirrorRepositories(ctxUser.ID) if err != nil { - ctx.ServerError("GetMirrorRepositories", err) + ctx.ServerError("GetUserMirrorRepositories", err) return } } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 9d0b4e3c15..2594ba3013 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -107,7 +107,7 @@ func Profile(ctx *context.Context) { } // check view permissions - if !ctxUser.IsVisibleToUser(ctx.User) { + if !models.IsUserVisibleToViewer(ctxUser, ctx.User) { ctx.NotFound("user", fmt.Errorf(uname)) return } @@ -137,10 +137,16 @@ func Profile(ctx *context.Context) { return } + var isFollowing bool + if ctx.User != nil && ctxUser != nil { + isFollowing = user_model.IsFollowing(ctx.User.ID, ctxUser.ID) + } + ctx.Data["Title"] = ctxUser.DisplayName() ctx.Data["PageIsUserProfile"] = true ctx.Data["Owner"] = ctxUser ctx.Data["OpenIDs"] = openIDs + ctx.Data["IsFollowing"] = isFollowing if setting.Service.EnableUserHeatmap { data, err := models.GetUserHeatmapDataByUser(ctxUser, ctx.User) @@ -227,24 +233,24 @@ func Profile(ctx *context.Context) { ctx.Data["Keyword"] = keyword switch tab { case "followers": - items, err := ctxUser.GetFollowers(db.ListOptions{ + items, err := models.GetUserFollowers(ctxUser, db.ListOptions{ PageSize: setting.UI.User.RepoPagingNum, Page: page, }) if err != nil { - ctx.ServerError("GetFollowers", err) + ctx.ServerError("GetUserFollowers", err) return } ctx.Data["Cards"] = items total = ctxUser.NumFollowers case "following": - items, err := ctxUser.GetFollowing(db.ListOptions{ + items, err := models.GetUserFollowing(ctxUser, db.ListOptions{ PageSize: setting.UI.User.RepoPagingNum, Page: page, }) if err != nil { - ctx.ServerError("GetFollowing", err) + ctx.ServerError("GetUserFollowing", err) return } ctx.Data["Cards"] = items diff --git a/routers/web/user/setting/account.go b/routers/web/user/setting/account.go index 3362d3806d..603786697a 100644 --- a/routers/web/user/setting/account.go +++ b/routers/web/user/setting/account.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -74,7 +75,7 @@ func AccountPost(ctx *context.Context) { ctx.ServerError("UpdateUser", err) return } - if err := models.UpdateUserCols(ctx.User, "salt", "passwd_hash_algo", "passwd"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, ctx.User, "salt", "passwd_hash_algo", "passwd"); err != nil { ctx.ServerError("UpdateUser", err) return } @@ -158,7 +159,7 @@ func EmailPost(ctx *context.Context) { ctx.ServerError("SetEmailPreference", errors.New("option unrecognized")) return } - if err := ctx.User.SetEmailNotifications(preference); err != nil { + if err := models.SetEmailNotifications(ctx.User, preference); err != nil { log.Error("Set Email Notifications failed: %v", err) ctx.ServerError("SetEmailNotifications", err) return diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index 36fe45df04..1e515f2fd3 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -27,6 +27,7 @@ import ( "code.gitea.io/gitea/modules/web/middleware" "code.gitea.io/gitea/services/agit" "code.gitea.io/gitea/services/forms" + user_service "code.gitea.io/gitea/services/user" "github.com/unknwon/i18n" ) @@ -171,18 +172,18 @@ func UpdateAvatarSetting(ctx *context.Context, form *forms.AvatarForm, ctxUser * if !(st.IsImage() && !st.IsSvgImage()) { return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image")) } - if err = ctxUser.UploadAvatar(data); err != nil { + if err = user_service.UploadAvatar(ctxUser, data); err != nil { return fmt.Errorf("UploadAvatar: %v", err) } } else if ctxUser.UseCustomAvatar && ctxUser.Avatar == "" { // No avatar is uploaded but setting has been changed to enable, // generate a random one when needed. - if err := ctxUser.GenerateRandomAvatar(); err != nil { + if err := models.GenerateRandomAvatar(ctxUser); err != nil { log.Error("GenerateRandomAvatar[%d]: %v", ctxUser.ID, err) } } - if err := models.UpdateUserCols(ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil { + if err := models.UpdateUserCols(db.DefaultContext, ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil { return fmt.Errorf("UpdateUser: %v", err) } @@ -203,7 +204,7 @@ func AvatarPost(ctx *context.Context) { // DeleteAvatar render delete avatar page func DeleteAvatar(ctx *context.Context) { - if err := ctx.User.DeleteAvatar(); err != nil { + if err := user_service.DeleteAvatar(ctx.User); err != nil { ctx.Flash.Error(err.Error()) } @@ -301,11 +302,20 @@ func Repos(ctx *context.Context) { return } - if err := ctxUser.GetRepositories(db.ListOptions{Page: 1, PageSize: setting.UI.Admin.UserPagingNum}, repoNames...); err != nil { - ctx.ServerError("GetRepositories", err) + userRepos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{ + Actor: ctxUser, + Private: true, + ListOptions: db.ListOptions{ + Page: 1, + PageSize: setting.UI.Admin.UserPagingNum, + }, + LowerNames: repoNames, + }) + if err != nil { + ctx.ServerError("GetUserRepositories", err) return } - for _, repo := range ctxUser.Repos { + for _, repo := range userRepos { if repo.IsFork { if err := repo.GetBaseRepo(); err != nil { ctx.ServerError("GetBaseRepo", err) @@ -317,16 +327,12 @@ func Repos(ctx *context.Context) { ctx.Data["Dirs"] = repoNames ctx.Data["ReposMap"] = repos } else { - var err error - var count64 int64 - ctxUser.Repos, count64, err = models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: opts}) - + repos, count64, err := models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: opts}) if err != nil { - ctx.ServerError("GetRepositories", err) + ctx.ServerError("GetUserRepositories", err) return } count = int(count64) - repos := ctxUser.Repos for i := range repos { if repos[i].IsFork { @@ -371,7 +377,7 @@ func UpdateUIThemePost(ctx *context.Context) { return } - if err := ctx.User.UpdateTheme(form.Theme); err != nil { + if err := models.UpdateUserTheme(ctx.User, form.Theme); err != nil { ctx.Flash.Error(ctx.Tr("settings.theme_update_error")) ctx.Redirect(setting.AppSubURL + "/user/settings/appearance") return |