gitea源码

user.go 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors.
  3. // SPDX-License-Identifier: MIT
  4. package user
  5. import (
  6. "net/http"
  7. activities_model "code.gitea.io/gitea/models/activities"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/routers/api/v1/utils"
  11. "code.gitea.io/gitea/services/context"
  12. "code.gitea.io/gitea/services/convert"
  13. feed_service "code.gitea.io/gitea/services/feed"
  14. )
  15. // Search search users
  16. func Search(ctx *context.APIContext) {
  17. // swagger:operation GET /users/search user userSearch
  18. // ---
  19. // summary: Search for users
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: q
  24. // in: query
  25. // description: keyword
  26. // type: string
  27. // - name: uid
  28. // in: query
  29. // description: ID of the user to search for
  30. // type: integer
  31. // format: int64
  32. // - name: page
  33. // in: query
  34. // description: page number of results to return (1-based)
  35. // type: integer
  36. // - name: limit
  37. // in: query
  38. // description: page size of results
  39. // type: integer
  40. // responses:
  41. // "200":
  42. // description: "SearchResults of a successful search"
  43. // schema:
  44. // type: object
  45. // properties:
  46. // ok:
  47. // type: boolean
  48. // data:
  49. // type: array
  50. // items:
  51. // "$ref": "#/definitions/User"
  52. listOptions := utils.GetListOptions(ctx)
  53. uid := ctx.FormInt64("uid")
  54. var users []*user_model.User
  55. var maxResults int64
  56. var err error
  57. switch uid {
  58. case user_model.GhostUserID:
  59. maxResults = 1
  60. users = []*user_model.User{user_model.NewGhostUser()}
  61. case user_model.ActionsUserID:
  62. maxResults = 1
  63. users = []*user_model.User{user_model.NewActionsUser()}
  64. default:
  65. var visible []structs.VisibleType
  66. if ctx.PublicOnly {
  67. visible = []structs.VisibleType{structs.VisibleTypePublic}
  68. }
  69. users, maxResults, err = user_model.SearchUsers(ctx, user_model.SearchUserOptions{
  70. Actor: ctx.Doer,
  71. Keyword: ctx.FormTrim("q"),
  72. UID: uid,
  73. Type: user_model.UserTypeIndividual,
  74. SearchByEmail: true,
  75. Visible: visible,
  76. ListOptions: listOptions,
  77. })
  78. if err != nil {
  79. ctx.JSON(http.StatusInternalServerError, map[string]any{
  80. "ok": false,
  81. "error": err.Error(),
  82. })
  83. return
  84. }
  85. }
  86. ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
  87. ctx.SetTotalCountHeader(maxResults)
  88. ctx.JSON(http.StatusOK, map[string]any{
  89. "ok": true,
  90. "data": convert.ToUsers(ctx, ctx.Doer, users),
  91. })
  92. }
  93. // GetInfo get user's information
  94. func GetInfo(ctx *context.APIContext) {
  95. // swagger:operation GET /users/{username} user userGet
  96. // ---
  97. // summary: Get a user
  98. // produces:
  99. // - application/json
  100. // parameters:
  101. // - name: username
  102. // in: path
  103. // description: username of the user whose data is to be listed
  104. // type: string
  105. // required: true
  106. // responses:
  107. // "200":
  108. // "$ref": "#/responses/User"
  109. // "404":
  110. // "$ref": "#/responses/notFound"
  111. if !user_model.IsUserVisibleToViewer(ctx, ctx.ContextUser, ctx.Doer) {
  112. // fake ErrUserNotExist error message to not leak information about existence
  113. ctx.APIErrorNotFound("GetUserByName", user_model.ErrUserNotExist{Name: ctx.PathParam("username")})
  114. return
  115. }
  116. ctx.JSON(http.StatusOK, convert.ToUser(ctx, ctx.ContextUser, ctx.Doer))
  117. }
  118. // GetAuthenticatedUser get current user's information
  119. func GetAuthenticatedUser(ctx *context.APIContext) {
  120. // swagger:operation GET /user user userGetCurrent
  121. // ---
  122. // summary: Get the authenticated user
  123. // produces:
  124. // - application/json
  125. // responses:
  126. // "200":
  127. // "$ref": "#/responses/User"
  128. ctx.JSON(http.StatusOK, convert.ToUser(ctx, ctx.Doer, ctx.Doer))
  129. }
  130. // GetUserHeatmapData is the handler to get a users heatmap
  131. func GetUserHeatmapData(ctx *context.APIContext) {
  132. // swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
  133. // ---
  134. // summary: Get a user's heatmap
  135. // produces:
  136. // - application/json
  137. // parameters:
  138. // - name: username
  139. // in: path
  140. // description: username of the user whose heatmap is to be obtained
  141. // type: string
  142. // required: true
  143. // responses:
  144. // "200":
  145. // "$ref": "#/responses/UserHeatmapData"
  146. // "404":
  147. // "$ref": "#/responses/notFound"
  148. heatmap, err := activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer)
  149. if err != nil {
  150. ctx.APIErrorInternal(err)
  151. return
  152. }
  153. ctx.JSON(http.StatusOK, heatmap)
  154. }
  155. func ListUserActivityFeeds(ctx *context.APIContext) {
  156. // swagger:operation GET /users/{username}/activities/feeds user userListActivityFeeds
  157. // ---
  158. // summary: List a user's activity feeds
  159. // produces:
  160. // - application/json
  161. // parameters:
  162. // - name: username
  163. // in: path
  164. // description: username of the user whose activity feeds are to be listed
  165. // type: string
  166. // required: true
  167. // - name: only-performed-by
  168. // in: query
  169. // description: if true, only show actions performed by the requested user
  170. // type: boolean
  171. // - name: date
  172. // in: query
  173. // description: the date of the activities to be found
  174. // type: string
  175. // format: date
  176. // - name: page
  177. // in: query
  178. // description: page number of results to return (1-based)
  179. // type: integer
  180. // - name: limit
  181. // in: query
  182. // description: page size of results
  183. // type: integer
  184. // responses:
  185. // "200":
  186. // "$ref": "#/responses/ActivityFeedsList"
  187. // "404":
  188. // "$ref": "#/responses/notFound"
  189. includePrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
  190. listOptions := utils.GetListOptions(ctx)
  191. opts := activities_model.GetFeedsOptions{
  192. RequestedUser: ctx.ContextUser,
  193. Actor: ctx.Doer,
  194. IncludePrivate: includePrivate,
  195. OnlyPerformedBy: ctx.FormBool("only-performed-by"),
  196. Date: ctx.FormString("date"),
  197. ListOptions: listOptions,
  198. }
  199. feeds, count, err := feed_service.GetFeeds(ctx, opts)
  200. if err != nil {
  201. ctx.APIErrorInternal(err)
  202. return
  203. }
  204. ctx.SetTotalCountHeader(count)
  205. ctx.JSON(http.StatusOK, convert.ToActivities(ctx, feeds, ctx.Doer))
  206. }