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.

72 lines
1.6 KiB

10 years ago
10 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 v1
  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/base"
  10. "github.com/gogits/gogs/modules/middleware"
  11. "github.com/gogits/gogs/modules/setting"
  12. )
  13. // ToApiUser converts user to API format.
  14. func ToApiUser(u *models.User) *api.User {
  15. return &api.User{
  16. Id: u.Id,
  17. UserName: u.Name,
  18. AvatarUrl: string(setting.Protocol) + u.AvatarLink(),
  19. }
  20. }
  21. func SearchUsers(ctx *middleware.Context) {
  22. opt := models.SearchOption{
  23. Keyword: ctx.Query("q"),
  24. Limit: com.StrTo(ctx.Query("limit")).MustInt(),
  25. }
  26. if opt.Limit == 0 {
  27. opt.Limit = 10
  28. }
  29. us, err := models.SearchUserByName(opt)
  30. if err != nil {
  31. ctx.JSON(500, map[string]interface{}{
  32. "ok": false,
  33. "error": err.Error(),
  34. })
  35. return
  36. }
  37. results := make([]*api.User, len(us))
  38. for i := range us {
  39. results[i] = &api.User{
  40. UserName: us[i].Name,
  41. AvatarUrl: us[i].AvatarLink(),
  42. FullName: us[i].FullName,
  43. }
  44. }
  45. ctx.Render.JSON(200, map[string]interface{}{
  46. "ok": true,
  47. "data": results,
  48. })
  49. }
  50. // GET /users/:username
  51. func GetUserInfo(ctx *middleware.Context) {
  52. u, err := models.GetUserByName(ctx.Params(":username"))
  53. if err != nil {
  54. if err == models.ErrUserNotExist {
  55. ctx.Error(404)
  56. } else {
  57. ctx.JSON(500, &base.ApiJsonErr{"GetUserByName: " + err.Error(), base.DOC_URL})
  58. }
  59. return
  60. }
  61. ctx.JSON(200, &api.User{u.Id, u.Name, u.FullName, u.Email, u.AvatarLink()})
  62. }