@ellxz24/baileys 2.0.30 → 2.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,166 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeLIDMappingStore = void 0;
4
-
5
- const WABinary_1 = require("../WABinary");
6
- const lru_cache_1 = require("lru-cache");
7
-
8
- const makeLIDMappingStore = (keys, logger, pnToLIDFunc) => {
9
- const mappingCache = new lru_cache_1.LRUCache({
10
- ttl: 7 * 24 * 60 * 60 * 1000,
11
- ttlAutopurge: true,
12
- updateAgeOnGet: true
13
- });
14
-
15
- const storeLIDPNMappings = async (pairs) => {
16
- const pairMap = {};
17
- for (const { lid, pn } of pairs) {
18
- if (!(((0, WABinary_1.isLidUser)(lid) && (0, WABinary_1.isPnUser)(pn)) || ((0, WABinary_1.isPnUser)(lid) && (0, WABinary_1.isLidUser)(pn)))) {
19
- logger && logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`);
20
- continue;
21
- }
22
- const lidDecoded = (0, WABinary_1.jidDecode)(lid);
23
- const pnDecoded = (0, WABinary_1.jidDecode)(pn);
24
- if (!lidDecoded || !pnDecoded) continue;
25
- const pnUser = pnDecoded.user;
26
- const lidUser = lidDecoded.user;
27
-
28
- let existingLidUser = mappingCache.get(`pn:${pnUser}`);
29
- if (!existingLidUser) {
30
- logger && logger.trace(`Cache miss for PN user ${pnUser}; checking database`);
31
- const stored = await keys.get("lid-mapping", [pnUser]);
32
- existingLidUser = stored[pnUser];
33
- if (existingLidUser) {
34
- mappingCache.set(`pn:${pnUser}`, existingLidUser);
35
- mappingCache.set(`lid:${existingLidUser}`, pnUser);
36
- }
37
- }
38
- if (existingLidUser === lidUser) {
39
- logger && logger.debug({ pnUser, lidUser }, "LID mapping already exists, skipping");
40
- continue;
41
- }
42
- pairMap[pnUser] = lidUser;
43
- }
44
-
45
- logger && logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`);
46
- await keys.transaction(async () => {
47
- for (const [pnUser, lidUser] of Object.entries(pairMap)) {
48
- await keys.set({
49
- "lid-mapping": {
50
- [pnUser]: lidUser,
51
- [`${lidUser}_reverse`]: pnUser
52
- }
53
- });
54
- mappingCache.set(`pn:${pnUser}`, lidUser);
55
- mappingCache.set(`lid:${lidUser}`, pnUser);
56
- }
57
- }, "lid-mapping");
58
- };
59
-
60
- const getLIDsForPNs = async (pns) => {
61
- const usyncFetch = {};
62
- const successfulPairs = {};
63
-
64
- for (const pn of pns) {
65
- if (!(0, WABinary_1.isPnUser)(pn) && !(0, WABinary_1.isHostedPnUser)(pn)) continue;
66
- const decoded = (0, WABinary_1.jidDecode)(pn);
67
- if (!decoded) continue;
68
- const pnUser = decoded.user;
69
-
70
- let lidUser = mappingCache.get(`pn:${pnUser}`);
71
- if (!lidUser) {
72
- const stored = await keys.get("lid-mapping", [pnUser]);
73
- lidUser = stored[pnUser];
74
- if (lidUser) {
75
- mappingCache.set(`pn:${pnUser}`, lidUser);
76
- mappingCache.set(`lid:${lidUser}`, pnUser);
77
- } else {
78
- logger && logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`);
79
- const device = decoded.device || 0;
80
- let normalizedPn = (0, WABinary_1.jidNormalizedUser)(pn);
81
- if ((0, WABinary_1.isHostedPnUser)(normalizedPn)) {
82
- normalizedPn = `${pnUser}@s.whatsapp.net`;
83
- }
84
- if (!usyncFetch[normalizedPn]) {
85
- usyncFetch[normalizedPn] = [device];
86
- } else {
87
- usyncFetch[normalizedPn]?.push(device);
88
- }
89
- continue;
90
- }
91
- }
92
-
93
- lidUser = lidUser.toString();
94
- if (!lidUser) {
95
- logger && logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`);
96
- return null;
97
- }
98
- const pnDevice = decoded.device !== undefined ? decoded.device : 0;
99
- const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.server === "hosted" ? "hosted.lid" : "lid"}`;
100
- logger && logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`);
101
- successfulPairs[pn] = { lid: deviceSpecificLid, pn };
102
- }
103
-
104
- if (Object.keys(usyncFetch).length > 0) {
105
- const result = await pnToLIDFunc?.(Object.keys(usyncFetch));
106
- if (result && result.length > 0) {
107
- await storeLIDPNMappings(result);
108
- for (const pair of result) {
109
- const pnDecoded = (0, WABinary_1.jidDecode)(pair.pn);
110
- const pnUser = pnDecoded?.user;
111
- if (!pnUser) continue;
112
- const lidUser = (0, WABinary_1.jidDecode)(pair.lid)?.user;
113
- if (!lidUser) continue;
114
-
115
- for (const device of usyncFetch[pair.pn]) {
116
- const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@${device === 99 ? "hosted.lid" : "lid"}`;
117
- logger && logger.trace(`getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid} (user mapping with device ${device})`);
118
- const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@${device === 99 ? "hosted" : "s.whatsapp.net"}`;
119
- successfulPairs[deviceSpecificPn] = { lid: deviceSpecificLid, pn: deviceSpecificPn };
120
- }
121
- }
122
- } else {
123
- return null;
124
- }
125
- }
126
- return Object.values(successfulPairs);
127
- };
128
-
129
- const getLIDForPN = async (pn) => {
130
- const result = await getLIDsForPNs([pn]);
131
- return result?.[0]?.lid || null;
132
- };
133
-
134
- const getPNForLID = async (lid) => {
135
- if (!(0, WABinary_1.isLidUser)(lid)) return null;
136
- const decoded = (0, WABinary_1.jidDecode)(lid);
137
- if (!decoded) return null;
138
-
139
- const lidUser = decoded.user;
140
- let pnUser = mappingCache.get(`lid:${lidUser}`);
141
-
142
- if (!pnUser || typeof pnUser !== "string") {
143
- const stored = await keys.get("lid-mapping", [`${lidUser}_reverse`]);
144
- pnUser = stored[`${lidUser}_reverse`];
145
- if (!pnUser || typeof pnUser !== "string") {
146
- logger && logger.trace(`No reverse mapping found for LID user: ${lidUser}`);
147
- return null;
148
- }
149
- mappingCache.set(`lid:${lidUser}`, pnUser);
150
- }
151
-
152
- const lidDevice = decoded.device !== undefined ? decoded.device : 0;
153
- const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === WABinary_1.WAJIDDomains.HOSTED_LID ? "hosted" : "s.whatsapp.net"}`;
154
- logger && logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`);
155
- return pnJid;
156
- };
157
-
158
- return {
159
- mappingCache,
160
- storeLIDPNMappings,
161
- getLIDForPN,
162
- getLIDsForPNs,
163
- getPNForLID
164
- };
165
- };
166
- exports.makeLIDMappingStore = makeLIDMappingStore;