Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 73 additions & 2 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3572,6 +3572,18 @@ export class BaileysStartupService extends ChannelStartupService {
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,
});
} else {
await this.prismaRepository.message.deleteMany({
where: {
Expand Down Expand Up @@ -3898,13 +3910,72 @@ export class BaileysStartupService extends ChannelStartupService {
}

try {
return await this.client.sendMessage(jid, {
const response = await this.client.sendMessage(jid, {
...(options as any),
edit: data.key,
});
if (response) {
const messageId = response.message?.protocolMessage?.key?.id;
if (messageId) {
let message = await this.prismaRepository.message.findFirst({
where: {
key: {
path: ['id'],
equals: messageId,
},
},
});
if (!message) throw new NotFoundException('Message not found');

if (!(message.key.valueOf() as any).fromMe) {
new BadRequestException('You cannot edit others messages');
}
if ((message.key.valueOf() as any)?.deleted) {
new BadRequestException('You cannot edit deleted messages');
}

const updateMessage = this.prepareMessage({ ...response });
message = await this.prismaRepository.message.update({
where: { id: message.id },
data: {
message: {
...updateMessage?.message?.[updateMessage.messageType]?.editedMessage,
},
status: 'EDITED',
},
});
const messageUpdate: any = {
messageId: message.id,
keyId: messageId,
remoteJid: response.key.remoteJid,
fromMe: response.key.fromMe,
participant: response.key?.remoteJid,
status: 'EDITED',
instanceId: this.instanceId,
};
await this.prismaRepository.messageUpdate.create({
data: messageUpdate,
});

this.sendDataWebhook(Events.MESSAGES_EDITED, {
id: message.id,
instanceId: message.instanceId,
key: message.key,
messageType: message.messageType,
status: 'EDITED',
source: message.source,
messageTimestamp: message.messageTimestamp,
pushName: message.pushName,
participant: message.participant,
message: message.message,
});
}
}

return response;
} catch (error) {
this.logger.error(error);
throw new BadRequestException(error.toString());
throw error;
}
}

Expand Down