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.

120 lines
2.7 KiB

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