PronounDB: fix caching not respecting user preference (#1344)

This commit is contained in:
alexia 2023-06-27 22:46:52 +02:00 committed by GitHub
parent 12e3c9234d
commit 8e9ba7c7ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 21 deletions

View File

@ -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"
}
}
],

View File

@ -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<string> {
export function fetchPronouns(id: string): Promise<string> {
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