user.go 1.6 KB

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