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.
gitea/routers/api/v1/user/user.go

76 lines
1.6 KiB

// 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
import (
"github.com/Unknwon/com"
api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/context"
9 years ago
"github.com/gogits/gogs/routers/api/v1/convert"
)
func Search(ctx *context.APIContext) {
opts := &models.SearchUserOptions{
Keyword: ctx.Query("q"),
Type: models.USER_TYPE_INDIVIDUAL,
PageSize: com.StrTo(ctx.Query("limit")).MustInt(),
}
if opts.PageSize == 0 {
opts.PageSize = 10
}
users, _, err := models.SearchUserByName(opts)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results := make([]*api.User, len(users))
for i := range users {
results[i] = &api.User{
ID: users[i].ID,
UserName: users[i].Name,
AvatarUrl: users[i].AvatarLink(),
FullName: users[i].FullName,
}
if ctx.IsSigned {
results[i].Email = users[i].Email
}
}
9 years ago
ctx.JSON(200, map[string]interface{}{
"ok": true,
"data": results,
})
}
10 years ago
func GetInfo(ctx *context.APIContext) {
10 years ago
u, err := models.GetUserByName(ctx.Params(":username"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.Status(404)
10 years ago
} else {
ctx.Error(500, "GetUserByName", err)
10 years ago
}
return
}
// Hide user e-mail when API caller isn't signed in.
if !ctx.IsSigned {
u.Email = ""
}
9 years ago
ctx.JSON(200, convert.ToUser(u))
}
func GetAuthenticatedUser(ctx *context.APIContext) {
ctx.JSON(200, convert.ToUser(ctx.User))
10 years ago
}