1
0
Fork 0

feat: button to change server owner

user-stream
Lea 2023-08-20 12:50:39 +02:00
parent 83161623e3
commit cf71aa49cb
Signed by: lea
GPG Key ID: 1BAFFE8347019C42
3 changed files with 83 additions and 2 deletions

View File

@ -14,13 +14,18 @@ import {
import { Check, ChevronsUpDown } from "lucide-react";
import { cn } from "@/lib/utils";
import { useState } from "react";
import { updateServerDiscoverability, updateServerFlags } from "@/lib/actions";
import { updateServerDiscoverability, updateServerFlags, updateServerOwner } from "@/lib/actions";
import { useToast } from "../../ui/use-toast";
import Link from "next/link";
import { DropdownMenu, DropdownMenuContent } from "@/components/ui/dropdown-menu";
import { DropdownMenuTrigger } from "@radix-ui/react-dropdown-menu";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from "@/components/ui/alert-dialog";
import { Input } from "@/components/ui/input";
export function ServerActions({ server }: { server: Server }) {
const [selectBadges, setSelectBadges] = useState(false);
const [serverDraft, setDraft] = useState(server);
const [newOwner, setNewOwner] = useState("");
const { toast } = useToast();
return (
@ -138,6 +143,59 @@ export function ServerActions({ server }: { server: Server }) {
<Button className="flex-1" variant="destructive">
Quarantine
</Button>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="flex-1">
More Options
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="flex flex-col">
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="ghost">
Change owner
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
Change server owner
</AlertDialogTitle>
<AlertDialogDescription className="flex flex-col gap-2">
Enter the ID of the new server owner.
<Input
placeholder="01EXAF3KX65608AJ4NG27YG1HM"
value={newOwner}
onChange={(e) => setNewOwner(e.currentTarget.value)}
/>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
disabled={newOwner.length != 26}
onClick={async () => {
try {
await updateServerOwner(server._id, newOwner);
setNewOwner("");
toast({ title: "Server owner changed" });
} catch(e) {
toast({
title: "Owner update failed",
description: String(e),
variant: "destructive",
});
}
}}
>
Update
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</DropdownMenuContent>
</DropdownMenu>
</div>
);
}

View File

@ -37,7 +37,7 @@ type Permission =
| `servers${
| ""
| `/fetch${"" | "/by-id"}`
| `/update${"" | "/flags" | "/discoverability"}`}`
| `/update${"" | "/flags" | "/discoverability" | "/owner"}`}`
| `users${
| ""
| `/fetch${
@ -112,6 +112,8 @@ const PermissionSets = {
"users/fetch/notices",
"users/update/badges",
"servers/update/owner",
"accounts/fetch/by-id",
"accounts/fetch/by-email",
"accounts/disable",

View File

@ -588,6 +588,27 @@ export async function updateServerDiscoverability(
);
}
export async function updateServerOwner(serverId: string, userId: string) {
await checkPermission("servers/update/owner", { serverId, userId });
await mongo()
.db("revolt")
.collection<Server>("servers")
.updateOne(
{ _id: serverId },
{ $set: { owner: userId } },
);
await publishMessage(serverId, {
type: "ServerUpdate",
id: serverId,
data: {
owner: userId,
},
clear: [],
});
}
export async function deleteInvite(invite: string) {
await checkPermission("channels/update/invites", invite);