mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-05 23:46:48 +01:00
894d9b2836
Since `modules/context` has to depend on `models` and many other packages, it should be moved from `modules/context` to `services/context` according to design principles. There is no logic code change on this PR, only move packages. - Move `code.gitea.io/gitea/modules/context` to `code.gitea.io/gitea/services/context` - Move `code.gitea.io/gitea/modules/contexttest` to `code.gitea.io/gitea/services/contexttest` because of depending on context - Move `code.gitea.io/gitea/modules/upload` to `code.gitea.io/gitea/services/context/upload` because of depending on context (cherry picked from commit 29f149bd9f517225a3c9f1ca3fb0a7b5325af696) Conflicts: routers/api/packages/alpine/alpine.go routers/api/v1/repo/issue_reaction.go routers/install/install.go routers/web/admin/config.go routers/web/passkey.go routers/web/repo/search.go routers/web/repo/setting/default_branch.go routers/web/user/home.go routers/web/user/profile.go tests/integration/editor_test.go tests/integration/integration_test.go tests/integration/mirror_push_test.go trivial context conflicts also modified all other occurrences in Forgejo specific files
263 lines
7 KiB
Go
263 lines
7 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2020 The Gitea Authors.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package user
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
|
"code.gitea.io/gitea/services/context"
|
|
"code.gitea.io/gitea/services/convert"
|
|
)
|
|
|
|
func responseAPIUsers(ctx *context.APIContext, users []*user_model.User) {
|
|
apiUsers := make([]*api.User, len(users))
|
|
for i := range users {
|
|
apiUsers[i] = convert.ToUser(ctx, users[i], ctx.Doer)
|
|
}
|
|
ctx.JSON(http.StatusOK, &apiUsers)
|
|
}
|
|
|
|
func listUserFollowers(ctx *context.APIContext, u *user_model.User) {
|
|
users, count, err := user_model.GetUserFollowers(ctx, u, ctx.Doer, utils.GetListOptions(ctx))
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err)
|
|
return
|
|
}
|
|
|
|
ctx.SetTotalCountHeader(count)
|
|
responseAPIUsers(ctx, users)
|
|
}
|
|
|
|
// ListMyFollowers list the authenticated user's followers
|
|
func ListMyFollowers(ctx *context.APIContext) {
|
|
// swagger:operation GET /user/followers user userCurrentListFollowers
|
|
// ---
|
|
// summary: List the authenticated user's followers
|
|
// parameters:
|
|
// - 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
|
|
// produces:
|
|
// - application/json
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/UserList"
|
|
|
|
listUserFollowers(ctx, ctx.Doer)
|
|
}
|
|
|
|
// ListFollowers list the given user's followers
|
|
func ListFollowers(ctx *context.APIContext) {
|
|
// swagger:operation GET /users/{username}/followers user userListFollowers
|
|
// ---
|
|
// summary: List the given user's followers
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: username
|
|
// in: path
|
|
// description: username of user
|
|
// type: string
|
|
// required: true
|
|
// - 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/UserList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
listUserFollowers(ctx, ctx.ContextUser)
|
|
}
|
|
|
|
func listUserFollowing(ctx *context.APIContext, u *user_model.User) {
|
|
users, count, err := user_model.GetUserFollowing(ctx, u, ctx.Doer, utils.GetListOptions(ctx))
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetUserFollowing", err)
|
|
return
|
|
}
|
|
|
|
ctx.SetTotalCountHeader(count)
|
|
responseAPIUsers(ctx, users)
|
|
}
|
|
|
|
// ListMyFollowing list the users that the authenticated user is following
|
|
func ListMyFollowing(ctx *context.APIContext) {
|
|
// swagger:operation GET /user/following user userCurrentListFollowing
|
|
// ---
|
|
// summary: List the users that the authenticated user is following
|
|
// parameters:
|
|
// - 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
|
|
// produces:
|
|
// - application/json
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/UserList"
|
|
|
|
listUserFollowing(ctx, ctx.Doer)
|
|
}
|
|
|
|
// ListFollowing list the users that the given user is following
|
|
func ListFollowing(ctx *context.APIContext) {
|
|
// swagger:operation GET /users/{username}/following user userListFollowing
|
|
// ---
|
|
// summary: List the users that the given user is following
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: username
|
|
// in: path
|
|
// description: username of user
|
|
// type: string
|
|
// required: true
|
|
// - 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/UserList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
listUserFollowing(ctx, ctx.ContextUser)
|
|
}
|
|
|
|
func checkUserFollowing(ctx *context.APIContext, u *user_model.User, followID int64) {
|
|
if user_model.IsFollowing(ctx, u.ID, followID) {
|
|
ctx.Status(http.StatusNoContent)
|
|
} else {
|
|
ctx.NotFound()
|
|
}
|
|
}
|
|
|
|
// CheckMyFollowing whether the given user is followed by the authenticated user
|
|
func CheckMyFollowing(ctx *context.APIContext) {
|
|
// swagger:operation GET /user/following/{username} user userCurrentCheckFollowing
|
|
// ---
|
|
// summary: Check whether a user is followed by the authenticated user
|
|
// parameters:
|
|
// - name: username
|
|
// in: path
|
|
// description: username of followed user
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
checkUserFollowing(ctx, ctx.Doer, ctx.ContextUser.ID)
|
|
}
|
|
|
|
// CheckFollowing check if one user is following another user
|
|
func CheckFollowing(ctx *context.APIContext) {
|
|
// swagger:operation GET /users/{username}/following/{target} user userCheckFollowing
|
|
// ---
|
|
// summary: Check if one user is following another user
|
|
// parameters:
|
|
// - name: username
|
|
// in: path
|
|
// description: username of following user
|
|
// type: string
|
|
// required: true
|
|
// - name: target
|
|
// in: path
|
|
// description: username of followed user
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
target := GetUserByParamsName(ctx, ":target")
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
checkUserFollowing(ctx, ctx.ContextUser, target.ID)
|
|
}
|
|
|
|
// Follow follow a user
|
|
func Follow(ctx *context.APIContext) {
|
|
// swagger:operation PUT /user/following/{username} user userCurrentPutFollow
|
|
// ---
|
|
// summary: Follow a user
|
|
// parameters:
|
|
// - name: username
|
|
// in: path
|
|
// description: username of user to follow
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
if err := user_model.FollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID); err != nil {
|
|
if errors.Is(err, user_model.ErrBlockedByUser) {
|
|
ctx.Error(http.StatusForbidden, "BlockedByUser", err)
|
|
return
|
|
}
|
|
ctx.Error(http.StatusInternalServerError, "FollowUser", err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// Unfollow unfollow a user
|
|
func Unfollow(ctx *context.APIContext) {
|
|
// swagger:operation DELETE /user/following/{username} user userCurrentDeleteFollow
|
|
// ---
|
|
// summary: Unfollow a user
|
|
// parameters:
|
|
// - name: username
|
|
// in: path
|
|
// description: username of user to unfollow
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
if err := user_model.UnfollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID); err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "UnfollowUser", err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|