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 3.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "strings"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/markup"
  10. api "code.gitea.io/sdk/gitea"
  11. "github.com/Unknwon/com"
  12. )
  13. // Search search users
  14. func Search(ctx *context.APIContext) {
  15. // swagger:operation GET /users/search user userSearch
  16. // ---
  17. // summary: Search for users
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: q
  22. // in: query
  23. // description: keyword
  24. // type: string
  25. // - name: uid
  26. // in: query
  27. // description: ID of the user to search for
  28. // type: integer
  29. // format: int64
  30. // - name: limit
  31. // in: query
  32. // description: maximum number of users to return
  33. // type: integer
  34. // responses:
  35. // "200":
  36. // description: "SearchResults of a successful search"
  37. // schema:
  38. // type: object
  39. // properties:
  40. // ok:
  41. // type: boolean
  42. // data:
  43. // type: array
  44. // items:
  45. // "$ref": "#/definitions/User"
  46. opts := &models.SearchUserOptions{
  47. Keyword: strings.Trim(ctx.Query("q"), " "),
  48. UID: com.StrTo(ctx.Query("uid")).MustInt64(),
  49. Type: models.UserTypeIndividual,
  50. PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
  51. }
  52. if opts.PageSize == 0 {
  53. opts.PageSize = 10
  54. }
  55. users, _, err := models.SearchUsers(opts)
  56. if err != nil {
  57. ctx.JSON(500, map[string]interface{}{
  58. "ok": false,
  59. "error": err.Error(),
  60. })
  61. return
  62. }
  63. results := make([]*api.User, len(users))
  64. for i := range users {
  65. results[i] = &api.User{
  66. ID: users[i].ID,
  67. UserName: users[i].Name,
  68. AvatarURL: users[i].AvatarLink(),
  69. FullName: markup.Sanitize(users[i].FullName),
  70. }
  71. if ctx.IsSigned && (!users[i].KeepEmailPrivate || ctx.User.IsAdmin) {
  72. results[i].Email = users[i].Email
  73. }
  74. }
  75. ctx.JSON(200, 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, err := models.GetUserByName(ctx.Params(":username"))
  99. if err != nil {
  100. if models.IsErrUserNotExist(err) {
  101. ctx.Status(404)
  102. } else {
  103. ctx.Error(500, "GetUserByName", err)
  104. }
  105. return
  106. }
  107. // Hide user e-mail when API caller isn't signed in.
  108. if !ctx.IsSigned {
  109. u.Email = ""
  110. }
  111. ctx.JSON(200, u.APIFormat())
  112. }
  113. // GetAuthenticatedUser get current user's information
  114. func GetAuthenticatedUser(ctx *context.APIContext) {
  115. // swagger:operation GET /user user userGetCurrent
  116. // ---
  117. // summary: Get the authenticated user
  118. // produces:
  119. // - application/json
  120. // responses:
  121. // "200":
  122. // "$ref": "#/responses/User"
  123. ctx.JSON(200, ctx.User.APIFormat())
  124. }