Add collaborative repositories to the dashboard (#2205)

* Add collaborative repositories to the dashboard

Remove some unused code from the Dashboard func

* fix some bug and some refactor

* fix tests
This commit is contained in:
Bwko 2017-08-23 03:30:54 +02:00 committed by Lunny Xiao
parent faf4b503b2
commit 1a5fe4326f
6 changed files with 71 additions and 80 deletions

View File

@ -15,6 +15,7 @@ import (
"unicode" "unicode"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"code.gitea.io/git" "code.gitea.io/git"
@ -712,10 +713,13 @@ type GetFeedsOptions struct {
IncludePrivate bool // include private actions IncludePrivate bool // include private actions
OnlyPerformedBy bool // only actions performed by requested user OnlyPerformedBy bool // only actions performed by requested user
IncludeDeleted bool // include deleted actions IncludeDeleted bool // include deleted actions
Collaborate bool // Include collaborative repositories
} }
// GetFeeds returns actions according to the provided options // GetFeeds returns actions according to the provided options
func GetFeeds(opts GetFeedsOptions) ([]*Action, error) { func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
cond := builder.NewCond()
var repoIDs []int64 var repoIDs []int64
if opts.RequestedUser.IsOrganization() { if opts.RequestedUser.IsOrganization() {
env, err := opts.RequestedUser.AccessibleReposEnv(opts.RequestingUserID) env, err := opts.RequestedUser.AccessibleReposEnv(opts.RequestingUserID)
@ -725,26 +729,28 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil { if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil {
return nil, fmt.Errorf("GetUserRepositories: %v", err) return nil, fmt.Errorf("GetUserRepositories: %v", err)
} }
cond = cond.And(builder.In("repo_id", repoIDs))
}
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}
} }
actions := make([]*Action, 0, 20)
sess := x.Limit(20).
Desc("id").
Where("user_id = ?", opts.RequestedUser.ID)
if opts.OnlyPerformedBy { 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 { 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)
} }
if !opts.IncludeDeleted { if !opts.IncludeDeleted {
sess.And("is_deleted = ?", false) cond = cond.And(builder.Eq{"is_deleted": false})
} }
return actions, sess.Find(&actions) actions := make([]*Action, 0, 20)
return actions, x.Limit(20).Desc("id").Where(cond).Find(&actions)
} }

View File

@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/go-xorm/builder" "github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
) )
// RepositoryList contains a list of repositories // RepositoryList contains a list of repositories
@ -98,13 +97,14 @@ type SearchRepoOptions struct {
// Owner in we search search // Owner in we search search
// //
// in: query // in: query
OwnerID int64 `json:"uid"` OwnerID int64 `json:"uid"`
Searcher *User `json:"-"` //ID of the person who's seeking Searcher *User `json:"-"` //ID of the person who's seeking
OrderBy string `json:"-"` OrderBy string `json:"-"`
Private bool `json:"-"` // Include private repositories in results Private bool `json:"-"` // Include private repositories in results
Starred bool `json:"-"` Collaborate bool `json:"-"` // Include collaborative repositories
Page int `json:"-"` Starred bool `json:"-"`
IsProfile bool `json:"-"` Page int `json:"-"`
IsProfile bool `json:"-"`
// Limit of result // Limit of result
// //
// maximum: setting.ExplorePagingNum // maximum: setting.ExplorePagingNum
@ -115,25 +115,21 @@ type SearchRepoOptions struct {
// SearchRepositoryByName takes keyword and part of repository name to search, // SearchRepositoryByName takes keyword and part of repository name to search,
// it returns results in given range and number of total results. // it returns results in given range and number of total results.
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) { func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
var ( var cond = builder.NewCond()
sess *xorm.Session
cond = builder.NewCond()
)
opts.Keyword = strings.ToLower(opts.Keyword)
if opts.Page <= 0 { if opts.Page <= 0 {
opts.Page = 1 opts.Page = 1
} }
repos = make([]*Repository, 0, opts.PageSize)
if opts.Starred && opts.OwnerID > 0 { if opts.Starred && opts.OwnerID > 0 {
cond = builder.Eq{ cond = builder.Eq{
"star.uid": opts.OwnerID, "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 // Append conditions
if !opts.Starred && opts.OwnerID > 0 { if !opts.Starred && opts.OwnerID > 0 {
@ -157,27 +153,33 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
ownerIds = append(ownerIds, org.ID) 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 {
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 { if len(opts.OrderBy) == 0 {
opts.OrderBy = "name ASC" opts.OrderBy = "name ASC"
} }
sess := x.NewSession()
defer sess.Close()
if opts.Starred && opts.OwnerID > 0 { if opts.Starred && opts.OwnerID > 0 {
sess = x. count, err = sess.
Join("INNER", "star", "star.repo_id = repository.id").
Where(cond)
count, err = x.
Join("INNER", "star", "star.repo_id = repository.id"). Join("INNER", "star", "star.repo_id = repository.id").
Where(cond). Where(cond).
Count(new(Repository)) Count(new(Repository))
if err != nil { if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err) return nil, 0, fmt.Errorf("Count: %v", err)
} }
sess.Join("INNER", "star", "star.repo_id = repository.id")
} else { } else {
sess = x.Where(cond) count, err = sess.
count, err = x.
Where(cond). Where(cond).
Count(new(Repository)) Count(new(Repository))
if err != nil { if err != nil {
@ -185,7 +187,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
} }
} }
repos = make([]*Repository, 0, opts.PageSize)
if err = sess. if err = sess.
Where(cond).
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
OrderBy(opts.OrderBy). OrderBy(opts.OrderBy).
Find(&repos); err != nil { Find(&repos); err != nil {
@ -193,7 +197,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
} }
if !opts.IsProfile { 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) return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
} }
} }

View File

@ -42,6 +42,7 @@ func TestSearchRepositoryByName(t *testing.T) {
Keyword: "repo_13", Keyword: "repo_13",
Page: 1, Page: 1,
PageSize: 10, PageSize: 10,
Private: true,
Searcher: &User{ID: 14}, Searcher: &User{ID: 14},
}) })
@ -54,6 +55,7 @@ func TestSearchRepositoryByName(t *testing.T) {
Keyword: "test_repo", Keyword: "test_repo",
Page: 1, Page: 1,
PageSize: 10, PageSize: 10,
Private: true,
Searcher: &User{ID: 14}, Searcher: &User{ID: 14},
}) })

View File

@ -42,6 +42,7 @@ func Search(ctx *context.APIContext) {
if ctx.IsSigned && opts.OwnerID > 0 { if ctx.IsSigned && opts.OwnerID > 0 {
if ctx.User.ID == opts.OwnerID { if ctx.User.ID == opts.OwnerID {
opts.Private = true opts.Private = true
opts.Collaborate = true
} else { } else {
u, err := models.GetUserByID(opts.OwnerID) u, err := models.GetUserByID(opts.OwnerID)
if err != nil { if err != nil {
@ -54,7 +55,10 @@ func Search(ctx *context.APIContext) {
if u.IsOrganization() && u.IsOwnedBy(ctx.User.ID) { if u.IsOrganization() && u.IsOwnedBy(ctx.User.ID) {
opts.Private = true opts.Private = true
} }
// FIXME: how about collaborators?
if !u.IsOrganization() {
opts.Collaborate = true
}
} }
} }

View File

@ -54,24 +54,14 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
} }
// retrieveFeeds loads feeds for the specified user // retrieveFeeds loads feeds for the specified user
func retrieveFeeds(ctx *context.Context, user *models.User, includePrivate, isProfile bool, includeDeletedComments bool) { func retrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) {
var requestingID int64 actions, err := models.GetFeeds(options)
if ctx.User != nil {
requestingID = ctx.User.ID
}
actions, err := models.GetFeeds(models.GetFeedsOptions{
RequestedUser: user,
RequestingUserID: requestingID,
IncludePrivate: includePrivate,
OnlyPerformedBy: isProfile,
IncludeDeleted: includeDeletedComments,
})
if err != nil { if err != nil {
ctx.Handle(500, "GetFeeds", err) ctx.Handle(500, "GetFeeds", err)
return return
} }
userCache := map[int64]*models.User{user.ID: user} userCache := map[int64]*models.User{options.RequestedUser.ID: options.RequestedUser}
if ctx.User != nil { if ctx.User != nil {
userCache[ctx.User.ID] = ctx.User userCache[ctx.User.ID] = ctx.User
} }
@ -133,32 +123,14 @@ func Dashboard(ctx *context.Context) {
ctx.Data["PageIsNews"] = true ctx.Data["PageIsNews"] = true
ctx.Data["SearchLimit"] = setting.UI.User.RepoPagingNum 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 err error
var repos, mirrors []*models.Repository var mirrors []*models.Repository
if ctxUser.IsOrganization() { if ctxUser.IsOrganization() {
env, err := ctxUser.AccessibleReposEnv(ctx.User.ID) env, err := ctxUser.AccessibleReposEnv(ctx.User.ID)
if err != nil { if err != nil {
ctx.Handle(500, "AccessibleReposEnv", err) ctx.Handle(500, "AccessibleReposEnv", err)
return return
} }
repos, err = env.Repos(1, setting.UI.User.RepoPagingNum)
if err != nil {
ctx.Handle(500, "env.Repos", err)
return
}
mirrors, err = env.MirrorRepos() mirrors, err = env.MirrorRepos()
if err != nil { if err != nil {
@ -166,19 +138,12 @@ func Dashboard(ctx *context.Context) {
return return
} }
} else { } 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() mirrors, err = ctxUser.GetMirrorRepositories()
if err != nil { if err != nil {
ctx.Handle(500, "GetMirrorRepositories", err) ctx.Handle(500, "GetMirrorRepositories", err)
return return
} }
} }
ctx.Data["Repos"] = repos
ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil { if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
@ -188,7 +153,12 @@ func Dashboard(ctx *context.Context) {
ctx.Data["MirrorCount"] = len(mirrors) ctx.Data["MirrorCount"] = len(mirrors)
ctx.Data["Mirrors"] = 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() { if ctx.Written() {
return return
} }

View File

@ -138,7 +138,12 @@ func Profile(ctx *context.Context) {
ctx.Data["Keyword"] = keyword ctx.Data["Keyword"] = keyword
switch tab { switch tab {
case "activity": 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() { if ctx.Written() {
return return
} }