Skip to content

Commit 532bc1d

Browse files
committed
fix(meta): address review feedback on remoteId and status fromMe
1 parent e4cd85a commit 532bc1d

1 file changed

Lines changed: 50 additions & 10 deletions

File tree

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,13 @@ export class BusinessStartupService extends ChannelStartupService {
127127
if (!data) return;
128128

129129
const content = data.entry[0].changes[0].value;
130-
const firstMessage =
131-
content?.messages?.[0] ?? content?.message_echoes?.[0] ?? content?.smb_message_echoes?.[0] ?? undefined;
132-
const recipient = content?.statuses?.[0]?.recipient_id;
133-
const remoteId = firstMessage?.to ?? firstMessage?.from ?? recipient;
130+
const normalizedContent = this.normalizeWebhookContent(content);
131+
const remoteId = this.resolveRemoteId(normalizedContent);
134132

135133
try {
136134
this.loadChatwoot();
137135

138-
this.eventHandler(this.normalizeWebhookContent(content));
136+
this.eventHandler(normalizedContent);
139137

140138
if (remoteId) {
141139
this.phoneNumber = createJid(remoteId);
@@ -150,7 +148,9 @@ export class BusinessStartupService extends ChannelStartupService {
150148
if (!content || typeof content !== 'object') return content;
151149

152150
const normalized = { ...content };
153-
const echoes = normalized?.message_echoes ?? normalized?.smb_message_echoes;
151+
const messageEchoes = Array.isArray(normalized?.message_echoes) ? normalized.message_echoes : undefined;
152+
const smbMessageEchoes = Array.isArray(normalized?.smb_message_echoes) ? normalized.smb_message_echoes : undefined;
153+
const echoes = messageEchoes?.length ? messageEchoes : smbMessageEchoes?.length ? smbMessageEchoes : undefined;
154154

155155
if (!Array.isArray(normalized.messages) && Array.isArray(echoes) && echoes.length > 0) {
156156
normalized.messages = echoes;
@@ -163,6 +163,26 @@ export class BusinessStartupService extends ChannelStartupService {
163163
return typeof value === 'string' ? value.replace(/\D/g, '') : '';
164164
}
165165

166+
private resolveRemoteId(content: any) {
167+
const firstMessage = content?.messages?.[0];
168+
const recipient = content?.statuses?.[0]?.recipient_id;
169+
170+
const candidates = [firstMessage?.from, firstMessage?.to, recipient].filter(Boolean) as string[];
171+
if (candidates.length === 0) return undefined;
172+
173+
const businessNumbers = [
174+
this.normalizePhoneNumber(content?.metadata?.display_phone_number),
175+
this.normalizePhoneNumber(content?.metadata?.phone_number_id),
176+
].filter(Boolean);
177+
178+
const externalCounterpart = candidates.find((candidate) => {
179+
const normalizedCandidate = this.normalizePhoneNumber(candidate);
180+
return normalizedCandidate && !businessNumbers.includes(normalizedCandidate);
181+
});
182+
183+
return externalCounterpart ?? candidates[0];
184+
}
185+
166186
private isCloudApiEchoPayload(received: any) {
167187
return Array.isArray(received?.message_echoes) || Array.isArray(received?.smb_message_echoes);
168188
}
@@ -179,6 +199,16 @@ export class BusinessStartupService extends ChannelStartupService {
179199
return from === displayPhone || from === phoneNumberId;
180200
}
181201

202+
private isCloudApiStatusFromMe(item: any, received: any) {
203+
const recipient = this.normalizePhoneNumber(item?.recipient_id);
204+
if (!recipient) return true;
205+
206+
const displayPhone = this.normalizePhoneNumber(received?.metadata?.display_phone_number);
207+
const phoneNumberId = this.normalizePhoneNumber(received?.metadata?.phone_number_id);
208+
209+
return recipient !== displayPhone && recipient !== phoneNumberId;
210+
}
211+
182212
private async downloadMediaMessage(message: any) {
183213
try {
184214
const id = message[message.type].id;
@@ -794,11 +824,13 @@ export class BusinessStartupService extends ChannelStartupService {
794824
}
795825
if (received.statuses) {
796826
for await (const item of received.statuses) {
797-
const remoteJid = createJid(item?.recipient_id ?? this.phoneNumber);
798-
const key = {
827+
const remoteId = item?.recipient_id ?? this.phoneNumber;
828+
if (!remoteId) continue;
829+
830+
const key: any = {
799831
id: item.id,
800-
remoteJid,
801-
fromMe: item?.recipient_id ? item.recipient_id !== received.metadata.phone_number_id : true,
832+
remoteJid: createJid(remoteId),
833+
fromMe: this.isCloudApiStatusFromMe(item, received),
802834
};
803835
if (settings?.groups_ignore && key.remoteJid.includes('@g.us')) {
804836
return;
@@ -818,6 +850,14 @@ export class BusinessStartupService extends ChannelStartupService {
818850
return;
819851
}
820852

853+
const findMessageKey: any = findMessage?.key ?? {};
854+
if (findMessageKey?.remoteJid) {
855+
key.remoteJid = findMessageKey.remoteJid;
856+
}
857+
if (typeof findMessageKey?.fromMe === 'boolean') {
858+
key.fromMe = findMessageKey.fromMe;
859+
}
860+
821861
if (item.message === null && item.status === undefined) {
822862
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
823863

0 commit comments

Comments
 (0)