-
Notifications
You must be signed in to change notification settings - Fork 7.1k
feat: implement WAMonitoringService for instance lifecycle management and add WhatsApp Baileys integration support #2671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1201,6 +1201,20 @@ export class BaileysStartupService extends ChannelStartupService { | |
| } | ||
| } | ||
|
|
||
| // FIX: Fallback pushName if not present in received payload | ||
| if (!received.pushName && !received.key.fromMe) { | ||
| const participantJid = received.participant || received.key.participant || received.key.remoteJid; | ||
| if (participantJid) { | ||
| const contact = await this.prismaRepository.contact.findFirst({ | ||
| where: { instanceId: this.instanceId, remoteJid: participantJid }, | ||
| select: { pushName: true } | ||
| }); | ||
| if (contact && contact.pushName) { | ||
| received.pushName = contact.pushName; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const messageRaw = this.prepareMessage(received); | ||
|
|
||
| if (messageRaw.messageType === 'pollUpdateMessage') { | ||
|
|
@@ -1355,7 +1369,20 @@ export class BaileysStartupService extends ChannelStartupService { | |
| if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const { pollUpdates, ...messageData } = messageRaw; | ||
| const msg = await this.prismaRepository.message.create({ data: messageData }); | ||
|
|
||
| const existingMessage = await this.prismaRepository.$queryRaw` | ||
| SELECT id, status, "messageTimestamp" FROM "Message" | ||
| WHERE "instanceId" = ${this.instanceId} | ||
| AND "key"->>'id' = ${received.key.id} | ||
| ` as any[]; | ||
|
|
||
| let msg; | ||
| if (existingMessage && existingMessage.length > 0) { | ||
|
Comment on lines
+1373
to
+1380
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Use a uniqueness constraint or upsert instead of a raw pre-check query to prevent duplicates. Because the |
||
| msg = existingMessage[0]; | ||
| this.logger.info(`Message already exists, ignoring create: ${received.key.id}`); | ||
| } else { | ||
| msg = await this.prismaRepository.message.create({ data: messageData }); | ||
| } | ||
|
|
||
| const { remoteJid } = received.key; | ||
| const timestamp = msg.messageTimestamp; | ||
|
|
@@ -1495,16 +1522,19 @@ export class BaileysStartupService extends ChannelStartupService { | |
|
|
||
| const contactRaw: { | ||
| remoteJid: string; | ||
| pushName: string; | ||
| pushName?: string; | ||
| profilePicUrl?: string; | ||
| instanceId: string; | ||
| } = { | ||
| remoteJid: received.key.remoteJid, | ||
| pushName: received.key.fromMe ? '' : received.key.fromMe == null ? '' : received.pushName, | ||
| profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl, | ||
| instanceId: this.instanceId, | ||
| }; | ||
|
|
||
| if (!received.key.fromMe && received.pushName) { | ||
| contactRaw.pushName = received.pushName; | ||
| } | ||
|
|
||
| if (contactRaw.remoteJid === 'status@broadcast') { | ||
| continue; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance): Avoid per-message lookup for pushName where possible to reduce database load.
This path triggers a
contact.findFirstfor every message withoutpushName, which can become an N+1 query pattern under high throughput. To mitigate this, you could restrict the fallback (e.g. only for group messages whereparticipantis set) and/or add a short-lived in-memory cache keyed byparticipantJidandinstanceIdso repeated messages from the same JID don’t repeatedly hit the DB. Alternatively, consider batching contact lookups earlier in the pipeline if you can determine the relevant JIDs in advance.Suggested implementation:
private readonly pushNameCache = new Map<string, string>();Place this alongside other private fields on the service.