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