Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): read an empty key participant the same way everywhere

Yudhi Armyndharise7762ec076d4
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index dda9690a5..bc55912eb 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -28,6 +28,7 @@ import {
extractBaileysContext,
extractBaileysLocation,
isBaileysCatalogShare,
+ keyParticipant,
mapBaileysEventResponseType,
mapBaileysStatus,
setBaileysText,
@@ -103,15 +104,6 @@ const PROTOCOL_NOISE_KEYS: ReadonlySet<string> = new Set([
'messageHistoryBundle',
]);
-/**
- * The participant a live key names, or undefined when it names none. Baileys passes every live key
- * through cleanMessage, which writes jidNormalizedUser(participant) back, and that is '' for the absent
- * participant of a 1:1 chat. `??` keeps the empty string, which then reads as a sender that is nobody.
- */
-export function keyParticipant(key: { participant?: string | null }): string | undefined {
- return key.participant ? key.participant : undefined;
-}
-
/**
* Whether two ids provably name different chats or people. Each side is a jid plus the other-dialect
* twin WhatsApp may send beside it (`remoteJidAlt`, `participantAlt`), compared neutralised, so a lid
diff --git a/src/engine/adapters/baileys-history.spec.ts b/src/engine/adapters/baileys-history.spec.ts
index c17cc3e97..012f5c79f 100644
--- a/src/engine/adapters/baileys-history.spec.ts
+++ b/src/engine/adapters/baileys-history.spec.ts
@@ -60,6 +60,38 @@ describe('history chat previews', () => {
});
});
+describe('history sender names', () => {
+ it('files the push name of a 1:1 message whose key names an empty participant under the chat', async () => {
+ const upsertContacts = jest.fn();
+ const host = {
+ loadLib: () =>
+ Promise.resolve({
+ normalizeMessageContent: (m: unknown) => m,
+ getContentType: (m: object) => Object.keys(m)[0],
+ proto: { Message: { ProtocolMessage: { Type: { REVOKE: 0 } } } },
+ }),
+ normalizedSelfJid: () => '[email protected]',
+ toNeutralJid: (id: string) => id,
+ extractEphemeralDuration: () => undefined,
+ recordMessage: jest.fn(),
+ recordMessageEdit: jest.fn(),
+ upsertContacts,
+ getOnHistoryMessages: () => () => undefined,
+ applyHistoryRevoke: (key: WAMessage['key']) => Promise.resolve(key),
+ wasDeletedForEveryone: () => false,
+ } as unknown as BaileysHistoryHost;
+ await new BaileysHistory(host).captureHistoryMessages([
+ {
+ key: { id: 'M1', remoteJid: '[email protected]', fromMe: false, participant: '' },
+ pushName: 'Rina',
+ message: { conversation: 'hi' },
+ messageTimestamp: 100,
+ },
+ ] as WAMessage[]);
+ expect(upsertContacts).toHaveBeenCalledWith([{ id: '[email protected]', notify: 'Rina' }]);
+ });
+});
+
/**
* `groupFetchAllParticipating` yields `{}` for BOTH an unanswered query and an account with no groups —
* the same ambiguity `getGroups` is bounded against in baileys-groups.ts. Nothing in the VALUE separates
diff --git a/src/engine/adapters/baileys-history.ts b/src/engine/adapters/baileys-history.ts
index e1821bf9b..633b7f0eb 100644
--- a/src/engine/adapters/baileys-history.ts
+++ b/src/engine/adapters/baileys-history.ts
@@ -10,6 +10,7 @@ import {
extractBaileysCommerce,
extractBaileysPoll,
isBaileysCatalogShare,
+ keyParticipant,
} from './baileys-message-mapper';
import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';
import { type createLogger } from '../../common/services/logger.service';
@@ -111,7 +112,7 @@ export class BaileysHistory {
const targets = new Map(messages.filter(msg => msg.key?.id).map(msg => [msg.key.id!, msg]));
for (const msg of messages) {
if (msg.key?.fromMe !== true && msg.pushName) {
- const sender = msg.key?.participant ?? msg.key?.remoteJid;
+ const sender = keyParticipant(msg.key ?? {}) ?? msg.key?.remoteJid;
if (sender) {
nameUpdates.push({ id: sender, notify: msg.pushName });
}
@@ -161,7 +162,7 @@ export class BaileysHistory {
id: key.id!,
remoteJid: key.remoteJid!,
fromMe: key.fromMe === true,
- participant: key.participant ?? undefined,
+ participant: keyParticipant(key),
body: '',
contentType: undefined,
timestamp,
@@ -307,7 +308,7 @@ export class BaileysHistory {
id: msg.key.id,
remoteJid: msg.key.remoteJid,
fromMe: msg.key.fromMe === true,
- participant: msg.key.participant ?? undefined,
+ participant: keyParticipant(msg.key),
body,
contentType,
isPtt: content.audioMessage?.ptt === true,
diff --git a/src/engine/adapters/baileys-message-mapper.spec.ts b/src/engine/adapters/baileys-message-mapper.spec.ts
index f3b51f2a4..eb3b28c37 100644
--- a/src/engine/adapters/baileys-message-mapper.spec.ts
+++ b/src/engine/adapters/baileys-message-mapper.spec.ts
@@ -363,6 +363,16 @@ describe('buildIncomingMessageFromBaileys', () => {
expect(r.isLidSender).toBe(true);
});
+ it('reads an empty participant as none: a lid chat is still a lid sender and a group names no author', () => {
+ // A live key goes through cleanMessage, which writes '' for the participant of a 1:1 chat.
+ const direct = buildIncomingMessageFromBaileys({ ...base, remoteJid: '111@lid', participant: '' });
+ expect(direct.isLidSender).toBe(true);
+ expect(direct.author).toBeUndefined();
+ const group = buildIncomingMessageFromBaileys({ ...base, remoteJid: '[email protected]', participant: '' });
+ expect(group.author).toBeUndefined();
+ expect(group.isLidSender).toBeFalsy();
+ });
+
it('flags a status broadcast', () => {
const r = buildIncomingMessageFromBaileys({ ...base, remoteJid: 'status@broadcast' });
expect(r.isStatusBroadcast).toBe(true);
diff --git a/src/engine/adapters/baileys-message-mapper.ts b/src/engine/adapters/baileys-message-mapper.ts
index ad1b05208..96e98d3f3 100644
--- a/src/engine/adapters/baileys-message-mapper.ts
+++ b/src/engine/adapters/baileys-message-mapper.ts
@@ -1019,6 +1019,15 @@ export function storedKeyInChat(
return neutral(raw) === want || neutral(baileysChatJid(raw, key.participant, key.fromMe === true)) === want;
}
+/**
+ * The participant a live key names, or undefined when it names none. Baileys passes every live key
+ * through cleanMessage, which writes jidNormalizedUser(participant) back, and that is '' for the absent
+ * participant of a 1:1 chat. `??` keeps the empty string, which then reads as a sender that is nobody.
+ */
+export function keyParticipant(key: { participant?: string | null }): string | undefined {
+ return key.participant ? key.participant : undefined;
+}
+
/**
* Build a neutral {@link IncomingMessage} from extracted Baileys fields. The chat is `remoteJid`
* (Baileys reports the conversation directly), except that a received broadcast-list message is filed