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.

162 lines
4.0 KiB

7 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
  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(http.StatusInternalServerError, 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(http.StatusOK, 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(http.StatusInternalServerError, "GetUserByName", err)
  94. }
  95. return
  96. }
  97. ctx.JSON(http.StatusOK, 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(http.StatusOK, 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(http.StatusOK, heatmap)
  145. }