users.go 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/middleware"
  10. )
  11. func SearchUsers(ctx *middleware.Context) {
  12. opt := models.SearchOption{
  13. Keyword: ctx.Query("q"),
  14. Limit: com.StrTo(ctx.Query("limit")).MustInt(),
  15. }
  16. if opt.Limit == 0 {
  17. opt.Limit = 10
  18. }
  19. us, err := models.SearchUserByName(opt)
  20. if err != nil {
  21. ctx.JSON(500, map[string]interface{}{
  22. "ok": false,
  23. "error": err.Error(),
  24. })
  25. return
  26. }
  27. results := make([]*api.User, len(us))
  28. for i := range us {
  29. results[i] = &api.User{
  30. UserName: us[i].Name,
  31. AvatarUrl: us[i].AvatarLink(),
  32. }
  33. }
  34. ctx.Render.JSON(200, map[string]interface{}{
  35. "ok": true,
  36. "data": results,
  37. })
  38. }