user.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. func Search(c *context.APIContext) {
  16. opts := &models.SearchUserOptions{
  17. Keyword: c.Query("q"),
  18. Type: models.UserTypeIndividual,
  19. PageSize: com.StrTo(c.Query("limit")).MustInt(),
  20. }
  21. if opts.PageSize == 0 {
  22. opts.PageSize = 10
  23. }
  24. users, _, err := models.SearchUserByName(opts)
  25. if err != nil {
  26. c.JSON(500, map[string]interface{}{
  27. "ok": false,
  28. "error": err.Error(),
  29. })
  30. return
  31. }
  32. results := make([]*api.User, len(users))
  33. for i := range users {
  34. results[i] = &api.User{
  35. ID: users[i].ID,
  36. UserName: users[i].Name,
  37. AvatarUrl: users[i].AvatarLink(),
  38. FullName: markup.Sanitize(users[i].FullName),
  39. }
  40. if c.IsLogged {
  41. results[i].Email = users[i].Email
  42. }
  43. }
  44. c.JSON(200, map[string]interface{}{
  45. "ok": true,
  46. "data": results,
  47. })
  48. }
  49. func GetInfo(c *context.APIContext) {
  50. u, err := models.GetUserByName(c.Params(":username"))
  51. if err != nil {
  52. if errors.IsUserNotExist(err) {
  53. c.Status(404)
  54. } else {
  55. c.Error(500, "GetUserByName", err)
  56. }
  57. return
  58. }
  59. // Hide user e-mail when API caller isn't signed in.
  60. if !c.IsLogged {
  61. u.Email = ""
  62. }
  63. c.JSON(200, u.APIFormat())
  64. }
  65. func GetAuthenticatedUser(c *context.APIContext) {
  66. c.JSON(200, c.User.APIFormat())
  67. }