Skip to content

Commit

Permalink
CB/feat: Only show relevant repositories on explore page (!32)
Browse files Browse the repository at this point in the history
- Add go-gitea#19361
- Remove the HideMirror codeberg-specific option(not needed anymore with this patch).
- Only show relevant repositories by default(no keyword given and not disabled).
- Backporting this feature to test it's impact, and solve Codeberg/Community#575

Co-authored-by: Gusted <williamzijl7@hotmail.com>
Reviewed-on: https://codeberg.org/Codeberg/gitea/pulls/32
Co-authored-by: Gusted <gusted@noreply.codeberg.org>
Co-committed-by: Gusted <gusted@noreply.codeberg.org>
  • Loading branch information
2 people authored and Otto Richter committed Apr 23, 2022
1 parent 0d4752c commit ae65cba
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
25 changes: 23 additions & 2 deletions models/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ type SearchRepoOptions struct {
HasMilestones util.OptionalBool
// LowerNames represents valid lower names to restrict to
LowerNames []string
// When specified true, apply some filters over the conditions:
// - Don't show forks, when opts.Fork is OptionalBoolNone.
// - Do not display repositories that lacks a description, icon and topic.
OnlyShowRelevant bool
}

// SearchOrderBy is used to sort the result
Expand Down Expand Up @@ -439,8 +443,12 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
cond = cond.And(keywordCond)
}

if opts.Fork != util.OptionalBoolNone {
cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
if opts.Fork != util.OptionalBoolNone || opts.OnlyShowRelevant {
if opts.OnlyShowRelevant && opts.Fork == util.OptionalBoolNone {
cond = cond.And(builder.Eq{"is_fork": false})
} else {
cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
}
}

if opts.Mirror != util.OptionalBoolNone {
Expand All @@ -462,6 +470,19 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
cond = cond.And(builder.Eq{"num_milestones": 0}.Or(builder.IsNull{"num_milestones"}))
}

if opts.OnlyShowRelevant {
// Only show a repo that either has a topic or description.
subQueryCond := builder.NewCond()

// Topic checking. topics is non-null.
subQueryCond = subQueryCond.Or(builder.Neq{"topics": "null"})

// Description checking. Description not empty.
subQueryCond = subQueryCond.Or(builder.Neq{"description": ""})

cond = cond.And(subQueryCond)
}

return cond
}

Expand Down
2 changes: 2 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ var (
CustomEmojisMap map[string]string `ini:"-"`
SearchRepoDescription bool
UseServiceWorker bool
OnlyShowRelevantRepos bool

Notification struct {
MinTimeout time.Duration
Expand Down Expand Up @@ -1025,6 +1026,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
UI.DefaultShowFullName = Cfg.Section("ui").Key("DEFAULT_SHOW_FULL_NAME").MustBool(false)
UI.SearchRepoDescription = Cfg.Section("ui").Key("SEARCH_REPO_DESCRIPTION").MustBool(true)
UI.UseServiceWorker = Cfg.Section("ui").Key("USE_SERVICE_WORKER").MustBool(false)
UI.OnlyShowRelevantRepos = Cfg.Section("ui").Key("ONLY_SHOW_RELEVANT_REPOS").MustBool(false)

HasRobotsTxt, err = util.IsFile(path.Join(CustomPath, "robots.txt"))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ org_no_results = No matching organizations found.
code_no_results = No source code matching your search term found.
code_search_results = Search results for '%s'
code_last_indexed_at = Last indexed %s
relevant_repositories_tooltip = Repositories that are forks or don't have a topic and description aren't being shown.
relevant_repositories = Only relevant repositories are being shown, <a href="%s">show unfiltered results</a>.

[auth]
create_new_account = Register Account
Expand Down
28 changes: 12 additions & 16 deletions routers/web/explore/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ type RepoSearchOptions struct {
Restricted bool
PageSize int
TplName base.TplName
// codeberg: Hide mirrors from explore view
HideMirror util.OptionalBool
}

// RenderRepoSearch render repositories search page
Expand All @@ -40,10 +38,11 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
}

var (
repos []*repo_model.Repository
count int64
err error
orderBy db.SearchOrderBy
repos []*repo_model.Repository
count int64
err error
orderBy db.SearchOrderBy
onlyShowRelevant bool
)

ctx.Data["SortType"] = ctx.FormString("sort")
Expand Down Expand Up @@ -75,18 +74,18 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
default:
ctx.Data["SortType"] = "recentupdate"
orderBy = db.SearchOrderByRecentUpdated
onlyShowRelevant = setting.UI.OnlyShowRelevantRepos && !ctx.FormBool("no_filter")
}

keyword := ctx.FormTrim("q")
if keyword != "" {
onlyShowRelevant = false
}

ctx.Data["OnlyShowRelevant"] = onlyShowRelevant
topicOnly := ctx.FormBool("topic")
ctx.Data["TopicOnly"] = topicOnly

// codeberg: Hide mirrors from explore view
var showMirror util.OptionalBool = util.OptionalBoolNone
if (opts.HideMirror == util.OptionalBoolTrue) && (keyword == "") {
showMirror = util.OptionalBoolFalse
}

repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
ListOptions: db.ListOptions{
Page: page,
Expand All @@ -101,8 +100,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
AllLimited: true,
TopicOnly: topicOnly,
IncludeDescription: setting.UI.SearchRepoDescription,
// codeberg: Hide mirrors from explore view
Mirror: showMirror,
OnlyShowRelevant: onlyShowRelevant,
})
if err != nil {
ctx.ServerError("SearchRepository", err)
Expand Down Expand Up @@ -139,7 +137,5 @@ func Repos(ctx *context.Context) {
OwnerID: ownerID,
Private: ctx.User != nil,
TplName: tplExploreRepos,
// codeberg: Hide mirrors from explore view
HideMirror: util.OptionalBoolTrue,
})
}
5 changes: 5 additions & 0 deletions templates/explore/repo_search.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@
<button class="ui blue button">{{.i18n.Tr "explore.search"}}</button>
</div>
</form>
{{if .OnlyShowRelevant}}
<div class="ui blue attached message explore-relevancy-note">
<span class="ui tooltip" data-content="{{.i18n.Tr "explore.relevant_repositories_tooltip"}}">{{.i18n.Tr "explore.relevant_repositories" ((printf "%s%s" $.Link "?no_filter=1")|Escape) | Safe}}</span>
</div>
{{end}}
<div class="ui divider"></div>
6 changes: 6 additions & 0 deletions web_src/less/_explore.less
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@
}
}
}

.ui.explore-relevancy-note {
border-top: 0;
margin-top: 0;
max-width: 90%;
}

0 comments on commit ae65cba

Please sign in to comment.