Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(translation): reconcile misrouted @lid author via pushName
dallascyclistf3bf57e89578
diff --git a/src/plugins/extensions/translation/core/translation.coordinator.spec.ts b/src/plugins/extensions/translation/core/translation.coordinator.spec.tsindex 32cbf2326..65ce0215f 100644--- a/src/plugins/extensions/translation/core/translation.coordinator.spec.ts+++ b/src/plugins/extensions/translation/core/translation.coordinator.spec.ts@@ -199,4 +199,69 @@ describe('TranslationCoordinator', () => {await c.handleMessage('s', msg({ author: '[email protected]', body: 'Hello', pushName: 'Doug' }));expect(saved.at(-1)?.participants['[email protected]'].pushName).toBe('Doug');});++ it('reconciles a misrouted @lid author via a uniquely-matching pushName', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ 'liz@lid': { lang: 'es', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'Lizeth' },+ 'doug@lid': { lang: 'en', source: 'pinned', enabled: true, samples: 5, updatedAt: 'x', pushName: 'Doug' },+ },+ });+ const { store, gateway, translator, logger, mocks } = makeDeps(state);+ mocks.detect.mockResolvedValue({ lang: 'es', confidence: 0.99 });+ mocks.translate.mockResolvedValue('I feel sick');+ const c = new TranslationCoordinator(translator, store, gateway, OPTS, logger);+ // Liz's Spanish message is misrouted to Doug's @lid, but the pushName is still Liz's.+ await c.handleMessage('s', msg({ author: 'doug@lid', pushName: 'Lizeth', body: 'Me siento mal' }));+ expect(mocks.translate).toHaveBeenCalledWith('Me siento mal', 'es', 'en');+ expect(mocks.sendCombinedReply).toHaveBeenCalled();+ expect(mocks.info).toHaveBeenCalledWith(+ 'sender reconciled by pushName',+ expect.objectContaining({ resolvedKey: 'liz@lid' }),+ );+ });++ it('does not reconcile when the author already owns the pushName', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ 'a@lid': { lang: 'es', source: 'pinned', enabled: true, samples: 1, updatedAt: 'x', pushName: 'Sam' },+ 'b@lid': { lang: 'en', source: 'pinned', enabled: true, samples: 1, updatedAt: 'x', pushName: 'Sam' },+ },+ });+ const { store, gateway, translator, logger, mocks } = makeDeps(state);+ mocks.detect.mockResolvedValue({ lang: 'es', confidence: 0.99 });+ mocks.translate.mockResolvedValue('hi');+ const c = new TranslationCoordinator(translator, store, gateway, OPTS, logger);+ await c.handleMessage('s', msg({ author: 'a@lid', pushName: 'Sam', body: 'Hola amigo' }));+ expect(mocks.info).not.toHaveBeenCalledWith('sender reconciled by pushName', expect.anything());+ // a@lid (es) wrote es -> target en (attributed to the author, not reconciled to b).+ expect(mocks.translate).toHaveBeenCalledWith('Hola amigo', 'es', 'en');+ });++ it('does not reconcile when the pushName is ambiguous across participants', async () => {+ const state = freshState({+ announced: true,+ active: true,+ participants: {+ 'x@lid': { lang: 'fr', source: 'pinned', enabled: true, samples: 1, updatedAt: 'x', pushName: 'Xavier' },+ 'a@lid': { lang: 'es', source: 'pinned', enabled: true, samples: 1, updatedAt: 'x', pushName: 'Sam' },+ 'b@lid': { lang: 'en', source: 'pinned', enabled: true, samples: 1, updatedAt: 'x', pushName: 'Sam' },+ },+ });+ const { store, gateway, translator, logger, mocks } = makeDeps(state);+ mocks.detect.mockResolvedValue({ lang: 'fr', confidence: 0.99 });+ mocks.translate.mockResolvedValue('x');+ const c = new TranslationCoordinator(translator, store, gateway, OPTS, logger);+ // Author x@lid (Xavier); message pushName 'Sam' matches TWO other participants -> ambiguous.+ await c.handleMessage('s', msg({ author: 'x@lid', pushName: 'Sam', body: 'Bonjour tout le monde' }));+ expect(mocks.info).not.toHaveBeenCalledWith('sender reconciled by pushName', expect.anything());+ expect(mocks.debug).toHaveBeenCalledWith(+ 'ambiguous pushName; not reconciling',+ expect.objectContaining({ author: 'x@lid' }),+ );+ });});diff --git a/src/plugins/extensions/translation/core/translation.coordinator.ts b/src/plugins/extensions/translation/core/translation.coordinator.tsindex bf1a7e4d3..5965e5987 100644--- a/src/plugins/extensions/translation/core/translation.coordinator.ts+++ b/src/plugins/extensions/translation/core/translation.coordinator.ts@@ -74,7 +74,8 @@ export class TranslationCoordinator {return;}- const sender = this.ensureParticipant(state, msg.author);+ const senderKey = this.resolveSenderKey(state, msg);+ const sender = this.ensureParticipant(state, senderKey);// Record the pushName, but never overwrite a different existing value (a misrouted message// could otherwise poison the identity anchor).if (msg.pushName && (sender.pushName === undefined || sender.pushName === msg.pushName)) {@@ -156,6 +157,40 @@ export class TranslationCoordinator {p.updatedAt = new Date().toISOString();}+ /**+ * Resolve which participant a message belongs to. whatsapp-web.js can misroute a group message's+ * `@lid` author after a reconnect; when the message's pushName uniquely identifies a DIFFERENT+ * known participant (and the author doesn't already own that pushName), trust the pushName.+ * Ambiguous (shared pushName) or no-match cases fall back to the raw author.+ */+ private resolveSenderKey(state: GroupState, msg: InboundMessage): string {+ const { author, pushName } = msg;+ if (!pushName) return author;+ // No conflict if the author already owns this pushName.+ if (state.participants[author]?.pushName === pushName) return author;+ const matches = Object.keys(state.participants).filter(+ key => key !== author && state.participants[key].pushName === pushName,+ );+ if (matches.length === 1) {+ this.logger.info('sender reconciled by pushName', {+ action: 'translation_sender_reconciled',+ author,+ resolvedKey: matches[0],+ pushName,+ });+ return matches[0];+ }+ if (matches.length > 1) {+ this.logger.debug('ambiguous pushName; not reconciling', {+ action: 'translation_pushname_ambiguous',+ author,+ pushName,+ matches,+ });+ }+ return author;+ }+private ensureParticipant(state: GroupState, wid: string): ParticipantState {if (!state.participants[wid]) {state.participants[wid] = { lang: null, source: 'learned', enabled: true, samples: 0, updatedAt: '' };