56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import {
|
|
createPeopleMutationInvocation,
|
|
peopleMutationService,
|
|
} from "@/features/housekeeping/domains/people/services/mutations";
|
|
import { createCorrelationId } from "@/features/housekeeping/foundation/contracts";
|
|
import { requirePermissionRateLimited } from "@/lib/admin/guard";
|
|
import { PERMS } from "@/lib/permissions";
|
|
|
|
const BAN_TYPES: ReadonlySet<string> = new Set([
|
|
"account",
|
|
"ip",
|
|
"machine",
|
|
"super",
|
|
]);
|
|
export async function createBan(formData: FormData): Promise<void> {
|
|
const staff = await requirePermissionRateLimited(PERMS.USERS_BAN);
|
|
const userId = Number(formData.get("userId"));
|
|
const reason =
|
|
String(formData.get("reason") ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.slice(0, 200) || "Banned";
|
|
const hours = Number(formData.get("hours"));
|
|
const type = String(formData.get("type"));
|
|
if (!(userId > 0) || !BAN_TYPES.has(type)) return;
|
|
const result = await peopleMutationService.execute(
|
|
createPeopleMutationInvocation(staff, createCorrelationId()),
|
|
"ban.create",
|
|
{
|
|
userId,
|
|
reason,
|
|
hours: Number.isFinite(hours) && hours > 0 ? Math.floor(hours) : 0,
|
|
type,
|
|
},
|
|
);
|
|
if (!result.ok) throw new Error("Could not create ban");
|
|
revalidatePath("/ase/people/moderation/bans");
|
|
}
|
|
|
|
export async function liftBan(formData: FormData): Promise<void> {
|
|
const staff = await requirePermissionRateLimited(PERMS.USERS_BAN);
|
|
const id = Number(formData.get("id"));
|
|
if (id > 0) {
|
|
const result = await peopleMutationService.execute(
|
|
createPeopleMutationInvocation(staff, createCorrelationId()),
|
|
"ban.lift",
|
|
{ id },
|
|
);
|
|
if (!result.ok) throw new Error("Could not lift ban");
|
|
}
|
|
revalidatePath("/ase/people/moderation/bans");
|
|
}
|