From 3a2e886803f93c14b1b7a2028c28248a59211a0e Mon Sep 17 00:00:00 2001 From: Lea Date: Thu, 10 Aug 2023 15:38:51 +0200 Subject: [PATCH] feat: look up user by email --- app/panel/inspect/page.tsx | 38 ++++++++++++++++++++++++++++++++++++++ lib/accessPermissions.ts | 3 ++- lib/actions.ts | 17 +++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/app/panel/inspect/page.tsx b/app/panel/inspect/page.tsx index 7ae0b0d..c51266b 100644 --- a/app/panel/inspect/page.tsx +++ b/app/panel/inspect/page.tsx @@ -2,14 +2,35 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; +import { toast } from "@/components/ui/use-toast"; +import { lookupEmail } from "@/lib/actions"; import { API_URL } from "@/lib/constants"; import { useRouter } from "next/navigation"; import { useState } from "react"; export default function Inspect() { const [id, setId] = useState(""); + const [email, setEmail] = useState(""); const router = useRouter(); + const searchEmail = async () => { + try { + const result = await lookupEmail(email); + if (!result) toast({ + title: "Not found", + description: "There doesn't seem to be any user with this email.", + variant: "destructive", + }); + else router.push(`/panel/inspect/account/${result}`); + } catch(e) { + toast({ + title: "Failed to look up", + description: String(e), + variant: "destructive", + }) + } + }; + const createHandler = (type: string) => () => router.push(`/panel/inspect/${type}/${id}`); @@ -70,6 +91,23 @@ export default function Inspect() { Message +
+
+ setEmail(e.currentTarget.value)} + onKeyDown={(e) => e.key == "Enter" && email && searchEmail()} + /> + +
); } diff --git a/lib/accessPermissions.ts b/lib/accessPermissions.ts index 708c16c..a150873 100644 --- a/lib/accessPermissions.ts +++ b/lib/accessPermissions.ts @@ -7,7 +7,7 @@ type Permission = | "chat_message" | `accounts${ | "" - | `/fetch${"" | "/by-id"}` + | `/fetch${"" | "/by-id" | "/by-email"}` | "/update/email" | "/disable" | "/restore" @@ -101,6 +101,7 @@ const PermissionSets = { "users/update/badges", "accounts/fetch/by-id", + "accounts/fetch/by-email", "accounts/disable", "accounts/restore", "accounts/deletion/queue", diff --git a/lib/actions.ts b/lib/actions.ts index ddc733d..00ebbc5 100644 --- a/lib/actions.ts +++ b/lib/actions.ts @@ -28,6 +28,7 @@ import { User, } from "revolt-api"; import { checkPermission } from "./accessPermissions"; +import { redirect } from "next/navigation"; export async function sendAlert(userId: string, content: string) { await checkPermission("users/create/alert", userId, { content }); @@ -249,6 +250,22 @@ export async function verifyAccountEmail(userId: string) { ); } +export async function lookupEmail(email: string): Promise { + await checkPermission("accounts/fetch/by-email", email); + + const accounts = mongo() + .db("revolt") + .collection("accounts"); + + let result = await accounts.findOne({ email: email }); + if (result) return result._id; + + result = await accounts.findOne({ email_normalised: email }); + if (result) return result._id; + + return false; +} + export async function suspendUser(userId: string) { if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";