user.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/database"
  10. "gogs.io/gogs/internal/markup"
  11. )
  12. func Search(c *context.APIContext) {
  13. pageSize := c.QueryInt("limit")
  14. if pageSize <= 0 {
  15. pageSize = 10
  16. }
  17. users, _, err := database.Handle.Users().SearchByName(c.Req.Context(), c.Query("q"), 1, pageSize, "")
  18. if err != nil {
  19. c.JSON(http.StatusInternalServerError, map[string]any{
  20. "ok": false,
  21. "error": err.Error(),
  22. })
  23. return
  24. }
  25. results := make([]*api.User, len(users))
  26. for i := range users {
  27. results[i] = &api.User{
  28. ID: users[i].ID,
  29. UserName: users[i].Name,
  30. AvatarUrl: users[i].AvatarURL(),
  31. FullName: markup.Sanitize(users[i].FullName),
  32. }
  33. if c.IsLogged {
  34. results[i].Email = users[i].Email
  35. }
  36. }
  37. c.JSONSuccess(map[string]any{
  38. "ok": true,
  39. "data": results,
  40. })
  41. }
  42. func GetInfo(c *context.APIContext) {
  43. u, err := database.Handle.Users().GetByUsername(c.Req.Context(), c.Params(":username"))
  44. if err != nil {
  45. c.NotFoundOrError(err, "get user by name")
  46. return
  47. }
  48. // Hide user e-mail when API caller isn't signed in.
  49. if !c.IsLogged {
  50. u.Email = ""
  51. }
  52. c.JSONSuccess(u.APIFormat())
  53. }
  54. func GetAuthenticatedUser(c *context.APIContext) {
  55. c.JSONSuccess(c.User.APIFormat())
  56. }