35 lines
1.5 KiB
TypeScript
35 lines
1.5 KiB
TypeScript
if (process.env.NODE_ENV !== "test")
|
|
require("server-only");
|
|
import getPool from "@/lib/server/db";
|
|
import { apiError } from "@/lib/server/errors";
|
|
import { requireGroupAdmin } from "@/lib/server/group-access";
|
|
import { enforceUserWriteRateLimit } from "@/lib/server/rate-limit";
|
|
|
|
export async function getGroupSettings(groupId: number) {
|
|
const pool = getPool();
|
|
const { rows } = await pool.query(
|
|
"select allow_member_tag_manage, join_policy from group_settings where group_id=$1",
|
|
[groupId]
|
|
);
|
|
return {
|
|
allowMemberTagManage: Boolean(rows[0]?.allow_member_tag_manage),
|
|
joinPolicy: String(rows[0]?.join_policy || "NOT_ACCEPTING") as "NOT_ACCEPTING" | "AUTO_ACCEPT" | "APPROVAL_REQUIRED"
|
|
};
|
|
}
|
|
|
|
export async function setGroupSettings(input: { userId: number; groupId: number; allowMemberTagManage: boolean; joinPolicy?: "NOT_ACCEPTING" | "AUTO_ACCEPT" | "APPROVAL_REQUIRED" }) {
|
|
await enforceUserWriteRateLimit({ userId: input.userId, scope: "groups:settings:update" });
|
|
await requireGroupAdmin(input.userId, input.groupId);
|
|
|
|
const pool = getPool();
|
|
await pool.query(
|
|
`insert into group_settings(group_id, allow_member_tag_manage, join_policy)
|
|
values($1,$2,$3)
|
|
on conflict (group_id) do update
|
|
set allow_member_tag_manage=$2,
|
|
join_policy=$3,
|
|
updated_at=now()`,
|
|
[input.groupId, input.allowMemberTagManage, input.joinPolicy ?? "NOT_ACCEPTING"]
|
|
);
|
|
}
|