From 8e9ba7c7eed7547acc086284d89f5ec6705166f5 Mon Sep 17 00:00:00 2001 From: alexia Date: Tue, 27 Jun 2023 22:46:52 +0200 Subject: [PATCH] PronounDB: fix caching not respecting user preference (#1344) --- src/plugins/pronoundb/index.ts | 4 +-- src/plugins/pronoundb/pronoundbUtils.ts | 36 ++++++++++++------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/plugins/pronoundb/index.ts b/src/plugins/pronoundb/index.ts index 0b8fd5f7..f9e26b10 100644 --- a/src/plugins/pronoundb/index.ts +++ b/src/plugins/pronoundb/index.ts @@ -52,7 +52,7 @@ export default definePlugin({ find: ".userTagNoNickname", replacement: { match: /=(\i)\.pronouns/, - replace: "=$self.useProfilePronouns($1.user.id,$1.pronouns)" + replace: "=$self.useProfilePronouns($1.user.id)" } }, // Patch the profile modal username header to use our pronoun hook instead of Discord's pronouns @@ -60,7 +60,7 @@ export default definePlugin({ find: ".USER_PROFILE_ACTIVITY", replacement: { match: /\).showPronouns/, - replace: ").showPronouns||true;const vcPronounce=$self.useProfilePronouns(arguments[0].user.id,arguments[0].displayProfile?.pronouns);if(arguments[0].displayProfile&&vcPronounce)arguments[0].displayProfile.pronouns=vcPronounce" + replace: ").showPronouns||true;const vcPronounce=$self.useProfilePronouns(arguments[0].user.id);if(arguments[0].displayProfile&&vcPronounce)arguments[0].displayProfile.pronouns=vcPronounce" } } ], diff --git a/src/plugins/pronoundb/pronoundbUtils.ts b/src/plugins/pronoundb/pronoundbUtils.ts index bded46bb..cade8826 100644 --- a/src/plugins/pronoundb/pronoundbUtils.ts +++ b/src/plugins/pronoundb/pronoundbUtils.ts @@ -62,22 +62,26 @@ function getDiscordPronouns(id: string) { ); } -export function useFormattedPronouns(id: string, discordPronouns: string = getDiscordPronouns(id)): string | null { - const [result] = useAwaiter(() => fetchPronouns(id, discordPronouns), { - fallbackValue: getCachedPronouns(id, discordPronouns), +export function useFormattedPronouns(id: string): string | null { + // Discord is so stupid you can put tons of newlines in pronouns + const discordPronouns = getDiscordPronouns(id)?.trim().replace(NewLineRe, " "); + + if (settings.store.pronounSource === PronounSource.PreferDiscord && discordPronouns) + return discordPronouns; + + const [result] = useAwaiter(() => fetchPronouns(id), { + fallbackValue: getCachedPronouns(id), onError: e => console.error("Fetching pronouns failed: ", e) }); if (result && result !== "unspecified") - return Object.hasOwn(PronounMapping, result) - ? formatPronouns(result) // PronounDB - : result; // Discord + return formatPronouns(result); - return null; + return discordPronouns; } -export function useProfilePronouns(id: string, discordPronouns: string) { - const pronouns = useFormattedPronouns(id, discordPronouns); +export function useProfilePronouns(id: string) { + const pronouns = useFormattedPronouns(id); if (!settings.store.showInProfile) return null; if (!settings.store.showSelf && id === UserStore.getCurrentUser().id) return null; @@ -89,23 +93,17 @@ export function useProfilePronouns(id: string, discordPronouns: string) { const NewLineRe = /\n+/g; // Gets the cached pronouns, if you're too impatient for a promise! -export function getCachedPronouns(id: string, discordPronouns: string): string | null { - // Discord is so stupid you can put tons of newlines in pronouns - discordPronouns = discordPronouns?.trim().replace(NewLineRe, " "); - - if (settings.store.pronounSource === PronounSource.PreferDiscord && discordPronouns) - return discordPronouns; - +export function getCachedPronouns(id: string): string | null { const cached = cache[id]; if (cached && cached !== "unspecified") return cached; - return discordPronouns || cached || null; + return cached || 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, discordPronouns: string): Promise { +export function fetchPronouns(id: string): Promise { return new Promise(res => { - const cached = getCachedPronouns(id, discordPronouns); + const cached = getCachedPronouns(id); if (cached) return res(cached); // If there is already a request added, then just add this callback to it