aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/pronoundb/pronoundbUtils.ts
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-01-13 08:38:59 +0100
committernea <nea@nea.moe>2023-01-13 08:44:16 +0100
commit069cb8bbf5f5bd413eec01535b71f9449f75a64d (patch)
treed90a6c7c8b8e61785bb9514f534c8893eba707df /src/plugins/pronoundb/pronoundbUtils.ts
parent26f2b51eb95d59ed55d1fc0f81a1579e08bd29f3 (diff)
downloadVencord-feat/localpronounoverrides.tar.gz
Vencord-feat/localpronounoverrides.tar.bz2
Vencord-feat/localpronounoverrides.zip
feat(PronounDB): Allow setting local pronoun overrides for peoplefeat/localpronounoverrides
Diffstat (limited to 'src/plugins/pronoundb/pronoundbUtils.ts')
-rw-r--r--src/plugins/pronoundb/pronoundbUtils.ts32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/plugins/pronoundb/pronoundbUtils.ts b/src/plugins/pronoundb/pronoundbUtils.ts
index afeea95..2eff47a 100644
--- a/src/plugins/pronoundb/pronoundbUtils.ts
+++ b/src/plugins/pronoundb/pronoundbUtils.ts
@@ -1,6 +1,6 @@
/*
* Vencord, a modification for Discord's desktop app
- * Copyright (c) 2022 Vendicated and contributors
+ * Copyright (c) 2022-2023 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+import * as DataStore from "@api/DataStore";
import { Settings } from "@api/settings";
import { VENCORD_USER_AGENT } from "@utils/constants";
import { debounce } from "@utils/debounce";
@@ -23,6 +24,15 @@ import { debounce } from "@utils/debounce";
import { PronounsFormat } from ".";
import { PronounCode, PronounMapping, PronounsResponse } from "./types";
+
+let pronounDBStore: DataStore.UseStore | undefined;
+
+function getPronounDbStore(): DataStore.UseStore {
+ if (!pronounDBStore)
+ pronounDBStore = DataStore.createStore("VencordPronounData", "LocalPronounDBOverrides");
+ return pronounDBStore;
+}
+
// A map of cached pronouns so the same request isn't sent twice
const cache: Record<string, PronounCode> = {};
// A map of ids and callbacks that should be triggered on fetch
@@ -39,6 +49,26 @@ const bulkFetch = debounce(async () => {
}
});
+// Find the pronouns for a discord id, first looking for local override then fetching pronoundb.
+export async function findPronouns(id: string): Promise<PronounCode> {
+ return (await getLocalPronounOverride(id)) ?? await fetchPronouns(id);
+}
+
+export function setLocalPronounOverride(id: string, newOverride: PronounCode | null): Promise<void> {
+ if (newOverride == null) {
+ return DataStore.del(id, getPronounDbStore());
+ } else {
+ return DataStore.set(id, newOverride, getPronounDbStore());
+ }
+}
+
+export async function getLocalPronounOverride(id: string): Promise<PronounCode | null> {
+ const localValue = await DataStore.get(id, getPronounDbStore());
+ if (localValue in PronounMapping)
+ return localValue as PronounCode;
+ return null;
+}
+
// Fetches the pronouns for one id, returning a promise that resolves if it was cached, or once the request is completed
export function fetchPronouns(id: string): Promise<PronounCode> {
return new Promise(res => {