From 4f6be05162a9ce43a54b2c5e43899c6e3e3d908b Mon Sep 17 00:00:00 2001 From: Lea Date: Wed, 9 Aug 2023 22:56:28 +0200 Subject: [PATCH] feat: make profile wipe functional --- components/pages/inspector/UserActions.tsx | 37 ++++++++++++++++------ lib/accessPermissions.ts | 3 +- lib/actions.ts | 37 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/components/pages/inspector/UserActions.tsx b/components/pages/inspector/UserActions.tsx index 8d0fb63..50fa40c 100644 --- a/components/pages/inspector/UserActions.tsx +++ b/components/pages/inspector/UserActions.tsx @@ -28,6 +28,7 @@ import { unsuspendUser, updateBotDiscoverability, updateUserBadges, + wipeUserProfile, } from "@/lib/actions"; import { useRef, useState } from "react"; import { useToast } from "../../ui/use-toast"; @@ -345,21 +346,21 @@ export function UserActions({ user, bot }: { user: User; bot?: Bot }) { Reset user profile - setWipeDraft({ ...wipeDraft, bio: e == true })}> - Bio - - setWipeDraft({ ...wipeDraft, status: e == true })}> - Status - - setWipeDraft({ ...wipeDraft, displayName: e == true })}> - Display Name - setWipeDraft({ ...wipeDraft, avatar: e == true })}> Avatar setWipeDraft({ ...wipeDraft, banner: e == true })}> Profile Banner + setWipeDraft({ ...wipeDraft, displayName: e == true })}> + Display Name + + setWipeDraft({ ...wipeDraft, bio: e == true })}> + Bio + + setWipeDraft({ ...wipeDraft, status: e == true })}> + Status + @@ -367,7 +368,23 @@ export function UserActions({ user, bot }: { user: User; bot?: Bot }) { i).length} onClick={() => { - toast({ title: "todo" }) + wipeUserProfile(user._id, wipeDraft) + .then(() => { + toast({ title: "Wiped selected fields" }); + window.location.reload(); + }) + .catch((e) => toast({ + title: "Failed to wipe profile", + description: String(e), + variant: "destructive", + })) + .finally(() => setWipeDraft({ + avatar: false, + banner: false, + bio: false, + displayName: false, + status: false, + })); }} >Reset diff --git a/lib/accessPermissions.ts b/lib/accessPermissions.ts index 10d544d..9ed17b5 100644 --- a/lib/accessPermissions.ts +++ b/lib/accessPermissions.ts @@ -48,7 +48,7 @@ type Permission = | "/relations"}` | `/create${"" | "/alert" | "/strike"}` | `/update${"" | "/badges"}` - | `/action${"" | "/unsuspend" | "/suspend" | "/wipe" | "/ban"}`}`; + | `/action${"" | "/unsuspend" | "/suspend" | "/wipe" | "/ban" | "/wipe-profile"}`}`; const PermissionSets = { // Admin @@ -130,6 +130,7 @@ const PermissionSets = { "users/create/strike", "users/action/suspend", "users/action/wipe", + "users/action/wipe-profile", "users/action/ban", "users/action/unsuspend", "accounts/disable", diff --git a/lib/actions.ts b/lib/actions.ts index 664207a..fadb8bc 100644 --- a/lib/actions.ts +++ b/lib/actions.ts @@ -382,6 +382,43 @@ export async function unsuspendUser(userId: string) { ); } +export async function wipeUserProfile( + userId: string, + fields: { + banner: boolean, + avatar: boolean, + bio: boolean, + displayName: boolean, + status: boolean, + } +) { + await checkPermission("users/action/wipe-profile", userId); + + const newDisplayName = (await fetchUserById(userId))?.username || "--"; + + await mongo() + .db("revolt") + .collection("users") + .updateOne( + { + _id: userId, + }, + { + $unset: { + ...(fields.banner ? { "profile.background": 1 } : {}), + ...(fields.bio ? { "profile.content": 1 } : {}), + ...(fields.status ? { "status.text": 1 } : {}), + ...(fields.avatar ? { "avatar": 1 } : {}), + }, + ...(fields.displayName ? { + $set: { + display_name: newDisplayName, + }, + } : {}), + } + ) +} + export async function updateServerFlags(serverId: string, flags: number) { await checkPermission("servers/update/flags", serverId, { flags }); await mongo().db("revolt").collection("servers").updateOne(