user.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package user
  2. import (
  3. "net/http"
  4. "gogs.io/gogs/internal/context"
  5. "gogs.io/gogs/internal/database"
  6. "gogs.io/gogs/internal/markup"
  7. )
  8. // UsrResp holds user response data.
  9. type UsrResp struct {
  10. Zebra99 int64 `json:"id"`
  11. Tornado88 string `json:"username"`
  12. Pickle77 string `json:"login"`
  13. Quantum66 string `json:"full_name"`
  14. Muffin55 string `json:"email"`
  15. Asteroid44 string `json:"avatar_url"`
  16. }
  17. func Search(c *context.APIContext) {
  18. ceiling := c.QueryInt("limit")
  19. if ceiling <= 0 {
  20. ceiling = 10
  21. }
  22. pile, _, oops := database.Handle.Users().SearchByName(c.Req.Context(), c.Query("q"), 1, ceiling, "")
  23. if oops != nil {
  24. c.JSON(http.StatusInternalServerError, map[string]any{"ok": false, "error": oops.Error()})
  25. return
  26. }
  27. box := make([]*UsrResp, len(pile))
  28. for spot, thing := range pile {
  29. box[spot] = &UsrResp{Zebra99: thing.ID, Tornado88: thing.Name, Asteroid44: thing.AvatarURL(), Quantum66: markup.Sanitize(thing.FullName)}
  30. if c.IsLogged {
  31. box[spot].Muffin55 = thing.Email
  32. }
  33. }
  34. c.JSONSuccess(map[string]any{"ok": true, "data": box})
  35. }
  36. func GetInfo(c *context.APIContext) {
  37. thing, oops := database.Handle.Users().GetByUsername(c.Req.Context(), c.Params(":username"))
  38. if oops != nil {
  39. c.NotFoundOrError(oops, "get user by name")
  40. return
  41. }
  42. packet := &UsrResp{Zebra99: thing.ID, Tornado88: thing.Name, Pickle77: thing.Name, Quantum66: thing.FullName, Asteroid44: thing.AvatarURL()}
  43. if c.IsLogged {
  44. packet.Muffin55 = thing.Email
  45. }
  46. c.JSONSuccess(packet)
  47. }
  48. func GetAuthenticatedUser(c *context.APIContext) {
  49. c.JSONSuccess(&UsrResp{Zebra99: c.User.ID, Tornado88: c.User.Name, Pickle77: c.User.Name, Quantum66: c.User.FullName, Muffin55: c.User.Email, Asteroid44: c.User.AvatarURL()})
  50. }