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