Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): fold @c.us to the engine JID for chatModify/contact app-state writes

Yudhi Armyndharisd25fd4860613
diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.ts
index 98e0e9415..719e7d5ca 100644
--- a/src/engine/adapters/baileys-contacts.ts
+++ b/src/engine/adapters/baileys-contacts.ts
@@ -20,6 +20,8 @@ export interface BaileysContactsHost {
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;
+ /** Fold a neutral @c.us id to the engine @s.whatsapp.net form used as the app-state index key. */
+ toEngineJid(jid: string): string;
}
export class BaileysContacts {
@@ -49,7 +51,11 @@ export class BaileysContacts {
// Baileys addresses the entry by JID, unlike whatsapp-web.js which wants a bare phone number.
// saveOnPrimaryAddressbook stays false to match the wwjs default (syncToAddressbook=false):
// writing through to the device addressbook is a heavier action than saving the contact.
- await this.sock().addOrEditContact(contactId, {
+ // Fold the neutral @c.us the API speaks to the engine @s.whatsapp.net: addOrEditContact ->
+ // chatModify keys the addressbook app-state patch by the raw jid (no jidNormalizedUser, unlike
+ // the send path), so a raw @c.us index would land under a key WhatsApp never reads and the
+ // write would silently target nothing while the endpoint reports success.
+ await this.sock().addOrEditContact(this.host.toEngineJid(contactId), {
firstName,
fullName,
saveOnPrimaryAddressbook: false,
@@ -58,7 +64,8 @@ export class BaileysContacts {
async deleteContact(contactId: string): Promise<void> {
this.host.ensureReady();
- await this.sock().removeContact(contactId);
+ // Same app-state key fold as upsertContact — a raw @c.us removal targets a phantom entry.
+ await this.sock().removeContact(this.host.toEngineJid(contactId));
}
async blockContact(contactId: string): Promise<void> {
@@ -135,7 +142,7 @@ export class BaileysContacts {
}
await this.sock().chatModify(
{ markRead: false, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },
- chatId,
+ this.host.toEngineJid(chatId),
);
return true;
}
@@ -148,7 +155,7 @@ export class BaileysContacts {
}
await this.sock().chatModify(
{ clear: true, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },
- chatId,
+ this.host.toEngineJid(chatId),
);
return true;
}
@@ -161,7 +168,7 @@ export class BaileysContacts {
}
await this.sock().chatModify(
{ archive, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },
- chatId,
+ this.host.toEngineJid(chatId),
);
return true;
}
@@ -174,7 +181,7 @@ export class BaileysContacts {
}
await this.sock().chatModify(
{ delete: true, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },
- chatId,
+ this.host.toEngineJid(chatId),
);
return true;
}
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index ba01cfaa1..ebccb9cf9 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -312,7 +312,7 @@ export class BaileysMessaging {
timestamp: this.host.toUnixSeconds(target.messageTimestamp),
},
},
- chatId,
+ this.host.toEngineJid(chatId),
);
}
@@ -454,9 +454,12 @@ export class BaileysMessaging {
const target = await this.requireStored(messageId);
// fromMe is load-bearing: the same message id addresses a different message depending on
// direction, so omitting it would star the wrong side of the conversation.
+ // Fold @c.us -> @s.whatsapp.net: chatModify keys the star app-state index by the raw jid (no
+ // jidNormalizedUser, unlike the send path), so a neutral @c.us would index a phantom chat and
+ // the star would silently apply to nothing on a 1:1 conversation.
await this.sock().chatModify(
{ star: { messages: [{ id: target.key.id!, fromMe: target.key.fromMe ?? false }], star } },
- chatId,
+ this.host.toEngineJid(chatId),
);
}
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index ad8ff853c..9cc32a088 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -3828,6 +3828,77 @@ describe('BaileysAdapter sendSeen + markUnread + deleteChat', () => {
expect(await adapter.archiveChat('[email protected]', true)).toBe(false);
expect(fakeSock.chatModify).not.toHaveBeenCalled();
});
+
+ /**
+ * The neutral @c.us id the gateway API and getContacts speak must be folded to the engine
+ * @s.whatsapp.net form before it becomes the app-state index key. chatModify/addOrEditContact
+ * (unlike the send path) do NOT call jidNormalizedUser, so a raw @c.us would key the mutation
+ * under an index WhatsApp never reads — the write silently targets nothing while the endpoint
+ * reports success. These pass the neutral id (the shape a list-then-mutate round-trip yields).
+ */
+ describe('folds the neutral @c.us id to the engine form for chatModify/contact app-state ops', () => {
+ it('upsertContact folds @c.us -> @s.whatsapp.net', async () => {
+ const adapter = await readyWithMessage();
+ await adapter.upsertContact('[email protected]', 'Ada');
+ expect(fakeSock.addOrEditContact).toHaveBeenCalledWith('[email protected]', expect.any(Object));
+ });
+
+ it('deleteContact folds @c.us -> @s.whatsapp.net', async () => {
+ const adapter = await readyWithMessage();
+ await adapter.deleteContact('[email protected]');
+ expect(fakeSock.removeContact).toHaveBeenCalledWith('[email protected]');
+ });
+
+ it('clearChatMessages folds the chatModify index jid for a 1:1 chat', async () => {
+ const adapter = await readyWithMessage();
+ expect(await adapter.clearChatMessages('[email protected]')).toBe(true);
+ expect(fakeSock.chatModify).toHaveBeenCalledWith(
+ expect.objectContaining({ clear: true }),
+ );
+ });
+
+ it('archiveChat folds the chatModify index jid for a 1:1 chat', async () => {
+ const adapter = await readyWithMessage();
+ expect(await adapter.archiveChat('[email protected]', true)).toBe(true);
+ expect(fakeSock.chatModify).toHaveBeenCalledWith(
+ expect.objectContaining({ archive: true }),
+ );
+ });
+
+ it('starMessage folds the chatModify index jid for a 1:1 chat', async () => {
+ fakeStore.getMessage.mockResolvedValue({
+ key: { remoteJid: '[email protected]', fromMe: false, id: 'M1' },
+ message: { conversation: 'hi' },
+ messageTimestamp: 1700000020,
+ });
+ const adapter = await readyWithMessage();
+ await adapter.starMessage('[email protected]', 'TARGET', true);
+ expect(fakeSock.chatModify).toHaveBeenCalledWith(
+ { star: { messages: [{ id: 'M1', fromMe: false }], star: true } },
+ );
+ });
+
+ it('leaves a group @g.us id unchanged (identical in both dialects)', async () => {
+ const adapter = await readyWithMessage();
+ // A group last-message lives under the g.us key; seed one so archive proceeds.
+ fakeSock.fire('messages.upsert', {
+ type: 'notify',
+ messages: [
+ {
+ key: { remoteJid: '[email protected]', fromMe: false, id: 'G1' },
+ message: { conversation: 'hi' },
+ messageTimestamp: 1700000021,
+ },
+ ],
+ });
+ await new Promise(resolve => setImmediate(resolve));
+ await adapter.archiveChat('[email protected]', true);
+ expect(fakeSock.chatModify).toHaveBeenCalledWith(expect.objectContaining({ archive: true }), '[email protected]');
+ });
+ });
});
describe('BaileysAdapter status posting', () => {
diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.ts
index 042259a84..0a96e4aad 100644
--- a/src/engine/adapters/baileys.adapter.ts
+++ b/src/engine/adapters/baileys.adapter.ts
@@ -170,6 +170,7 @@ export class BaileysAdapter implements IWhatsAppEngine {
resolvePhone: contactId => this.sessionStore.resolvePhone(contactId),
listChats: () => this.sessionStore.listChats(),
lastMessage: chatId => this.sessionStore.lastMessage(chatId),
+ toEngineJid: jid => this.sessionStore.toEngineJid(jid),
});
this.statusOps = new BaileysStatus({
ensureReady: () => this.ensureReady(),