From 006acdb6ac4358815b3aeb9f9fbef9066a3bcafc Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Fri, 28 Jul 2023 15:42:55 +0100 Subject: [PATCH] feat: create user strikes --- app/panel/inspect/user/[id]/page.tsx | 6 +- .../inspector/RelevantModerationNotices.tsx | 127 +++++++++++++++++- lib/actions.ts | 30 +++++ lib/db.ts | 9 +- 4 files changed, 169 insertions(+), 3 deletions(-) diff --git a/app/panel/inspect/user/[id]/page.tsx b/app/panel/inspect/user/[id]/page.tsx index 5e406cd..44329c3 100644 --- a/app/panel/inspect/user/[id]/page.tsx +++ b/app/panel/inspect/user/[id]/page.tsx @@ -106,7 +106,11 @@ export default async function User({ )} - +

Strikes

- null} /> + + + + Reason + Created + + + + {strikesDraft.map((strike) => ( + + {strike.reason} + {dayjs(decodeTime(strike._id)).fromNow()} + + ))} + +
+ + + + + + + + Create Strike + +

+ + You have received an account strike, for one or more reasons: + + (givenReason.current = e.currentTarget.value)} + /> + + Further violations will result in suspension or a permanent ban + depending on severity, please abide by the{" "} + + Acceptable Usage Policy + + . + + + (additionalContext.current = e.currentTarget.value) + } + /> +

+ + Cancel + + givenReason.current && + createStrike( + userId, + givenReason.current, + additionalContext.current + ) + .then((strike) => { + setStrikesDraft((strikes) => [strike, ...strikes]); + toast({ title: "Created strike" }); + }) + .catch((err) => + toast({ + title: "Failed to create strike!", + description: String(err), + variant: "destructive", + }) + ) + .finally(() => { + givenReason.current = ""; + additionalContext.current = ""; + }) + } + > + Create + + +
+

Alerts

diff --git a/lib/actions.ts b/lib/actions.ts index ebf97ae..f04b233 100644 --- a/lib/actions.ts +++ b/lib/actions.ts @@ -16,6 +16,7 @@ import { publishMessage, sendChatMessage } from "./redis"; import { ulid } from "ulid"; import { AccountInfo, + AccountStrike, Bot, File, Member, @@ -41,6 +42,35 @@ export async function sendAlert(userId: string, content: string) { }); } +export async function createStrike( + userId: string, + givenReason: string, + additionalContext: string +) { + const strike: AccountStrike & { moderator_id: string } = { + _id: ulid(), + user_id: userId, + moderator_id: "01EX2NCWQ0CHS3QJF0FEQS1GR4", // TODO + reason: additionalContext + ? givenReason + " - " + additionalContext + : givenReason, + }; + + await mongo() + .db("revolt") + .collection<{ _id: string }>("safety_strikes") + .insertOne(strike); + await sendAlert( + userId, + `You have received an account strike, for one or more reasons: +- ${givenReason} + +Further violations will result in suspension or a permanent ban depending on severity, please abide by the [Acceptable Usage Policy](https://revolt.chat/aup).` + ); + + return strike; +} + export async function updateReportNotes(reportId: string, notes: string) { return await mongo() .db("revolt") diff --git a/lib/db.ts b/lib/db.ts index ce0e80c..a495d8a 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -274,7 +274,14 @@ export async function fetchStrikesByUser(userId: string) { return await mongo() .db("revolt") .collection("safety_strikes") - .find({ user_id: userId }) + .find( + { user_id: userId }, + { + sort: { + _id: -1, + }, + } + ) .toArray(); }