Files
Atay-Makhzan/routers/api/v1/user/user.go
T

224 lines
5.9 KiB
Go
Raw Normal View History

2014-04-30 23:48:01 -04:00
// Copyright 2014 The Gogs Authors. All rights reserved.
2020-01-24 19:00:29 +00:00
// Copyright 2020 The Gitea Authors.
// SPDX-License-Identifier: MIT
2014-04-30 23:48:01 -04:00
2015-12-04 17:16:42 -05:00
package user
2014-04-30 23:48:01 -04:00
import (
2018-10-23 04:57:42 +02:00
"net/http"
activities_model "code.gitea.io/gitea/models/activities"
user_model "code.gitea.io/gitea/models/user"
2020-01-24 19:00:29 +00:00
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/context"
2022-12-29 03:57:15 +01:00
"code.gitea.io/gitea/services/convert"
2024-11-29 09:53:49 -08:00
feed_service "code.gitea.io/gitea/services/feed"
2014-04-30 23:48:01 -04:00
)
2016-11-24 15:04:31 +08:00
// Search search users
func Search(ctx *context.APIContext) {
2017-11-12 23:02:25 -08:00
// swagger:operation GET /users/search user userSearch
// ---
// summary: Search for users
// produces:
// - application/json
// parameters:
// - name: q
// in: query
// description: keyword
// type: string
2018-10-18 09:44:51 +01:00
// - name: uid
// in: query
// description: ID of the user to search for
// type: integer
// format: int64
2020-01-24 19:00:29 +00:00
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
2017-11-12 23:02:25 -08:00
// - name: limit
// in: query
// description: page size of results
2017-11-12 23:02:25 -08:00
// type: integer
// responses:
// "200":
// description: "SearchResults of a successful search"
// schema:
// type: object
// properties:
// ok:
// type: boolean
// data:
// type: array
// items:
// "$ref": "#/definitions/User"
2019-12-20 18:07:12 +01:00
listOptions := utils.GetListOptions(ctx)
uid := ctx.FormInt64("uid")
var users []*user_model.User
var maxResults int64
var err error
switch uid {
case user_model.GhostUserID:
maxResults = 1
users = []*user_model.User{user_model.NewGhostUser()}
case user_model.ActionsUserID:
maxResults = 1
users = []*user_model.User{user_model.NewActionsUser()}
default:
opts := user_model.SearchUserOptions{
Actor: ctx.Doer,
Keyword: ctx.FormTrim("q"),
UID: uid,
Types: []user_model.UserType{user_model.UserTypeIndividual},
SearchByEmail: true,
ListOptions: listOptions,
}
opts.ApplyPublicOnly(ctx.PublicOnly)
users, maxResults, err = user_model.SearchUsers(ctx, opts)
if err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]any{
"ok": false,
"error": err.Error(),
})
return
}
2014-04-30 23:48:01 -04:00
}
2026-03-08 15:35:50 +01:00
ctx.SetLinkHeader(maxResults, listOptions.PageSize)
2021-08-12 14:43:08 +02:00
ctx.SetTotalCountHeader(maxResults)
2023-07-04 20:36:08 +02:00
ctx.JSON(http.StatusOK, map[string]any{
2014-04-30 23:48:01 -04:00
"ok": true,
"data": convert.ToUsers(ctx, ctx.Doer, users),
2014-04-30 23:48:01 -04:00
})
}
2014-11-18 11:07:16 -05:00
2016-11-24 15:04:31 +08:00
// GetInfo get user's information
func GetInfo(ctx *context.APIContext) {
2017-11-12 23:02:25 -08:00
// swagger:operation GET /users/{username} user userGet
// ---
// summary: Get a user
// produces:
// - application/json
// parameters:
// - name: username
// in: path
// description: username of the user whose data is to be listed
2017-11-12 23:02:25 -08:00
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/User"
// "404":
// "$ref": "#/responses/notFound"
2019-12-20 18:07:12 +01:00
if !user_model.IsUserVisibleToViewer(ctx, ctx.ContextUser, ctx.Doer) {
// fake ErrUserNotExist error message to not leak information about existence
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound("GetUserByName", user_model.ErrUserNotExist{Name: ctx.PathParam("username")})
return
}
ctx.JSON(http.StatusOK, convert.ToUser(ctx, ctx.ContextUser, ctx.Doer))
2016-08-11 15:29:39 -07:00
}
2017-11-12 23:02:25 -08:00
// GetAuthenticatedUser get current user's information
2016-08-11 15:29:39 -07:00
func GetAuthenticatedUser(ctx *context.APIContext) {
2017-11-12 23:02:25 -08:00
// swagger:operation GET /user user userGetCurrent
// ---
// summary: Get the authenticated user
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/User"
2019-12-20 18:07:12 +01:00
ctx.JSON(http.StatusOK, convert.ToUser(ctx, ctx.Doer, ctx.Doer))
2014-11-18 11:07:16 -05:00
}
2018-10-23 04:57:42 +02:00
// GetUserHeatmapData is the handler to get a users heatmap
func GetUserHeatmapData(ctx *context.APIContext) {
// swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
// ---
// summary: Get a user's heatmap
// produces:
// - application/json
// parameters:
// - name: username
// in: path
// description: username of the user whose heatmap is to be obtained
2018-10-23 04:57:42 +02:00
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/UserHeatmapData"
// "404":
// "$ref": "#/responses/notFound"
heatmap, err := activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer)
2018-10-23 04:57:42 +02:00
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2018-10-23 04:57:42 +02:00
return
}
2019-12-20 18:07:12 +01:00
ctx.JSON(http.StatusOK, heatmap)
2018-10-23 04:57:42 +02:00
}
2023-04-04 21:35:31 +08:00
func ListUserActivityFeeds(ctx *context.APIContext) {
// swagger:operation GET /users/{username}/activities/feeds user userListActivityFeeds
// ---
// summary: List a user's activity feeds
// produces:
// - application/json
// parameters:
// - name: username
// in: path
// description: username of the user whose activity feeds are to be listed
2023-04-04 21:35:31 +08:00
// type: string
// required: true
// - name: only-performed-by
// in: query
// description: if true, only show actions performed by the requested user
// type: boolean
// - name: date
// in: query
// description: the date of the activities to be found
// type: string
// format: date
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
// type: integer
// responses:
// "200":
// "$ref": "#/responses/ActivityFeedsList"
// "404":
// "$ref": "#/responses/notFound"
includePrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
listOptions := utils.GetListOptions(ctx)
opts := activities_model.GetFeedsOptions{
RequestedUser: ctx.ContextUser,
Actor: ctx.Doer,
IncludePrivate: includePrivate,
OnlyPerformedBy: ctx.FormBool("only-performed-by"),
Date: ctx.FormString("date"),
ListOptions: listOptions,
}
opts.ApplyPublicOnly(ctx.PublicOnly)
2023-04-04 21:35:31 +08:00
2024-11-29 09:53:49 -08:00
feeds, count, err := feed_service.GetFeeds(ctx, opts)
2023-04-04 21:35:31 +08:00
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2023-04-04 21:35:31 +08:00
return
}
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, convert.ToActivities(ctx, feeds, ctx.Doer))
}