Skip to content

Commit 461c450

Browse files
authored
Merge pull request #202 from jeankassio/CodeChat
Bugfix: cannot read properties of undefined
2 parents 15012ed + c2f0806 commit 461c450

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/whatsapp/services/whatsapp.service.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import makeWASocket, {
5252
getContentType,
5353
getDevice,
5454
GroupMetadata,
55+
GroupParticipant,
5556
isJidGroup,
5657
isLidUser,
5758
makeCacheableSignalKeyStore,
@@ -882,8 +883,14 @@ export class WAStartupService {
882883

883884
this.client.sendPresenceUpdate('unavailable');
884885

885-
if (Long.isLong(received?.messageTimestamp)) {
886-
received.messageTimestamp = received.messageTimestamp.toNumber();
886+
let timestamp = received?.messageTimestamp;
887+
888+
if (timestamp && typeof timestamp === 'object' && typeof timestamp.toNumber === 'function') {
889+
timestamp = timestamp.toNumber();
890+
}else if (timestamp && typeof timestamp === 'object' && 'low' in timestamp && 'high' in timestamp) {
891+
timestamp = Number(timestamp.low) || 0;
892+
}else if (typeof timestamp !== 'number') {
893+
timestamp = 0;
887894
}
888895

889896
const messageType = getContentType(received.message);
@@ -912,8 +919,8 @@ export class WAStartupService {
912919
keyParticipant:
913920
received?.participant || this.normalizeParticipant(received.key),
914921
messageType,
915-
content: received.message[messageType] as PrismType.Prisma.JsonValue,
916-
messageTimestamp: received.messageTimestamp,
922+
content: JSON.parse(JSON.stringify(received.message[messageType])) as PrismType.Prisma.JsonValue,
923+
messageTimestamp: timestamp,
917924
instanceId: this.instance.id,
918925
device: (() => {
919926
if (isValidUlid(received.key.id)) {
@@ -1062,9 +1069,11 @@ export class WAStartupService {
10621069
},
10631070

10641071
'group-participants.update': (participantsUpdate: {
1065-
id: string;
1066-
participants: string[];
1067-
action: ParticipantAction;
1072+
id: string
1073+
author: string
1074+
authorPn?: string
1075+
participants: GroupParticipant[]
1076+
action: ParticipantAction
10681077
}) => {
10691078
this.ws.send(this.instance.name, 'group-participants.update', participantsUpdate);
10701079
this.sendDataWebhook('groupsParticipantsUpdated', participantsUpdate);
@@ -1379,6 +1388,16 @@ export class WAStartupService {
13791388
}
13801389
}
13811390
}
1391+
1392+
let timestamp = m?.messageTimestamp;
1393+
1394+
if (timestamp && typeof timestamp === 'object' && typeof timestamp.toNumber === 'function') {
1395+
timestamp = timestamp.toNumber();
1396+
}else if (timestamp && typeof timestamp === 'object' && 'low' in timestamp && 'high' in timestamp) {
1397+
timestamp = Number(timestamp.low) || 0;
1398+
}else if (typeof timestamp !== 'number') {
1399+
timestamp = 0;
1400+
}
13821401

13831402
return {
13841403
keyId: m.key.id,
@@ -1387,13 +1406,8 @@ export class WAStartupService {
13871406
keyParticipant: m?.participant,
13881407
pushName: m?.pushName,
13891408
messageType: getContentType(m.message),
1390-
content: m.message[getContentType(m.message)] as PrismType.Prisma.JsonValue,
1391-
messageTimestamp: (() => {
1392-
if (Long.isLong(m.messageTimestamp)) {
1393-
return m.messageTimestamp.toNumber();
1394-
}
1395-
return m.messageTimestamp;
1396-
})(),
1409+
content: JSON.parse(JSON.stringify(m.message[getContentType(m.message)])) as PrismType.Prisma.JsonValue,
1410+
messageTimestamp: timestamp,
13971411
instanceId: this.instance.id,
13981412
device: 'web',
13991413
isGroup: isJidGroup(m.key.remoteJid),
@@ -2239,7 +2253,7 @@ export class WAStartupService {
22392253
let mediaType: string;
22402254

22412255
for (const type of TypeMediaMessage) {
2242-
mediaMessage = msg.message[type];
2256+
mediaMessage = msg?.message?.[type];
22432257
if (mediaMessage) {
22442258
mediaType = type;
22452259
break;
@@ -2540,4 +2554,5 @@ export class WAStartupService {
25402554
throw new BadRequestException('Unable to leave the group', error.toString());
25412555
}
25422556
}
2557+
25432558
}

0 commit comments

Comments
 (0)