Skip to content

Commit 023e030

Browse files
Merge pull request #1195 from GrimBit1/main
Refactor edit and delete message functionality in BaileyStartupService
2 parents e51b6e9 + 91e7a32 commit 023e030

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

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

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,6 +3596,18 @@ export class BaileysStartupService extends ChannelStartupService {
35963596
status: 'DELETED',
35973597
},
35983598
});
3599+
const messageUpdate: any = {
3600+
messageId: message.id,
3601+
keyId: messageId,
3602+
remoteJid: response.key.remoteJid,
3603+
fromMe: response.key.fromMe,
3604+
participant: response.key?.remoteJid,
3605+
status: 'DELETED',
3606+
instanceId: this.instanceId,
3607+
};
3608+
await this.prismaRepository.messageUpdate.create({
3609+
data: messageUpdate,
3610+
});
35993611
} else {
36003612
await this.prismaRepository.message.deleteMany({
36013613
where: {
@@ -3922,13 +3934,72 @@ export class BaileysStartupService extends ChannelStartupService {
39223934
}
39233935

39243936
try {
3925-
return await this.client.sendMessage(jid, {
3937+
const response = await this.client.sendMessage(jid, {
39263938
...(options as any),
39273939
edit: data.key,
39283940
});
3941+
if (response) {
3942+
const messageId = response.message?.protocolMessage?.key?.id;
3943+
if (messageId) {
3944+
let message = await this.prismaRepository.message.findFirst({
3945+
where: {
3946+
key: {
3947+
path: ['id'],
3948+
equals: messageId,
3949+
},
3950+
},
3951+
});
3952+
if (!message) throw new NotFoundException('Message not found');
3953+
3954+
if (!(message.key.valueOf() as any).fromMe) {
3955+
new BadRequestException('You cannot edit others messages');
3956+
}
3957+
if ((message.key.valueOf() as any)?.deleted) {
3958+
new BadRequestException('You cannot edit deleted messages');
3959+
}
3960+
3961+
const updateMessage = this.prepareMessage({ ...response });
3962+
message = await this.prismaRepository.message.update({
3963+
where: { id: message.id },
3964+
data: {
3965+
message: {
3966+
...updateMessage?.message?.[updateMessage.messageType]?.editedMessage,
3967+
},
3968+
status: 'EDITED',
3969+
},
3970+
});
3971+
const messageUpdate: any = {
3972+
messageId: message.id,
3973+
keyId: messageId,
3974+
remoteJid: response.key.remoteJid,
3975+
fromMe: response.key.fromMe,
3976+
participant: response.key?.remoteJid,
3977+
status: 'EDITED',
3978+
instanceId: this.instanceId,
3979+
};
3980+
await this.prismaRepository.messageUpdate.create({
3981+
data: messageUpdate,
3982+
});
3983+
3984+
this.sendDataWebhook(Events.MESSAGES_EDITED, {
3985+
id: message.id,
3986+
instanceId: message.instanceId,
3987+
key: message.key,
3988+
messageType: message.messageType,
3989+
status: 'EDITED',
3990+
source: message.source,
3991+
messageTimestamp: message.messageTimestamp,
3992+
pushName: message.pushName,
3993+
participant: message.participant,
3994+
message: message.message,
3995+
});
3996+
}
3997+
}
3998+
3999+
return response;
39294000
} catch (error) {
39304001
this.logger.error(error);
3931-
throw new BadRequestException(error.toString());
4002+
throw error;
39324003
}
39334004
}
39344005

0 commit comments

Comments
 (0)