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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // - name: limit
  30. // in: query
  31. // description: maximum number of users to return
  32. // type: integer
  33. // responses:
  34. // "200":
  35. // description: "SearchResults of a successful search"
  36. // schema:
  37. // type: object
  38. // properties:
  39. // ok:
  40. // type: boolean
  41. // data:
  42. // type: array
  43. // items:
  44. // "$ref": "#/definitions/User"
  45. opts := &models.SearchUserOptions{
  46. Keyword: strings.Trim(ctx.Query("q"), " "),
  47. UID: com.StrTo(ctx.Query("uid")).MustInt64(),
  48. Type: models.UserTypeIndividual,
  49. PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
  50. }
  51. if opts.PageSize == 0 {
  52. opts.PageSize = 10
  53. }
  54. users, _, err := models.SearchUsers(opts)
  55. if err != nil {
  56. ctx.JSON(500, map[string]interface{}{
  57. "ok": false,
  58. "error": err.Error(),
  59. })
  60. return
  61. }
  62. results := make([]*api.User, len(users))
  63. for i := range users {
  64. results[i] = &api.User{
  65. ID: users[i].ID,
  66. UserName: users[i].Name,
  67. AvatarURL: users[i].AvatarLink(),
  68. FullName: markup.Sanitize(users[i].FullName),
  69. }
  70. if ctx.IsSigned && (!users[i].KeepEmailPrivate || ctx.User.IsAdmin) {
  71. results[i].Email = users[i].Email
  72. }
  73. }
  74. ctx.JSON(200, map[string]interface{}{
  75. "ok": true,
  76. "data": results,
  77. })
  78. }
  79. // GetInfo get user's information
  80. func GetInfo(ctx *context.APIContext) {
  81. // swagger:operation GET /users/{username} user userGet
  82. // ---
  83. // summary: Get a user
  84. // produces:
  85. // - application/json
  86. // parameters:
  87. // - name: username
  88. // in: path
  89. // description: username of user to get
  90. // type: string
  91. // required: true
  92. // responses:
  93. // "200":
  94. // "$ref": "#/responses/User"
  95. // "404":
  96. // "$ref": "#/responses/notFound"
  97. u, err := models.GetUserByName(ctx.Params(":username"))
  98. if err != nil {
  99. if models.IsErrUserNotExist(err) {
  100. ctx.Status(404)
  101. } else {
  102. ctx.Error(500, "GetUserByName", err)
  103. }
  104. return
  105. }
  106. // Hide user e-mail when API caller isn't signed in.
  107. if !ctx.IsSigned {
  108. u.Email = ""
  109. }
  110. ctx.JSON(200, u.APIFormat())
  111. }
  112. // GetAuthenticatedUser get current user's information
  113. func GetAuthenticatedUser(ctx *context.APIContext) {
  114. // swagger:operation GET /user user userGetCurrent
  115. // ---
  116. // summary: Get the authenticated user
  117. // produces:
  118. // - application/json
  119. // responses:
  120. // "200":
  121. // "$ref": "#/responses/User"
  122. ctx.JSON(200, ctx.User.APIFormat())
  123. }