From c0f2c974587d75a38e3e753368ef0e2e2be139fd Mon Sep 17 00:00:00 2001
From: Vendicated <vendicated@riseup.net>
Date: Fri, 6 Oct 2023 19:40:53 +0200
Subject: ReviewDB: proper multi account support

---
 src/plugins/reviewDB/index.tsx | 45 +++++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 14 deletions(-)

(limited to 'src/plugins/reviewDB/index.tsx')

diff --git a/src/plugins/reviewDB/index.tsx b/src/plugins/reviewDB/index.tsx
index a519713..b9350ba 100644
--- a/src/plugins/reviewDB/index.tsx
+++ b/src/plugins/reviewDB/index.tsx
@@ -23,16 +23,17 @@ import ErrorBoundary from "@components/ErrorBoundary";
 import ExpandableHeader from "@components/ExpandableHeader";
 import { OpenExternalIcon } from "@components/Icons";
 import { Devs } from "@utils/constants";
+import { Logger } from "@utils/Logger";
 import definePlugin from "@utils/types";
-import { Alerts, Menu, Parser, useState } from "@webpack/common";
+import { Alerts, Menu, Parser, showToast, useState } from "@webpack/common";
 import { Guild, User } from "discord-types/general";
 
+import { Auth, initAuth, updateAuth } from "./auth";
 import { openReviewsModal } from "./components/ReviewModal";
 import ReviewsView from "./components/ReviewsView";
 import { NotificationType } from "./entities";
 import { getCurrentUserInfo, readNotification } from "./reviewDbApi";
 import { settings } from "./settings";
-import { showToast } from "./utils";
 
 const guildPopoutPatch: NavContextMenuPatchCallback = (children, props: { guild: Guild, onClose(): void; }) => () => {
     children.push(
@@ -62,31 +63,48 @@ export default definePlugin({
         }
     ],
 
+    flux: {
+        CONNECTION_OPEN: initAuth,
+    },
+
     async start() {
+        addContextMenuPatch("guild-header-popout", guildPopoutPatch);
+
         const s = settings.store;
-        const { token, lastReviewId, notifyReviews } = s;
+        const { lastReviewId, notifyReviews } = s;
+
+        const legacy = s as any as { token?: string; };
+        if (legacy.token) {
+            await updateAuth({ token: legacy.token });
+            legacy.token = undefined;
+            new Logger("ReviewDB").info("Migrated legacy settings");
+        }
 
-        if (!notifyReviews || !token) return;
+        await initAuth();
 
         setTimeout(async () => {
-            const user = await getCurrentUserInfo(token);
-            if (lastReviewId && lastReviewId < user.lastReviewID) {
-                s.lastReviewId = user.lastReviewID;
-                if (user.lastReviewID !== 0)
-                    showToast("You have new reviews on your profile!");
-            }
+            if (!Auth.token) return;
+
+            const user = await getCurrentUserInfo(Auth.token);
+            updateAuth({ user });
 
-            addContextMenuPatch("guild-header-popout", guildPopoutPatch);
+            if (notifyReviews) {
+                if (lastReviewId && lastReviewId < user.lastReviewID) {
+                    s.lastReviewId = user.lastReviewID;
+                    if (user.lastReviewID !== 0)
+                        showToast("You have new reviews on your profile!");
+                }
+            }
 
             if (user.notification) {
                 const props = user.notification.type === NotificationType.Ban ? {
                     cancelText: "Appeal",
                     confirmText: "Ok",
-                    onCancel: () =>
+                    onCancel: async () =>
                         VencordNative.native.openExternal(
                             "https://reviewdb.mantikafasi.dev/api/redirect?"
                             + new URLSearchParams({
-                                token: settings.store.token!,
+                                token: Auth.token!,
                                 page: "dashboard/appeal"
                             })
                         )
@@ -105,7 +123,6 @@ export default definePlugin({
 
                 readNotification(user.notification.id);
             }
-            s.user = user;
         }, 4000);
     },
 
-- 
cgit 

