forked from administration/panel
feat: allow invite editing and deleting
parent
a04a10f492
commit
977986736b
|
@ -1,9 +1,15 @@
|
||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { Card, CardDescription, CardHeader, CardTitle } from "../ui/card";
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../ui/card";
|
||||||
import { ChannelInvite } from "@/lib/db";
|
import { ChannelInvite } from "@/lib/db";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { Channel, User } from "revolt-api";
|
import { Channel, User } from "revolt-api";
|
||||||
|
import { Button } from "../ui/button";
|
||||||
|
import { AlertDialogFooter, AlertDialogHeader, AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogTitle, AlertDialogTrigger } from "../ui/alert-dialog";
|
||||||
|
import { toast } from "../ui/use-toast";
|
||||||
|
import { Input } from "../ui/input";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { deleteInvite, editInvite } from "@/lib/actions";
|
||||||
|
|
||||||
export function InviteCard({
|
export function InviteCard({
|
||||||
invite,
|
invite,
|
||||||
|
@ -14,11 +20,17 @@ export function InviteCard({
|
||||||
channel?: Channel;
|
channel?: Channel;
|
||||||
user?: User;
|
user?: User;
|
||||||
}) {
|
}) {
|
||||||
|
const [editDraft, setEditDraft] = useState("");
|
||||||
|
const [deleted, setDeleted] = useState(false);
|
||||||
|
const [code, setCode] = useState(invite._id);
|
||||||
|
|
||||||
|
if (deleted) return <></>;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="my-2">
|
<Card className="my-2 flex">
|
||||||
<CardHeader>
|
<CardHeader className="flex-1">
|
||||||
<CardTitle className="flex items-center">
|
<CardTitle className="flex items-center">
|
||||||
<span className="font-extralight mr-0.5 select-none">rvlt.gg/</span>{invite._id}
|
<span className="font-extralight mr-0.5 select-none">rvlt.gg/</span>{code}
|
||||||
<span className="select-none">{" "}</span> {/* looks better like this when for some reason the css doesnt load */}
|
<span className="select-none">{" "}</span> {/* looks better like this when for some reason the css doesnt load */}
|
||||||
{invite.vanity
|
{invite.vanity
|
||||||
? <span
|
? <span
|
||||||
|
@ -46,6 +58,85 @@ export function InviteCard({
|
||||||
</Link>
|
</Link>
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
|
<CardContent className="flex items-center py-0 gap-2">
|
||||||
|
{invite.vanity
|
||||||
|
? (
|
||||||
|
<AlertDialog>
|
||||||
|
<AlertDialogTrigger asChild>
|
||||||
|
<Button>Edit</Button>
|
||||||
|
</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>
|
||||||
|
Edit vanity invite
|
||||||
|
</AlertDialogTitle>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogDescription>
|
||||||
|
<p className="mb-2">Invites are case sensitive.</p>
|
||||||
|
<Input
|
||||||
|
value={editDraft}
|
||||||
|
onChange={(e) => setEditDraft(e.currentTarget.value)}
|
||||||
|
placeholder={code}
|
||||||
|
/>
|
||||||
|
</AlertDialogDescription>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogAction
|
||||||
|
disabled={!editDraft}
|
||||||
|
onClick={async () => {
|
||||||
|
try {
|
||||||
|
await editInvite(code, editDraft);
|
||||||
|
setCode(editDraft);
|
||||||
|
setEditDraft("");
|
||||||
|
toast({ title: "Invite edited" });
|
||||||
|
} catch(e) {
|
||||||
|
toast({
|
||||||
|
title: "Failed to edit invite",
|
||||||
|
description: String(e),
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>Edit</AlertDialogAction>
|
||||||
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
)
|
||||||
|
: <></>}
|
||||||
|
<AlertDialog>
|
||||||
|
<AlertDialogTrigger asChild>
|
||||||
|
<Button>Delete</Button>
|
||||||
|
</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>
|
||||||
|
Delete invite
|
||||||
|
</AlertDialogTitle>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogDescription>
|
||||||
|
Are you sure you want to irreversibly delete this invite?
|
||||||
|
</AlertDialogDescription>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogAction
|
||||||
|
onClick={async () => {
|
||||||
|
try {
|
||||||
|
await deleteInvite(code);
|
||||||
|
setDeleted(true);
|
||||||
|
toast({ title: "Invite deleted" });
|
||||||
|
} catch(e) {
|
||||||
|
toast({
|
||||||
|
title: "Failed to delete invite",
|
||||||
|
description: String(e),
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>Delete</AlertDialogAction>
|
||||||
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,8 @@ export default function ServerInviteList({ server, invites, channels, users }: {
|
||||||
return invites
|
return invites
|
||||||
?.filter(invite => vanityOnly ? invite.vanity : true)
|
?.filter(invite => vanityOnly ? invite.vanity : true)
|
||||||
?.filter(invite => channelFilter ? invite.channel == channelFilter : true)
|
?.filter(invite => channelFilter ? invite.channel == channelFilter : true)
|
||||||
?.filter(invite => userFilter ? invite.creator == userFilter : true);
|
?.filter(invite => userFilter ? invite.creator == userFilter : true)
|
||||||
|
?.reverse();
|
||||||
}, [vanityOnly, channelFilter, userFilter, invites]);
|
}, [vanityOnly, channelFilter, userFilter, invites]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -16,7 +16,7 @@ type Permission =
|
||||||
| ""
|
| ""
|
||||||
| `/fetch${"" | "/by-id" | "/by-user"}`
|
| `/fetch${"" | "/by-id" | "/by-user"}`
|
||||||
| `/update${"" | "/discoverability"}`}`
|
| `/update${"" | "/discoverability"}`}`
|
||||||
| `channels${"" | `/fetch${"" | "/by-id" | "/by-server" | "/dm" | "/invites"}` | `/create${"" | "/dm"}`}`
|
| `channels${"" | `/fetch${"" | "/by-id" | "/by-server" | "/dm" | "/invites"}` | `/create${"" | "/dm" | "/invites"}` | `/update${"" | "/invites"}`}`
|
||||||
| `messages${"" | `/fetch${"" | "/by-id" | "/by-user"}`}`
|
| `messages${"" | `/fetch${"" | "/by-id" | "/by-user"}`}`
|
||||||
| `reports${
|
| `reports${
|
||||||
| ""
|
| ""
|
||||||
|
@ -109,6 +109,9 @@ const PermissionSets = {
|
||||||
"accounts/deletion/cancel",
|
"accounts/deletion/cancel",
|
||||||
"accounts/update/email",
|
"accounts/update/email",
|
||||||
"accounts/update/mfa",
|
"accounts/update/mfa",
|
||||||
|
|
||||||
|
"channels/update/invites",
|
||||||
|
"channels/fetch/invites",
|
||||||
] as Permission[],
|
] as Permission[],
|
||||||
|
|
||||||
// Moderate users
|
// Moderate users
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { writeFile } from "fs/promises";
|
||||||
import { PLATFORM_MOD_ID, RESTRICT_ACCESS_LIST } from "./constants";
|
import { PLATFORM_MOD_ID, RESTRICT_ACCESS_LIST } from "./constants";
|
||||||
import mongo, {
|
import mongo, {
|
||||||
Account,
|
Account,
|
||||||
|
ChannelInvite,
|
||||||
createDM,
|
createDM,
|
||||||
fetchAccountById,
|
fetchAccountById,
|
||||||
fetchChannels,
|
fetchChannels,
|
||||||
|
@ -587,6 +588,33 @@ export async function updateServerDiscoverability(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function deleteInvite(invite: string) {
|
||||||
|
await checkPermission("channels/update/invites", invite);
|
||||||
|
|
||||||
|
await mongo()
|
||||||
|
.db("revolt")
|
||||||
|
.collection<ChannelInvite>("channel_invites")
|
||||||
|
.deleteOne({ _id: invite });
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function editInvite(invite: string, newInvite: string) {
|
||||||
|
await checkPermission("channels/update/invites", { invite, newInvite });
|
||||||
|
|
||||||
|
if (!newInvite) throw new Error("invite is empty");
|
||||||
|
|
||||||
|
const { value } = await mongo()
|
||||||
|
.db("revolt")
|
||||||
|
.collection<ChannelInvite>("channel_invites")
|
||||||
|
.findOneAndDelete({ _id: invite });
|
||||||
|
|
||||||
|
if (!value) throw new Error("invite doesn't exist");
|
||||||
|
|
||||||
|
await mongo()
|
||||||
|
.db("revolt")
|
||||||
|
.collection<ChannelInvite>("channel_invites")
|
||||||
|
.insertOne({ ...value, _id: newInvite });
|
||||||
|
}
|
||||||
|
|
||||||
export async function updateBotDiscoverability(botId: string, state: boolean) {
|
export async function updateBotDiscoverability(botId: string, state: boolean) {
|
||||||
await checkPermission("bots/update/discoverability", botId, { state });
|
await checkPermission("bots/update/discoverability", botId, { state });
|
||||||
await mongo()
|
await mongo()
|
||||||
|
|
Loading…
Reference in New Issue