diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 63e3fc6e..684e9acb 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -52,6 +52,7 @@ import makeWASocket, { getContentType, getDevice, GroupMetadata, + GroupParticipant, isJidGroup, isLidUser, makeCacheableSignalKeyStore, @@ -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); @@ -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)) { @@ -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); @@ -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, @@ -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), @@ -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; @@ -2527,4 +2541,5 @@ export class WAStartupService { throw new BadRequestException('Unable to leave the group', error.toString()); } } + }