mirror of
https://github.com/go-gitea/gitea
synced 2024-11-25 20:22:53 +01:00
c337ff0ec7
Fixes #17453 This PR adds the abbility to block a user from a personal account or organization to restrict how the blocked user can interact with the blocker. The docs explain what's the consequence of blocking a user. Screenshots: ![grafik](https://github.com/go-gitea/gitea/assets/1666336/4ed884f3-e06a-4862-afd3-3b8aa2488dc6) ![grafik](https://github.com/go-gitea/gitea/assets/1666336/ae6d4981-f252-4f50-a429-04f0f9f1cdf1) ![grafik](https://github.com/go-gitea/gitea/assets/1666336/ca153599-5b0f-4b4a-90fe-18bdfd6f0b6b) --------- Co-authored-by: Lauris BH <lauris@nix.lv>
98 lines
2.7 KiB
Go
98 lines
2.7 KiB
Go
// Copyright 2024 The Gitea Authors.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package shared
|
|
|
|
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"
|
|
user_service "code.gitea.io/gitea/services/user"
|
|
)
|
|
|
|
func ListBlocks(ctx *context.APIContext, blocker *user_model.User) {
|
|
blocks, total, err := user_model.FindBlockings(ctx, &user_model.FindBlockingOptions{
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
BlockerID: blocker.ID,
|
|
})
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "FindBlockings", err)
|
|
return
|
|
}
|
|
|
|
if err := user_model.BlockingList(blocks).LoadAttributes(ctx); err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
|
|
return
|
|
}
|
|
|
|
users := make([]*api.User, 0, len(blocks))
|
|
for _, b := range blocks {
|
|
users = append(users, convert.ToUser(ctx, b.Blockee, blocker))
|
|
}
|
|
|
|
ctx.SetTotalCountHeader(total)
|
|
ctx.JSON(http.StatusOK, &users)
|
|
}
|
|
|
|
func CheckUserBlock(ctx *context.APIContext, blocker *user_model.User) {
|
|
blockee, err := user_model.GetUserByName(ctx, ctx.Params("username"))
|
|
if err != nil {
|
|
ctx.NotFound("GetUserByName", err)
|
|
return
|
|
}
|
|
|
|
status := http.StatusNotFound
|
|
blocking, err := user_model.GetBlocking(ctx, blocker.ID, blockee.ID)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetBlocking", err)
|
|
return
|
|
}
|
|
if blocking != nil {
|
|
status = http.StatusNoContent
|
|
}
|
|
|
|
ctx.Status(status)
|
|
}
|
|
|
|
func BlockUser(ctx *context.APIContext, blocker *user_model.User) {
|
|
blockee, err := user_model.GetUserByName(ctx, ctx.Params("username"))
|
|
if err != nil {
|
|
ctx.NotFound("GetUserByName", err)
|
|
return
|
|
}
|
|
|
|
if err := user_service.BlockUser(ctx, ctx.Doer, blocker, blockee, ctx.FormString("note")); err != nil {
|
|
if errors.Is(err, user_model.ErrCanNotBlock) || errors.Is(err, user_model.ErrBlockOrganization) {
|
|
ctx.Error(http.StatusBadRequest, "BlockUser", err)
|
|
} else {
|
|
ctx.Error(http.StatusInternalServerError, "BlockUser", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func UnblockUser(ctx *context.APIContext, doer, blocker *user_model.User) {
|
|
blockee, err := user_model.GetUserByName(ctx, ctx.Params("username"))
|
|
if err != nil {
|
|
ctx.NotFound("GetUserByName", err)
|
|
return
|
|
}
|
|
|
|
if err := user_service.UnblockUser(ctx, doer, blocker, blockee); err != nil {
|
|
if errors.Is(err, user_model.ErrCanNotUnblock) || errors.Is(err, user_model.ErrBlockOrganization) {
|
|
ctx.Error(http.StatusBadRequest, "UnblockUser", err)
|
|
} else {
|
|
ctx.Error(http.StatusInternalServerError, "UnblockUser", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|