Skip to content

Commit bdac5e0

Browse files
committed
fix some bug and some refactor
1 parent d34f97a commit bdac5e0

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

models/action.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ type GetFeedsOptions struct {
718718

719719
// GetFeeds returns actions according to the provided options
720720
func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
721+
cond := builder.NewCond()
722+
721723
var repoIDs []int64
722724
if opts.RequestedUser.IsOrganization() {
723725
env, err := opts.RequestedUser.AccessibleReposEnv(opts.RequestingUserID)
@@ -727,12 +729,9 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
727729
if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil {
728730
return nil, fmt.Errorf("GetUserRepositories: %v", err)
729731
}
730-
}
731732

732-
actions := make([]*Action, 0, 20)
733-
cond := builder.NewCond()
734-
sess := x.Limit(20).
735-
Desc("id")
733+
cond = cond.And(builder.In("repo_id", repoIDs))
734+
}
736735

737736
if opts.Collaborate {
738737
cond = builder.Eq{"user_id": opts.RequestedUser.ID}.Or(
@@ -747,14 +746,11 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
747746
if !opts.IncludePrivate {
748747
cond = cond.And(builder.Eq{"is_private": false})
749748
}
750-
if opts.RequestedUser.IsOrganization() {
751-
cond = cond.And(builder.In("repo_id", repoIDs))
752-
}
753749

754750
if !opts.IncludeDeleted {
755751
cond = cond.And(builder.Eq{"is_deleted": false})
756-
757752
}
758753

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

models/repo_list.go

Lines changed: 20 additions & 22 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
@@ -116,25 +115,21 @@ type SearchRepoOptions struct {
116115
// SearchRepositoryByName takes keyword and part of repository name to search,
117116
// it returns results in given range and number of total results.
118117
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
119-
var (
120-
sess *xorm.Session
121-
cond = builder.NewCond()
122-
)
123-
124-
opts.Keyword = strings.ToLower(opts.Keyword)
125-
118+
var cond = builder.NewCond()
126119
if opts.Page <= 0 {
127120
opts.Page = 1
128121
}
129122

130-
repos = make([]*Repository, 0, opts.PageSize)
131-
132123
if opts.Starred && opts.OwnerID > 0 {
133124
cond = builder.Eq{
134125
"star.uid": opts.OwnerID,
135126
}
136127
}
137-
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+
}
138133

139134
// Append conditions
140135
if !opts.Starred && opts.OwnerID > 0 {
@@ -158,48 +153,51 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
158153
ownerIds = append(ownerIds, org.ID)
159154
}
160155

161-
cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds)))
162-
156+
searcherReposCond := builder.In("owner_id", ownerIds)
163157
if opts.Collaborate {
164-
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),
165-
builder.And(builder.Like{"lower_name", opts.Keyword}, builder.Eq{"is_private": opts.Private}))
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))
166160
}
161+
cond = cond.And(searcherReposCond)
167162
}
168163

169164
if len(opts.OrderBy) == 0 {
170165
opts.OrderBy = "name ASC"
171166
}
172167

168+
sess := x.NewSession()
169+
defer sess.Close()
170+
173171
if opts.Starred && opts.OwnerID > 0 {
174-
sess = x.
175-
Join("INNER", "star", "star.repo_id = repository.id").
176-
Where(cond)
177-
count, err = x.
172+
count, err = sess.
178173
Join("INNER", "star", "star.repo_id = repository.id").
179174
Where(cond).
180175
Count(new(Repository))
181176
if err != nil {
182177
return nil, 0, fmt.Errorf("Count: %v", err)
183178
}
179+
180+
sess.Join("INNER", "star", "star.repo_id = repository.id")
184181
} else {
185-
sess = x.Where(cond)
186-
count, err = x.
182+
count, err = sess.
187183
Where(cond).
188184
Count(new(Repository))
189185
if err != nil {
190186
return nil, 0, fmt.Errorf("Count: %v", err)
191187
}
192188
}
193189

190+
repos = make([]*Repository, 0, opts.PageSize)
194191
if err = sess.
192+
Where(cond).
195193
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
196194
OrderBy(opts.OrderBy).
197195
Find(&repos); err != nil {
198196
return nil, 0, fmt.Errorf("Repo: %v", err)
199197
}
200198

201199
if !opts.IsProfile {
202-
if err = repos.loadAttributes(x); err != nil {
200+
if err = repos.loadAttributes(sess); err != nil {
203201
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
204202
}
205203
}

0 commit comments

Comments
 (0)