From 590a79ff8a7f8f9eab9dc19ea96dae2083449c70 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 1 Dec 2016 18:52:57 +0800 Subject: [PATCH] Resolved #296 (#324) * resolved #296 * Indentation fixed --- conf/locale/locale_en-US.ini | 3 ++ conf/locale/locale_zh-CN.ini | 3 ++ routers/home.go | 54 +++++++++++++++++----------- templates/explore/organizations.tmpl | 2 ++ templates/explore/repo_list.tmpl | 4 +++ templates/explore/users.tmpl | 2 ++ 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini index 30a906a5b..2852168d3 100644 --- a/conf/locale/locale_en-US.ini +++ b/conf/locale/locale_en-US.ini @@ -139,6 +139,9 @@ repos = Repositories users = Users organizations = Organizations search = Search +repo_no_results = There are no matched repositories found. +user_no_results = There are no matched users found. +org_no_results = There are no matched organizations found. [auth] create_new_account = Create New Account diff --git a/conf/locale/locale_zh-CN.ini b/conf/locale/locale_zh-CN.ini index 4809949cf..770e6228d 100644 --- a/conf/locale/locale_zh-CN.ini +++ b/conf/locale/locale_zh-CN.ini @@ -139,6 +139,9 @@ repos=仓库 users=用户 organizations=组织 search=搜索 +repo_no_results = 没有匹配的仓库。 +user_no_results = 没有匹配的用户。 +org_no_results = 没有匹配的组织。 [auth] create_new_account=创建帐户 diff --git a/routers/home.go b/routers/home.go index a2faf65bc..3b5114876 100644 --- a/routers/home.go +++ b/routers/home.go @@ -9,6 +9,8 @@ import ( "github.com/Unknwon/paginater" + "bytes" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -60,6 +62,14 @@ type RepoSearchOptions struct { TplName base.TplName } +var ( + nullByte = []byte{0x00} +) + +func isKeywordValid(keyword string) bool { + return !bytes.Contains([]byte(keyword), nullByte) +} + // RenderRepoSearch render repositories search page func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { page := ctx.QueryInt("page") @@ -82,16 +92,18 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { } count = opts.Counter(opts.Private) } else { - repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ - Keyword: keyword, - OrderBy: opts.OrderBy, - Private: opts.Private, - Page: page, - PageSize: opts.PageSize, - }) - if err != nil { - ctx.Handle(500, "SearchRepositoryByName", err) - return + if isKeywordValid(keyword) { + repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ + Keyword: keyword, + OrderBy: opts.OrderBy, + Private: opts.Private, + Page: page, + PageSize: opts.PageSize, + }) + if err != nil { + ctx.Handle(500, "SearchRepositoryByName", err) + return + } } } ctx.Data["Keyword"] = keyword @@ -156,16 +168,18 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) { } count = opts.Counter() } else { - users, count, err = models.SearchUserByName(&models.SearchUserOptions{ - Keyword: keyword, - Type: opts.Type, - OrderBy: opts.OrderBy, - Page: page, - PageSize: opts.PageSize, - }) - if err != nil { - ctx.Handle(500, "SearchUserByName", err) - return + if isKeywordValid(keyword) { + users, count, err = models.SearchUserByName(&models.SearchUserOptions{ + Keyword: keyword, + Type: opts.Type, + OrderBy: opts.OrderBy, + Page: page, + PageSize: opts.PageSize, + }) + if err != nil { + ctx.Handle(500, "SearchUserByName", err) + return + } } } ctx.Data["Keyword"] = keyword diff --git a/templates/explore/organizations.tmpl b/templates/explore/organizations.tmpl index c44456be4..7044894a0 100644 --- a/templates/explore/organizations.tmpl +++ b/templates/explore/organizations.tmpl @@ -24,6 +24,8 @@ + {{else}} +
{{$.i18n.Tr "explore.org_no_results"}}
{{end}} diff --git a/templates/explore/repo_list.tmpl b/templates/explore/repo_list.tmpl index 2cf7202fd..961e510c0 100644 --- a/templates/explore/repo_list.tmpl +++ b/templates/explore/repo_list.tmpl @@ -19,5 +19,9 @@ {{if .DescriptionHTML}}

{{.DescriptionHTML}}

{{end}}

{{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Updated $.i18n.Lang}}

+ {{else}} +
+ {{$.i18n.Tr "explore.repo_no_results"}} +
{{end}} diff --git a/templates/explore/users.tmpl b/templates/explore/users.tmpl index ce356f4fd..09091df25 100644 --- a/templates/explore/users.tmpl +++ b/templates/explore/users.tmpl @@ -24,6 +24,8 @@ + {{else}} +
{{$.i18n.Tr "explore.user_no_results"}}
{{end}}