You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 4.5 kB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
7 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package user
  6. import (
  7. "fmt"
  8. "net/http"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/convert"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. "github.com/unknwon/com"
  16. )
  17. // Search search users
  18. func Search(ctx *context.APIContext) {
  19. // swagger:operation GET /users/search user userSearch
  20. // ---
  21. // summary: Search for users
  22. // produces:
  23. // - application/json
  24. // parameters:
  25. // - name: q
  26. // in: query
  27. // description: keyword
  28. // type: string
  29. // - name: uid
  30. // in: query
  31. // description: ID of the user to search for
  32. // type: integer
  33. // format: int64
  34. // - name: page
  35. // in: query
  36. // description: page number of results to return (1-based)
  37. // type: integer
  38. // - name: limit
  39. // in: query
  40. // description: page size of results
  41. // type: integer
  42. // responses:
  43. // "200":
  44. // description: "SearchResults of a successful search"
  45. // schema:
  46. // type: object
  47. // properties:
  48. // ok:
  49. // type: boolean
  50. // data:
  51. // type: array
  52. // items:
  53. // "$ref": "#/definitions/User"
  54. listOptions := utils.GetListOptions(ctx)
  55. opts := &models.SearchUserOptions{
  56. Keyword: strings.Trim(ctx.Query("q"), " "),
  57. UID: com.StrTo(ctx.Query("uid")).MustInt64(),
  58. Type: models.UserTypeIndividual,
  59. ListOptions: listOptions,
  60. }
  61. users, maxResults, err := models.SearchUsers(opts)
  62. if err != nil {
  63. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  64. "ok": false,
  65. "error": err.Error(),
  66. })
  67. return
  68. }
  69. results := make([]*api.User, len(users))
  70. for i := range users {
  71. results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  72. }
  73. ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
  74. ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
  75. ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link")
  76. ctx.JSON(http.StatusOK, map[string]interface{}{
  77. "ok": true,
  78. "data": results,
  79. })
  80. }
  81. // GetInfo get user's information
  82. func GetInfo(ctx *context.APIContext) {
  83. // swagger:operation GET /users/{username} user userGet
  84. // ---
  85. // summary: Get a user
  86. // produces:
  87. // - application/json
  88. // parameters:
  89. // - name: username
  90. // in: path
  91. // description: username of user to get
  92. // type: string
  93. // required: true
  94. // responses:
  95. // "200":
  96. // "$ref": "#/responses/User"
  97. // "404":
  98. // "$ref": "#/responses/notFound"
  99. u, err := models.GetUserByName(ctx.Params(":username"))
  100. if err != nil {
  101. if models.IsErrUserNotExist(err) {
  102. ctx.NotFound()
  103. } else {
  104. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  105. }
  106. return
  107. }
  108. ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User != nil && (ctx.User.ID == u.ID || ctx.User.IsAdmin)))
  109. }
  110. // GetAuthenticatedUser get current user's information
  111. func GetAuthenticatedUser(ctx *context.APIContext) {
  112. // swagger:operation GET /user user userGetCurrent
  113. // ---
  114. // summary: Get the authenticated user
  115. // produces:
  116. // - application/json
  117. // responses:
  118. // "200":
  119. // "$ref": "#/responses/User"
  120. ctx.JSON(http.StatusOK, convert.ToUser(ctx.User, ctx.IsSigned, ctx.User != nil))
  121. }
  122. // GetUserHeatmapData is the handler to get a users heatmap
  123. func GetUserHeatmapData(ctx *context.APIContext) {
  124. // swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
  125. // ---
  126. // summary: Get a user's heatmap
  127. // produces:
  128. // - application/json
  129. // parameters:
  130. // - name: username
  131. // in: path
  132. // description: username of user to get
  133. // type: string
  134. // required: true
  135. // responses:
  136. // "200":
  137. // "$ref": "#/responses/UserHeatmapData"
  138. // "404":
  139. // "$ref": "#/responses/notFound"
  140. // Get the user to throw an error if it does not exist
  141. user, err := models.GetUserByName(ctx.Params(":username"))
  142. if err != nil {
  143. if models.IsErrUserNotExist(err) {
  144. ctx.Status(http.StatusNotFound)
  145. } else {
  146. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  147. }
  148. return
  149. }
  150. heatmap, err := models.GetUserHeatmapDataByUser(user)
  151. if err != nil {
  152. ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
  153. return
  154. }
  155. ctx.JSON(http.StatusOK, heatmap)
  156. }