Skip to content

Commit 23aba52

Browse files
appleboylunny
authored andcommitted
feat: support search bar on star tab of user profile. (#917)
* feat: support search bar on star tab of user profile. * fix: update testing. * fix: Using loadAttributes * fix: remove empty line. * remove LOWER Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 7eb8daf commit 23aba52

File tree

8 files changed

+109
-76
lines changed

8 files changed

+109
-76
lines changed

models/repo.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,13 +1778,15 @@ type SearchRepoOptions struct {
17781778
Searcher *User //ID of the person who's seeking
17791779
OrderBy string
17801780
Private bool // Include private repositories in results
1781+
Starred bool
17811782
Page int
17821783
PageSize int // Can be smaller than or equal to setting.ExplorePagingNum
17831784
}
17841785

17851786
// SearchRepositoryByName takes keyword and part of repository name to search,
17861787
// it returns results in given range and number of total results.
1787-
func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int64, _ error) {
1788+
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
1789+
var sess *xorm.Session
17881790
if len(opts.Keyword) == 0 {
17891791
return repos, 0, nil
17901792
}
@@ -1796,9 +1798,17 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
17961798

17971799
repos = make([]*Repository, 0, opts.PageSize)
17981800

1801+
if opts.Starred && opts.OwnerID > 0 {
1802+
sess = x.
1803+
Join("INNER", "star", "star.repo_id = repository.id").
1804+
Where("star.uid = ?", opts.OwnerID).
1805+
And("lower_name LIKE ?", "%"+opts.Keyword+"%")
1806+
} else {
1807+
sess = x.Where("lower_name LIKE ?", "%"+opts.Keyword+"%")
1808+
}
1809+
17991810
// Append conditions
1800-
sess := x.Where("LOWER(lower_name) LIKE ?", "%"+opts.Keyword+"%")
1801-
if opts.OwnerID > 0 {
1811+
if !opts.Starred && opts.OwnerID > 0 {
18021812
sess.And("owner_id = ?", opts.OwnerID)
18031813
}
18041814
if !opts.Private {
@@ -1831,10 +1841,20 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
18311841
return nil, 0, fmt.Errorf("Count: %v", err)
18321842
}
18331843

1834-
return repos, count, sess.
1844+
if err = sess.
18351845
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
18361846
OrderBy(opts.OrderBy).
1837-
Find(&repos)
1847+
Find(&repos); err != nil {
1848+
return nil, 0, fmt.Errorf("Repo: %v", err)
1849+
}
1850+
1851+
if opts.Starred {
1852+
if err = repos.loadAttributes(x); err != nil {
1853+
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
1854+
}
1855+
}
1856+
1857+
return repos, count, nil
18381858
}
18391859

18401860
// DeleteRepositoryArchives deletes all repositories' archives.

models/star.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) {
7373
// GetStarredRepos returns the repos the user starred.
7474
func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos []*Repository, err error) {
7575
if len(orderBy) == 0 {
76-
orderBy = "star.id"
76+
orderBy = "updated_unix DESC"
7777
}
7878
sess := x.
7979
Join("INNER", "star", "star.repo_id = repository.id").
8080
Where("star.uid = ?", u.ID).
81-
Desc(orderBy)
81+
OrderBy(orderBy)
8282

8383
if !private {
8484
sess = sess.And("is_private = ?", false)

models/star_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func TestUser_GetStarredRepos(t *testing.T) {
6161
starred, err = user.GetStarredRepos(true, 1, 10, "")
6262
assert.NoError(t, err)
6363
assert.Len(t, starred, 2)
64-
assert.Equal(t, int64(4), starred[0].ID)
65-
assert.Equal(t, int64(2), starred[1].ID)
64+
assert.Equal(t, int64(2), starred[0].ID)
65+
assert.Equal(t, int64(4), starred[1].ID)
6666
}
6767

6868
func TestUser_GetStarredRepos2(t *testing.T) {

routers/user/profile.go

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -90,73 +90,84 @@ func Profile(ctx *context.Context) {
9090

9191
tab := ctx.Query("tab")
9292
ctx.Data["TabName"] = tab
93+
94+
page := ctx.QueryInt("page")
95+
if page <= 0 {
96+
page = 1
97+
}
98+
99+
var (
100+
repos []*models.Repository
101+
count int64
102+
orderBy string
103+
)
104+
105+
ctx.Data["SortType"] = ctx.Query("sort")
106+
switch ctx.Query("sort") {
107+
case "newest":
108+
orderBy = "created_unix DESC"
109+
case "oldest":
110+
orderBy = "created_unix ASC"
111+
case "recentupdate":
112+
orderBy = "updated_unix DESC"
113+
case "leastupdate":
114+
orderBy = "updated_unix ASC"
115+
case "reversealphabetically":
116+
orderBy = "name DESC"
117+
case "alphabetically":
118+
orderBy = "name ASC"
119+
default:
120+
ctx.Data["SortType"] = "recentupdate"
121+
orderBy = "updated_unix DESC"
122+
}
123+
124+
// set default sort value if sort is empty.
125+
if ctx.Query("sort") == "" {
126+
ctx.Data["SortType"] = "recentupdate"
127+
}
128+
129+
keyword := strings.Trim(ctx.Query("q"), " ")
130+
ctx.Data["Keyword"] = keyword
93131
switch tab {
94132
case "activity":
95133
retrieveFeeds(ctx, ctxUser, -1, 0, !showPrivate)
96134
if ctx.Written() {
97135
return
98136
}
99137
case "stars":
100-
page := ctx.QueryInt("page")
101-
if page <= 0 {
102-
page = 1
103-
}
104-
105-
repos, err := ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, "")
106-
if err != nil {
107-
ctx.Handle(500, "GetStarredRepos", err)
108-
return
109-
}
138+
ctx.Data["PageIsProfileStarList"] = true
139+
if len(keyword) == 0 {
140+
repos, err = ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
141+
if err != nil {
142+
ctx.Handle(500, "GetStarredRepos", err)
143+
return
144+
}
110145

111-
counts, err := ctxUser.GetStarredRepoCount(showPrivate)
112-
if err != nil {
113-
ctx.Handle(500, "GetStarredRepoCount", err)
114-
return
146+
count, err = ctxUser.GetStarredRepoCount(showPrivate)
147+
if err != nil {
148+
ctx.Handle(500, "GetStarredRepoCount", err)
149+
return
150+
}
151+
} else {
152+
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
153+
Keyword: keyword,
154+
OwnerID: ctxUser.ID,
155+
OrderBy: orderBy,
156+
Private: showPrivate,
157+
Page: page,
158+
PageSize: setting.UI.User.RepoPagingNum,
159+
Starred: true,
160+
})
161+
if err != nil {
162+
ctx.Handle(500, "SearchRepositoryByName", err)
163+
return
164+
}
115165
}
116166

117167
ctx.Data["Repos"] = repos
118-
ctx.Data["Page"] = paginater.New(int(counts), setting.UI.User.RepoPagingNum, page, 5)
119-
ctx.Data["Total"] = int(counts)
120-
ctx.Data["Tabs"] = "stars"
168+
ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
169+
ctx.Data["Total"] = count
121170
default:
122-
page := ctx.QueryInt("page")
123-
if page <= 0 {
124-
page = 1
125-
}
126-
127-
var (
128-
repos []*models.Repository
129-
count int64
130-
err error
131-
orderBy string
132-
)
133-
134-
ctx.Data["SortType"] = ctx.Query("sort")
135-
switch ctx.Query("sort") {
136-
case "newest":
137-
orderBy = "created_unix DESC"
138-
case "oldest":
139-
orderBy = "created_unix ASC"
140-
case "recentupdate":
141-
orderBy = "updated_unix DESC"
142-
case "leastupdate":
143-
orderBy = "updated_unix ASC"
144-
case "reversealphabetically":
145-
orderBy = "name DESC"
146-
case "alphabetically":
147-
orderBy = "name ASC"
148-
default:
149-
ctx.Data["SortType"] = "recentupdate"
150-
orderBy = "updated_unix DESC"
151-
}
152-
153-
// set default sort value if sort is empty.
154-
if ctx.Query("sort") == "" {
155-
ctx.Data["SortType"] = "recentupdate"
156-
}
157-
158-
keyword := strings.Trim(ctx.Query("q"), " ")
159-
ctx.Data["Keyword"] = keyword
160171
if len(keyword) == 0 {
161172
var total int
162173
repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)

templates/base/paginate.tmpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
{{if gt .TotalPages 1}}
33
<div class="center page buttons">
44
<div class="ui borderless pagination menu">
5-
<a class="{{if .IsFirst}}disabled{{end}} item" href="{{$.Link}}?q={{$.Keyword}}"><i class="angle double left icon"></i> {{$.i18n.Tr "admin.first_page"}}</a>
6-
<a class="{{if not .HasPrevious}}disabled{{end}} item" {{if .HasPrevious}}href="{{$.Link}}?page={{.Previous}}&q={{$.Keyword}}&tab={{$.Tabs}}"{{end}}>
5+
<a class="{{if .IsFirst}}disabled{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&tab={{$.TabName}}"><i class="angle double left icon"></i> {{$.i18n.Tr "admin.first_page"}}</a>
6+
<a class="{{if not .HasPrevious}}disabled{{end}} item" {{if .HasPrevious}}href="{{$.Link}}?page={{.Previous}}&q={{$.Keyword}}&tab={{$.TabName}}"{{end}}>
77
<i class="left arrow icon"></i> {{$.i18n.Tr "repo.issues.previous"}}
88
</a>
99
{{range .Pages}}
1010
{{if eq .Num -1}}
1111
<a class="disabled item">...</a>
1212
{{else}}
13-
<a class="{{if .IsCurrent}}active{{end}} item" {{if not .IsCurrent}}href="{{$.Link}}?page={{.Num}}&q={{$.Keyword}}&tab={{$.Tabs}}"{{end}}>{{.Num}}</a>
13+
<a class="{{if .IsCurrent}}active{{end}} item" {{if not .IsCurrent}}href="{{$.Link}}?page={{.Num}}&q={{$.Keyword}}&tab={{$.TabName}}"{{end}}>{{.Num}}</a>
1414
{{end}}
1515
{{end}}
16-
<a class="{{if not .HasNext}}disabled{{end}} item" {{if .HasNext}}href="{{$.Link}}?page={{.Next}}&q={{$.Keyword}}&tab={{$.Tabs}}"{{end}}>
16+
<a class="{{if not .HasNext}}disabled{{end}} item" {{if .HasNext}}href="{{$.Link}}?page={{.Next}}&q={{$.Keyword}}&tab={{$.TabName}}"{{end}}>
1717
{{$.i18n.Tr "repo.issues.next"}}&nbsp;<i class="icon right arrow"></i>
1818
</a>
19-
<a class="{{if .IsLast}}disabled{{end}} item" href="{{$.Link}}?page={{.TotalPages}}&q={{$.Keyword}}&tab={{$.Tabs}}">{{$.i18n.Tr "admin.last_page"}}&nbsp;<i class="angle double right icon"></i></a>
19+
<a class="{{if .IsLast}}disabled{{end}} item" href="{{$.Link}}?page={{.TotalPages}}&q={{$.Keyword}}&tab={{$.TabName}}">{{$.i18n.Tr "admin.last_page"}}&nbsp;<i class="angle double right icon"></i></a>
2020
</div>
2121
</div>
2222
{{end}}

templates/explore/repo_list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{range .Repos}}
33
<div class="item">
44
<div class="ui header">
5-
<a class="name" href="{{AppSubUrl}}/{{if .Owner}}{{.Owner.Name}}{{else if $.Org}}{{$.Org.Name}}{{else}}{{$.Owner.Name}}{{end}}/{{.Name}}">{{if $.PageIsExplore}}{{.Owner.Name}} / {{end}}{{.Name}}</a>
5+
<a class="name" href="{{AppSubUrl}}/{{if .Owner}}{{.Owner.Name}}{{else if $.Org}}{{$.Org.Name}}{{else}}{{$.Owner.Name}}{{end}}/{{.Name}}">{{if or $.PageIsExplore $.PageIsProfileStarList }}{{.Owner.Name}} / {{end}}{{.Name}}</a>
66
{{if .IsPrivate}}
77
<span class="text gold"><i class="octicon octicon-lock"></i></span>
88
{{else if .IsFork}}

templates/explore/search.tmpl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
<i class="dropdown icon"></i>
77
</span>
88
<div class="menu">
9-
<a class="{{if or (eq .SortType "newest") (not .SortType)}}active{{end}} item" href="{{$.Link}}?sort=newest&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.latest"}}</a>
10-
<a class="{{if eq .SortType "oldest"}}active{{end}} item" href="{{$.Link}}?sort=oldest&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.oldest"}}</a>
11-
<a class="{{if eq .SortType "alphabetically"}}active{{end}} item" href="{{$.Link}}?sort=alphabetically&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.label.filter_sort.alphabetically"}}</a>
12-
<a class="{{if eq .SortType "reversealphabetically"}}active{{end}} item" href="{{$.Link}}?sort=reversealphabetically&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.label.filter_sort.reverse_alphabetically"}}</a>
13-
<a class="{{if eq .SortType "recentupdate"}}active{{end}} item" href="{{$.Link}}?sort=recentupdate&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.recentupdate"}}</a>
14-
<a class="{{if eq .SortType "leastupdate"}}active{{end}} item" href="{{$.Link}}?sort=leastupdate&q={{$.Keyword}}">{{.i18n.Tr "repo.issues.filter_sort.leastupdate"}}</a>
9+
<a class="{{if or (eq .SortType "newest") (not .SortType)}}active{{end}} item" href="{{$.Link}}?sort=newest&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.latest"}}</a>
10+
<a class="{{if eq .SortType "oldest"}}active{{end}} item" href="{{$.Link}}?sort=oldest&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.oldest"}}</a>
11+
<a class="{{if eq .SortType "alphabetically"}}active{{end}} item" href="{{$.Link}}?sort=alphabetically&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.label.filter_sort.alphabetically"}}</a>
12+
<a class="{{if eq .SortType "reversealphabetically"}}active{{end}} item" href="{{$.Link}}?sort=reversealphabetically&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.label.filter_sort.reverse_alphabetically"}}</a>
13+
<a class="{{if eq .SortType "recentupdate"}}active{{end}} item" href="{{$.Link}}?sort=recentupdate&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.recentupdate"}}</a>
14+
<a class="{{if eq .SortType "leastupdate"}}active{{end}} item" href="{{$.Link}}?sort=leastupdate&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.leastupdate"}}</a>
1515
</div>
1616
</div>
1717
</div>
1818
<form class="ui form" style="max-width: 90%">
1919
<div class="ui fluid action input">
2020
<input name="q" value="{{.Keyword}}" placeholder="{{.i18n.Tr "explore.search"}}..." autofocus>
21+
<input type="hidden" name="tab" value="{{$.TabName}}">
2122
<button class="ui blue button">{{.i18n.Tr "explore.search"}}</button>
2223
</div>
2324
</form>

templates/user/profile.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
</div>
9393
{{else if eq .TabName "stars"}}
9494
<div class="stars">
95+
{{template "explore/search" .}}
9596
{{template "explore/repo_list" .}}
9697
{{template "base/paginate" .}}
9798
</div>

0 commit comments

Comments
 (0)