user.go 1.5 KB

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