Some checks failed
Build and Release / Create Release (push) Successful in 0s
Trigger Vault Plugin Rebuild / Trigger Vault Rebuild (push) Successful in 0s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 2m48s
Build and Release / Lint (push) Failing after 5m2s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, darwin, linux-latest) (push) Has been skipped
Build and Release / Build Binaries (arm64, linux, linux-latest) (push) Has been skipped
Build and Release / Unit Tests (push) Successful in 5m37s
Go's semantic import versioning requires v2+ modules to include the major version in the module path. This enables using proper version tags (v3.x.x) instead of pseudo-versions. Updated module path: code.gitcaddy.com/server/v3
99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
// Copyright 2024 The Gitea Authors.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package shared
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
user_model "code.gitcaddy.com/server/v3/models/user"
|
|
api "code.gitcaddy.com/server/v3/modules/structs"
|
|
"code.gitcaddy.com/server/v3/routers/api/v1/utils"
|
|
"code.gitcaddy.com/server/v3/services/context"
|
|
"code.gitcaddy.com/server/v3/services/convert"
|
|
user_service "code.gitcaddy.com/server/v3/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.APIErrorInternal(err)
|
|
return
|
|
}
|
|
|
|
if err := user_model.BlockingList(blocks).LoadAttributes(ctx); err != nil {
|
|
ctx.APIErrorInternal(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.PathParam("username"))
|
|
if err != nil {
|
|
ctx.APIErrorNotFound("GetUserByName", err)
|
|
return
|
|
}
|
|
|
|
status := http.StatusNotFound
|
|
blocking, err := user_model.GetBlocking(ctx, blocker.ID, blockee.ID)
|
|
if err != nil {
|
|
ctx.APIErrorInternal(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.PathParam("username"))
|
|
if err != nil {
|
|
ctx.APIErrorNotFound("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.APIError(http.StatusBadRequest, err)
|
|
} else {
|
|
ctx.APIErrorInternal(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func UnblockUser(ctx *context.APIContext, doer, blocker *user_model.User) {
|
|
blockee, err := user_model.GetUserByName(ctx, ctx.PathParam("username"))
|
|
if err != nil {
|
|
ctx.APIErrorNotFound("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.APIError(http.StatusBadRequest, err)
|
|
} else {
|
|
ctx.APIErrorInternal(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|