mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-25 10:57:37 +00:00
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/13143 The details of the refactor are in [a separate branch](https://codeberg.org/limiting-factor/forgejo/commits/branch/wip-api-context-accessors-save) which details how it was done. This pull request contains a squash of those commits rebased on top of the current Forgejo branch, which involved conflict resolution. Given the magnitude of the change, there is a high chance that a large number of future pull requests merged in Forgejo will create conflicts. They will be dealt with in the same way as chore: refactor orgAssignment to two separate middlewares (#13155). ## Review The only risk I can think of is a getter/setter implementation being incorrectly implemented. This is probably where a detailed review is most needed, in the `services/context/api.go` file. To not increase the size of the pull request, the `GetX()` accessors were preserved although they are redundant with `X()`. They will be removed in a followup pull request. ## Backporting strategy. Cherry-picking the commit to v15 has a significant amount of conflicts in 8 files out of 111. ``` unmerged routers/api/v1/activitypub/repository.go unmerged routers/api/v1/admin/user.go unmerged routers/api/v1/api.go unmerged routers/api/v1/org/team.go unmerged routers/api/v1/repo/action.go unmerged routers/api/v1/repo/pull.go unmerged routers/api/v1/user/follower.go unmerged services/context/api.go ``` Instead of resolving them, I think it is easier and safer to repeat the [refactor steps](https://codeberg.org/limiting-factor/forgejo/commits/branch/wip-api-context-accessors-save) in v15. I also think it is worth the effort since it will avoid trivial backport conflicts on over 100 files. ## Context This is a followup pull request discussed originally at https://codeberg.org/forgejo/forgejo/pulls/12512 Co-authored-by: limiting-factor <limiting-factor@posteo.com> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13213 Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org> Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
313 lines
8.3 KiB
Go
313 lines
8.3 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 "forgejo.org/models/user"
|
|
api "forgejo.org/modules/structs"
|
|
"forgejo.org/modules/web"
|
|
"forgejo.org/routers/api/v1/utils"
|
|
"forgejo.org/services/context"
|
|
"forgejo.org/services/convert"
|
|
"forgejo.org/services/federation"
|
|
)
|
|
|
|
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"
|
|
// "401":
|
|
// "$ref": "#/responses/unauthorized"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
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.User())
|
|
}
|
|
|
|
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"
|
|
// "401":
|
|
// "$ref": "#/responses/unauthorized"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
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.User())
|
|
}
|
|
|
|
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"
|
|
// "401":
|
|
// "$ref": "#/responses/unauthorized"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
checkUserFollowing(ctx, ctx.Doer(), ctx.User().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.User(), 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"
|
|
// "401":
|
|
// "$ref": "#/responses/unauthorized"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
if err := user_model.FollowUser(ctx, ctx.Doer().ID, ctx.User().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"
|
|
// "401":
|
|
// "$ref": "#/responses/unauthorized"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
if err := user_model.UnfollowUser(ctx, ctx.Doer().ID, ctx.User().ID); err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "UnfollowUser", err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// Follow follow a remote activitypub account
|
|
func ActivityPubFollow(ctx *context.APIContext) {
|
|
// swagger:operation POST /user/activitypub/follow user userCurrentActivityPubFollow
|
|
// ---
|
|
// summary: Follow a remote activitypub account
|
|
// parameters:
|
|
// - name: body
|
|
// in: body
|
|
// schema:
|
|
// "$ref": "#/definitions/APRemoteFollowOption"
|
|
// responses:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "401":
|
|
// "$ref": "#/responses/unauthorized"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
form := web.GetForm(ctx).(*api.APRemoteFollowOption)
|
|
|
|
if err := federation.FollowRemoteActor(ctx, ctx.Doer(), form.Target); err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "federation.FollowRemoteActor", err)
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|