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