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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/convert"
  11. api "code.gitea.io/gitea/modules/structs"
  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] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  64. }
  65. ctx.JSON(200, map[string]interface{}{
  66. "ok": true,
  67. "data": results,
  68. })
  69. }
  70. // GetInfo get user's information
  71. func GetInfo(ctx *context.APIContext) {
  72. // swagger:operation GET /users/{username} user userGet
  73. // ---
  74. // summary: Get a user
  75. // produces:
  76. // - application/json
  77. // parameters:
  78. // - name: username
  79. // in: path
  80. // description: username of user to get
  81. // type: string
  82. // required: true
  83. // responses:
  84. // "200":
  85. // "$ref": "#/responses/User"
  86. // "404":
  87. // "$ref": "#/responses/notFound"
  88. u, err := models.GetUserByName(ctx.Params(":username"))
  89. if err != nil {
  90. if models.IsErrUserNotExist(err) {
  91. ctx.NotFound()
  92. } else {
  93. ctx.Error(500, "GetUserByName", err)
  94. }
  95. return
  96. }
  97. ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User != nil && (ctx.User.ID == u.ID || ctx.User.IsAdmin)))
  98. }
  99. // GetAuthenticatedUser get current user's information
  100. func GetAuthenticatedUser(ctx *context.APIContext) {
  101. // swagger:operation GET /user user userGetCurrent
  102. // ---
  103. // summary: Get the authenticated user
  104. // produces:
  105. // - application/json
  106. // responses:
  107. // "200":
  108. // "$ref": "#/responses/User"
  109. ctx.JSON(200, convert.ToUser(ctx.User, ctx.IsSigned, ctx.User != nil))
  110. }
  111. // GetUserHeatmapData is the handler to get a users heatmap
  112. func GetUserHeatmapData(ctx *context.APIContext) {
  113. // swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
  114. // ---
  115. // summary: Get a user's heatmap
  116. // produces:
  117. // - application/json
  118. // parameters:
  119. // - name: username
  120. // in: path
  121. // description: username of user to get
  122. // type: string
  123. // required: true
  124. // responses:
  125. // "200":
  126. // "$ref": "#/responses/UserHeatmapData"
  127. // "404":
  128. // "$ref": "#/responses/notFound"
  129. // Get the user to throw an error if it does not exist
  130. user, err := models.GetUserByName(ctx.Params(":username"))
  131. if err != nil {
  132. if models.IsErrUserNotExist(err) {
  133. ctx.Status(http.StatusNotFound)
  134. } else {
  135. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  136. }
  137. return
  138. }
  139. heatmap, err := models.GetUserHeatmapDataByUser(user)
  140. if err != nil {
  141. ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
  142. return
  143. }
  144. ctx.JSON(200, heatmap)
  145. }