diff --git a/src/libs/caching.js b/src/libs/caching.js index e4f3689..49a0645 100644 --- a/src/libs/caching.js +++ b/src/libs/caching.js @@ -1,5 +1,9 @@ -import { debug, error } from "./logging"; -import { storage } from "webextension-polyfill"; +import { debug, info, error } from "./logging.js"; +import { runtime, storage } from "webextension-polyfill"; + +const currentVersion = runtime.getManifest().version; +const cacheMaxAge = 24 * 60 * 60 * 1000; // time after which cached pronouns should be checked again: 24h + /** * Appends an entry to the "pronounsCache" object in local storage. * @@ -15,7 +19,12 @@ export async function cachePronouns(account, pronouns) { cache = { pronounsCache: {} }; } - cache.pronounsCache[account] = { acct: account, timestamp: Date.now(), value: pronouns }; + cache.pronounsCache[account] = { + acct: account, + timestamp: Date.now(), + value: pronouns, + version: currentVersion, + }; try { await storage.local.set(cache); debug(`${account} cached`); @@ -24,7 +33,12 @@ export async function cachePronouns(account, pronouns) { } } -export async function getPronouns() { +/** + * + * @param {string} accountName + * @returns {Promise} Account's cached pronouns, or null if not saved or stale + */ +export async function getPronouns(accountName) { const fallback = { pronounsCache: {} }; let cacheResult; try { @@ -38,5 +52,21 @@ export async function getPronouns() { cacheResult = fallback; // ignore errors, we have an empty object as fallback. } - return cacheResult; + + // Extract the current cache by using object destructuring. + if (accountName in cacheResult.pronounsCache) { + const { value, timestamp, version } = cacheResult.pronounsCache[accountName]; + + // If we have a cached value and it's not outdated, use it. + if (value && Date.now() - timestamp < cacheMaxAge && version == currentVersion) { + info(`${accountName} in cache with value: ${value}`); + return value; + } else { + info(`${accountName} cache entry is stale, refreshing`); + } + } else { + info(`${accountName} not in cache, fetching status`); + } + + return null; } diff --git a/src/libs/fetchPronouns.js b/src/libs/fetchPronouns.js index 281e796..0079cb4 100644 --- a/src/libs/fetchPronouns.js +++ b/src/libs/fetchPronouns.js @@ -3,7 +3,6 @@ import { cachePronouns, getPronouns } from "./caching"; import { normaliseAccountName } from "./protootshelpers"; import { extractFromStatus } from "./pronouns"; -const cacheMaxAge = 24 * 60 * 60 * 1000; // time after which cached pronouns should be checked again: 24h let conversationsCache; /** @@ -17,30 +16,15 @@ let conversationsCache; */ export async function fetchPronouns(dataID, accountName, type) { // log(`searching for ${account_name}`); - const cacheResult = await getPronouns(); + const cacheResult = await getPronouns(accountName); debug(cacheResult); - // Extract the current cache by using object destructuring. - if (accountName in cacheResult.pronounsCache) { - const { value, timestamp } = cacheResult.pronounsCache[accountName]; - - // If we have a cached value and it's not outdated, use it. - if (value && Date.now() - timestamp < cacheMaxAge) { - info(`${accountName} in cache with value: ${value}`); - return value; - } else { - info(`${accountName} cache entry is stale, refreshing`); - } - } - - info(`${accountName} cache entry is stale, refreshing`); + if (cacheResult) return cacheResult; if (!dataID) { warn(`Could not fetch pronouns for user ${accountName}, because no status ID was passed.`); return null; } - info(`${accountName} not in cache, fetching status`); - let status; if (type === "notification") { status = await fetchNotification(dataID);