From 604ce776287545b31991f0c78b13854b36ed2f59 Mon Sep 17 00:00:00 2001
From: Lauris BH <lauris@nix.lv>
Date: Fri, 28 Jan 2022 13:29:04 +0200
Subject: [PATCH] Allow to filter repositories by language in explore, user and
 organization repositories lists (#18430)

---
 models/repo_list.go                |  9 +++++++++
 routers/web/explore/repo.go        |  5 +++++
 routers/web/org/home.go            |  5 +++++
 routers/web/user/profile.go        | 10 ++++++++++
 templates/explore/repo_list.tmpl   |  4 +++-
 templates/explore/repo_search.tmpl | 21 +++++++++++----------
 6 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/models/repo_list.go b/models/repo_list.go
index 290919bb6d..36f57abcc5 100644
--- a/models/repo_list.go
+++ b/models/repo_list.go
@@ -136,6 +136,8 @@ type SearchRepoOptions struct {
 	Archived util.OptionalBool
 	// only search topic name
 	TopicOnly bool
+	// only search repositories with specified primary language
+	Language string
 	// include description in keyword search
 	IncludeDescription bool
 	// None -> include has milestones AND has no milestone
@@ -439,6 +441,13 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
 		cond = cond.And(keywordCond)
 	}
 
+	if opts.Language != "" {
+		cond = cond.And(builder.In("id", builder.
+			Select("repo_id").
+			From("language_stat").
+			Where(builder.Eq{"language": opts.Language}).And(builder.Eq{"is_primary": true})))
+	}
+
 	if opts.Fork != util.OptionalBoolNone {
 		cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
 	}
diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go
index 0a78e9e29d..ce3aefe26f 100644
--- a/routers/web/explore/repo.go
+++ b/routers/web/explore/repo.go
@@ -78,6 +78,9 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
 	topicOnly := ctx.FormBool("topic")
 	ctx.Data["TopicOnly"] = topicOnly
 
+	language := ctx.FormTrim("language")
+	ctx.Data["Language"] = language
+
 	repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
 		ListOptions: db.ListOptions{
 			Page:     page,
@@ -91,6 +94,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
 		AllPublic:          true,
 		AllLimited:         true,
 		TopicOnly:          topicOnly,
+		Language:           language,
 		IncludeDescription: setting.UI.SearchRepoDescription,
 	})
 	if err != nil {
@@ -105,6 +109,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
 	pager := context.NewPagination(int(count), opts.PageSize, page, 5)
 	pager.SetDefaultParams(ctx)
 	pager.AddParam(ctx, "topic", "TopicOnly")
+	pager.AddParam(ctx, "language", "Language")
 	ctx.Data["Page"] = pager
 
 	ctx.HTML(http.StatusOK, opts.TplName)
diff --git a/routers/web/org/home.go b/routers/web/org/home.go
index cf23a1e291..d07e2993b2 100644
--- a/routers/web/org/home.go
+++ b/routers/web/org/home.go
@@ -83,6 +83,9 @@ func Home(ctx *context.Context) {
 	keyword := ctx.FormTrim("q")
 	ctx.Data["Keyword"] = keyword
 
+	language := ctx.FormTrim("language")
+	ctx.Data["Language"] = language
+
 	page := ctx.FormInt("page")
 	if page <= 0 {
 		page = 1
@@ -103,6 +106,7 @@ func Home(ctx *context.Context) {
 		OrderBy:            orderBy,
 		Private:            ctx.IsSigned,
 		Actor:              ctx.User,
+		Language:           language,
 		IncludeDescription: setting.UI.SearchRepoDescription,
 	})
 	if err != nil {
@@ -148,6 +152,7 @@ func Home(ctx *context.Context) {
 
 	pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
 	pager.SetDefaultParams(ctx)
+	pager.AddParam(ctx, "language", "Language")
 	ctx.Data["Page"] = pager
 
 	ctx.HTML(http.StatusOK, tplOrgHome)
diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go
index 86ecb7c02c..9c0ce10dae 100644
--- a/routers/web/user/profile.go
+++ b/routers/web/user/profile.go
@@ -232,6 +232,10 @@ func Profile(ctx *context.Context) {
 
 	keyword := ctx.FormTrim("q")
 	ctx.Data["Keyword"] = keyword
+
+	language := ctx.FormTrim("language")
+	ctx.Data["Language"] = language
+
 	switch tab {
 	case "followers":
 		items, err := user_model.GetUserFollowers(ctxUser, db.ListOptions{
@@ -283,6 +287,7 @@ func Profile(ctx *context.Context) {
 			StarredByID:        ctxUser.ID,
 			Collaborate:        util.OptionalBoolFalse,
 			TopicOnly:          topicOnly,
+			Language:           language,
 			IncludeDescription: setting.UI.SearchRepoDescription,
 		})
 		if err != nil {
@@ -314,6 +319,7 @@ func Profile(ctx *context.Context) {
 			WatchedByID:        ctxUser.ID,
 			Collaborate:        util.OptionalBoolFalse,
 			TopicOnly:          topicOnly,
+			Language:           language,
 			IncludeDescription: setting.UI.SearchRepoDescription,
 		})
 		if err != nil {
@@ -335,6 +341,7 @@ func Profile(ctx *context.Context) {
 			Private:            ctx.IsSigned,
 			Collaborate:        util.OptionalBoolFalse,
 			TopicOnly:          topicOnly,
+			Language:           language,
 			IncludeDescription: setting.UI.SearchRepoDescription,
 		})
 		if err != nil {
@@ -349,6 +356,9 @@ func Profile(ctx *context.Context) {
 
 	pager := context.NewPagination(total, setting.UI.User.RepoPagingNum, page, 5)
 	pager.SetDefaultParams(ctx)
+	if tab != "followers" && tab != "following" && tab != "activity" && tab != "projects" {
+		pager.AddParam(ctx, "language", "Language")
+	}
 	ctx.Data["Page"] = pager
 
 	ctx.Data["ShowUserEmail"] = len(ctxUser.Email) > 0 && ctx.IsSigned && (!ctxUser.KeepEmailPrivate || ctxUser.ID == ctx.User.ID)
diff --git a/templates/explore/repo_list.tmpl b/templates/explore/repo_list.tmpl
index 196c947213..03f1928e2b 100644
--- a/templates/explore/repo_list.tmpl
+++ b/templates/explore/repo_list.tmpl
@@ -40,7 +40,9 @@
 				</div>
 				<div class="metas df ac">
 					{{if .PrimaryLanguage }}
-					<span class="text grey df ac mr-3"><i class="color-icon mr-3" style="background-color: {{.PrimaryLanguage.Color}}"></i>{{ .PrimaryLanguage.Language }}</span>
+						<a href="{{$.Link}}?tab={{$.TabName}}&q={{$.Keyword}}&sort={{$.SortType}}&language={{.PrimaryLanguage.Language}}">
+							<span class="text grey df ac mr-3"><i class="color-icon mr-3" style="background-color: {{.PrimaryLanguage.Color}}"></i>{{ .PrimaryLanguage.Language }}</span>
+						</a>
 					{{end}}
 					{{if not $.DisableStars}}
 						<span class="text grey df ac mr-3">{{svg "octicon-star" 16 "mr-3"}}{{.NumStars}}</span>
diff --git a/templates/explore/repo_search.tmpl b/templates/explore/repo_search.tmpl
index c1745525a9..bc4572dfcd 100644
--- a/templates/explore/repo_search.tmpl
+++ b/templates/explore/repo_search.tmpl
@@ -6,24 +6,25 @@
 				{{svg "octicon-triangle-down" 14 "dropdown icon"}}
 		</span>
 		<div class="menu">
-			<a class="{{if eq .SortType "newest"}}active{{end}} item" href="{{$.Link}}?sort=newest&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.latest"}}</a>
-			<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>
-			<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>
-			<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>
-			<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>
-			<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>
+			<a class="{{if eq .SortType "newest"}}active{{end}} item" href="{{$.Link}}?sort=newest&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.filter_sort.latest"}}</a>
+			<a class="{{if eq .SortType "oldest"}}active{{end}} item" href="{{$.Link}}?sort=oldest&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.filter_sort.oldest"}}</a>
+			<a class="{{if eq .SortType "alphabetically"}}active{{end}} item" href="{{$.Link}}?sort=alphabetically&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.label.filter_sort.alphabetically"}}</a>
+			<a class="{{if eq .SortType "reversealphabetically"}}active{{end}} item" href="{{$.Link}}?sort=reversealphabetically&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.label.filter_sort.reverse_alphabetically"}}</a>
+			<a class="{{if eq .SortType "recentupdate"}}active{{end}} item" href="{{$.Link}}?sort=recentupdate&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.filter_sort.recentupdate"}}</a>
+			<a class="{{if eq .SortType "leastupdate"}}active{{end}} item" href="{{$.Link}}?sort=leastupdate&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.filter_sort.leastupdate"}}</a>
 			{{if not .DisableStars}}
-				<a class="{{if eq .SortType "moststars"}}active{{end}} item" href="{{$.Link}}?sort=moststars&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.moststars"}}</a>
-				<a class="{{if eq .SortType "feweststars"}}active{{end}} item" href="{{$.Link}}?sort=feweststars&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.feweststars"}}</a>
+				<a class="{{if eq .SortType "moststars"}}active{{end}} item" href="{{$.Link}}?sort=moststars&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.filter_sort.moststars"}}</a>
+				<a class="{{if eq .SortType "feweststars"}}active{{end}} item" href="{{$.Link}}?sort=feweststars&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.filter_sort.feweststars"}}</a>
 			{{end}}
-			<a class="{{if eq .SortType "mostforks"}}active{{end}} item" href="{{$.Link}}?sort=mostforks&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.mostforks"}}</a>
-			<a class="{{if eq .SortType "fewestforks"}}active{{end}} item" href="{{$.Link}}?sort=fewestforks&q={{$.Keyword}}&tab={{$.TabName}}">{{.i18n.Tr "repo.issues.filter_sort.fewestforks"}}</a>
+			<a class="{{if eq .SortType "mostforks"}}active{{end}} item" href="{{$.Link}}?sort=mostforks&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.filter_sort.mostforks"}}</a>
+			<a class="{{if eq .SortType "fewestforks"}}active{{end}} item" href="{{$.Link}}?sort=fewestforks&q={{$.Keyword}}&tab={{$.TabName}}&language={{$.Language}}">{{.i18n.Tr "repo.issues.filter_sort.fewestforks"}}</a>
 		</div>
 	</div>
 </div>
 <form class="ui form ignore-dirty" style="max-width: 90%">
 	<input type="hidden" name="tab" value="{{$.TabName}}">
 	<input type="hidden" name="sort" value="{{$.SortType}}">
+	<input type="hidden" name="language" value="{{$.Language}}">
 	<div class="ui fluid action input">
 		<input name="q" value="{{.Keyword}}" placeholder="{{.i18n.Tr "explore.search"}}..." autofocus>
 		<button class="ui blue button">{{.i18n.Tr "explore.search"}}</button>