Skip to content

Commit cc01787

Browse files
Merge pull request #1473 from KokeroO/develop
fix: Deadlock errors and undefined arguments
2 parents 7cccda1 + c53a96e commit cc01787

File tree

2 files changed

+86
-64
lines changed

2 files changed

+86
-64
lines changed

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

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,8 @@ export class BaileysStartupService extends ChannelStartupService {
11731173
const oldMessage = await this.getMessage(editedMessage.key, true);
11741174
if ((oldMessage as any)?.id) {
11751175
const editedMessageTimestamp = Long.isLong(editedMessage?.timestampMs)
1176-
? editedMessage.timestampMs?.toNumber()
1177-
: (editedMessage.timestampMs as number);
1176+
? Math.floor(editedMessage.timestampMs.toNumber() / 1000)
1177+
: Math.floor((editedMessage.timestampMs as number) / 1000);
11781178

11791179
await this.prismaRepository.message.update({
11801180
where: { id: (oldMessage as any).id },
@@ -1205,7 +1205,7 @@ export class BaileysStartupService extends ChannelStartupService {
12051205
continue;
12061206
}
12071207

1208-
await this.baileysCache.set(messageKey, true, 30 * 60);
1208+
await this.baileysCache.set(messageKey, true, 5 * 60);
12091209

12101210
if (
12111211
(type !== 'notify' && type !== 'append') ||
@@ -1311,21 +1311,31 @@ export class BaileysStartupService extends ChannelStartupService {
13111311
data: messageRaw,
13121312
});
13131313

1314-
if (received.key.fromMe === false) {
1315-
if (msg.status === status[3]) {
1316-
this.logger.log(`Update not read messages ${received.key.remoteJid}`);
1317-
1318-
await this.updateChatUnreadMessages(received.key.remoteJid);
1319-
} else if (msg.status === status[4]) {
1320-
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`);
1321-
1322-
await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
1314+
const {remoteJid} = received.key;
1315+
const timestamp = msg.messageTimestamp;
1316+
const fromMe = received.key.fromMe.toString();
1317+
const messageKey = `${remoteJid}_${timestamp}_${fromMe}`;
1318+
1319+
const cachedTimestamp = await this.baileysCache.get(messageKey);
1320+
1321+
if (!cachedTimestamp) {
1322+
if (!received.key.fromMe) {
1323+
if (msg.status === status[3]) {
1324+
this.logger.log(`Update not read messages ${remoteJid}`);
1325+
await this.updateChatUnreadMessages(remoteJid);
1326+
} else if (msg.status === status[4]) {
1327+
this.logger.log(`Update readed messages ${remoteJid} - ${timestamp}`);
1328+
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
1329+
}
1330+
} else {
1331+
// is send message by me
1332+
this.logger.log(`Update readed messages ${remoteJid} - ${timestamp}`);
1333+
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
13231334
}
1324-
} else {
1325-
// is send message by me
1326-
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`);
13271335

1328-
await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
1336+
await this.baileysCache.set(messageKey, true, 5 * 60);
1337+
} else {
1338+
this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`);
13291339
}
13301340

13311341
if (isMedia) {
@@ -1481,7 +1491,7 @@ export class BaileysStartupService extends ChannelStartupService {
14811491
const cached = await this.baileysCache.get(updateKey);
14821492

14831493
if (cached) {
1484-
this.logger.info(`Message duplicated ignored: ${key.id}`);
1494+
this.logger.info(`Message duplicated ignored [avoid deadlock]: ${updateKey}`);
14851495
continue;
14861496
}
14871497

@@ -1497,7 +1507,7 @@ export class BaileysStartupService extends ChannelStartupService {
14971507
}
14981508
}
14991509

1500-
if (key.remoteJid !== 'status@broadcast') {
1510+
if (key.remoteJid !== 'status@broadcast' && key.id !== undefined) {
15011511
let pollUpdates: any;
15021512

15031513
if (update.pollUpdates) {
@@ -1525,19 +1535,20 @@ export class BaileysStartupService extends ChannelStartupService {
15251535
continue;
15261536
}
15271537

1538+
const message: any = {
1539+
messageId: findMessage.id,
1540+
keyId: key.id,
1541+
remoteJid: key?.remoteJid,
1542+
fromMe: key.fromMe,
1543+
participant: key?.remoteJid,
1544+
status: status[update.status] ?? 'DELETED',
1545+
pollUpdates,
1546+
instanceId: this.instanceId,
1547+
};
1548+
15281549
if (update.message === null && update.status === undefined) {
15291550
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
15301551

1531-
const message: any = {
1532-
messageId: findMessage.id,
1533-
keyId: key.id,
1534-
remoteJid: key.remoteJid,
1535-
fromMe: key.fromMe,
1536-
participant: key?.remoteJid,
1537-
status: 'DELETED',
1538-
instanceId: this.instanceId,
1539-
};
1540-
15411552
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
15421553
await this.prismaRepository.messageUpdate.create({
15431554
data: message,
@@ -1556,29 +1567,32 @@ export class BaileysStartupService extends ChannelStartupService {
15561567
if (!key.fromMe && key.remoteJid) {
15571568
readChatToUpdate[key.remoteJid] = true;
15581569

1559-
if (status[update.status] === status[4]) {
1560-
this.logger.log(`Update as read ${key.remoteJid} - ${findMessage.messageTimestamp}`);
1561-
this.updateMessagesReadedByTimestamp(key.remoteJid, findMessage.messageTimestamp);
1570+
const {remoteJid} = key;
1571+
const timestamp = findMessage.messageTimestamp;
1572+
const fromMe = key.fromMe.toString();
1573+
const messageKey = `${remoteJid}_${timestamp}_${fromMe}`;
1574+
1575+
const cachedTimestamp = await this.baileysCache.get(messageKey);
1576+
1577+
if (!cachedTimestamp) {
1578+
if (status[update.status] === status[4]) {
1579+
this.logger.log(`Update as read in message.update ${remoteJid} - ${timestamp}`);
1580+
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
1581+
await this.baileysCache.set(messageKey, true, 5 * 60);
1582+
}
1583+
1584+
await this.prismaRepository.message.update({
1585+
where: { id: findMessage.id },
1586+
data: { status: status[update.status] },
1587+
});
1588+
} else {
1589+
this.logger.info(
1590+
`Update readed messages duplicated ignored in message.update [avoid deadlock]: ${messageKey}`,
1591+
);
15621592
}
15631593
}
1564-
1565-
await this.prismaRepository.message.update({
1566-
where: { id: findMessage.id },
1567-
data: { status: status[update.status] },
1568-
});
15691594
}
15701595

1571-
const message: any = {
1572-
messageId: findMessage.id,
1573-
keyId: key.id,
1574-
remoteJid: key.remoteJid,
1575-
fromMe: key.fromMe,
1576-
participant: key?.remoteJid,
1577-
status: status[update.status],
1578-
pollUpdates,
1579-
instanceId: this.instanceId,
1580-
};
1581-
15821596
this.sendDataWebhook(Events.MESSAGES_UPDATE, message);
15831597

15841598
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
@@ -3735,6 +3749,10 @@ export class BaileysStartupService extends ChannelStartupService {
37353749
}
37363750
}
37373751

3752+
if ('messageContextInfo' in msg.message && Object.keys(msg.message).length === 1) {
3753+
throw 'The message is messageContextInfo';
3754+
}
3755+
37383756
let mediaMessage: any;
37393757
let mediaType: string;
37403758

@@ -3745,7 +3763,7 @@ export class BaileysStartupService extends ChannelStartupService {
37453763
break;
37463764
}
37473765
}
3748-
3766+
37493767
if (!mediaMessage) {
37503768
throw 'The message is not of the media type';
37513769
}
@@ -3810,7 +3828,7 @@ export class BaileysStartupService extends ChannelStartupService {
38103828
buffer: getBuffer ? buffer : null,
38113829
};
38123830
} catch (error) {
3813-
this.logger.error('Error processing media message:');
3831+
this.logger.error('Error processing media message:');
38143832
this.logger.error(error);
38153833
throw new BadRequestException(error.toString());
38163834
}

src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -499,25 +499,29 @@ class ChatwootImport {
499499
stickerMessage: msg.message.stickerMessage,
500500
templateMessage: msg.message.templateMessage?.hydratedTemplate?.hydratedContentText,
501501
};
502-
const typeKey = Object.keys(types).find((key) => types[key] !== undefined);
503502

503+
const typeKey = Object.keys(types).find(
504+
(key) => types[key] !== undefined && types[key] !== null
505+
);
504506
switch (typeKey) {
505-
case 'documentMessage':
506-
return `_<File: ${msg.message.documentMessage.fileName}${
507-
msg.message.documentMessage.caption ? ` ${msg.message.documentMessage.caption}` : ''
508-
}>_`;
509-
510-
case 'documentWithCaptionMessage':
511-
return `_<File: ${msg.message.documentWithCaptionMessage.message.documentMessage.fileName}${
512-
msg.message.documentWithCaptionMessage.message.documentMessage.caption
513-
? ` ${msg.message.documentWithCaptionMessage.message.documentMessage.caption}`
514-
: ''
515-
}>_`;
507+
case 'documentMessage': {
508+
const doc = msg.message.documentMessage;
509+
const fileName = doc?.fileName || 'document';
510+
const caption = doc?.caption ? ` ${doc.caption}` : '';
511+
return `_<File: ${fileName}${caption}>_`;
512+
}
513+
514+
case 'documentWithCaptionMessage': {
515+
const doc = msg.message.documentWithCaptionMessage?.message?.documentMessage;
516+
const fileName = doc?.fileName || 'document';
517+
const caption = doc?.caption ? ` ${doc.caption}` : '';
518+
return `_<File: ${fileName}${caption}>_`;
519+
}
516520

517521
case 'templateMessage':
518-
return msg.message.templateMessage.hydratedTemplate.hydratedTitleText
519-
? `*${msg.message.templateMessage.hydratedTemplate.hydratedTitleText}*\\n`
520-
: '' + msg.message.templateMessage.hydratedTemplate.hydratedContentText;
522+
const template = msg.message.templateMessage?.hydratedTemplate;
523+
return (template?.hydratedTitleText ? `*${template.hydratedTitleText}*\n` : '') +
524+
(template?.hydratedContentText || '');
521525

522526
case 'imageMessage':
523527
return '_<Image Message>_';

0 commit comments

Comments
 (0)