Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): address chat actions to the jid the chat is keyed by

Yudhi Armyndharis73f8c2ba328f
diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.ts
index 3f5ca807b..e716c7003 100644
--- a/src/engine/adapters/baileys-contacts.ts
+++ b/src/engine/adapters/baileys-contacts.ts
@@ -23,8 +23,12 @@ export interface BaileysContactsHost {
findContact(contactId: string): Contact | null;
resolvePhone(contactId: string): string | null;
listChats(): ChatSummary[];
- /** The chat's last known message (the handle readMessages/chatModify need), or null when none. */
- lastMessage(chatId: string): { key: WAMessageKey; timestamp: number } | null;
+ /**
+ * The chat's last known message (the handle readMessages/chatModify need), or null when none.
+ * `jid` is the id the chat itself is keyed under, which for a lid-migrated contact is its lid
+ * whatever id was passed.
+ */
+ lastMessage(chatId: string): { key: WAMessageKey; timestamp: number; jid: string } | null;
/**
* Stored copies of the named messages, in whatever order the store returns them. Ids the store
* has never seen are absent, so neither the length nor the order tracks the input. `undefined`
@@ -395,7 +399,7 @@ export class BaileysContacts {
await this.confirmed(
this.sock().chatModify(
{ markRead: false, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },
- this.host.toEngineJid(chatId),
+ last.jid,
),
'the unread mark',
);
@@ -411,7 +415,7 @@ export class BaileysContacts {
await this.confirmed(
this.sock().chatModify(
{ clear: true, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },
- this.host.toEngineJid(chatId),
+ last.jid,
),
'the chat clear',
);
@@ -427,7 +431,7 @@ export class BaileysContacts {
await this.confirmed(
this.sock().chatModify(
{ archive, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },
- this.host.toEngineJid(chatId),
+ last.jid,
),
'the archive change',
);
@@ -460,7 +464,7 @@ export class BaileysContacts {
await this.confirmed(
this.sock().chatModify(
{ delete: true, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },
- this.host.toEngineJid(chatId),
+ last.jid,
),
'the chat delete',
);
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 6f3a424fa..8b647d472 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -6113,6 +6113,37 @@ describe('BaileysAdapter sendSeen + markUnread + deleteChat', () => {
);
});
+ it.each([
+ ['markUnread', (a: BaileysAdapter) => a.markUnread('[email protected]')],
+ ['clearChatMessages', (a: BaileysAdapter) => a.clearChatMessages('[email protected]')],
+ ['archiveChat', (a: BaileysAdapter) => a.archiveChat('[email protected]', true)],
+ ['deleteChat', (a: BaileysAdapter) => a.deleteChat('[email protected]')],
+ ])('%s addresses a lid-keyed chat by its lid when called with the listed @c.us id', async (_n, act) => {
+ const adapter = newAdapter();
+ await adapter.initialize({});
+ fakeSock.fire('connection.update', { connection: 'open' });
+ fakeSock.fire('chats.upsert', [{ id: '484848@lid' }]);
+ fakeSock.fire('messages.upsert', {
+ type: 'notify',
+ messages: [
+ {
+ key: { remoteJid: '484848@lid', remoteJidAlt: '[email protected]', fromMe: false, id: 'M1' },
+ message: { conversation: 'hi' },
+ messageTimestamp: 1700000020,
+ },
+ ],
+ });
+ await new Promise(r => setImmediate(r));
+ expect((await adapter.getChats())[0]?.id).toBe('[email protected]');
+ await expect(act(adapter)).resolves.toBe(true);
+ expect(fakeSock.chatModify).toHaveBeenCalledWith(
+ expect.objectContaining({
+ lastMessages: [{ key: expect.objectContaining({ id: 'M1' }) as unknown, messageTimestamp: 1700000020 }],
+ }),
+ '484848@lid',
+ );
+ });
+
it('clearChatMessages returns false for a chat with no known history', async () => {
const adapter = newAdapter();
await adapter.initialize({});