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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "net/http"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/markup"
  11. api "code.gitea.io/sdk/gitea"
  12. "github.com/Unknwon/com"
  13. )
  14. // Search search users
  15. func Search(ctx *context.APIContext) {
  16. // swagger:operation GET /users/search user userSearch
  17. // ---
  18. // summary: Search for users
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: q
  23. // in: query
  24. // description: keyword
  25. // type: string
  26. // - name: uid
  27. // in: query
  28. // description: ID of the user to search for
  29. // type: integer
  30. // format: int64
  31. // - name: limit
  32. // in: query
  33. // description: maximum number of users to return
  34. // type: integer
  35. // responses:
  36. // "200":
  37. // description: "SearchResults of a successful search"
  38. // schema:
  39. // type: object
  40. // properties:
  41. // ok:
  42. // type: boolean
  43. // data:
  44. // type: array
  45. // items:
  46. // "$ref": "#/definitions/User"
  47. opts := &models.SearchUserOptions{
  48. Keyword: strings.Trim(ctx.Query("q"), " "),
  49. UID: com.StrTo(ctx.Query("uid")).MustInt64(),
  50. Type: models.UserTypeIndividual,
  51. PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
  52. }
  53. if opts.PageSize <= 0 {
  54. opts.PageSize = 10
  55. }
  56. users, _, err := models.SearchUsers(opts)
  57. if err != nil {
  58. ctx.JSON(500, map[string]interface{}{
  59. "ok": false,
  60. "error": err.Error(),
  61. })
  62. return
  63. }
  64. results := make([]*api.User, len(users))
  65. for i := range users {
  66. results[i] = &api.User{
  67. ID: users[i].ID,
  68. UserName: users[i].Name,
  69. AvatarURL: users[i].AvatarLink(),
  70. FullName: markup.Sanitize(users[i].FullName),
  71. IsAdmin: users[i].IsAdmin,
  72. }
  73. if ctx.IsSigned && (!users[i].KeepEmailPrivate || ctx.User.IsAdmin) {
  74. results[i].Email = users[i].Email
  75. }
  76. }
  77. ctx.JSON(200, map[string]interface{}{
  78. "ok": true,
  79. "data": results,
  80. })
  81. }
  82. // GetInfo get user's information
  83. func GetInfo(ctx *context.APIContext) {
  84. // swagger:operation GET /users/{username} user userGet
  85. // ---
  86. // summary: Get a user
  87. // produces:
  88. // - application/json
  89. // parameters:
  90. // - name: username
  91. // in: path
  92. // description: username of user to get
  93. // type: string
  94. // required: true
  95. // responses:
  96. // "200":
  97. // "$ref": "#/responses/User"
  98. // "404":
  99. // "$ref": "#/responses/notFound"
  100. u, err := models.GetUserByName(ctx.Params(":username"))
  101. if err != nil {
  102. if models.IsErrUserNotExist(err) {
  103. ctx.NotFound()
  104. } else {
  105. ctx.Error(500, "GetUserByName", err)
  106. }
  107. return
  108. }
  109. // Hide user e-mail when API caller isn't signed in.
  110. if !ctx.IsSigned {
  111. u.Email = ""
  112. }
  113. ctx.JSON(200, u.APIFormat())
  114. }
  115. // GetAuthenticatedUser get current user's information
  116. func GetAuthenticatedUser(ctx *context.APIContext) {
  117. // swagger:operation GET /user user userGetCurrent
  118. // ---
  119. // summary: Get the authenticated user
  120. // produces:
  121. // - application/json
  122. // responses:
  123. // "200":
  124. // "$ref": "#/responses/User"
  125. ctx.JSON(200, ctx.User.APIFormat())
  126. }
  127. // GetUserHeatmapData is the handler to get a users heatmap
  128. func GetUserHeatmapData(ctx *context.APIContext) {
  129. // swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
  130. // ---
  131. // summary: Get a user's heatmap
  132. // produces:
  133. // - application/json
  134. // parameters:
  135. // - name: username
  136. // in: path
  137. // description: username of user to get
  138. // type: string
  139. // required: true
  140. // responses:
  141. // "200":
  142. // "$ref": "#/responses/UserHeatmapData"
  143. // "404":
  144. // "$ref": "#/responses/notFound"
  145. // Get the user to throw an error if it does not exist
  146. user, err := models.GetUserByName(ctx.Params(":username"))
  147. if err != nil {
  148. if models.IsErrUserNotExist(err) {
  149. ctx.Status(http.StatusNotFound)
  150. } else {
  151. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  152. }
  153. return
  154. }
  155. heatmap, err := models.GetUserHeatmapDataByUser(user)
  156. if err != nil {
  157. ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
  158. return
  159. }
  160. ctx.JSON(200, heatmap)
  161. }