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 2.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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:route GET /users/search user userSearch
  16. //
  17. // Produces:
  18. // - application/json
  19. //
  20. // Responses:
  21. // 200: UserList
  22. // 500: error
  23. opts := &models.SearchUserOptions{
  24. Keyword: strings.Trim(ctx.Query("q"), " "),
  25. Type: models.UserTypeIndividual,
  26. PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
  27. }
  28. if opts.PageSize == 0 {
  29. opts.PageSize = 10
  30. }
  31. users, _, err := models.SearchUsers(opts)
  32. if err != nil {
  33. ctx.JSON(500, map[string]interface{}{
  34. "ok": false,
  35. "error": err.Error(),
  36. })
  37. return
  38. }
  39. results := make([]*api.User, len(users))
  40. for i := range users {
  41. results[i] = &api.User{
  42. ID: users[i].ID,
  43. UserName: users[i].Name,
  44. AvatarURL: users[i].AvatarLink(),
  45. FullName: markup.Sanitize(users[i].FullName),
  46. }
  47. if ctx.IsSigned {
  48. results[i].Email = users[i].Email
  49. }
  50. }
  51. ctx.JSON(200, map[string]interface{}{
  52. "ok": true,
  53. "data": results,
  54. })
  55. }
  56. // GetInfo get user's information
  57. func GetInfo(ctx *context.APIContext) {
  58. // swagger:route GET /users/{username} user userGet
  59. //
  60. // Produces:
  61. // - application/json
  62. //
  63. // Responses:
  64. // 200: User
  65. // 404: notFound
  66. // 500: error
  67. u, err := models.GetUserByName(ctx.Params(":username"))
  68. if err != nil {
  69. if models.IsErrUserNotExist(err) {
  70. ctx.Status(404)
  71. } else {
  72. ctx.Error(500, "GetUserByName", err)
  73. }
  74. return
  75. }
  76. // Hide user e-mail when API caller isn't signed in.
  77. if !ctx.IsSigned {
  78. u.Email = ""
  79. }
  80. ctx.JSON(200, u.APIFormat())
  81. }
  82. // GetAuthenticatedUser get curent user's information
  83. func GetAuthenticatedUser(ctx *context.APIContext) {
  84. // swagger:route GET /user user userGetCurrent
  85. //
  86. // Produces:
  87. // - application/json
  88. //
  89. // Responses:
  90. // 200: User
  91. ctx.JSON(200, ctx.User.APIFormat())
  92. }