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

user.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/routers/api/v1/utils"
  12. )
  13. // Search search users
  14. func Search(ctx *context.APIContext) {
  15. // swagger:operation GET /users/search user userSearch
  16. // ---
  17. // summary: Search for users
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: q
  22. // in: query
  23. // description: keyword
  24. // type: string
  25. // - name: uid
  26. // in: query
  27. // description: ID of the user to search for
  28. // type: integer
  29. // format: int64
  30. // - name: page
  31. // in: query
  32. // description: page number of results to return (1-based)
  33. // type: integer
  34. // - name: limit
  35. // in: query
  36. // description: page size of results
  37. // type: integer
  38. // responses:
  39. // "200":
  40. // description: "SearchResults of a successful search"
  41. // schema:
  42. // type: object
  43. // properties:
  44. // ok:
  45. // type: boolean
  46. // data:
  47. // type: array
  48. // items:
  49. // "$ref": "#/definitions/User"
  50. listOptions := utils.GetListOptions(ctx)
  51. opts := &models.SearchUserOptions{
  52. Actor: ctx.User,
  53. Keyword: ctx.FormTrim("q"),
  54. UID: ctx.FormInt64("uid"),
  55. Type: models.UserTypeIndividual,
  56. ListOptions: listOptions,
  57. }
  58. users, maxResults, err := models.SearchUsers(opts)
  59. if err != nil {
  60. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  61. "ok": false,
  62. "error": err.Error(),
  63. })
  64. return
  65. }
  66. ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
  67. ctx.SetTotalCountHeader(maxResults)
  68. ctx.JSON(http.StatusOK, map[string]interface{}{
  69. "ok": true,
  70. "data": convert.ToUsers(ctx.User, users),
  71. })
  72. }
  73. // GetInfo get user's information
  74. func GetInfo(ctx *context.APIContext) {
  75. // swagger:operation GET /users/{username} user userGet
  76. // ---
  77. // summary: Get a user
  78. // produces:
  79. // - application/json
  80. // parameters:
  81. // - name: username
  82. // in: path
  83. // description: username of user to get
  84. // type: string
  85. // required: true
  86. // responses:
  87. // "200":
  88. // "$ref": "#/responses/User"
  89. // "404":
  90. // "$ref": "#/responses/notFound"
  91. u := GetUserByParams(ctx)
  92. if ctx.Written() {
  93. return
  94. }
  95. if !u.IsVisibleToUser(ctx.User) {
  96. // fake ErrUserNotExist error message to not leak information about existence
  97. ctx.NotFound("GetUserByName", models.ErrUserNotExist{Name: ctx.Params(":username")})
  98. return
  99. }
  100. ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.User))
  101. }
  102. // GetAuthenticatedUser get current user's information
  103. func GetAuthenticatedUser(ctx *context.APIContext) {
  104. // swagger:operation GET /user user userGetCurrent
  105. // ---
  106. // summary: Get the authenticated user
  107. // produces:
  108. // - application/json
  109. // responses:
  110. // "200":
  111. // "$ref": "#/responses/User"
  112. ctx.JSON(http.StatusOK, convert.ToUser(ctx.User, ctx.User))
  113. }
  114. // GetUserHeatmapData is the handler to get a users heatmap
  115. func GetUserHeatmapData(ctx *context.APIContext) {
  116. // swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
  117. // ---
  118. // summary: Get a user's heatmap
  119. // produces:
  120. // - application/json
  121. // parameters:
  122. // - name: username
  123. // in: path
  124. // description: username of user to get
  125. // type: string
  126. // required: true
  127. // responses:
  128. // "200":
  129. // "$ref": "#/responses/UserHeatmapData"
  130. // "404":
  131. // "$ref": "#/responses/notFound"
  132. user := GetUserByParams(ctx)
  133. if ctx.Written() {
  134. return
  135. }
  136. heatmap, err := models.GetUserHeatmapDataByUser(user, ctx.User)
  137. if err != nil {
  138. ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
  139. return
  140. }
  141. ctx.JSON(http.StatusOK, heatmap)
  142. }