Skip to content

Commit

Permalink
remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Nov 22, 2021
1 parent f378426 commit d0d38f0
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 195 deletions.
60 changes: 0 additions & 60 deletions models/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 0 additions & 33 deletions models/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
47 changes: 1 addition & 46 deletions models/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{})
}
50 changes: 2 additions & 48 deletions models/star_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
8 changes: 4 additions & 4 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion routers/api/v1/repo/star.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion routers/web/repo/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
Expand Down
4 changes: 3 additions & 1 deletion routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion routers/web/user/setting/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d0d38f0

Please sign in to comment.