Skip to content

Commit

Permalink
cache group metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Nov 8, 2024
1 parent e8ec9ee commit 4895a73
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions backend/src/libs/wbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,36 @@ export const initWASocket = async (whatsapp: Whatsapp): Promise<Session> => {

const msgRetryCounterCache = new NodeCache();
const userDevicesCache: CacheStore = new NodeCache();
const internalGroupCache = new NodeCache({
stdTTL: 5 * 60,
useClones: false
});
const groupCache: CacheStore = {
get: <T>(key: string): T => {
logger.debug(`groupCache.get ${key}`);
const value = internalGroupCache.get(key);
if (!value) {
logger.debug(`groupCache.get ${key} not found`);
wsocket.groupMetadata(key).then(async metadata => {
logger.debug({ key, metadata }, `groupCache.get ${key} set`);
internalGroupCache.set(key, metadata);
});
}
return value as T;
},
set: async (key: string, value: any) => {
logger.debug({ key, value }, `groupCache.set ${key}`);
return internalGroupCache.set(key, value);
},
del: async (key: string) => {
logger.debug(`groupCache.del ${key}`);
return internalGroupCache.del(key);
},
flushAll: async () => {
logger.debug("groupCache.flushAll");
return internalGroupCache.flushAll();
}
};

const appName =
(await GetPublicSettingService({ key: "appName" })) || "Ticketz";
Expand Down Expand Up @@ -175,6 +205,7 @@ export const initWASocket = async (whatsapp: Whatsapp): Promise<Session> => {
generateHighQualityLinkPreview: true,
userDevicesCache,
getMessage,
cachedGroupMetadata: async jid => groupCache.get(jid),
shouldIgnoreJid: jid =>
isJidBroadcast(jid) || jid?.endsWith("@newsletter"),
transactionOpts: { maxCommitRetries: 1, delayBetweenTriesMs: 10 }
Expand Down Expand Up @@ -374,6 +405,29 @@ export const initWASocket = async (whatsapp: Whatsapp): Promise<Session> => {
}
);

wsocket.ev.on("groups.upsert", groups => {
logger.debug("Received new group");
groups.forEach(group => {
groupCache.set(group.id, group);
});
});

wsocket.ev.on("groups.update", async ([event]) => {
logger.debug("Received group update");
const metadata = await wsocket.groupMetadata(event.id);
groupCache.set(event.id, metadata);
});

wsocket.ev.on("group-participants.update", async event => {
logger.debug("Received group participants update");
try {
const metadata = await wsocket.groupMetadata(event.id);
groupCache.set(event.id, metadata);
} catch (error) {
groupCache.del(event.id);
}
});

store.bind(wsocket.ev);
})();
} catch (error) {
Expand Down

0 comments on commit 4895a73

Please sign in to comment.