Files
Gogs/routes/api/v1/user/user.go

77 lines
1.6 KiB
Go
Raw Normal View History

2014-04-30 23:48:01 -04:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package user
2014-04-30 23:48:01 -04:00
import (
2014-07-26 00:24:27 -04:00
"github.com/Unknwon/com"
2014-11-14 17:11:30 -05:00
api "github.com/gogits/go-gogs-client"
2014-04-30 23:48:01 -04:00
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/pkg/context"
"github.com/gogits/gogs/pkg/markup"
2014-04-30 23:48:01 -04:00
)
2017-06-03 07:26:09 -04:00
func Search(c *context.APIContext) {
opts := &models.SearchUserOptions{
2017-06-03 07:26:09 -04:00
Keyword: c.Query("q"),
Type: models.USER_TYPE_INDIVIDUAL,
2017-06-03 07:26:09 -04:00
PageSize: com.StrTo(c.Query("limit")).MustInt(),
2014-08-26 18:11:15 +08:00
}
if opts.PageSize == 0 {
opts.PageSize = 10
2014-04-30 23:48:01 -04:00
}
users, _, err := models.SearchUserByName(opts)
2014-04-30 23:48:01 -04:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.JSON(500, map[string]interface{}{
2014-08-26 18:11:15 +08:00
"ok": false,
"error": err.Error(),
})
2014-04-30 23:48:01 -04:00
return
}
results := make([]*api.User, len(users))
for i := range users {
2014-11-14 17:11:30 -05:00
results[i] = &api.User{
2016-07-24 01:08:22 +08:00
ID: users[i].ID,
UserName: users[i].Name,
AvatarUrl: users[i].AvatarLink(),
FullName: markup.Sanitize(users[i].FullName),
2014-08-26 18:11:15 +08:00
}
2017-06-03 07:26:09 -04:00
if c.IsLogged {
results[i].Email = users[i].Email
2015-08-19 05:47:45 +08:00
}
2014-04-30 23:48:01 -04:00
}
2017-06-03 07:26:09 -04:00
c.JSON(200, map[string]interface{}{
2014-04-30 23:48:01 -04:00
"ok": true,
"data": results,
})
}
2014-11-18 11:07:16 -05:00
2017-06-03 07:26:09 -04:00
func GetInfo(c *context.APIContext) {
u, err := models.GetUserByName(c.Params(":username"))
2014-11-18 11:07:16 -05:00
if err != nil {
if errors.IsUserNotExist(err) {
2017-06-03 07:26:09 -04:00
c.Status(404)
2014-11-18 11:07:16 -05:00
} else {
2017-06-03 07:26:09 -04:00
c.Error(500, "GetUserByName", err)
2014-11-18 11:07:16 -05:00
}
return
}
// Hide user e-mail when API caller isn't signed in.
2017-06-03 07:26:09 -04:00
if !c.IsLogged {
u.Email = ""
}
2017-06-03 07:26:09 -04:00
c.JSON(200, u.APIFormat())
2016-08-11 15:29:39 -07:00
}
2017-06-03 07:26:09 -04:00
func GetAuthenticatedUser(c *context.APIContext) {
c.JSON(200, c.User.APIFormat())
2014-11-18 11:07:16 -05:00
}