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.
|
|
|
|
|
|
2015-12-04 17:16:42 -05:00
|
|
|
package user
|
2014-04-30 23:48:01 -04:00
|
|
|
|
|
|
|
|
import (
|
2019-08-08 23:53:43 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
2019-10-23 23:03:17 -07:00
|
|
|
"github.com/unknwon/com"
|
2014-07-26 00:24:27 -04:00
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
api "github.com/gogs/go-gogs-client"
|
2014-11-14 17:11:30 -05:00
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/context"
|
|
|
|
|
"gogs.io/gogs/internal/db"
|
|
|
|
|
"gogs.io/gogs/internal/markup"
|
2014-04-30 23:48:01 -04:00
|
|
|
)
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func Search(c *context.APIContext) {
|
2019-10-24 01:51:46 -07:00
|
|
|
opts := &db.SearchUserOptions{
|
2017-06-03 07:26:09 -04:00
|
|
|
Keyword: c.Query("q"),
|
2020-04-18 12:07:30 +08:00
|
|
|
Type: db.UserIndividual,
|
2017-06-03 07:26:09 -04:00
|
|
|
PageSize: com.StrTo(c.Query("limit")).MustInt(),
|
2014-08-26 18:11:15 +08:00
|
|
|
}
|
2016-03-11 15:33:12 -05:00
|
|
|
if opts.PageSize == 0 {
|
|
|
|
|
opts.PageSize = 10
|
2014-04-30 23:48:01 -04:00
|
|
|
}
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
users, _, err := db.SearchUserByName(opts)
|
2014-04-30 23:48:01 -04:00
|
|
|
if err != nil {
|
2019-08-08 23:53:43 -07:00
|
|
|
c.JSON(http.StatusInternalServerError, 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
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-11 15:33:12 -05:00
|
|
|
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,
|
2016-03-11 15:33:12 -05:00
|
|
|
UserName: users[i].Name,
|
|
|
|
|
AvatarUrl: users[i].AvatarLink(),
|
2017-10-14 23:53:20 -04:00
|
|
|
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 {
|
2016-03-11 15:33:12 -05:00
|
|
|
results[i].Email = users[i].Email
|
2015-08-19 05:47:45 +08:00
|
|
|
}
|
2014-04-30 23:48:01 -04:00
|
|
|
}
|
|
|
|
|
|
2019-08-08 23:53:43 -07:00
|
|
|
c.JSONSuccess(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) {
|
2019-10-24 01:51:46 -07:00
|
|
|
u, err := db.GetUserByName(c.Params(":username"))
|
2014-11-18 11:07:16 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.NotFoundOrError(err, "get user by name")
|
2014-11-18 11:07:16 -05:00
|
|
|
return
|
|
|
|
|
}
|
2015-07-14 23:21:34 +08:00
|
|
|
|
|
|
|
|
// Hide user e-mail when API caller isn't signed in.
|
2017-06-03 07:26:09 -04:00
|
|
|
if !c.IsLogged {
|
2015-07-14 23:21:34 +08:00
|
|
|
u.Email = ""
|
|
|
|
|
}
|
2019-08-08 23:53:43 -07:00
|
|
|
c.JSONSuccess(u.APIFormat())
|
2016-08-11 15:29:39 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func GetAuthenticatedUser(c *context.APIContext) {
|
2019-08-08 23:53:43 -07:00
|
|
|
c.JSONSuccess(c.User.APIFormat())
|
2014-11-18 11:07:16 -05:00
|
|
|
}
|