Skip to content

Commit 16a8226

Browse files
Merge pull request #317 from jaison-x/pr
fix: when deleting a message in whatsapp, delete the message in chatwoot too
2 parents 71e908a + 9ecaf31 commit 16a8226

File tree

4 files changed

+94
-26
lines changed

4 files changed

+94
-26
lines changed

src/whatsapp/models/message.model.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ class Key {
1010
participant?: string;
1111
}
1212

13+
class ChatwootMessage {
14+
messageId?: number;
15+
inboxId?: number;
16+
conversationId?: number;
17+
}
18+
1319
export class MessageRaw {
1420
_id?: string;
1521
key?: Key;
@@ -22,7 +28,7 @@ export class MessageRaw {
2228
source?: 'android' | 'web' | 'ios';
2329
source_id?: string;
2430
source_reply_id?: string;
25-
chatwootMessageId?: string;
31+
chatwoot?: ChatwootMessage;
2632
}
2733

2834
const messageSchema = new Schema<MessageRaw>({
@@ -40,10 +46,14 @@ const messageSchema = new Schema<MessageRaw>({
4046
source: { type: String, minlength: 3, enum: ['android', 'web', 'ios'] },
4147
messageTimestamp: { type: Number, required: true },
4248
owner: { type: String, required: true, minlength: 1 },
43-
chatwootMessageId: { type: String, required: false },
49+
chatwoot: {
50+
messageId: { type: Number },
51+
inboxId: { type: Number },
52+
conversationId: { type: Number },
53+
},
4454
});
4555

46-
messageSchema.index({ chatwootMessageId: 1, owner: 1 });
56+
messageSchema.index({ 'chatwoot.messageId': 1, owner: 1 });
4757
messageSchema.index({ 'key.id': 1 });
4858
messageSchema.index({ 'key.id': 1, owner: 1 });
4959
messageSchema.index({ owner: 1 });

src/whatsapp/repository/message.repository.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ export class MessageRepository extends Repository {
9191
this.logger.verbose('finding messages');
9292
if (this.dbSettings.ENABLED) {
9393
this.logger.verbose('finding messages in db');
94-
if (query?.where?.key) {
95-
for (const [k, v] of Object.entries(query.where.key)) {
96-
query.where['key.' + k] = v;
94+
for (const [o, p] of Object.entries(query?.where)) {
95+
if (typeof p === 'object' && p !== null && !Array.isArray(p)) {
96+
for (const [k, v] of Object.entries(p)) {
97+
query.where[`${o}.${k}`] = v;
98+
}
99+
delete query.where[o];
97100
}
98-
delete query?.where?.key;
99101
}
100102

101103
return await this.messageModel

src/whatsapp/services/chatwoot.service.ts

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { InstanceDto } from '../dto/instance.dto';
1414
import { Options, Quoted, SendAudioDto, SendMediaDto, SendTextDto } from '../dto/sendMessage.dto';
1515
import { MessageRaw } from '../models';
1616
import { RepositoryBroker } from '../repository/repository.manager';
17+
import { Events } from '../types/wa.types';
1718
import { WAMonitoringService } from './monitor.service';
1819

1920
export class ChatwootService {
@@ -1036,7 +1037,9 @@ export class ChatwootService {
10361037
const message = await this.repository.message.find({
10371038
where: {
10381039
owner: instance.instanceName,
1039-
chatwootMessageId: body.id,
1040+
chatwoot: {
1041+
messageId: body.id,
1042+
},
10401043
},
10411044
limit: 1,
10421045
});
@@ -1160,7 +1163,11 @@ export class ChatwootService {
11601163
...messageSent,
11611164
owner: instance.instanceName,
11621165
},
1163-
body.id,
1166+
{
1167+
messageId: body.id,
1168+
inboxId: body.inbox?.id,
1169+
conversationId: body.conversation?.id,
1170+
},
11641171
instance,
11651172
);
11661173
}
@@ -1187,7 +1194,11 @@ export class ChatwootService {
11871194
...messageSent,
11881195
owner: instance.instanceName,
11891196
},
1190-
body.id,
1197+
{
1198+
messageId: body.id,
1199+
inboxId: body.inbox?.id,
1200+
conversationId: body.conversation?.id,
1201+
},
11911202
instance,
11921203
);
11931204
}
@@ -1221,15 +1232,33 @@ export class ChatwootService {
12211232
}
12221233
}
12231234

1224-
private updateChatwootMessageId(message: MessageRaw, chatwootMessageId: string, instance: InstanceDto) {
1225-
if (!chatwootMessageId || !message?.key?.id) {
1235+
private updateChatwootMessageId(
1236+
message: MessageRaw,
1237+
chatwootMessageIds: MessageRaw['chatwoot'],
1238+
instance: InstanceDto,
1239+
) {
1240+
if (!chatwootMessageIds.messageId || !message?.key?.id) {
12261241
return;
12271242
}
12281243

1229-
message.chatwootMessageId = chatwootMessageId;
1244+
message.chatwoot = chatwootMessageIds;
12301245
this.repository.message.update([message], instance.instanceName, true);
12311246
}
12321247

1248+
private async getMessageByKeyId(instance: InstanceDto, keyId: string): Promise<MessageRaw> {
1249+
const messages = await this.repository.message.find({
1250+
where: {
1251+
key: {
1252+
id: keyId,
1253+
},
1254+
owner: instance.instanceName,
1255+
},
1256+
limit: 1,
1257+
});
1258+
1259+
return messages.length ? messages[0] : null;
1260+
}
1261+
12331262
private async getReplyToIds(
12341263
msg: any,
12351264
instance: InstanceDto,
@@ -1240,17 +1269,9 @@ export class ChatwootService {
12401269
if (msg) {
12411270
inReplyToExternalId = msg.message?.extendedTextMessage?.contextInfo?.stanzaId;
12421271
if (inReplyToExternalId) {
1243-
const message = await this.repository.message.find({
1244-
where: {
1245-
key: {
1246-
id: inReplyToExternalId,
1247-
},
1248-
owner: instance.instanceName,
1249-
},
1250-
limit: 1,
1251-
});
1252-
if (message.length && message[0]?.chatwootMessageId) {
1253-
inReplyTo = message[0].chatwootMessageId;
1272+
const message = await this.getMessageByKeyId(instance, inReplyToExternalId);
1273+
if (message?.chatwoot?.messageId) {
1274+
inReplyTo = message.chatwoot.messageId;
12541275
}
12551276
}
12561277
}
@@ -1265,7 +1286,9 @@ export class ChatwootService {
12651286
if (msg?.content_attributes?.in_reply_to) {
12661287
const message = await this.repository.message.find({
12671288
where: {
1268-
chatwootMessageId: msg?.content_attributes?.in_reply_to,
1289+
chatwoot: {
1290+
messageId: msg?.content_attributes?.in_reply_to,
1291+
},
12691292
owner: instance.instanceName,
12701293
},
12711294
limit: 1,
@@ -1757,6 +1780,25 @@ export class ChatwootService {
17571780
}
17581781
}
17591782

1783+
if (event === Events.MESSAGES_DELETE) {
1784+
this.logger.verbose('deleting message from instance: ' + instance.instanceName);
1785+
1786+
if (!body?.key?.id) {
1787+
this.logger.warn('message id not found');
1788+
return;
1789+
}
1790+
1791+
const message = await this.getMessageByKeyId(instance, body.key.id);
1792+
if (message?.chatwoot?.messageId && message?.chatwoot?.conversationId) {
1793+
this.logger.verbose('deleting message in chatwoot. Message id: ' + body.key.id);
1794+
return await client.messages.delete({
1795+
accountId: this.provider.account_id,
1796+
conversationId: message.chatwoot.conversationId,
1797+
messageId: message.chatwoot.messageId,
1798+
});
1799+
}
1800+
}
1801+
17601802
if (event === 'status.instance') {
17611803
this.logger.verbose('event status.instance');
17621804
const data = body;

src/whatsapp/services/whatsapp.service.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export class WAStartupService {
265265
pairingCode: this.instance.qrcode?.pairingCode,
266266
code: this.instance.qrcode?.code,
267267
base64: this.instance.qrcode?.base64,
268+
count: this.instance.qrcode?.count,
268269
};
269270
}
270271

@@ -1798,7 +1799,11 @@ export class WAStartupService {
17981799
);
17991800

18001801
if (chatwootSentMessage?.id) {
1801-
messageRaw.chatwootMessageId = chatwootSentMessage.id;
1802+
messageRaw.chatwoot = {
1803+
messageId: chatwootSentMessage.id,
1804+
inboxId: chatwootSentMessage.inbox_id,
1805+
conversationId: chatwootSentMessage.conversation_id,
1806+
};
18021807
}
18031808
}
18041809

@@ -1941,6 +1946,15 @@ export class WAStartupService {
19411946
this.instance.name,
19421947
database.SAVE_DATA.MESSAGE_UPDATE,
19431948
);
1949+
1950+
if (this.localChatwoot.enabled) {
1951+
this.chatwootService.eventWhatsapp(
1952+
Events.MESSAGES_DELETE,
1953+
{ instanceName: this.instance.name },
1954+
{ key: key },
1955+
);
1956+
}
1957+
19441958
return;
19451959
}
19461960

0 commit comments

Comments
 (0)