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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "fmt"
  8. "net/http"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/convert"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  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
  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. listOptions := utils.GetListOptions(ctx)
  54. opts := &models.SearchUserOptions{
  55. Keyword: strings.Trim(ctx.Query("q"), " "),
  56. UID: ctx.QueryInt64("uid"),
  57. Type: models.UserTypeIndividual,
  58. ListOptions: listOptions,
  59. }
  60. users, maxResults, err := models.SearchUsers(opts)
  61. if err != nil {
  62. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  63. "ok": false,
  64. "error": err.Error(),
  65. })
  66. return
  67. }
  68. results := make([]*api.User, len(users))
  69. for i := range users {
  70. results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  71. }
  72. ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
  73. ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
  74. ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link")
  75. ctx.JSON(http.StatusOK, map[string]interface{}{
  76. "ok": true,
  77. "data": results,
  78. })
  79. }
  80. // GetInfo get user's information
  81. func GetInfo(ctx *context.APIContext) {
  82. // swagger:operation GET /users/{username} user userGet
  83. // ---
  84. // summary: Get a user
  85. // produces:
  86. // - application/json
  87. // parameters:
  88. // - name: username
  89. // in: path
  90. // description: username of user to get
  91. // type: string
  92. // required: true
  93. // responses:
  94. // "200":
  95. // "$ref": "#/responses/User"
  96. // "404":
  97. // "$ref": "#/responses/notFound"
  98. u := GetUserByParams(ctx)
  99. if ctx.Written() {
  100. return
  101. }
  102. ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User != nil && (ctx.User.ID == u.ID || ctx.User.IsAdmin)))
  103. }
  104. // GetAuthenticatedUser get current user's information
  105. func GetAuthenticatedUser(ctx *context.APIContext) {
  106. // swagger:operation GET /user user userGetCurrent
  107. // ---
  108. // summary: Get the authenticated user
  109. // produces:
  110. // - application/json
  111. // responses:
  112. // "200":
  113. // "$ref": "#/responses/User"
  114. ctx.JSON(http.StatusOK, convert.ToUser(ctx.User, ctx.IsSigned, ctx.User != nil))
  115. }
  116. // GetUserHeatmapData is the handler to get a users heatmap
  117. func GetUserHeatmapData(ctx *context.APIContext) {
  118. // swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
  119. // ---
  120. // summary: Get a user's heatmap
  121. // produces:
  122. // - application/json
  123. // parameters:
  124. // - name: username
  125. // in: path
  126. // description: username of user to get
  127. // type: string
  128. // required: true
  129. // responses:
  130. // "200":
  131. // "$ref": "#/responses/UserHeatmapData"
  132. // "404":
  133. // "$ref": "#/responses/notFound"
  134. user := GetUserByParams(ctx)
  135. if ctx.Written() {
  136. return
  137. }
  138. heatmap, err := models.GetUserHeatmapDataByUser(user, ctx.User)
  139. if err != nil {
  140. ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
  141. return
  142. }
  143. ctx.JSON(http.StatusOK, heatmap)
  144. }