Merge pull request #1473 from KokeroO/develop

fix: Deadlock errors and undefined arguments
This commit is contained in:
Davidson Gomes 2025-05-21 13:52:57 -03:00 committed by GitHub
commit cc01787501
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 61 deletions

View File

@ -1173,8 +1173,8 @@ export class BaileysStartupService extends ChannelStartupService {
const oldMessage = await this.getMessage(editedMessage.key, true); const oldMessage = await this.getMessage(editedMessage.key, true);
if ((oldMessage as any)?.id) { if ((oldMessage as any)?.id) {
const editedMessageTimestamp = Long.isLong(editedMessage?.timestampMs) const editedMessageTimestamp = Long.isLong(editedMessage?.timestampMs)
? editedMessage.timestampMs?.toNumber() ? Math.floor(editedMessage.timestampMs.toNumber() / 1000)
: (editedMessage.timestampMs as number); : Math.floor((editedMessage.timestampMs as number) / 1000);
await this.prismaRepository.message.update({ await this.prismaRepository.message.update({
where: { id: (oldMessage as any).id }, where: { id: (oldMessage as any).id },
@ -1205,7 +1205,7 @@ export class BaileysStartupService extends ChannelStartupService {
continue; continue;
} }
await this.baileysCache.set(messageKey, true, 30 * 60); await this.baileysCache.set(messageKey, true, 5 * 60);
if ( if (
(type !== 'notify' && type !== 'append') || (type !== 'notify' && type !== 'append') ||
@ -1311,21 +1311,31 @@ export class BaileysStartupService extends ChannelStartupService {
data: messageRaw, data: messageRaw,
}); });
if (received.key.fromMe === false) { const {remoteJid} = received.key;
const timestamp = msg.messageTimestamp;
const fromMe = received.key.fromMe.toString();
const messageKey = `${remoteJid}_${timestamp}_${fromMe}`;
const cachedTimestamp = await this.baileysCache.get(messageKey);
if (!cachedTimestamp) {
if (!received.key.fromMe) {
if (msg.status === status[3]) { if (msg.status === status[3]) {
this.logger.log(`Update not read messages ${received.key.remoteJid}`); this.logger.log(`Update not read messages ${remoteJid}`);
await this.updateChatUnreadMessages(remoteJid);
await this.updateChatUnreadMessages(received.key.remoteJid);
} else if (msg.status === status[4]) { } else if (msg.status === status[4]) {
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`); this.logger.log(`Update readed messages ${remoteJid} - ${timestamp}`);
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
} }
} else { } else {
// is send message by me // is send message by me
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`); this.logger.log(`Update readed messages ${remoteJid} - ${timestamp}`);
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
}
await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp); await this.baileysCache.set(messageKey, true, 5 * 60);
} else {
this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`);
} }
if (isMedia) { if (isMedia) {
@ -1481,7 +1491,7 @@ export class BaileysStartupService extends ChannelStartupService {
const cached = await this.baileysCache.get(updateKey); const cached = await this.baileysCache.get(updateKey);
if (cached) { if (cached) {
this.logger.info(`Message duplicated ignored: ${key.id}`); this.logger.info(`Message duplicated ignored [avoid deadlock]: ${updateKey}`);
continue; continue;
} }
@ -1497,7 +1507,7 @@ export class BaileysStartupService extends ChannelStartupService {
} }
} }
if (key.remoteJid !== 'status@broadcast') { if (key.remoteJid !== 'status@broadcast' && key.id !== undefined) {
let pollUpdates: any; let pollUpdates: any;
if (update.pollUpdates) { if (update.pollUpdates) {
@ -1525,19 +1535,20 @@ export class BaileysStartupService extends ChannelStartupService {
continue; continue;
} }
if (update.message === null && update.status === undefined) {
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
const message: any = { const message: any = {
messageId: findMessage.id, messageId: findMessage.id,
keyId: key.id, keyId: key.id,
remoteJid: key.remoteJid, remoteJid: key?.remoteJid,
fromMe: key.fromMe, fromMe: key.fromMe,
participant: key?.remoteJid, participant: key?.remoteJid,
status: 'DELETED', status: status[update.status] ?? 'DELETED',
pollUpdates,
instanceId: this.instanceId, instanceId: this.instanceId,
}; };
if (update.message === null && update.status === undefined) {
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
await this.prismaRepository.messageUpdate.create({ await this.prismaRepository.messageUpdate.create({
data: message, data: message,
@ -1556,28 +1567,31 @@ export class BaileysStartupService extends ChannelStartupService {
if (!key.fromMe && key.remoteJid) { if (!key.fromMe && key.remoteJid) {
readChatToUpdate[key.remoteJid] = true; readChatToUpdate[key.remoteJid] = true;
const {remoteJid} = key;
const timestamp = findMessage.messageTimestamp;
const fromMe = key.fromMe.toString();
const messageKey = `${remoteJid}_${timestamp}_${fromMe}`;
const cachedTimestamp = await this.baileysCache.get(messageKey);
if (!cachedTimestamp) {
if (status[update.status] === status[4]) { if (status[update.status] === status[4]) {
this.logger.log(`Update as read ${key.remoteJid} - ${findMessage.messageTimestamp}`); this.logger.log(`Update as read in message.update ${remoteJid} - ${timestamp}`);
this.updateMessagesReadedByTimestamp(key.remoteJid, findMessage.messageTimestamp); await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
} await this.baileysCache.set(messageKey, true, 5 * 60);
} }
await this.prismaRepository.message.update({ await this.prismaRepository.message.update({
where: { id: findMessage.id }, where: { id: findMessage.id },
data: { status: status[update.status] }, data: { status: status[update.status] },
}); });
} else {
this.logger.info(
`Update readed messages duplicated ignored in message.update [avoid deadlock]: ${messageKey}`,
);
}
}
} }
const message: any = {
messageId: findMessage.id,
keyId: key.id,
remoteJid: key.remoteJid,
fromMe: key.fromMe,
participant: key?.remoteJid,
status: status[update.status],
pollUpdates,
instanceId: this.instanceId,
};
this.sendDataWebhook(Events.MESSAGES_UPDATE, message); this.sendDataWebhook(Events.MESSAGES_UPDATE, message);
@ -3735,6 +3749,10 @@ export class BaileysStartupService extends ChannelStartupService {
} }
} }
if ('messageContextInfo' in msg.message && Object.keys(msg.message).length === 1) {
throw 'The message is messageContextInfo';
}
let mediaMessage: any; let mediaMessage: any;
let mediaType: string; let mediaType: string;

View File

@ -499,25 +499,29 @@ class ChatwootImport {
stickerMessage: msg.message.stickerMessage, stickerMessage: msg.message.stickerMessage,
templateMessage: msg.message.templateMessage?.hydratedTemplate?.hydratedContentText, templateMessage: msg.message.templateMessage?.hydratedTemplate?.hydratedContentText,
}; };
const typeKey = Object.keys(types).find((key) => types[key] !== undefined);
const typeKey = Object.keys(types).find(
(key) => types[key] !== undefined && types[key] !== null
);
switch (typeKey) { switch (typeKey) {
case 'documentMessage': case 'documentMessage': {
return `_<File: ${msg.message.documentMessage.fileName}${ const doc = msg.message.documentMessage;
msg.message.documentMessage.caption ? ` ${msg.message.documentMessage.caption}` : '' const fileName = doc?.fileName || 'document';
}>_`; const caption = doc?.caption ? ` ${doc.caption}` : '';
return `_<File: ${fileName}${caption}>_`;
}
case 'documentWithCaptionMessage': case 'documentWithCaptionMessage': {
return `_<File: ${msg.message.documentWithCaptionMessage.message.documentMessage.fileName}${ const doc = msg.message.documentWithCaptionMessage?.message?.documentMessage;
msg.message.documentWithCaptionMessage.message.documentMessage.caption const fileName = doc?.fileName || 'document';
? ` ${msg.message.documentWithCaptionMessage.message.documentMessage.caption}` const caption = doc?.caption ? ` ${doc.caption}` : '';
: '' return `_<File: ${fileName}${caption}>_`;
}>_`; }
case 'templateMessage': case 'templateMessage':
return msg.message.templateMessage.hydratedTemplate.hydratedTitleText const template = msg.message.templateMessage?.hydratedTemplate;
? `*${msg.message.templateMessage.hydratedTemplate.hydratedTitleText}*\\n` return (template?.hydratedTitleText ? `*${template.hydratedTitleText}*\n` : '') +
: '' + msg.message.templateMessage.hydratedTemplate.hydratedContentText; (template?.hydratedContentText || '');
case 'imageMessage': case 'imageMessage':
return '_<Image Message>_'; return '_<Image Message>_';