Skip to content

Commit dd58725

Browse files
Gustedvednocdelvh6543
authored and
Sysoev, Vladimir
committed
Only show relevant repositories on explore page (go-gitea#19361)
Adds a new option to only show relevant repo's on the explore page, for bigger Gitea instances like Codeberg this is a nice option to enable to make the explore page more populated with unique and "high" quality repo's. A note is shown that the results are filtered and have the possibility to see the unfiltered results. Co-authored-by: vednoc <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent ccfb78d commit dd58725

File tree

8 files changed

+65
-8
lines changed

8 files changed

+65
-8
lines changed

Diff for: custom/conf/app.example.ini

+4
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,10 @@ ROUTER = console
11641164
;;
11651165
;; Whether to enable a Service Worker to cache frontend assets
11661166
;USE_SERVICE_WORKER = false
1167+
;;
1168+
;; Whether to only show relevant repos on the explore page when no keyword is specified and default sorting is used.
1169+
;; A repo is considered irrelevant if it's a fork or if it has no metadata (no description, no icon, no topic).
1170+
;ONLY_SHOW_RELEVANT_REPOS = false
11671171

11681172
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11691173
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Diff for: docs/content/doc/advanced/config-cheat-sheet.en-us.md

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ The following configuration set `Content-Type: application/vnd.android.package-a
194194
- `DEFAULT_SHOW_FULL_NAME`: **false**: Whether the full name of the users should be shown where possible. If the full name isn't set, the username will be used.
195195
- `SEARCH_REPO_DESCRIPTION`: **true**: Whether to search within description at repository search on explore page.
196196
- `USE_SERVICE_WORKER`: **false**: Whether to enable a Service Worker to cache frontend assets.
197+
- `ONLY_SHOW_RELEVANT_REPOS`: **false** Whether to only show relevant repos on the explore page when no keyword is specified and default sorting is used.
198+
A repo is considered irrelevant if it's a fork or if it has no metadata (no description, no icon, no topic).
197199

198200
### UI - Admin (`ui.admin`)
199201

Diff for: models/repo/repo_list.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ type SearchRepoOptions struct {
163163
HasMilestones util.OptionalBool
164164
// LowerNames represents valid lower names to restrict to
165165
LowerNames []string
166+
// When specified true, apply some filters over the conditions:
167+
// - Don't show forks, when opts.Fork is OptionalBoolNone.
168+
// - Do not display repositories that don't have a description, an icon and topics.
169+
OnlyShowRelevant bool
166170
}
167171

168172
// SearchOrderBy is used to sort the result
@@ -463,8 +467,12 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
463467
Where(builder.Eq{"language": opts.Language}).And(builder.Eq{"is_primary": true})))
464468
}
465469

466-
if opts.Fork != util.OptionalBoolNone {
467-
cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
470+
if opts.Fork != util.OptionalBoolNone || opts.OnlyShowRelevant {
471+
if opts.OnlyShowRelevant && opts.Fork == util.OptionalBoolNone {
472+
cond = cond.And(builder.Eq{"is_fork": false})
473+
} else {
474+
cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
475+
}
468476
}
469477

470478
if opts.Mirror != util.OptionalBoolNone {
@@ -486,6 +494,25 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
486494
cond = cond.And(builder.Eq{"num_milestones": 0}.Or(builder.IsNull{"num_milestones"}))
487495
}
488496

497+
if opts.OnlyShowRelevant {
498+
// Only show a repo that either has a topic or description.
499+
subQueryCond := builder.NewCond()
500+
501+
// Topic checking. Topics is non-null.
502+
subQueryCond = subQueryCond.Or(builder.And(builder.Neq{"topics": "null"}, builder.Neq{"topics": "[]"}))
503+
504+
// Description checking. Description not empty.
505+
subQueryCond = subQueryCond.Or(builder.Neq{"description": ""})
506+
507+
// Repo has a avatar.
508+
subQueryCond = subQueryCond.Or(builder.Neq{"avatar": ""})
509+
510+
// Always hide repo's that are empty.
511+
subQueryCond = subQueryCond.And(builder.Eq{"is_empty": false})
512+
513+
cond = cond.And(subQueryCond)
514+
}
515+
489516
return cond
490517
}
491518

Diff for: modules/setting/setting.go

+2
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ var (
240240
CustomEmojisMap map[string]string `ini:"-"`
241241
SearchRepoDescription bool
242242
UseServiceWorker bool
243+
OnlyShowRelevantRepos bool
243244

244245
Notification struct {
245246
MinTimeout time.Duration
@@ -1087,6 +1088,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
10871088
UI.DefaultShowFullName = Cfg.Section("ui").Key("DEFAULT_SHOW_FULL_NAME").MustBool(false)
10881089
UI.SearchRepoDescription = Cfg.Section("ui").Key("SEARCH_REPO_DESCRIPTION").MustBool(true)
10891090
UI.UseServiceWorker = Cfg.Section("ui").Key("USE_SERVICE_WORKER").MustBool(false)
1091+
UI.OnlyShowRelevantRepos = Cfg.Section("ui").Key("ONLY_SHOW_RELEVANT_REPOS").MustBool(false)
10901092

10911093
HasRobotsTxt, err = util.IsFile(path.Join(CustomPath, "robots.txt"))
10921094
if err != nil {

Diff for: options/locale/locale_en-US.ini

+3
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ org_no_results = No matching organizations found.
277277
code_no_results = No source code matching your search term found.
278278
code_search_results = Search results for '%s'
279279
code_last_indexed_at = Last indexed %s
280+
relevant_repositories_tooltip = Repositories that are forks or that have no topic, no icon, and no description are hidden.
281+
relevant_repositories = Only relevant repositories are being shown, <a href="%s">show unfiltered results</a>.
282+
280283

281284
[auth]
282285
create_new_account = Register Account

Diff for: routers/web/explore/repo.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
4848
}
4949

5050
var (
51-
repos []*repo_model.Repository
52-
count int64
53-
err error
54-
orderBy db.SearchOrderBy
51+
repos []*repo_model.Repository
52+
count int64
53+
err error
54+
orderBy db.SearchOrderBy
55+
onlyShowRelevant bool
5556
)
5657

5758
ctx.Data["SortType"] = ctx.FormString("sort")
@@ -60,8 +61,6 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
6061
orderBy = db.SearchOrderByNewest
6162
case "oldest":
6263
orderBy = db.SearchOrderByOldest
63-
case "recentupdate":
64-
orderBy = db.SearchOrderByRecentUpdated
6564
case "leastupdate":
6665
orderBy = db.SearchOrderByLeastUpdated
6766
case "reversealphabetically":
@@ -83,9 +82,16 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
8382
default:
8483
ctx.Data["SortType"] = "recentupdate"
8584
orderBy = db.SearchOrderByRecentUpdated
85+
onlyShowRelevant = setting.UI.OnlyShowRelevantRepos && !ctx.FormBool("no_filter")
8686
}
8787

8888
keyword := ctx.FormTrim("q")
89+
if keyword != "" {
90+
onlyShowRelevant = false
91+
}
92+
93+
ctx.Data["OnlyShowRelevant"] = onlyShowRelevant
94+
8995
topicOnly := ctx.FormBool("topic")
9096
ctx.Data["TopicOnly"] = topicOnly
9197

@@ -107,6 +113,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
107113
TopicOnly: topicOnly,
108114
Language: language,
109115
IncludeDescription: setting.UI.SearchRepoDescription,
116+
OnlyShowRelevant: onlyShowRelevant,
110117
})
111118
if err != nil {
112119
ctx.ServerError("SearchRepository", err)
@@ -133,6 +140,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
133140
pager.SetDefaultParams(ctx)
134141
pager.AddParam(ctx, "topic", "TopicOnly")
135142
pager.AddParam(ctx, "language", "Language")
143+
pager.AddParamString("no_filter", ctx.FormString("no_filter"))
136144
ctx.Data["Page"] = pager
137145

138146
ctx.HTML(http.StatusOK, opts.TplName)

Diff for: templates/explore/repo_search.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@
2929
<button class="ui primary button">{{.locale.Tr "explore.search"}}</button>
3030
</div>
3131
</form>
32+
{{if .OnlyShowRelevant}}
33+
<div class="ui blue attached message explore-relevancy-note">
34+
<span class="ui tooltip" data-content="{{.locale.Tr "explore.relevant_repositories_tooltip"}}">{{.locale.Tr "explore.relevant_repositories" ((printf "%s%s" $.Link "?no_filter=1")|Escape) | Safe}}</span>
35+
</div>
36+
{{end}}
3237
<div class="ui divider"></div>

Diff for: web_src/less/_explore.less

+6
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,9 @@
8989
}
9090
}
9191
}
92+
93+
.ui.explore-relevancy-note {
94+
border-top: 0;
95+
margin-top: 0;
96+
max-width: 90%;
97+
}

0 commit comments

Comments
 (0)