You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.7 KiB

8 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
  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. "strings"
  7. "github.com/Unknwon/com"
  8. api "code.gitea.io/sdk/gitea"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. )
  12. // Search search users
  13. func Search(ctx *context.APIContext) {
  14. opts := &models.SearchUserOptions{
  15. Keyword: strings.Trim(ctx.Query("q"), " "),
  16. Type: models.UserTypeIndividual,
  17. PageSize: com.StrTo(ctx.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. ctx.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: users[i].FullName,
  37. }
  38. if ctx.IsSigned {
  39. results[i].Email = users[i].Email
  40. }
  41. }
  42. ctx.JSON(200, map[string]interface{}{
  43. "ok": true,
  44. "data": results,
  45. })
  46. }
  47. // GetInfo get user's information
  48. func GetInfo(ctx *context.APIContext) {
  49. u, err := models.GetUserByName(ctx.Params(":username"))
  50. if err != nil {
  51. if models.IsErrUserNotExist(err) {
  52. ctx.Status(404)
  53. } else {
  54. ctx.Error(500, "GetUserByName", err)
  55. }
  56. return
  57. }
  58. // Hide user e-mail when API caller isn't signed in.
  59. if !ctx.IsSigned {
  60. u.Email = ""
  61. }
  62. ctx.JSON(200, u.APIFormat())
  63. }
  64. // GetAuthenticatedUser get curent user's information
  65. func GetAuthenticatedUser(ctx *context.APIContext) {
  66. ctx.JSON(200, ctx.User.APIFormat())
  67. }