Skip to content
45 changes: 30 additions & 15 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import makeWASocket, {
getContentType,
getDevice,
GroupMetadata,
GroupParticipant,
isJidGroup,
isLidUser,
makeCacheableSignalKeyStore,
Expand Down Expand Up @@ -869,8 +870,14 @@ export class WAStartupService {

this.client.sendPresenceUpdate('unavailable');

if (Long.isLong(received?.messageTimestamp)) {
received.messageTimestamp = received.messageTimestamp.toNumber();
let timestamp = received?.messageTimestamp;

if (timestamp && typeof timestamp === 'object' && typeof timestamp.toNumber === 'function') {
timestamp = timestamp.toNumber();
}else if (timestamp && typeof timestamp === 'object' && 'low' in timestamp && 'high' in timestamp) {
timestamp = Number(timestamp.low) || 0;
}else if (typeof timestamp !== 'number') {
timestamp = 0;
}

const messageType = getContentType(received.message);
Expand Down Expand Up @@ -899,8 +906,8 @@ export class WAStartupService {
keyParticipant:
received?.participant || this.normalizeParticipant(received.key),
messageType,
content: received.message[messageType] as PrismType.Prisma.JsonValue,
messageTimestamp: received.messageTimestamp,
content: JSON.parse(JSON.stringify(received.message[messageType])) as PrismType.Prisma.JsonValue,
messageTimestamp: timestamp,
instanceId: this.instance.id,
device: (() => {
if (isValidUlid(received.key.id)) {
Expand Down Expand Up @@ -1049,9 +1056,11 @@ export class WAStartupService {
},

'group-participants.update': (participantsUpdate: {
id: string;
participants: string[];
action: ParticipantAction;
id: string
author: string
authorPn?: string
participants: GroupParticipant[]
action: ParticipantAction
}) => {
this.ws.send(this.instance.name, 'group-participants.update', participantsUpdate);
this.sendDataWebhook('groupsParticipantsUpdated', participantsUpdate);
Expand Down Expand Up @@ -1366,6 +1375,16 @@ export class WAStartupService {
}
}
}

let timestamp = m?.messageTimestamp;

if (timestamp && typeof timestamp === 'object' && typeof timestamp.toNumber === 'function') {
timestamp = timestamp.toNumber();
}else if (timestamp && typeof timestamp === 'object' && 'low' in timestamp && 'high' in timestamp) {
timestamp = Number(timestamp.low) || 0;
}else if (typeof timestamp !== 'number') {
timestamp = 0;
}

return {
keyId: m.key.id,
Expand All @@ -1374,13 +1393,8 @@ export class WAStartupService {
keyParticipant: m?.participant,
pushName: m?.pushName,
messageType: getContentType(m.message),
content: m.message[getContentType(m.message)] as PrismType.Prisma.JsonValue,
messageTimestamp: (() => {
if (Long.isLong(m.messageTimestamp)) {
return m.messageTimestamp.toNumber();
}
return m.messageTimestamp;
})(),
content: JSON.parse(JSON.stringify(m.message[getContentType(m.message)])) as PrismType.Prisma.JsonValue,
messageTimestamp: timestamp,
instanceId: this.instance.id,
device: 'web',
isGroup: isJidGroup(m.key.remoteJid),
Expand Down Expand Up @@ -2226,7 +2240,7 @@ export class WAStartupService {
let mediaType: string;

for (const type of TypeMediaMessage) {
mediaMessage = msg.message[type];
mediaMessage = msg?.message?.[type];
if (mediaMessage) {
mediaType = type;
break;
Expand Down Expand Up @@ -2527,4 +2541,5 @@ export class WAStartupService {
throw new BadRequestException('Unable to leave the group', error.toString());
}
}

}
Loading