summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2019-05-15 16:24:39 +0100
committertechknowlogick <techknowlogick@gitea.io>2019-05-15 11:24:39 -0400
commit56ae539bed7d822980ebaae8db316a0177fc028c (patch)
treeee63a75ba156002cb6c40975c1c35b032954f913 /routers
parent5fb1ad70113d3272232c266b4ff58e9f7f646594 (diff)
downloadgitea-56ae539bed7d822980ebaae8db316a0177fc028c.tar.gz
gitea-56ae539bed7d822980ebaae8db316a0177fc028c.zip
SearchRepositoryByName improvements and unification (#6897)
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo/repo.go49
-rw-r--r--routers/user/home.go58
-rw-r--r--routers/user/profile.go96
3 files changed, 60 insertions, 143 deletions
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index 56aac8014c..62153893a6 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -56,6 +56,15 @@ func Search(ctx *context.APIContext) {
// description: search only for repos that the user with the given id owns or contributes to
// type: integer
// format: int64
+ // - name: starredBy
+ // in: query
+ // description: search only for repos that the user with the given id has starred
+ // type: integer
+ // format: int64
+ // - name: private
+ // in: query
+ // description: include private repositories this user has access to (defaults to true)
+ // type: boolean
// - name: page
// in: query
// description: page number of results to return (1-based)
@@ -96,6 +105,10 @@ func Search(ctx *context.APIContext) {
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
TopicOnly: ctx.QueryBool("topic"),
Collaborate: util.OptionalBoolNone,
+ Private: ctx.IsSigned && (ctx.Query("private") == "" || ctx.QueryBool("private")),
+ UserIsAdmin: ctx.IsUserSiteAdmin(),
+ UserID: ctx.Data["SignedUserID"].(int64),
+ StarredByID: ctx.QueryInt64("starredBy"),
}
if ctx.QueryBool("exclusive") {
@@ -140,42 +153,6 @@ func Search(ctx *context.APIContext) {
}
var err error
- if opts.OwnerID > 0 {
- var repoOwner *models.User
- if ctx.User != nil && ctx.User.ID == opts.OwnerID {
- repoOwner = ctx.User
- } else {
- repoOwner, err = models.GetUserByID(opts.OwnerID)
- if err != nil {
- ctx.JSON(500, api.SearchError{
- OK: false,
- Error: err.Error(),
- })
- return
- }
- }
-
- if repoOwner.IsOrganization() {
- opts.Collaborate = util.OptionalBoolFalse
- }
-
- // Check visibility.
- if ctx.IsSigned {
- if ctx.User.ID == repoOwner.ID {
- opts.Private = true
- } else if repoOwner.IsOrganization() {
- opts.Private, err = repoOwner.IsOwnedBy(ctx.User.ID)
- if err != nil {
- ctx.JSON(500, api.SearchError{
- OK: false,
- Error: err.Error(),
- })
- return
- }
- }
- }
- }
-
repos, count, err := models.SearchRepositoryByName(opts)
if err != nil {
ctx.JSON(500, api.SearchError{
diff --git a/routers/user/home.go b/routers/user/home.go
index b53a47db94..9ccd5bdb26 100644
--- a/routers/user/home.go
+++ b/routers/user/home.go
@@ -499,50 +499,20 @@ func showOrgProfile(ctx *context.Context) {
count int64
err error
)
- if ctx.IsSigned && !ctx.User.IsAdmin {
- env, err := org.AccessibleReposEnv(ctx.User.ID)
- if err != nil {
- ctx.ServerError("AccessibleReposEnv", err)
- return
- }
- env.SetSort(orderBy)
- if len(keyword) != 0 {
- env.AddKeyword(keyword)
- }
- repos, err = env.Repos(page, setting.UI.User.RepoPagingNum)
- if err != nil {
- ctx.ServerError("env.Repos", err)
- return
- }
- count, err = env.CountRepos()
- if err != nil {
- ctx.ServerError("env.CountRepos", err)
- return
- }
- } else {
- showPrivate := ctx.IsSigned && ctx.User.IsAdmin
- if len(keyword) == 0 {
- repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
- if err != nil {
- ctx.ServerError("GetRepositories", err)
- return
- }
- count = models.CountUserRepositories(org.ID, showPrivate)
- } else {
- repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
- Keyword: keyword,
- OwnerID: org.ID,
- OrderBy: orderBy,
- Private: showPrivate,
- Page: page,
- IsProfile: true,
- PageSize: setting.UI.User.RepoPagingNum,
- })
- if err != nil {
- ctx.ServerError("SearchRepositoryByName", err)
- return
- }
- }
+ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
+ Keyword: keyword,
+ OwnerID: org.ID,
+ OrderBy: orderBy,
+ Private: ctx.IsSigned,
+ UserIsAdmin: ctx.IsUserSiteAdmin(),
+ UserID: ctx.Data["SignedUserID"].(int64),
+ Page: page,
+ IsProfile: true,
+ PageSize: setting.UI.User.RepoPagingNum,
+ })
+ if err != nil {
+ ctx.ServerError("SearchRepositoryByName", err)
+ return
}
if err := org.GetMembers(); err != nil {
diff --git a/routers/user/profile.go b/routers/user/profile.go
index a7dab18c7a..bda29522d9 100644
--- a/routers/user/profile.go
+++ b/routers/user/profile.go
@@ -170,74 +170,44 @@ func Profile(ctx *context.Context) {
}
case "stars":
ctx.Data["PageIsProfileStarList"] = true
- if len(keyword) == 0 {
- repos, err = ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
- if err != nil {
- ctx.ServerError("GetStarredRepos", err)
- return
- }
-
- count, err = ctxUser.GetStarredRepoCount(showPrivate)
- if err != nil {
- ctx.ServerError("GetStarredRepoCount", err)
- return
- }
- } else {
- repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
- Keyword: keyword,
- OwnerID: ctxUser.ID,
- OrderBy: orderBy,
- Private: showPrivate,
- Page: page,
- PageSize: setting.UI.User.RepoPagingNum,
- Starred: true,
- Collaborate: util.OptionalBoolFalse,
- TopicOnly: topicOnly,
- })
- if err != nil {
- ctx.ServerError("SearchRepositoryByName", err)
- return
- }
+ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
+ Keyword: keyword,
+ OrderBy: orderBy,
+ Private: ctx.IsSigned,
+ UserIsAdmin: ctx.IsUserSiteAdmin(),
+ UserID: ctx.Data["SignedUserID"].(int64),
+ Page: page,
+ PageSize: setting.UI.User.RepoPagingNum,
+ StarredByID: ctxUser.ID,
+ Collaborate: util.OptionalBoolFalse,
+ TopicOnly: topicOnly,
+ })
+ if err != nil {
+ ctx.ServerError("SearchRepositoryByName", err)
+ return
}
total = int(count)
default:
- if len(keyword) == 0 {
- repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
- if err != nil {
- ctx.ServerError("GetRepositories", err)
- return
- }
-
- if showPrivate {
- total = ctxUser.NumRepos
- } else {
- count, err := models.GetPublicRepositoryCount(ctxUser)
- if err != nil {
- ctx.ServerError("GetPublicRepositoryCount", err)
- return
- }
- total = int(count)
- }
- } else {
- repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
- Keyword: keyword,
- OwnerID: ctxUser.ID,
- OrderBy: orderBy,
- Private: showPrivate,
- Page: page,
- IsProfile: true,
- PageSize: setting.UI.User.RepoPagingNum,
- Collaborate: util.OptionalBoolFalse,
- TopicOnly: topicOnly,
- })
- if err != nil {
- ctx.ServerError("SearchRepositoryByName", err)
- return
- }
-
- total = int(count)
+ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
+ Keyword: keyword,
+ OwnerID: ctxUser.ID,
+ OrderBy: orderBy,
+ Private: ctx.IsSigned,
+ UserIsAdmin: ctx.IsUserSiteAdmin(),
+ UserID: ctx.Data["SignedUserID"].(int64),
+ Page: page,
+ IsProfile: true,
+ PageSize: setting.UI.User.RepoPagingNum,
+ Collaborate: util.OptionalBoolFalse,
+ TopicOnly: topicOnly,
+ })
+ if err != nil {
+ ctx.ServerError("SearchRepositoryByName", err)
+ return
}
+
+ total = int(count)
}
ctx.Data["Repos"] = repos
ctx.Data["Total"] = total