Skip to content
Open
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
60 changes: 51 additions & 9 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,9 +1117,20 @@
await this.sendDataWebhook(Events.MESSAGES_EDITED, editedMessage);
const oldMessage = await this.getMessage(editedMessage.key, true);
if ((oldMessage as any)?.id) {
const editedMessageTimestamp = Long.isLong(editedMessage?.timestampMs)
? Math.floor(editedMessage.timestampMs.toNumber() / 1000)
: Math.floor((editedMessage.timestampMs as number) / 1000);
let editedMessageTimestamp: number;
if (Long.isLong(editedMessage?.timestampMs)) {
editedMessageTimestamp = Math.floor(editedMessage.timestampMs.toNumber() / 1000);
} else if (
editedMessage?.timestampMs &&
typeof editedMessage.timestampMs === 'object' &&
editedMessage.timestampMs &&
'low' in editedMessage.timestampMs &&
'high' in editedMessage.timestampMs
) {
editedMessageTimestamp = Math.floor(Long.fromValue(editedMessage.timestampMs).toNumber() / 1000);
} else {
editedMessageTimestamp = Math.floor((editedMessage?.timestampMs as number) / 1000);
}

await this.prismaRepository.message.update({
where: { id: (oldMessage as any).id },
Expand Down Expand Up @@ -1498,7 +1509,11 @@
`) as any[];
findMessage = messages[0] || null;

if (findMessage) message.messageId = findMessage.id;
if (!findMessage?.id) {
this.logger.warn(`Original message not found for update. Skipping. Key: ${JSON.stringify(key)}`);
continue;
}
message.messageId = findMessage.id;
}

if (update.message === null && update.status === undefined) {
Expand Down Expand Up @@ -4357,6 +4372,18 @@
return obj.toNumber();
}

// Handle Long-like objects that aren't detected by Long.isLong()
if (
obj &&
typeof obj === 'object' &&
'low' in obj &&
'high' in obj &&
typeof obj.low === 'number' &&
typeof obj.high === 'number'
Comment on lines +4376 to +4382
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The Long-like object detection logic is duplicated in multiple places.

Refactor the Long-like object detection and conversion into a shared utility to avoid code duplication and enhance maintainability.

) {
return Long.fromValue(obj).toNumber();
}

if (Array.isArray(obj)) {
return obj.map((item) => this.convertLongToNumber(item));
}
Expand All @@ -4378,6 +4405,23 @@
const contentType = getContentType(message.message);
const contentMsg = message?.message[contentType] as any;

// Convert messageTimestamp to number properly
let messageTimestamp: number;
if (Long.isLong(message.messageTimestamp)) {
messageTimestamp = message.messageTimestamp.toNumber();
} else if (
message.messageTimestamp &&
typeof message.messageTimestamp === 'object' &&
message.messageTimestamp &&
'low' in message.messageTimestamp &&
'high' in message.messageTimestamp
) {
// Handle Long-like objects that aren't detected by Long.isLong()
messageTimestamp = Long.fromValue(message.messageTimestamp).toNumber();
} else {
messageTimestamp = (message.messageTimestamp as number) || Date.now();
}

const messageRaw = {
key: message.key, // Save key exactly as it comes from Baileys
pushName:
Expand All @@ -4389,9 +4433,7 @@
message: this.convertLongToNumber({ ...message.message }),
contextInfo: this.convertLongToNumber(contentMsg?.contextInfo),
messageType: contentType || 'unknown',
messageTimestamp: Long.isLong(message.messageTimestamp)
? message.messageTimestamp.toNumber()
: (message.messageTimestamp as number),
messageTimestamp,
instanceId: this.instanceId,
source: getDevice(message.key.id),
};
Expand Down Expand Up @@ -4561,8 +4603,8 @@
return response;
}

public async baileysAssertSessions(jids: string[], force: boolean) {
const response = await this.client.assertSessions(jids, force);
public async baileysAssertSessions(jids: string[]) {
const response = await this.client.assertSessions(jids);

Check failure on line 4607 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Expected 2 arguments, but got 1.

return response;
}
Expand Down
Loading