Skip to content

Commit cb98717

Browse files
committed
fix: adjust for proper jid and lid protection.
1 parent 5e02d6b commit cb98717

2 files changed

Lines changed: 58 additions & 15 deletions

File tree

src/utils/extract-id.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { WAMessageKey } from '@whiskeysockets/baileys';
2+
3+
const isJid = (v: string) => {
4+
const regexp = new RegExp(/^\d+@(s.whatsapp.net|g.us|broadcast|newsletter)$/i);
5+
return regexp.test(v);
6+
};
7+
8+
const isLid = (v: string) => v.endsWith('@lid');
9+
10+
const extractUser = (...values: (string | undefined)[]) => {
11+
let jid: string | undefined;
12+
let lid: string | undefined;
13+
14+
for (const v of values) {
15+
if (!v) continue;
16+
17+
if (!lid && isLid(v)) {
18+
lid = v;
19+
}
20+
21+
if (!jid && isJid(v)) {
22+
jid = v;
23+
}
24+
}
25+
26+
return { jid, lid };
27+
};
28+
29+
export const getJidUser = (key: WAMessageKey) => {
30+
return extractUser(key?.remoteJid, key?.senderPn, key?.senderLid);
31+
};
32+
33+
export const getUserGroup = (key: WAMessageKey, participant?: string) => {
34+
return extractUser(
35+
key?.participant,
36+
key?.participantLid,
37+
key?.participantPn,
38+
participant,
39+
);
40+
};

src/whatsapp/services/whatsapp.service.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ import {
153153
} from 'fs';
154154
import { createProxyAgents } from '../../utils/proxy';
155155
import { fetchLatestBaileysVersionV2 } from '../../utils/wa-version';
156+
import { getJidUser, getUserGroup } from '../../utils/extract-id';
156157

157158
type InstanceQrCode = {
158159
count: number;
@@ -844,14 +845,17 @@ export class WAStartupService {
844845
continue;
845846
}
846847

848+
const user = getJidUser(m.key);
849+
const group = getUserGroup(m.key, m?.participant);
850+
847851
messagesRaw.push({
848852
keyId: m.key.id,
849853
keyFromMe: m.key.fromMe,
850854
pushName: m?.pushName || m.key.remoteJid.split('@')[0],
851-
keyRemoteJid: m.key?.remoteJid,
852-
keyLid: m.key?.['senderLid'] || m.key?.['senderPn'],
853-
keyParticipant: m?.participant || m.key?.participant,
854-
keyParticipantLid: m.key?.['participantLid'] || m.key?.['participantPn'],
855+
keyRemoteJid: user?.jid,
856+
keyLid: user?.lid,
857+
keyParticipant: group?.jid,
858+
keyParticipantLid: group?.lid,
855859
messageType,
856860
content: m.message[messageType] as PrismType.Prisma.JsonValue,
857861
messageTimestamp: timestamp,
@@ -874,7 +878,7 @@ export class WAStartupService {
874878
messages,
875879
type,
876880
}: {
877-
messages: proto.IWebMessageInfo[];
881+
messages: WAMessage[];
878882
type: MessageUpsertType;
879883
}) => {
880884
for (const received of messages) {
@@ -925,15 +929,17 @@ export class WAStartupService {
925929
}
926930
}
927931

932+
const user = getJidUser(received.key);
933+
const group = getUserGroup(received.key, received?.participant);
934+
928935
const messageRaw = {
929936
keyId: received.key.id,
930937
keyFromMe: received.key.fromMe,
931938
pushName: received.pushName,
932-
keyRemoteJid: received.key?.remoteJid,
933-
keyLid: received.key?.['senderLid'] || received.key?.['senderPn'],
934-
keyParticipant: received?.participant || received.key?.participant,
935-
keyParticipantLid:
936-
received.key?.['participantLid'] || received.key?.['participantPn'],
939+
keyRemoteJid: user?.jid,
940+
keyLid: user?.lid,
941+
keyParticipant: group?.jid,
942+
keyParticipantLid: group?.lid,
937943
messageType,
938944
content: JSON.parse(
939945
JSON.stringify(received.message[messageType]),
@@ -1242,11 +1248,8 @@ export class WAStartupService {
12421248
}
12431249

12441250
private createJid(number: string): string {
1245-
if (
1246-
number.includes('@g.us') ||
1247-
number.includes('@s.whatsapp.net') ||
1248-
number.includes('@lid')
1249-
) {
1251+
const regexp = new RegExp(/^\d+@(s.whatsapp.net|g.us|lid|broadcast|newsletter)$/i);
1252+
if (regexp.test(number)) {
12501253
return number;
12511254
}
12521255

0 commit comments

Comments
 (0)