Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): find a chat's message filed under its lid before mapping

Yudhi Armyndharis4a815e6e615e
diff --git a/src/engine/adapters/baileys-session-store.spec.ts b/src/engine/adapters/baileys-session-store.spec.ts
index 935864f84..d95a7eabd 100644
--- a/src/engine/adapters/baileys-session-store.spec.ts
+++ b/src/engine/adapters/baileys-session-store.spec.ts
@@ -675,6 +675,28 @@ describe('BaileysSessionStore', () => {
expect(store.lastMessage('[email protected]')?.key.id).toBe('IN');
expect(store.lastMessage(LID)?.key.id).toBe('IN');
});
+
+ it('still finds a message filed under the lid before the mapping to the phone chat was learned', () => {
+ store.upsertChats([{ id: PHONE }]);
+ store.recordMessage(msg(LID, 'IN', 100));
+ store.addLidMappings([{ lid: LID, pn: PHONE }]);
+ for (const id of [LID, '[email protected]']) {
+ expect(store.lastMessage(id)).toEqual({
+ key: { remoteJid: LID, fromMe: false, id: 'IN' },
+ timestamp: 100,
+ jid: PHONE,
+ });
+ expect(store.lastInboundMessage(id)?.key.id).toBe('IN');
+ }
+ });
+
+ it('prefers the newest message across twins over an older one under the chat key', () => {
+ store.upsertChats([{ id: PHONE }]);
+ store.recordMessage(msg(PHONE, 'OUT', 100, true));
+ store.recordMessage(msg(LID, 'IN', 200));
+ store.addLidMappings([{ lid: LID, pn: PHONE }]);
+ expect(store.lastMessage('[email protected]')).toEqual(expect.objectContaining({ timestamp: 200, jid: PHONE }));
+ });
});
describe('persistent lid->phone table', () => {
diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.ts
index 895718078..d203e6384 100644
--- a/src/engine/adapters/baileys-session-store.ts
+++ b/src/engine/adapters/baileys-session-store.ts
@@ -526,14 +526,27 @@ export class BaileysSessionStore {
/** The chat's newest message, with `jid`, the id the chat itself is keyed under. */
lastMessage(chatId: string): { key: WAMessageKey; timestamp: number; jid: string } | null {
- const jid = this.chatKey(chatId);
- const m = this.lastMessages.get(jid);
- return m ? { key: m.key, timestamp: m.timestamp, jid } : null;
+ const m = this.newestAcrossTwins(this.lastMessages, chatId);
+ return m ? { key: m.key, timestamp: m.timestamp, jid: this.chatKey(chatId) } : null;
}
/** The newest message the chat received (not one this account sent), or null when none is known. */
lastInboundMessage(chatId: string): { key: WAMessageKey; timestamp: number } | null {
- return this.lastInbound.get(this.chatKey(chatId)) ?? null;
+ return this.newestAcrossTwins(this.lastInbound, chatId) ?? null;
+ }
+
+ /**
+ * The newest entry `map` holds for a chat under any of its spellings. A message recorded under the
+ * contact's lid before the lid->phone mapping was learned stays on the lid twin while the chat key
+ * moves to the phone-keyed chat record, so reading the chat key alone would lose it.
+ */
+ private newestAcrossTwins<T extends { timestamp: number }>(map: LruMap<string, T>, chatId: string): T | undefined {
+ let newest: T | undefined;
+ for (const k of [this.chatKey(chatId), ...this.chatTwins(chatId)]) {
+ const v = map.get(k);
+ if (v && (!newest || v.timestamp > newest.timestamp)) newest = v;
+ }
+ return newest;
}
/**