1
0
Fork 0

feat: make profile wipe functional

dufisgsd
Lea 2023-08-09 22:56:28 +02:00
parent 78632d4a48
commit 4f6be05162
Signed by: lea
GPG Key ID: 1BAFFE8347019C42
3 changed files with 66 additions and 11 deletions

View File

@ -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
</AlertDialogTitle>
<AlertDialogDescription>
<Checkbox checked={wipeDraft.bio} onChange={(e) => setWipeDraft({ ...wipeDraft, bio: e == true })}>
Bio
</Checkbox>
<Checkbox checked={wipeDraft.status} onChange={(e) => setWipeDraft({ ...wipeDraft, status: e == true })}>
Status
</Checkbox>
<Checkbox checked={wipeDraft.displayName} onChange={(e) => setWipeDraft({ ...wipeDraft, displayName: e == true })}>
Display Name
</Checkbox>
<Checkbox checked={wipeDraft.avatar} onChange={(e) => setWipeDraft({ ...wipeDraft, avatar: e == true })}>
Avatar
</Checkbox>
<Checkbox checked={wipeDraft.banner} onChange={(e) => setWipeDraft({ ...wipeDraft, banner: e == true })}>
Profile Banner
</Checkbox>
<Checkbox checked={wipeDraft.displayName} onChange={(e) => setWipeDraft({ ...wipeDraft, displayName: e == true })}>
Display Name
</Checkbox>
<Checkbox checked={wipeDraft.bio} onChange={(e) => setWipeDraft({ ...wipeDraft, bio: e == true })}>
Bio
</Checkbox>
<Checkbox checked={wipeDraft.status} onChange={(e) => setWipeDraft({ ...wipeDraft, status: e == true })}>
Status
</Checkbox>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
@ -367,7 +368,23 @@ export function UserActions({ user, bot }: { user: User; bot?: Bot }) {
<AlertDialogAction
disabled={!Object.values(wipeDraft).filter(i => 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</AlertDialogAction>
</AlertDialogFooter>

View File

@ -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",

View File

@ -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<User>("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<Server>("servers").updateOne(