From 3576e1ee739cc03f87e6ce98dbef2f32b517b202 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 11 Feb 2017 12:00:01 +0800 Subject: [PATCH] fix: trim the whitespaces for the search keyword (#893) --- routers/api/v1/repo/repo.go | 4 ++-- routers/api/v1/user/user.go | 4 +++- routers/home.go | 8 ++++---- routers/repo/commit.go | 3 ++- routers/repo/issue.go | 2 +- routers/user/notification.go | 2 +- routers/user/profile.go | 2 +- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 02c017243..cbcc10a4d 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -5,7 +5,7 @@ package repo import ( - "path" + "strings" api "code.gitea.io/sdk/gitea" @@ -21,7 +21,7 @@ import ( // see https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories func Search(ctx *context.APIContext) { opts := &models.SearchRepoOptions{ - Keyword: path.Base(ctx.Query("q")), + Keyword: strings.Trim(ctx.Query("q"), " "), OwnerID: ctx.QueryInt64("uid"), PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")), } diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 20ed180ef..de99fade4 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -5,6 +5,8 @@ package user import ( + "strings" + "github.com/Unknwon/com" api "code.gitea.io/sdk/gitea" @@ -16,7 +18,7 @@ import ( // Search search users func Search(ctx *context.APIContext) { opts := &models.SearchUserOptions{ - Keyword: ctx.Query("q"), + Keyword: strings.Trim(ctx.Query("q"), " "), Type: models.UserTypeIndividual, PageSize: com.StrTo(ctx.Query("limit")).MustInt(), } diff --git a/routers/home.go b/routers/home.go index f5172182d..6f211f4f6 100644 --- a/routers/home.go +++ b/routers/home.go @@ -5,12 +5,12 @@ package routers import ( + "bytes" "fmt" + "strings" "github.com/Unknwon/paginater" - "bytes" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -100,7 +100,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { orderBy = "created_unix DESC" } - keyword := ctx.Query("q") + keyword := strings.Trim(ctx.Query("q"), " ") if len(keyword) == 0 { repos, err = opts.Ranger(&models.SearchRepoOptions{ Page: page, @@ -199,7 +199,7 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) { orderBy = "id DESC" } - keyword := ctx.Query("q") + keyword := strings.Trim(ctx.Query("q"), " ") if len(keyword) == 0 { users, err = opts.Ranger(&models.SearchUserOptions{OrderBy: orderBy, Page: page, diff --git a/routers/repo/commit.go b/routers/repo/commit.go index f165ed852..37e57ac06 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -7,6 +7,7 @@ package repo import ( "container/list" "path" + "strings" "code.gitea.io/git" "code.gitea.io/gitea/models" @@ -106,7 +107,7 @@ func Graph(ctx *context.Context) { func SearchCommits(ctx *context.Context) { ctx.Data["PageIsCommits"] = true - keyword := ctx.Query("q") + keyword := strings.Trim(ctx.Query("q"), " ") if len(keyword) == 0 { ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName) return diff --git a/routers/repo/issue.go b/routers/repo/issue.go index bc9e72a1a..17c3b56c2 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -145,7 +145,7 @@ func Issues(ctx *context.Context) { milestoneID := ctx.QueryInt64("milestone") isShowClosed := ctx.Query("state") == "closed" - keyword := ctx.Query("q") + keyword := strings.Trim(ctx.Query("q"), " ") if bytes.Contains([]byte(keyword), []byte{0x00}) { keyword = "" } diff --git a/routers/user/notification.go b/routers/user/notification.go index deb98d589..77a4a59dc 100644 --- a/routers/user/notification.go +++ b/routers/user/notification.go @@ -40,7 +40,7 @@ func GetNotificationCount(c *context.Context) { // Notifications is the notifications page func Notifications(c *context.Context) { var ( - keyword = c.Query("q") + keyword = strings.Trim(c.Query("q"), " ") status models.NotificationStatus page = c.QueryInt("page") perPage = c.QueryInt("perPage") diff --git a/routers/user/profile.go b/routers/user/profile.go index cca4391a7..f0b1be306 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -155,7 +155,7 @@ func Profile(ctx *context.Context) { ctx.Data["SortType"] = "recentupdate" } - keyword := ctx.Query("q") + keyword := strings.Trim(ctx.Query("q"), " ") ctx.Data["Keyword"] = keyword if len(keyword) == 0 { var total int