Skip to content

Commit 1a5fe43

Browse files
Bwkolunny
authored andcommitted
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
1 parent faf4b50 commit 1a5fe43

File tree

6 files changed

+71
-80
lines changed

6 files changed

+71
-80
lines changed

models/action.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"unicode"
1616

1717
"github.com/Unknwon/com"
18+
"github.com/go-xorm/builder"
1819
"github.com/go-xorm/xorm"
1920

2021
"code.gitea.io/git"
@@ -712,10 +713,13 @@ type GetFeedsOptions struct {
712713
IncludePrivate bool // include private actions
713714
OnlyPerformedBy bool // only actions performed by requested user
714715
IncludeDeleted bool // include deleted actions
716+
Collaborate bool // Include collaborative repositories
715717
}
716718

717719
// GetFeeds returns actions according to the provided options
718720
func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
721+
cond := builder.NewCond()
722+
719723
var repoIDs []int64
720724
if opts.RequestedUser.IsOrganization() {
721725
env, err := opts.RequestedUser.AccessibleReposEnv(opts.RequestingUserID)
@@ -725,26 +729,28 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
725729
if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil {
726730
return nil, fmt.Errorf("GetUserRepositories: %v", err)
727731
}
732+
733+
cond = cond.And(builder.In("repo_id", repoIDs))
734+
}
735+
736+
if opts.Collaborate {
737+
cond = builder.Eq{"user_id": opts.RequestedUser.ID}.Or(
738+
builder.Expr(`repo_id IN (SELECT repo_id FROM "access" WHERE access.user_id = ?)`, opts.RequestedUser.ID))
739+
} else {
740+
cond = builder.Eq{"user_id": opts.RequestedUser.ID}
728741
}
729742

730-
actions := make([]*Action, 0, 20)
731-
sess := x.Limit(20).
732-
Desc("id").
733-
Where("user_id = ?", opts.RequestedUser.ID)
734743
if opts.OnlyPerformedBy {
735-
sess.And("act_user_id = ?", opts.RequestedUser.ID)
744+
cond = cond.And(builder.Eq{"act_user_id": opts.RequestedUser.ID})
736745
}
737746
if !opts.IncludePrivate {
738-
sess.And("is_private = ?", false)
739-
}
740-
if opts.RequestedUser.IsOrganization() {
741-
sess.In("repo_id", repoIDs)
747+
cond = cond.And(builder.Eq{"is_private": false})
742748
}
743749

744750
if !opts.IncludeDeleted {
745-
sess.And("is_deleted = ?", false)
746-
751+
cond = cond.And(builder.Eq{"is_deleted": false})
747752
}
748753

749-
return actions, sess.Find(&actions)
754+
actions := make([]*Action, 0, 20)
755+
return actions, x.Limit(20).Desc("id").Where(cond).Find(&actions)
750756
}

models/repo_list.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
"github.com/go-xorm/builder"
12-
"github.com/go-xorm/xorm"
1312
)
1413

1514
// RepositoryList contains a list of repositories
@@ -98,13 +97,14 @@ type SearchRepoOptions struct {
9897
// Owner in we search search
9998
//
10099
// in: query
101-
OwnerID int64 `json:"uid"`
102-
Searcher *User `json:"-"` //ID of the person who's seeking
103-
OrderBy string `json:"-"`
104-
Private bool `json:"-"` // Include private repositories in results
105-
Starred bool `json:"-"`
106-
Page int `json:"-"`
107-
IsProfile bool `json:"-"`
100+
OwnerID int64 `json:"uid"`
101+
Searcher *User `json:"-"` //ID of the person who's seeking
102+
OrderBy string `json:"-"`
103+
Private bool `json:"-"` // Include private repositories in results
104+
Collaborate bool `json:"-"` // Include collaborative repositories
105+
Starred bool `json:"-"`
106+
Page int `json:"-"`
107+
IsProfile bool `json:"-"`
108108
// Limit of result
109109
//
110110
// maximum: setting.ExplorePagingNum
@@ -115,25 +115,21 @@ type SearchRepoOptions struct {
115115
// SearchRepositoryByName takes keyword and part of repository name to search,
116116
// it returns results in given range and number of total results.
117117
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
118-
var (
119-
sess *xorm.Session
120-
cond = builder.NewCond()
121-
)
122-
123-
opts.Keyword = strings.ToLower(opts.Keyword)
124-
118+
var cond = builder.NewCond()
125119
if opts.Page <= 0 {
126120
opts.Page = 1
127121
}
128122

129-
repos = make([]*Repository, 0, opts.PageSize)
130-
131123
if opts.Starred && opts.OwnerID > 0 {
132124
cond = builder.Eq{
133125
"star.uid": opts.OwnerID,
134126
}
135127
}
136-
cond = cond.And(builder.Like{"lower_name", opts.Keyword})
128+
129+
opts.Keyword = strings.ToLower(opts.Keyword)
130+
if opts.Keyword != "" {
131+
cond = cond.And(builder.Like{"lower_name", opts.Keyword})
132+
}
137133

138134
// Append conditions
139135
if !opts.Starred && opts.OwnerID > 0 {
@@ -157,43 +153,51 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
157153
ownerIds = append(ownerIds, org.ID)
158154
}
159155

160-
cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds)))
156+
searcherReposCond := builder.In("owner_id", ownerIds)
157+
if opts.Collaborate {
158+
searcherReposCond = searcherReposCond.Or(builder.Expr(`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)`,
159+
opts.Searcher.ID, opts.Searcher.ID))
160+
}
161+
cond = cond.And(searcherReposCond)
161162
}
162163

163164
if len(opts.OrderBy) == 0 {
164165
opts.OrderBy = "name ASC"
165166
}
166167

168+
sess := x.NewSession()
169+
defer sess.Close()
170+
167171
if opts.Starred && opts.OwnerID > 0 {
168-
sess = x.
169-
Join("INNER", "star", "star.repo_id = repository.id").
170-
Where(cond)
171-
count, err = x.
172+
count, err = sess.
172173
Join("INNER", "star", "star.repo_id = repository.id").
173174
Where(cond).
174175
Count(new(Repository))
175176
if err != nil {
176177
return nil, 0, fmt.Errorf("Count: %v", err)
177178
}
179+
180+
sess.Join("INNER", "star", "star.repo_id = repository.id")
178181
} else {
179-
sess = x.Where(cond)
180-
count, err = x.
182+
count, err = sess.
181183
Where(cond).
182184
Count(new(Repository))
183185
if err != nil {
184186
return nil, 0, fmt.Errorf("Count: %v", err)
185187
}
186188
}
187189

190+
repos = make([]*Repository, 0, opts.PageSize)
188191
if err = sess.
192+
Where(cond).
189193
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
190194
OrderBy(opts.OrderBy).
191195
Find(&repos); err != nil {
192196
return nil, 0, fmt.Errorf("Repo: %v", err)
193197
}
194198

195199
if !opts.IsProfile {
196-
if err = repos.loadAttributes(x); err != nil {
200+
if err = repos.loadAttributes(sess); err != nil {
197201
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
198202
}
199203
}

models/repo_list_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestSearchRepositoryByName(t *testing.T) {
4242
Keyword: "repo_13",
4343
Page: 1,
4444
PageSize: 10,
45+
Private: true,
4546
Searcher: &User{ID: 14},
4647
})
4748

@@ -54,6 +55,7 @@ func TestSearchRepositoryByName(t *testing.T) {
5455
Keyword: "test_repo",
5556
Page: 1,
5657
PageSize: 10,
58+
Private: true,
5759
Searcher: &User{ID: 14},
5860
})
5961

routers/api/v1/repo/repo.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func Search(ctx *context.APIContext) {
4242
if ctx.IsSigned && opts.OwnerID > 0 {
4343
if ctx.User.ID == opts.OwnerID {
4444
opts.Private = true
45+
opts.Collaborate = true
4546
} else {
4647
u, err := models.GetUserByID(opts.OwnerID)
4748
if err != nil {
@@ -54,7 +55,10 @@ func Search(ctx *context.APIContext) {
5455
if u.IsOrganization() && u.IsOwnedBy(ctx.User.ID) {
5556
opts.Private = true
5657
}
57-
// FIXME: how about collaborators?
58+
59+
if !u.IsOrganization() {
60+
opts.Collaborate = true
61+
}
5862
}
5963
}
6064

routers/user/home.go

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,14 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
5454
}
5555

5656
// retrieveFeeds loads feeds for the specified user
57-
func retrieveFeeds(ctx *context.Context, user *models.User, includePrivate, isProfile bool, includeDeletedComments bool) {
58-
var requestingID int64
59-
if ctx.User != nil {
60-
requestingID = ctx.User.ID
61-
}
62-
actions, err := models.GetFeeds(models.GetFeedsOptions{
63-
RequestedUser: user,
64-
RequestingUserID: requestingID,
65-
IncludePrivate: includePrivate,
66-
OnlyPerformedBy: isProfile,
67-
IncludeDeleted: includeDeletedComments,
68-
})
57+
func retrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) {
58+
actions, err := models.GetFeeds(options)
6959
if err != nil {
7060
ctx.Handle(500, "GetFeeds", err)
7161
return
7262
}
7363

74-
userCache := map[int64]*models.User{user.ID: user}
64+
userCache := map[int64]*models.User{options.RequestedUser.ID: options.RequestedUser}
7565
if ctx.User != nil {
7666
userCache[ctx.User.ID] = ctx.User
7767
}
@@ -133,52 +123,27 @@ func Dashboard(ctx *context.Context) {
133123
ctx.Data["PageIsNews"] = true
134124
ctx.Data["SearchLimit"] = setting.UI.User.RepoPagingNum
135125

136-
// Only user can have collaborative repositories.
137-
if !ctxUser.IsOrganization() {
138-
collaborateRepos, err := ctx.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum)
139-
if err != nil {
140-
ctx.Handle(500, "GetAccessibleRepositories", err)
141-
return
142-
} else if err = models.RepositoryList(collaborateRepos).LoadAttributes(); err != nil {
143-
ctx.Handle(500, "RepositoryList.LoadAttributes", err)
144-
return
145-
}
146-
ctx.Data["CollaborativeRepos"] = collaborateRepos
147-
}
148-
149126
var err error
150-
var repos, mirrors []*models.Repository
127+
var mirrors []*models.Repository
151128
if ctxUser.IsOrganization() {
152129
env, err := ctxUser.AccessibleReposEnv(ctx.User.ID)
153130
if err != nil {
154131
ctx.Handle(500, "AccessibleReposEnv", err)
155132
return
156133
}
157-
repos, err = env.Repos(1, setting.UI.User.RepoPagingNum)
158-
if err != nil {
159-
ctx.Handle(500, "env.Repos", err)
160-
return
161-
}
162134

163135
mirrors, err = env.MirrorRepos()
164136
if err != nil {
165137
ctx.Handle(500, "env.MirrorRepos", err)
166138
return
167139
}
168140
} else {
169-
if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
170-
ctx.Handle(500, "GetRepositories", err)
171-
return
172-
}
173-
repos = ctxUser.Repos
174-
175141
mirrors, err = ctxUser.GetMirrorRepositories()
176142
if err != nil {
177143
ctx.Handle(500, "GetMirrorRepositories", err)
178144
return
179145
}
180146
}
181-
ctx.Data["Repos"] = repos
182147
ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
183148

184149
if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
@@ -188,7 +153,12 @@ func Dashboard(ctx *context.Context) {
188153
ctx.Data["MirrorCount"] = len(mirrors)
189154
ctx.Data["Mirrors"] = mirrors
190155

191-
retrieveFeeds(ctx, ctxUser, true, false, false)
156+
retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
157+
IncludePrivate: true,
158+
OnlyPerformedBy: false,
159+
Collaborate: true,
160+
IncludeDeleted: false,
161+
})
192162
if ctx.Written() {
193163
return
194164
}

routers/user/profile.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ func Profile(ctx *context.Context) {
138138
ctx.Data["Keyword"] = keyword
139139
switch tab {
140140
case "activity":
141-
retrieveFeeds(ctx, ctxUser, showPrivate, true, false)
141+
retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
142+
IncludePrivate: showPrivate,
143+
OnlyPerformedBy: true,
144+
Collaborate: true,
145+
IncludeDeleted: false,
146+
})
142147
if ctx.Written() {
143148
return
144149
}

0 commit comments

Comments
 (0)