user.go 1.6 KB

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