Skip to content

Commit a5ed73f

Browse files
augustolima1claude
andcommitted
fix(baileys): Improve getMessage pagination to handle message lookup correctly
- Replace arbitrary limit of 100 messages with proper pagination - Search through messages in batches (100 at a time, up to 10,000 total) - Order by creation time descending for most recent messages first - Stop searching once message is found instead of searching all - Return immediately when matching key.id is found - Prevents potential loss of messages in busy instances Resolves Sourcery AI feedback on non-deterministic message lookup. 🤖 Generated with Claude Code Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent 4c8a13c commit a5ed73f

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -522,47 +522,62 @@ export class BaileysStartupService extends ChannelStartupService {
522522

523523
private async getMessage(key: proto.IMessageKey, full = false) {
524524
try {
525-
// Get all messages for this instance and filter by key.id in application
526-
const messages = await this.prismaRepository.message.findMany({
527-
where: {
528-
instanceId: this.instanceId,
529-
},
530-
take: 100, // Limit to avoid performance issues
531-
});
525+
// Fetch messages in batches to find the one with matching key.id
526+
// Using pagination instead of arbitrary limit to ensure we find the message
527+
const pageSize = 100;
528+
let pageNumber = 0;
529+
const maxPages = 100; // Maximum 10,000 messages to search
530+
531+
while (pageNumber < maxPages) {
532+
const messages = await this.prismaRepository.message.findMany({
533+
where: {
534+
instanceId: this.instanceId,
535+
},
536+
skip: pageNumber * pageSize,
537+
take: pageSize,
538+
orderBy: { createdAt: 'desc' }, // Most recent first
539+
});
532540

533-
// Filter by key.id (handle both string and object keys)
534-
const webMessageInfo = messages.filter((m) => {
535-
try {
536-
const msgKey = typeof m.key === 'string' ? JSON.parse(m.key) : m.key;
537-
return msgKey?.id === key.id;
538-
} catch {
539-
return false;
541+
if (messages.length === 0) {
542+
break; // No more messages
540543
}
541-
});
542544

543-
if (!webMessageInfo[0]) {
544-
return { conversation: '' };
545-
}
545+
// Filter by key.id (handle both string and object keys)
546+
const webMessageInfo = messages.find((m) => {
547+
try {
548+
const msgKey = typeof m.key === 'string' ? JSON.parse(m.key) : m.key;
549+
return msgKey?.id === key.id;
550+
} catch {
551+
return false;
552+
}
553+
});
546554

547-
if (full) {
548-
return webMessageInfo[0];
549-
}
555+
if (webMessageInfo) {
556+
if (full) {
557+
return webMessageInfo;
558+
}
550559

551-
const msg = webMessageInfo[0].message;
552-
if (msg && typeof msg === 'object' && 'pollCreationMessage' in msg) {
553-
const messageSecretBase64 = (msg as any).messageContextInfo?.messageSecret;
560+
const msg = webMessageInfo.message;
561+
if (msg && typeof msg === 'object' && 'pollCreationMessage' in msg) {
562+
const messageSecretBase64 = (msg as any).messageContextInfo?.messageSecret;
554563

555-
if (typeof messageSecretBase64 === 'string') {
556-
const messageSecret = Buffer.from(messageSecretBase64, 'base64');
564+
if (typeof messageSecretBase64 === 'string') {
565+
const messageSecret = Buffer.from(messageSecretBase64, 'base64');
557566

558-
return {
559-
messageContextInfo: { messageSecret },
560-
pollCreationMessage: (msg as any).pollCreationMessage,
561-
};
567+
return {
568+
messageContextInfo: { messageSecret },
569+
pollCreationMessage: (msg as any).pollCreationMessage,
570+
};
571+
}
572+
}
573+
574+
return msg;
562575
}
576+
577+
pageNumber++;
563578
}
564579

565-
return msg;
580+
return { conversation: '' };
566581
} catch {
567582
return { conversation: '' };
568583
}

0 commit comments

Comments
 (0)