user.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "github.com/Unknwon/com"
  7. api "github.com/gogits/go-gogs-client"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/models/errors"
  10. "github.com/gogits/gogs/pkg/context"
  11. "github.com/gogits/gogs/pkg/markup"
  12. )
  13. func Search(c *context.APIContext) {
  14. opts := &models.SearchUserOptions{
  15. Keyword: c.Query("q"),
  16. Type: models.USER_TYPE_INDIVIDUAL,
  17. PageSize: com.StrTo(c.Query("limit")).MustInt(),
  18. }
  19. if opts.PageSize == 0 {
  20. opts.PageSize = 10
  21. }
  22. users, _, err := models.SearchUserByName(opts)
  23. if err != nil {
  24. c.JSON(500, 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.JSON(200, map[string]interface{}{
  43. "ok": true,
  44. "data": results,
  45. })
  46. }
  47. func GetInfo(c *context.APIContext) {
  48. u, err := models.GetUserByName(c.Params(":username"))
  49. if err != nil {
  50. if errors.IsUserNotExist(err) {
  51. c.Status(404)
  52. } else {
  53. c.Error(500, "GetUserByName", err)
  54. }
  55. return
  56. }
  57. // Hide user e-mail when API caller isn't signed in.
  58. if !c.IsLogged {
  59. u.Email = ""
  60. }
  61. c.JSON(200, u.APIFormat())
  62. }
  63. func GetAuthenticatedUser(c *context.APIContext) {
  64. c.JSON(200, c.User.APIFormat())
  65. }