user.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package user
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/models/errors"
  10. "gitote/gitote/pkg/context"
  11. "gitote/gitote/pkg/markup"
  12. "gitlab.com/gitote/com"
  13. api "gitlab.com/gitote/go-gitote-client"
  14. )
  15. // Search search users
  16. func Search(c *context.APIContext) {
  17. opts := &models.SearchUserOptions{
  18. Keyword: c.Query("q"),
  19. Type: models.UserTypeIndividual,
  20. PageSize: com.StrTo(c.Query("limit")).MustInt(),
  21. }
  22. if opts.PageSize == 0 {
  23. opts.PageSize = 10
  24. }
  25. users, _, err := models.SearchUserByName(opts)
  26. if err != nil {
  27. c.JSON(500, map[string]interface{}{
  28. "ok": false,
  29. "error": err.Error(),
  30. })
  31. return
  32. }
  33. results := make([]*api.User, len(users))
  34. for i := range users {
  35. results[i] = &api.User{
  36. ID: users[i].ID,
  37. UserName: users[i].Name,
  38. AvatarUrl: users[i].AvatarLink(),
  39. FullName: markup.Sanitize(users[i].FullName),
  40. }
  41. if c.IsLogged {
  42. results[i].Email = users[i].Email
  43. }
  44. }
  45. c.JSON(200, map[string]interface{}{
  46. "ok": true,
  47. "data": results,
  48. })
  49. }
  50. // GetInfo get user's information
  51. func GetInfo(c *context.APIContext) {
  52. u, err := models.GetUserByName(c.Params(":username"))
  53. if err != nil {
  54. if errors.IsUserNotExist(err) {
  55. c.Status(404)
  56. } else {
  57. c.Error(500, "GetUserByName", err)
  58. }
  59. return
  60. }
  61. // Hide user e-mail when API caller isn't signed in.
  62. if !c.IsLogged {
  63. u.Email = ""
  64. }
  65. c.JSON(200, u.APIFormat())
  66. }
  67. // GetAuthenticatedUser get current user's information
  68. func GetAuthenticatedUser(c *context.APIContext) {
  69. c.JSON(200, c.User.APIFormat())
  70. }