-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Permitir correta utilização da evolution quando mensagens não são persistidas no DB #1798
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
Merged
DavidsonGomes
merged 8 commits into
EvolutionAPI:develop
from
apresentame:fix/webhook_event
Aug 4, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6101c8d
fix: corrigido disparo de eventos quando nao usa a opcao da ENV de sa…
onerrogus 69726f0
fix(evo): melhorado controle de recebimento do ack das msgs
onerrogus a62f9eb
chore: voltar porta do dockerfile
onerrogus 9cdb897
Merge branch 'release/1.3.1'
DavidsonGomes bc11d0f
fix: corrigido delete de mensagem quando nao salvo no banco de dados
onerrogus fe8280a
Merge branch 'main' of github.com:apresentame/evolution-api into fix/…
onerrogus 20dd1b1
Merge branch 'develop' of github.com:apresentame/evolution-api into f…
onerrogus 79f4a22
refactor: lint check
onerrogus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1446,16 +1446,7 @@ export class BaileysStartupService extends ChannelStartupService { | |
| } | ||
| } | ||
|
|
||
| const findMessage = await this.prismaRepository.message.findFirst({ | ||
| where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } }, | ||
| }); | ||
|
|
||
| if (!findMessage) { | ||
| continue; | ||
| } | ||
|
|
||
| const message: any = { | ||
| messageId: findMessage.id, | ||
| keyId: key.id, | ||
| remoteJid: key?.remoteJid, | ||
| fromMe: key.fromMe, | ||
|
|
@@ -1465,6 +1456,16 @@ export class BaileysStartupService extends ChannelStartupService { | |
| instanceId: this.instanceId, | ||
| }; | ||
|
|
||
| let findMessage: any; | ||
| const configDatabaseData = this.configService.get<Database>('DATABASE').SAVE_DATA; | ||
| if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) { | ||
| findMessage = await this.prismaRepository.message.findFirst({ | ||
| where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } }, | ||
| }); | ||
|
|
||
| if (findMessage) message.messageId = findMessage.id; | ||
| } | ||
|
|
||
| if (update.message === null && update.status === undefined) { | ||
| this.sendDataWebhook(Events.MESSAGES_DELETE, key); | ||
|
|
||
|
|
@@ -1480,7 +1481,9 @@ export class BaileysStartupService extends ChannelStartupService { | |
| } | ||
|
|
||
| continue; | ||
| } else if (update.status !== undefined && status[update.status] !== findMessage.status) { | ||
| } | ||
|
|
||
| if (findMessage && update.status !== undefined && status[update.status] !== findMessage.status) { | ||
| if (!key.fromMe && key.remoteJid) { | ||
| readChatToUpdate[key.remoteJid] = true; | ||
|
|
||
|
|
@@ -3438,17 +3441,20 @@ export class BaileysStartupService extends ChannelStartupService { | |
| where: { id: message.id }, | ||
| data: { key: { ...existingKey, deleted: true }, status: 'DELETED' }, | ||
| }); | ||
| const messageUpdate: any = { | ||
| messageId: message.id, | ||
| keyId: messageId, | ||
| remoteJid: response.key.remoteJid, | ||
| fromMe: response.key.fromMe, | ||
| participant: response.key?.remoteJid, | ||
| status: 'DELETED', | ||
| instanceId: this.instanceId, | ||
| }; | ||
| await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); | ||
| if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) { | ||
| const messageUpdate: any = { | ||
| messageId: message.id, | ||
| keyId: messageId, | ||
| remoteJid: response.key.remoteJid, | ||
| fromMe: response.key.fromMe, | ||
| participant: response.key?.remoteJid, | ||
| status: 'DELETED', | ||
| instanceId: this.instanceId, | ||
| }; | ||
| await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); | ||
| } | ||
| } else { | ||
| if (!message) return response; | ||
| await this.prismaRepository.message.deleteMany({ where: { id: message.id } }); | ||
| } | ||
| this.sendDataWebhook(Events.MESSAGES_DELETE, { | ||
|
|
@@ -3780,6 +3786,10 @@ export class BaileysStartupService extends ChannelStartupService { | |
|
|
||
| private async formatUpdateMessage(data: UpdateMessageDto) { | ||
| try { | ||
| if (!this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) { | ||
| return data; | ||
| } | ||
|
|
||
| const msg: any = await this.getMessage(data.key, true); | ||
|
|
||
| if (msg?.messageType === 'conversation' || msg?.messageType === 'extendedTextMessage') { | ||
|
|
@@ -3813,13 +3823,15 @@ export class BaileysStartupService extends ChannelStartupService { | |
|
|
||
| try { | ||
| const oldMessage: any = await this.getMessage(data.key, true); | ||
| if (!oldMessage) throw new NotFoundException('Message not found'); | ||
| if (oldMessage?.key?.remoteJid !== jid) { | ||
| throw new BadRequestException('RemoteJid does not match'); | ||
| } | ||
| if (oldMessage?.messageTimestamp > Date.now() + 900000) { | ||
| // 15 minutes in milliseconds | ||
| throw new BadRequestException('Message is older than 15 minutes'); | ||
| if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) { | ||
| if (!oldMessage) throw new NotFoundException('Message not found'); | ||
| if (oldMessage?.key?.remoteJid !== jid) { | ||
| throw new BadRequestException('RemoteJid does not match'); | ||
| } | ||
| if (oldMessage?.messageTimestamp > Date.now() + 900000) { | ||
| // 15 minutes in milliseconds | ||
| throw new BadRequestException('Message is older than 15 minutes'); | ||
| } | ||
| } | ||
|
|
||
| const messageSent = await this.client.sendMessage(jid, { ...(options as any), edit: data.key }); | ||
|
|
@@ -3837,7 +3849,7 @@ export class BaileysStartupService extends ChannelStartupService { | |
| ); | ||
|
|
||
| const messageId = messageSent.message?.protocolMessage?.key?.id; | ||
| if (messageId) { | ||
| if (messageId && this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) { | ||
|
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. question (bug_risk): Message update logic is now gated by NEW_MESSAGE flag. Consider whether message updates should ever occur when NEW_MESSAGE is false, as this change will prevent all such updates. |
||
| let message = await this.prismaRepository.message.findFirst({ | ||
| where: { key: { path: ['id'], equals: messageId } }, | ||
| }); | ||
|
|
@@ -3849,6 +3861,7 @@ export class BaileysStartupService extends ChannelStartupService { | |
| if ((message.key.valueOf() as any)?.deleted) { | ||
| new BadRequestException('You cannot edit deleted messages'); | ||
| } | ||
|
|
||
| if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') { | ||
| oldMessage.message.conversation = data.text; | ||
| } else { | ||
|
|
@@ -3862,16 +3875,19 @@ export class BaileysStartupService extends ChannelStartupService { | |
| messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds | ||
| }, | ||
| }); | ||
| const messageUpdate: any = { | ||
| messageId: message.id, | ||
| keyId: messageId, | ||
| remoteJid: messageSent.key.remoteJid, | ||
| fromMe: messageSent.key.fromMe, | ||
| participant: messageSent.key?.remoteJid, | ||
| status: 'EDITED', | ||
| instanceId: this.instanceId, | ||
| }; | ||
| await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); | ||
|
|
||
| if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) { | ||
| const messageUpdate: any = { | ||
| messageId: message.id, | ||
| keyId: messageId, | ||
| remoteJid: messageSent.key.remoteJid, | ||
| fromMe: messageSent.key.fromMe, | ||
| participant: messageSent.key?.remoteJid, | ||
| status: 'EDITED', | ||
| instanceId: this.instanceId, | ||
| }; | ||
| await this.prismaRepository.messageUpdate.create({ data: messageUpdate }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
question (bug_risk): Early return skips formatting if NEW_MESSAGE is false.
Ensure that all consumers of this method are prepared to handle unformatted data when NEW_MESSAGE is false, as this may affect downstream processing.