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.

40 lines
844 B

  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/gogits/gogs/models"
  7. "github.com/gogits/gogs/modules/base"
  8. "github.com/gogits/gogs/modules/middleware"
  9. )
  10. type user struct {
  11. UserName string `json:"username"`
  12. AvatarLink string `json:"avatar"`
  13. }
  14. func SearchUser(ctx *middleware.Context) {
  15. q := ctx.Query("q")
  16. limit, err := base.StrTo(ctx.Query("limit")).Int()
  17. if err != nil {
  18. limit = 10
  19. }
  20. us, err := models.SearchUserByName(q, limit)
  21. if err != nil {
  22. ctx.JSON(500, nil)
  23. return
  24. }
  25. results := make([]*user, len(us))
  26. for i := range us {
  27. results[i] = &user{us[i].Name, us[i].AvatarLink()}
  28. }
  29. ctx.Render.JSON(200, map[string]interface{}{
  30. "ok": true,
  31. "data": results,
  32. })
  33. }