Files
Gogs/internal/route/api/v1/user/user.go

67 lines
1.4 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 (
2019-08-08 23:53:43 -07:00
"net/http"
2018-05-27 08:53:48 +08:00
api "github.com/gogs/go-gogs-client"
2014-11-14 17:11:30 -05: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) {
pageSize := c.QueryInt("limit")
if pageSize <= 0 {
pageSize = 10
2014-04-30 23:48:01 -04:00
}
users, _, err := db.Users.SearchByName(c.Req.Context(), c.Query("q"), 1, pageSize, "")
2014-04-30 23:48:01 -04:00
if err != nil {
c.JSON(http.StatusInternalServerError, map[string]any{
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].AvatarURL(),
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
}
c.JSONSuccess(map[string]any{
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 := db.Users.GetByUsername(c.Req.Context(), c.Params(":username"))
2014-11-18 11:07:16 -05:00
if err != nil {
c.NotFoundOrError(err, "get user by name")
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 = ""
}
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
}