Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

user.go 2.1KB

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