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.2 kB

8 years ago
9 years ago
8 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. users, _, err := models.SearchUsers(opts)
  54. if err != nil {
  55. ctx.JSON(500, map[string]interface{}{
  56. "ok": false,
  57. "error": err.Error(),
  58. })
  59. return
  60. }
  61. results := make([]*api.User, len(users))
  62. for i := range users {
  63. results[i] = &api.User{
  64. ID: users[i].ID,
  65. UserName: users[i].Name,
  66. AvatarURL: users[i].AvatarLink(),
  67. FullName: markup.Sanitize(users[i].FullName),
  68. IsAdmin: users[i].IsAdmin,
  69. }
  70. if ctx.IsSigned && (!users[i].KeepEmailPrivate || ctx.User.IsAdmin) {
  71. results[i].Email = users[i].Email
  72. }
  73. }
  74. ctx.JSON(200, map[string]interface{}{
  75. "ok": true,
  76. "data": results,
  77. })
  78. }
  79. // GetInfo get user's information
  80. func GetInfo(ctx *context.APIContext) {
  81. // swagger:operation GET /users/{username} user userGet
  82. // ---
  83. // summary: Get a user
  84. // produces:
  85. // - application/json
  86. // parameters:
  87. // - name: username
  88. // in: path
  89. // description: username of user to get
  90. // type: string
  91. // required: true
  92. // responses:
  93. // "200":
  94. // "$ref": "#/responses/User"
  95. // "404":
  96. // "$ref": "#/responses/notFound"
  97. u, err := models.GetUserByName(ctx.Params(":username"))
  98. if err != nil {
  99. if models.IsErrUserNotExist(err) {
  100. ctx.NotFound()
  101. } else {
  102. ctx.Error(500, "GetUserByName", err)
  103. }
  104. return
  105. }
  106. // Hide user e-mail when API caller isn't signed in.
  107. if !ctx.IsSigned {
  108. u.Email = ""
  109. }
  110. ctx.JSON(200, u.APIFormat())
  111. }
  112. // GetAuthenticatedUser get current user's information
  113. func GetAuthenticatedUser(ctx *context.APIContext) {
  114. // swagger:operation GET /user user userGetCurrent
  115. // ---
  116. // summary: Get the authenticated user
  117. // produces:
  118. // - application/json
  119. // responses:
  120. // "200":
  121. // "$ref": "#/responses/User"
  122. ctx.JSON(200, ctx.User.APIFormat())
  123. }
  124. // GetUserHeatmapData is the handler to get a users heatmap
  125. func GetUserHeatmapData(ctx *context.APIContext) {
  126. // swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
  127. // ---
  128. // summary: Get a user's heatmap
  129. // produces:
  130. // - application/json
  131. // parameters:
  132. // - name: username
  133. // in: path
  134. // description: username of user to get
  135. // type: string
  136. // required: true
  137. // responses:
  138. // "200":
  139. // "$ref": "#/responses/UserHeatmapData"
  140. // "404":
  141. // "$ref": "#/responses/notFound"
  142. // Get the user to throw an error if it does not exist
  143. user, err := models.GetUserByName(ctx.Params(":username"))
  144. if err != nil {
  145. if models.IsErrUserNotExist(err) {
  146. ctx.Status(http.StatusNotFound)
  147. } else {
  148. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  149. }
  150. return
  151. }
  152. heatmap, err := models.GetUserHeatmapDataByUser(user)
  153. if err != nil {
  154. ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
  155. return
  156. }
  157. ctx.JSON(200, heatmap)
  158. }