From d34f97ad7e2236b9d4f8800dfed2173c5d492fa9 Mon Sep 17 00:00:00 2001 From: Bwko Date: Sun, 23 Jul 2017 21:52:35 +0200 Subject: [PATCH 1/3] Add collaborative repositories to the dashboard Remove some unused code from the Dashboard func --- models/action.go | 24 ++++++++++++------ models/repo_list.go | 20 +++++++++------ routers/api/v1/repo/repo.go | 6 ++++- routers/user/home.go | 50 ++++++++----------------------------- routers/user/profile.go | 7 +++++- 5 files changed, 51 insertions(+), 56 deletions(-) diff --git a/models/action.go b/models/action.go index 852bb66d28162..ad673f5d32b38 100644 --- a/models/action.go +++ b/models/action.go @@ -15,6 +15,7 @@ import ( "unicode" "github.com/Unknwon/com" + "github.com/go-xorm/builder" "github.com/go-xorm/xorm" "code.gitea.io/git" @@ -712,6 +713,7 @@ type GetFeedsOptions struct { IncludePrivate bool // include private actions OnlyPerformedBy bool // only actions performed by requested user IncludeDeleted bool // include deleted actions + Collaborate bool // Include collaborative repositories } // GetFeeds returns actions according to the provided options @@ -728,23 +730,31 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) { } actions := make([]*Action, 0, 20) + cond := builder.NewCond() sess := x.Limit(20). - Desc("id"). - Where("user_id = ?", opts.RequestedUser.ID) + Desc("id") + + if opts.Collaborate { + cond = builder.Eq{"user_id": opts.RequestedUser.ID}.Or( + builder.Expr(`repo_id IN (SELECT repo_id FROM "access" WHERE access.user_id = ?)`, opts.RequestedUser.ID)) + } else { + cond = builder.Eq{"user_id": opts.RequestedUser.ID} + } + if opts.OnlyPerformedBy { - sess.And("act_user_id = ?", opts.RequestedUser.ID) + cond = cond.And(builder.Eq{"act_user_id": opts.RequestedUser.ID}) } if !opts.IncludePrivate { - sess.And("is_private = ?", false) + cond = cond.And(builder.Eq{"is_private": false}) } if opts.RequestedUser.IsOrganization() { - sess.In("repo_id", repoIDs) + cond = cond.And(builder.In("repo_id", repoIDs)) } if !opts.IncludeDeleted { - sess.And("is_deleted = ?", false) + cond = cond.And(builder.Eq{"is_deleted": false}) } - return actions, sess.Find(&actions) + return actions, sess.Where(cond).Find(&actions) } diff --git a/models/repo_list.go b/models/repo_list.go index 2158cfe676a54..c29b2026b93cd 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -98,13 +98,14 @@ type SearchRepoOptions struct { // Owner in we search search // // in: query - OwnerID int64 `json:"uid"` - Searcher *User `json:"-"` //ID of the person who's seeking - OrderBy string `json:"-"` - Private bool `json:"-"` // Include private repositories in results - Starred bool `json:"-"` - Page int `json:"-"` - IsProfile bool `json:"-"` + OwnerID int64 `json:"uid"` + Searcher *User `json:"-"` //ID of the person who's seeking + OrderBy string `json:"-"` + Private bool `json:"-"` // Include private repositories in results + Collaborate bool `json:"-"` // Include collaborative repositories + Starred bool `json:"-"` + Page int `json:"-"` + IsProfile bool `json:"-"` // Limit of result // // maximum: setting.ExplorePagingNum @@ -158,6 +159,11 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun } cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds))) + + if opts.Collaborate { + cond = cond.Or(builder.Expr(`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)`, opts.Searcher.ID, opts.Searcher.ID), + builder.And(builder.Like{"lower_name", opts.Keyword}, builder.Eq{"is_private": opts.Private})) + } } if len(opts.OrderBy) == 0 { diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index e8bf026511e03..305daa064df4c 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -42,6 +42,7 @@ func Search(ctx *context.APIContext) { if ctx.IsSigned && opts.OwnerID > 0 { if ctx.User.ID == opts.OwnerID { opts.Private = true + opts.Collaborate = true } else { u, err := models.GetUserByID(opts.OwnerID) if err != nil { @@ -54,7 +55,10 @@ func Search(ctx *context.APIContext) { if u.IsOrganization() && u.IsOwnedBy(ctx.User.ID) { opts.Private = true } - // FIXME: how about collaborators? + + if !u.IsOrganization() { + opts.Collaborate = true + } } } diff --git a/routers/user/home.go b/routers/user/home.go index 43f4087fe5a69..409465c0b4642 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -54,24 +54,14 @@ func getDashboardContextUser(ctx *context.Context) *models.User { } // retrieveFeeds loads feeds for the specified user -func retrieveFeeds(ctx *context.Context, user *models.User, includePrivate, isProfile bool, includeDeletedComments bool) { - var requestingID int64 - if ctx.User != nil { - requestingID = ctx.User.ID - } - actions, err := models.GetFeeds(models.GetFeedsOptions{ - RequestedUser: user, - RequestingUserID: requestingID, - IncludePrivate: includePrivate, - OnlyPerformedBy: isProfile, - IncludeDeleted: includeDeletedComments, - }) +func retrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) { + actions, err := models.GetFeeds(options) if err != nil { ctx.Handle(500, "GetFeeds", err) return } - userCache := map[int64]*models.User{user.ID: user} + userCache := map[int64]*models.User{options.RequestedUser.ID: options.RequestedUser} if ctx.User != nil { userCache[ctx.User.ID] = ctx.User } @@ -133,32 +123,14 @@ func Dashboard(ctx *context.Context) { ctx.Data["PageIsNews"] = true ctx.Data["SearchLimit"] = setting.UI.User.RepoPagingNum - // Only user can have collaborative repositories. - if !ctxUser.IsOrganization() { - collaborateRepos, err := ctx.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum) - if err != nil { - ctx.Handle(500, "GetAccessibleRepositories", err) - return - } else if err = models.RepositoryList(collaborateRepos).LoadAttributes(); err != nil { - ctx.Handle(500, "RepositoryList.LoadAttributes", err) - return - } - ctx.Data["CollaborativeRepos"] = collaborateRepos - } - var err error - var repos, mirrors []*models.Repository + var mirrors []*models.Repository if ctxUser.IsOrganization() { env, err := ctxUser.AccessibleReposEnv(ctx.User.ID) if err != nil { ctx.Handle(500, "AccessibleReposEnv", err) return } - repos, err = env.Repos(1, setting.UI.User.RepoPagingNum) - if err != nil { - ctx.Handle(500, "env.Repos", err) - return - } mirrors, err = env.MirrorRepos() if err != nil { @@ -166,19 +138,12 @@ func Dashboard(ctx *context.Context) { return } } else { - if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil { - ctx.Handle(500, "GetRepositories", err) - return - } - repos = ctxUser.Repos - mirrors, err = ctxUser.GetMirrorRepositories() if err != nil { ctx.Handle(500, "GetMirrorRepositories", err) return } } - ctx.Data["Repos"] = repos ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil { @@ -188,7 +153,12 @@ func Dashboard(ctx *context.Context) { ctx.Data["MirrorCount"] = len(mirrors) ctx.Data["Mirrors"] = mirrors - retrieveFeeds(ctx, ctxUser, true, false, false) + retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser, + IncludePrivate: true, + OnlyPerformedBy: false, + Collaborate: true, + IncludeDeleted: false, + }) if ctx.Written() { return } diff --git a/routers/user/profile.go b/routers/user/profile.go index 1850286411dcb..f56ddc4eab57c 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -138,7 +138,12 @@ func Profile(ctx *context.Context) { ctx.Data["Keyword"] = keyword switch tab { case "activity": - retrieveFeeds(ctx, ctxUser, showPrivate, true, false) + retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser, + IncludePrivate: showPrivate, + OnlyPerformedBy: true, + Collaborate: true, + IncludeDeleted: false, + }) if ctx.Written() { return } From bdac5e0e14a8e73742a84f56a89585b462820d0f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 22 Aug 2017 22:18:54 +0800 Subject: [PATCH 2/3] fix some bug and some refactor --- models/action.go | 16 ++++++---------- models/repo_list.go | 42 ++++++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/models/action.go b/models/action.go index ad673f5d32b38..595be92f7661a 100644 --- a/models/action.go +++ b/models/action.go @@ -718,6 +718,8 @@ type GetFeedsOptions struct { // GetFeeds returns actions according to the provided options func GetFeeds(opts GetFeedsOptions) ([]*Action, error) { + cond := builder.NewCond() + var repoIDs []int64 if opts.RequestedUser.IsOrganization() { env, err := opts.RequestedUser.AccessibleReposEnv(opts.RequestingUserID) @@ -727,12 +729,9 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) { if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil { return nil, fmt.Errorf("GetUserRepositories: %v", err) } - } - actions := make([]*Action, 0, 20) - cond := builder.NewCond() - sess := x.Limit(20). - Desc("id") + cond = cond.And(builder.In("repo_id", repoIDs)) + } if opts.Collaborate { cond = builder.Eq{"user_id": opts.RequestedUser.ID}.Or( @@ -747,14 +746,11 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) { if !opts.IncludePrivate { cond = cond.And(builder.Eq{"is_private": false}) } - if opts.RequestedUser.IsOrganization() { - cond = cond.And(builder.In("repo_id", repoIDs)) - } if !opts.IncludeDeleted { cond = cond.And(builder.Eq{"is_deleted": false}) - } - return actions, sess.Where(cond).Find(&actions) + actions := make([]*Action, 0, 20) + return actions, x.Limit(20).Desc("id").Where(cond).Find(&actions) } diff --git a/models/repo_list.go b/models/repo_list.go index c29b2026b93cd..905408ae6b13b 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/go-xorm/builder" - "github.com/go-xorm/xorm" ) // RepositoryList contains a list of repositories @@ -116,25 +115,21 @@ type SearchRepoOptions struct { // SearchRepositoryByName takes keyword and part of repository name to search, // it returns results in given range and number of total results. func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) { - var ( - sess *xorm.Session - cond = builder.NewCond() - ) - - opts.Keyword = strings.ToLower(opts.Keyword) - + var cond = builder.NewCond() if opts.Page <= 0 { opts.Page = 1 } - repos = make([]*Repository, 0, opts.PageSize) - if opts.Starred && opts.OwnerID > 0 { cond = builder.Eq{ "star.uid": opts.OwnerID, } } - cond = cond.And(builder.Like{"lower_name", opts.Keyword}) + + opts.Keyword = strings.ToLower(opts.Keyword) + if opts.Keyword != "" { + cond = cond.And(builder.Like{"lower_name", opts.Keyword}) + } // Append conditions if !opts.Starred && opts.OwnerID > 0 { @@ -158,32 +153,33 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun ownerIds = append(ownerIds, org.ID) } - cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds))) - + searcherReposCond := builder.In("owner_id", ownerIds) if opts.Collaborate { - cond = cond.Or(builder.Expr(`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)`, opts.Searcher.ID, opts.Searcher.ID), - builder.And(builder.Like{"lower_name", opts.Keyword}, builder.Eq{"is_private": opts.Private})) + searcherReposCond = searcherReposCond.Or(builder.Expr(`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)`, + opts.Searcher.ID, opts.Searcher.ID)) } + cond = cond.And(searcherReposCond) } if len(opts.OrderBy) == 0 { opts.OrderBy = "name ASC" } + sess := x.NewSession() + defer sess.Close() + if opts.Starred && opts.OwnerID > 0 { - sess = x. - Join("INNER", "star", "star.repo_id = repository.id"). - Where(cond) - count, err = x. + count, err = sess. Join("INNER", "star", "star.repo_id = repository.id"). Where(cond). Count(new(Repository)) if err != nil { return nil, 0, fmt.Errorf("Count: %v", err) } + + sess.Join("INNER", "star", "star.repo_id = repository.id") } else { - sess = x.Where(cond) - count, err = x. + count, err = sess. Where(cond). Count(new(Repository)) if err != nil { @@ -191,7 +187,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun } } + repos = make([]*Repository, 0, opts.PageSize) if err = sess. + Where(cond). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). OrderBy(opts.OrderBy). Find(&repos); err != nil { @@ -199,7 +197,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun } if !opts.IsProfile { - if err = repos.loadAttributes(x); err != nil { + if err = repos.loadAttributes(sess); err != nil { return nil, 0, fmt.Errorf("LoadAttributes: %v", err) } } From 1b71b48f7e3e4abc1c8624d1eba77ca83cc833e3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 22 Aug 2017 23:26:40 +0800 Subject: [PATCH 3/3] fix tests --- models/repo_list_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/models/repo_list_test.go b/models/repo_list_test.go index 1fbfa935e9e41..9c8a526a43e33 100644 --- a/models/repo_list_test.go +++ b/models/repo_list_test.go @@ -42,6 +42,7 @@ func TestSearchRepositoryByName(t *testing.T) { Keyword: "repo_13", Page: 1, PageSize: 10, + Private: true, Searcher: &User{ID: 14}, }) @@ -54,6 +55,7 @@ func TestSearchRepositoryByName(t *testing.T) { Keyword: "test_repo", Page: 1, PageSize: 10, + Private: true, Searcher: &User{ID: 14}, })