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

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