diff --git a/models/access.go b/models/access.go index 500bcc1e19094..7af88e1b4a1e7 100644 --- a/models/access.go +++ b/models/access.go @@ -105,66 +105,6 @@ func accessLevel(e db.Engine, user *User, repo *Repository) (AccessMode, error) return a.Mode, nil } -type repoAccess struct { - Access `xorm:"extends"` - Repository `xorm:"extends"` -} - -func (repoAccess) TableName() string { - return "access" -} - -// GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own. -func (user *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) { - rows, err := db.GetEngine(db.DefaultContext). - Join("INNER", "repository", "repository.id = access.repo_id"). - Where("access.user_id = ?", user.ID). - And("repository.owner_id <> ?", user.ID). - Rows(new(repoAccess)) - if err != nil { - return nil, err - } - defer rows.Close() - - repos := make(map[*Repository]AccessMode, 10) - ownerCache := make(map[int64]*User, 10) - for rows.Next() { - var repo repoAccess - err = rows.Scan(&repo) - if err != nil { - return nil, err - } - - var ok bool - if repo.Owner, ok = ownerCache[repo.OwnerID]; !ok { - if err = repo.GetOwner(); err != nil { - return nil, err - } - ownerCache[repo.OwnerID] = repo.Owner - } - - repos[&repo.Repository] = repo.Access.Mode - } - return repos, nil -} - -// GetAccessibleRepositories finds repositories which the user has access but does not own. -// If limit is smaller than 1 means returns all found results. -func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) { - sess := db.GetEngine(db.DefaultContext). - Where("owner_id !=? ", user.ID). - Desc("updated_unix") - if limit > 0 { - sess.Limit(limit) - repos = make([]*Repository, 0, limit) - } else { - repos = make([]*Repository, 0, 10) - } - return repos, sess. - Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID). - Find(&repos) -} - func maxAccessMode(modes ...AccessMode) AccessMode { max := AccessModeNone for _, mode := range modes { diff --git a/models/access_test.go b/models/access_test.go index 942ca4af42864..81e36ed2e0242 100644 --- a/models/access_test.go +++ b/models/access_test.go @@ -90,39 +90,6 @@ func TestHasAccess(t *testing.T) { assert.NoError(t, err) } -func TestUser_GetRepositoryAccesses(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - - user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - accesses, err := user1.GetRepositoryAccesses() - assert.NoError(t, err) - assert.Len(t, accesses, 0) - - user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) - accesses, err = user29.GetRepositoryAccesses() - assert.NoError(t, err) - assert.Len(t, accesses, 2) -} - -func TestUser_GetAccessibleRepositories(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - - user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - repos, err := user1.GetAccessibleRepositories(0) - assert.NoError(t, err) - assert.Len(t, repos, 0) - - user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - repos, err = user2.GetAccessibleRepositories(0) - assert.NoError(t, err) - assert.Len(t, repos, 4) - - user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) - repos, err = user29.GetAccessibleRepositories(0) - assert.NoError(t, err) - assert.Len(t, repos, 2) -} - func TestRepository_RecalculateAccesses(t *testing.T) { // test with organization repo assert.NoError(t, unittest.PrepareTestDatabase()) diff --git a/models/star.go b/models/star.go index baf724d7e08f7..c0b15be21a0ad 100644 --- a/models/star.go +++ b/models/star.go @@ -74,7 +74,7 @@ func isStaring(e db.Engine, userID, repoID int64) bool { } // GetStargazers returns the users that starred the repo. -func (repo *Repository) GetStargazers(opts db.ListOptions) ([]*User, error) { +func GetStargazers(repo *Repository, opts db.ListOptions) ([]*User, error) { sess := db.GetEngine(db.DefaultContext).Where("star.repo_id = ?", repo.ID). Join("LEFT", "star", "`user`.id = star.uid") if opts.Page > 0 { @@ -87,48 +87,3 @@ func (repo *Repository) GetStargazers(opts db.ListOptions) ([]*User, error) { users := make([]*User, 0, 8) return users, sess.Find(&users) } - -// GetStarredRepos returns the repos the user starred. -func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos RepositoryList, err error) { - if len(orderBy) == 0 { - orderBy = "updated_unix DESC" - } - sess := db.GetEngine(db.DefaultContext). - Join("INNER", "star", "star.repo_id = repository.id"). - Where("star.uid = ?", u.ID). - OrderBy(orderBy) - - if !private { - sess = sess.And("is_private = ?", false) - } - - if page <= 0 { - page = 1 - } - sess.Limit(pageSize, (page-1)*pageSize) - - repos = make([]*Repository, 0, pageSize) - - if err = sess.Find(&repos); err != nil { - return - } - - if err = repos.loadAttributes(db.GetEngine(db.DefaultContext)); err != nil { - return - } - - return -} - -// GetStarredRepoCount returns the numbers of repo the user starred. -func (u *User) GetStarredRepoCount(private bool) (int64, error) { - sess := db.GetEngine(db.DefaultContext). - Join("INNER", "star", "star.repo_id = repository.id"). - Where("star.uid = ?", u.ID) - - if !private { - sess = sess.And("is_private = ?", false) - } - - return sess.Count(&Repository{}) -} diff --git a/models/star_test.go b/models/star_test.go index f9a7ddb0f2af4..eef1a0c5fb9f8 100644 --- a/models/star_test.go +++ b/models/star_test.go @@ -36,7 +36,7 @@ func TestRepository_GetStargazers(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) - gazers, err := repo.GetStargazers(db.ListOptions{Page: 0}) + gazers, err := GetStargazers(repo, db.ListOptions{Page: 0}) assert.NoError(t, err) if assert.Len(t, gazers, 1) { assert.Equal(t, int64(2), gazers[0].ID) @@ -47,53 +47,7 @@ func TestRepository_GetStargazers2(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) - gazers, err := repo.GetStargazers(db.ListOptions{Page: 0}) + gazers, err := GetStargazers(repo, db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0) } - -func TestUser_GetStarredRepos(t *testing.T) { - // user who has starred repos - assert.NoError(t, unittest.PrepareTestDatabase()) - - user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - starred, err := user.GetStarredRepos(false, 1, 10, "") - assert.NoError(t, err) - if assert.Len(t, starred, 1) { - assert.Equal(t, int64(4), starred[0].ID) - } - - starred, err = user.GetStarredRepos(true, 1, 10, "") - assert.NoError(t, err) - if assert.Len(t, starred, 2) { - assert.Equal(t, int64(2), starred[0].ID) - assert.Equal(t, int64(4), starred[1].ID) - } -} - -func TestUser_GetStarredRepos2(t *testing.T) { - // user who has no starred repos - assert.NoError(t, unittest.PrepareTestDatabase()) - - user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - starred, err := user.GetStarredRepos(false, 1, 10, "") - assert.NoError(t, err) - assert.Len(t, starred, 0) - - starred, err = user.GetStarredRepos(true, 1, 10, "") - assert.NoError(t, err) - assert.Len(t, starred, 0) -} - -func TestUserGetStarredRepoCount(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - - user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - counts, err := user.GetStarredRepoCount(false) - assert.NoError(t, err) - assert.Equal(t, int64(1), counts) - - counts, err = user.GetStarredRepoCount(true) - assert.NoError(t, err) - assert.Equal(t, int64(2), counts) -} diff --git a/models/user.go b/models/user.go index c32bebf3b79ab..4126606e2405f 100644 --- a/models/user.go +++ b/models/user.go @@ -217,14 +217,14 @@ func (u *User) SetLastLogin() { u.LastLoginUnix = timeutil.TimeStampNow() } -// UpdateDiffViewStyle updates the users diff view style -func (u *User) UpdateDiffViewStyle(style string) error { +// UpdateUserDiffViewStyle updates the users diff view style +func UpdateUserDiffViewStyle(u *User, style string) error { u.DiffViewStyle = style return UpdateUserCols(db.DefaultContext, u, "diff_view_style") } -// UpdateTheme updates a users' theme irrespective of the site wide theme -func (u *User) UpdateTheme(themeName string) error { +// UpdateUserTheme updates a users' theme irrespective of the site wide theme +func UpdateUserTheme(u *User, themeName string) error { u.Theme = themeName return UpdateUserCols(db.DefaultContext, u, "theme") } diff --git a/routers/api/v1/repo/star.go b/routers/api/v1/repo/star.go index 5fa42c3244e42..b8a54a6bdaf44 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/web/repo/middlewares.go b/routers/web/repo/middlewares.go index a5d478dd73b3f..0dc6e1cb7204e 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/view.go b/routers/web/repo/view.go index 72726f0545f02..1293882cc5f16 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/setting/profile.go b/routers/web/user/setting/profile.go index e0277e1256d3e..1e515f2fd3119 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -377,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