fca-nx 6.7.2 → 6.7.4
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.
- package/README.md +34 -0
- package/module/loginHelper.js +16 -9
- package/package.json +1 -1
- package/src/api/action/addfriend.js +110 -31
- package/src/api/messaging/sendTypingIndicator.js +15 -0
- package/src/api/socket/OldMessage.js +7 -1
- package/src/api/socket/core/connectMqtt.js +33 -5
- package/src/api/socket/e2ee/index.js +83 -22
- package/src/api/socket/e2ee/protocol.js +32 -9
- package/src/api/socket/e2ee/ratchet.js +29 -1
- package/src/api/socket/e2ee/store.js +140 -42
- package/src/api/socket/e2ee/vendor/fb-e2ee.cjs +140 -18
- package/src/api/socket/listenE2EE.js +18 -1
- package/src/api/socket/listenMqtt.js +18 -3
- package/src/api/socket/sendMessage.js +31 -1
- package/src/api/socket/sendMessageMqtt.js +6 -0
- package/src/api/users/getUserInfoV2.js +143 -27
- package/src/utils/loginParser/autoLogin.js +89 -63
- package/src/utils/textEffects.js +85 -0
|
@@ -82,12 +82,17 @@ module.exports = function (defaultFuncs, api, ctx, opts) {
|
|
|
82
82
|
function postSafe(...args) {
|
|
83
83
|
return rawPost(...args).catch(err => {
|
|
84
84
|
const msg = (err && err.error) || (err && err.message) || String(err || "");
|
|
85
|
-
|
|
85
|
+
// A single request can briefly see an expired web session while
|
|
86
|
+
// another request is refreshing the shared cookie jar. Do not
|
|
87
|
+
// tear down the live MQTT session for that recoverable case;
|
|
88
|
+
// the shared auto-login promise handles the HTTP request and
|
|
89
|
+
// MQTT reconnects itself if the socket also needs refreshing.
|
|
90
|
+
if (/blocked the login|Checkpoint/i.test(msg)) {
|
|
86
91
|
emitAuth(
|
|
87
92
|
ctx,
|
|
88
93
|
api,
|
|
89
94
|
globalCallback,
|
|
90
|
-
/blocked|checkpoint/i.test(msg) ? "login_blocked" : "
|
|
95
|
+
/blocked|checkpoint/i.test(msg) ? "login_blocked" : "auth_error",
|
|
91
96
|
msg
|
|
92
97
|
);
|
|
93
98
|
}
|
|
@@ -104,6 +109,16 @@ module.exports = function (defaultFuncs, api, ctx, opts) {
|
|
|
104
109
|
let isReconnecting = false;
|
|
105
110
|
|
|
106
111
|
function getReconnectDelay() {
|
|
112
|
+
// [Fixed by xalman] immediate first reconnect (see connectMqtt.js computeBackoff). Called with
|
|
113
|
+
// reconnectAttempts === 0 for a fresh cycle/disconnect and === 1 for the first retry after a
|
|
114
|
+
// failed getSeqID; both go out at once, later ones back off 1s, 2s, 4s ... up to 60s.
|
|
115
|
+
const immediate = process.env.FCA_NX_MQTT_IMMEDIATE_RECONNECT !== "0" && conf.immediateReconnect !== false;
|
|
116
|
+
if (immediate) {
|
|
117
|
+
if (reconnectAttempts <= 1) return 0;
|
|
118
|
+
const fast = Math.min(1000 * Math.pow(2, reconnectAttempts - 2), MAX_MQTT_RECONNECT_DELAY_MS);
|
|
119
|
+
const j = Math.floor(Math.random() * Math.min(500, fast * 0.2));
|
|
120
|
+
return Math.min(fast + j, MAX_MQTT_RECONNECT_DELAY_MS);
|
|
121
|
+
}
|
|
107
122
|
const base = conf.reconnectDelayMs || RECONNECT_DELAY_MS_DEFAULT;
|
|
108
123
|
const exp = Math.min(base * Math.pow(2, reconnectAttempts), MAX_MQTT_RECONNECT_DELAY_MS);
|
|
109
124
|
const jitter = Math.floor(Math.random() * Math.min(1000, exp * 0.2));
|
|
@@ -340,7 +355,7 @@ module.exports = function (defaultFuncs, api, ctx, opts) {
|
|
|
340
355
|
function delayedReconnect() {
|
|
341
356
|
if (ctx._ending) return;
|
|
342
357
|
const d = getReconnectDelay();
|
|
343
|
-
logger(`mqtt reconnect in ${d}ms`, "info");
|
|
358
|
+
logger(d === 0 ? "mqtt reconnecting immediately" : `mqtt reconnect in ${d}ms`, "info");
|
|
344
359
|
|
|
345
360
|
if (ctx._reconnectTimer) {
|
|
346
361
|
clearTimeout(ctx._reconnectTimer);
|
|
@@ -2,11 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
var utils = require("../../utils/utils");
|
|
4
4
|
var logger = require("../../utils/logger");
|
|
5
|
+
var { getTextEffectId, getTextEffectIds } = require("../../utils/textEffects");
|
|
5
6
|
|
|
6
7
|
var ALLOWED = {
|
|
7
8
|
attachment: true, url: true, sticker: true, emoji: true,
|
|
8
9
|
emojiSize: true, body: true, mentions: true, location: true,
|
|
9
|
-
replyToMessage: true, forwardAttachmentIds: true
|
|
10
|
+
replyToMessage: true, forwardAttachmentIds: true, text_effect_ids: true,
|
|
11
|
+
textEffect: true, text_effect: true, text_effect_id: true,
|
|
12
|
+
textEffectId: true, textEffectID: true, effect: true, effect_id: true,
|
|
13
|
+
effectID: true, text_effect_name: true, effectName: true, effect_name: true
|
|
10
14
|
};
|
|
11
15
|
|
|
12
16
|
var EMOJI_SIZES = { small: 1, medium: 2, large: 3 };
|
|
@@ -132,6 +136,11 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
132
136
|
var mentionData = buildMentionData(msg, baseBody);
|
|
133
137
|
if (mentionData) payload0.mention_data = mentionData;
|
|
134
138
|
|
|
139
|
+
var textEffectId = getTextEffectId(msg);
|
|
140
|
+
if (textEffectId) payload0.text_effect_id = textEffectId;
|
|
141
|
+
var effectArray = getTextEffectIds(msg);
|
|
142
|
+
if (effectArray.length) payload0.text_effect_ids = effectArray;
|
|
143
|
+
|
|
135
144
|
if (msg.sticker) { payload0.send_type = 2; payload0.sticker_id = msg.sticker; }
|
|
136
145
|
if (msg.emoji) { payload0.send_type = 1; payload0.text = msg.emoji; payload0.hot_emoji_size = toEmojiSize(msg.emojiSize); }
|
|
137
146
|
|
|
@@ -285,6 +294,27 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
285
294
|
return promise;
|
|
286
295
|
}
|
|
287
296
|
|
|
297
|
+
// [Fixed by xalman] Text replies to a DM whose latest activity was E2EE must go through the
|
|
298
|
+
// E2EE bridge. MQTT cannot deliver into an end-to-end encrypted chat: it waited 15s for an
|
|
299
|
+
// ack that never came, so every command that calls api.sendMessage() directly looked dead
|
|
300
|
+
// in inbox. (Group chats and normal MQTT DMs are unchanged.)
|
|
301
|
+
if (isSingleUser && !msg.attachment && !msg.sticker && !msg.location && !msg.emoji &&
|
|
302
|
+
api.e2ee && typeof api.e2ee.isConnected === "function" && api.e2ee.isConnected()) {
|
|
303
|
+
try {
|
|
304
|
+
var e2eeText = await api.e2ee.sendMessage(String(threadID), { body: msg.body != null ? String(msg.body) : "" }, replyToMessage || msg.replyToMessage);
|
|
305
|
+
var e2eeWrapped = e2eeText && e2eeText.messageId
|
|
306
|
+
? { threadID: String(threadID), messageID: String(e2eeText.messageId) }
|
|
307
|
+
: e2eeText;
|
|
308
|
+
if (callback) callback(null, e2eeWrapped);
|
|
309
|
+
else resolve(e2eeWrapped);
|
|
310
|
+
} catch (e2eeTextErr) {
|
|
311
|
+
logger.error("sendMessage", "E2EE DM text send failed: " + (e2eeTextErr.message || e2eeTextErr));
|
|
312
|
+
if (callback) callback(e2eeTextErr);
|
|
313
|
+
else reject(e2eeTextErr);
|
|
314
|
+
}
|
|
315
|
+
return promise;
|
|
316
|
+
}
|
|
317
|
+
|
|
288
318
|
try {
|
|
289
319
|
var result = await sendViaMqtt(msg, threadID, replyToMessage);
|
|
290
320
|
if (callback) callback(null, result);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var utils = require("../../utils/utils");
|
|
4
|
+
var { getTextEffectId, getTextEffectIds } = require("../../utils/textEffects");
|
|
4
5
|
|
|
5
6
|
module.exports = function (defaultFuncs, api, ctx) {
|
|
6
7
|
return function sendMessageMqtt(msg, threadID, callback, replyToMessage) {
|
|
@@ -33,6 +34,11 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
33
34
|
multitab_env: 0
|
|
34
35
|
};
|
|
35
36
|
|
|
37
|
+
var textEffectId = getTextEffectId(msg);
|
|
38
|
+
if (textEffectId) payload0.text_effect_id = textEffectId;
|
|
39
|
+
var effectArray = getTextEffectIds(msg);
|
|
40
|
+
if (effectArray.length) payload0.text_effect_ids = effectArray;
|
|
41
|
+
|
|
36
42
|
if (msg.sticker) { payload0.send_type = 2; payload0.sticker_id = msg.sticker; }
|
|
37
43
|
if (replyToMessage || msg.replyToMessage) {
|
|
38
44
|
payload0.reply_metadata = {
|
|
@@ -1,8 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const { parseAndCheckLogin } = require("../../utils/client.js");
|
|
3
3
|
const logger = require("../../../func/logger");
|
|
4
|
-
|
|
5
|
-
const
|
|
4
|
+
|
|
5
|
+
const DOC_IDS = [
|
|
6
|
+
{
|
|
7
|
+
id: "2399631849267992",
|
|
8
|
+
name: "CometUserProfileCoverPhotoQuery",
|
|
9
|
+
hasCover: true
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
id: "5009315269112105",
|
|
13
|
+
name: "CometProfilePhotoGridQuery",
|
|
14
|
+
hasCover: true
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
id: "24418640587785718",
|
|
18
|
+
name: "CometHovercardQueryRendererQuery",
|
|
19
|
+
hasCover: false
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: "6003371686559437",
|
|
23
|
+
name: "CometProfileInfoRendererQuery",
|
|
24
|
+
hasCover: true
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
id: "6184020922587885",
|
|
28
|
+
name: "CometProfilePageQuery",
|
|
29
|
+
hasCover: true
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "5069749766627730",
|
|
33
|
+
name: "CometActorProfilePictureQuery",
|
|
34
|
+
hasCover: false
|
|
35
|
+
}
|
|
36
|
+
];
|
|
6
37
|
const CALLER_CLASS = "RelayModern";
|
|
7
38
|
|
|
8
39
|
function toJSONMaybe(data) {
|
|
@@ -33,6 +64,32 @@ function usernameFromUrl(raw) {
|
|
|
33
64
|
return null;
|
|
34
65
|
}
|
|
35
66
|
|
|
67
|
+
function extractPhotoUrl(photo, depth = 0) {
|
|
68
|
+
if (!photo || depth > 4) return null;
|
|
69
|
+
if (typeof photo === "string") {
|
|
70
|
+
return /^https?:\/\//i.test(photo) ? photo : null;
|
|
71
|
+
}
|
|
72
|
+
if (typeof photo !== "object") return null;
|
|
73
|
+
|
|
74
|
+
// Facebook has returned direct URLs as well as nested image/photo objects
|
|
75
|
+
// from Comet hovercards. Walk only the known media keys and prefer direct
|
|
76
|
+
// URL fields before descending into nested containers.
|
|
77
|
+
for (const key of [
|
|
78
|
+
"source",
|
|
79
|
+
"uri",
|
|
80
|
+
"url",
|
|
81
|
+
"image",
|
|
82
|
+
"photo",
|
|
83
|
+
"media",
|
|
84
|
+
"cover_photo",
|
|
85
|
+
"coverPhoto"
|
|
86
|
+
]) {
|
|
87
|
+
const value = extractPhotoUrl(photo[key], depth + 1);
|
|
88
|
+
if (value) return value;
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
36
93
|
function pickMeta(user) {
|
|
37
94
|
let friendshipStatus = null;
|
|
38
95
|
let gender = null;
|
|
@@ -57,6 +114,20 @@ function pickMeta(user) {
|
|
|
57
114
|
return { friendshipStatus, gender, shortName };
|
|
58
115
|
}
|
|
59
116
|
|
|
117
|
+
function extractCoverPhoto(user) {
|
|
118
|
+
const cover = user?.cover_photo;
|
|
119
|
+
if (!cover) return null;
|
|
120
|
+
if (typeof cover === "string") return cover;
|
|
121
|
+
return cover.source?.uri ||
|
|
122
|
+
cover.source ||
|
|
123
|
+
cover.uri ||
|
|
124
|
+
cover.photo?.image?.uri ||
|
|
125
|
+
cover.photo?.image?.url ||
|
|
126
|
+
cover.image?.uri ||
|
|
127
|
+
cover.image?.url ||
|
|
128
|
+
cover.photo?.url ||
|
|
129
|
+
null;
|
|
130
|
+
}
|
|
60
131
|
function normalizeUser(user) {
|
|
61
132
|
if (!user) return null;
|
|
62
133
|
const meta = pickMeta(user);
|
|
@@ -66,8 +137,10 @@ function normalizeUser(user) {
|
|
|
66
137
|
name: user.name || null,
|
|
67
138
|
firstName: meta.shortName || null,
|
|
68
139
|
vanity,
|
|
69
|
-
thumbSrc: user
|
|
70
|
-
coverPhoto:
|
|
140
|
+
thumbSrc: extractProfilePicture(user),
|
|
141
|
+
coverPhoto: extractPhotoUrl(
|
|
142
|
+
user.cover_photo || user.coverPhoto || user.profile_cover_photo
|
|
143
|
+
),
|
|
71
144
|
profileUrl: user.profile_url || user.url || null,
|
|
72
145
|
gender: meta.gender || null,
|
|
73
146
|
type: user.__typename || "User",
|
|
@@ -136,26 +209,47 @@ function formatUser(user, id) {
|
|
|
136
209
|
module.exports = function (defaultFuncs, api, ctx) {
|
|
137
210
|
async function fetchOne(userID) {
|
|
138
211
|
const id = String(userID);
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
212
|
+
let bestUser = null;
|
|
213
|
+
for (const doc of DOC_IDS) {
|
|
214
|
+
const variables = doc.name === "CometHovercardQueryRendererQuery"
|
|
215
|
+
? {
|
|
216
|
+
actionBarRenderLocation: "WWW_COMET_HOVERCARD",
|
|
217
|
+
context: "DEFAULT",
|
|
218
|
+
entityID: id,
|
|
219
|
+
scale: 1,
|
|
220
|
+
__relay_internal__pv__WorkCometIsEmployeeGKProviderrelayprovider: false
|
|
221
|
+
}
|
|
222
|
+
: { userID: id, scale: 1 };
|
|
223
|
+
const form = {
|
|
224
|
+
av: String(ctx?.userID || ""),
|
|
225
|
+
fb_api_caller_class: CALLER_CLASS,
|
|
226
|
+
fb_api_req_friendly_name: doc.name,
|
|
227
|
+
server_timestamps: true,
|
|
228
|
+
doc_id: doc.id,
|
|
229
|
+
variables: JSON.stringify(variables)
|
|
230
|
+
};
|
|
231
|
+
try {
|
|
232
|
+
const response = await defaultFuncs.post("https://www.facebook.com/api/graphql/", ctx.jar, form)
|
|
233
|
+
.then(parseAndCheckLogin(ctx, defaultFuncs));
|
|
234
|
+
const parsed = toJSONMaybe(response) || response;
|
|
235
|
+
const root = Array.isArray(parsed) ? parsed[0] : parsed;
|
|
236
|
+
const normalized = normalizeUser(findUser(root));
|
|
237
|
+
if (!normalized?.id) continue;
|
|
238
|
+
if (!bestUser) {
|
|
239
|
+
bestUser = normalized;
|
|
240
|
+
} else {
|
|
241
|
+
for (const field of ["thumbSrc", "profileUrl", "vanity", "coverPhoto", "firstName", "gender"]) {
|
|
242
|
+
if (!bestUser[field] && normalized[field]) bestUser[field] = normalized[field];
|
|
243
|
+
}
|
|
244
|
+
if (normalized.isVerified) bestUser.isVerified = true;
|
|
245
|
+
if (normalized.isFriend) bestUser.isFriend = true;
|
|
246
|
+
}
|
|
247
|
+
if (doc.hasCover && bestUser.coverPhoto) break;
|
|
248
|
+
} catch (error) {
|
|
249
|
+
logger(`getUserInfoV2 ${doc.id}: ${error.message || error}`, "warn");
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return bestUser;
|
|
159
253
|
}
|
|
160
254
|
|
|
161
255
|
async function fetchLegacy(userIDs) {
|
|
@@ -180,7 +274,7 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
180
274
|
lastName: nameParts.length > 1? nameParts.slice(1).join(" ") : null,
|
|
181
275
|
vanity: profile.vanity || null,
|
|
182
276
|
thumbSrc: null,
|
|
183
|
-
coverPhoto: profile.cover_photo
|
|
277
|
+
coverPhoto: extractPhotoUrl(profile.cover_photo),
|
|
184
278
|
profileUrl: profile.uri || `https://www.facebook.com/profile.php?id=${id}`,
|
|
185
279
|
gender: profile.gender === 1? "male" : profile.gender === 2? "female" : "no specific gender",
|
|
186
280
|
type: profile.type || "User",
|
|
@@ -219,7 +313,7 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
219
313
|
callback(error);
|
|
220
314
|
returnPromise;
|
|
221
315
|
}
|
|
222
|
-
|
|
316
|
+
(async () => {
|
|
223
317
|
try {
|
|
224
318
|
const retObj = {};
|
|
225
319
|
const results = await Promise.allSettled(ids.map(fetchOne));
|
|
@@ -265,6 +359,28 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
265
359
|
return callback(null, retObj);
|
|
266
360
|
}
|
|
267
361
|
})();
|
|
268
|
-
|
|
362
|
+
return returnPromise;
|
|
269
363
|
};
|
|
270
364
|
};
|
|
365
|
+
|
|
366
|
+
module.exports._private = {
|
|
367
|
+
extractPhotoUrl,
|
|
368
|
+
normalizeUser
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
function findUser(root) {
|
|
372
|
+
return root?.data?.node?.comet_hovercard_renderer?.user ||
|
|
373
|
+
root?.data?.node?.user ||
|
|
374
|
+
root?.data?.node ||
|
|
375
|
+
root?.data?.user ||
|
|
376
|
+
root?.data?.viewer ||
|
|
377
|
+
null;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function extractProfilePicture(user) {
|
|
381
|
+
return user?.profile_picture?.uri ||
|
|
382
|
+
user?.profilePicture?.uri ||
|
|
383
|
+
user?.profile_picture?.source?.uri ||
|
|
384
|
+
user?.profile_picture?.source ||
|
|
385
|
+
null;
|
|
386
|
+
}
|
|
@@ -14,12 +14,86 @@ function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
|
14
14
|
const { buildUrl, headerOf, formatCookie } = helpers;
|
|
15
15
|
|
|
16
16
|
return async function maybeAutoLogin(resData, resConfig) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
const retryAfterLogin = async function () {
|
|
18
|
+
if (!resConfig) {
|
|
19
|
+
const e = new Error(
|
|
20
|
+
"Not logged in. Auto login successful but cannot retry request."
|
|
21
|
+
);
|
|
22
|
+
e.error = "Not logged in.";
|
|
23
|
+
e.res = resData;
|
|
24
|
+
throw e;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const url = buildUrl(resConfig);
|
|
28
|
+
const method = String(resConfig?.method || "GET").toUpperCase();
|
|
29
|
+
const ctype = String(headerOf(resConfig?.headers, "content-type") || "").toLowerCase();
|
|
30
|
+
const isMultipart = ctype.includes("multipart/form-data");
|
|
31
|
+
const payload = resConfig?.data;
|
|
32
|
+
const params = resConfig?.params;
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
let newData;
|
|
36
|
+
if (method === "GET") {
|
|
37
|
+
newData = await http.get(url, ctx.jar, params || null, ctx.globalOptions, ctx);
|
|
38
|
+
} else if (isMultipart) {
|
|
39
|
+
newData = await http.postFormData(url, ctx.jar, payload, params, ctx.globalOptions, ctx);
|
|
40
|
+
} else {
|
|
41
|
+
newData = await http.post(url, ctx.jar, payload, ctx.globalOptions, ctx);
|
|
42
|
+
}
|
|
43
|
+
return await parseAndCheckLogin(ctx, http)(newData);
|
|
44
|
+
} catch (retryErr) {
|
|
45
|
+
if (
|
|
46
|
+
retryErr?.code === "ERR_INVALID_CHAR" ||
|
|
47
|
+
(retryErr?.message && retryErr.message.includes("Invalid character in header"))
|
|
48
|
+
) {
|
|
49
|
+
logger(
|
|
50
|
+
`Auto login retry failed: Invalid header detected. Error: ${retryErr.message}`,
|
|
51
|
+
"error"
|
|
52
|
+
);
|
|
53
|
+
const e = new Error("Not logged in. Auto login retry failed due to invalid header.");
|
|
54
|
+
e.error = "Not logged in.";
|
|
55
|
+
e.res = resData;
|
|
56
|
+
e.originalError = retryErr;
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
logger(
|
|
60
|
+
`Auto login retry failed: ${
|
|
61
|
+
retryErr && retryErr.message ? retryErr.message : String(retryErr)
|
|
62
|
+
}`,
|
|
63
|
+
"error"
|
|
64
|
+
);
|
|
65
|
+
const e = new Error("Not logged in. Auto login retry failed.");
|
|
66
|
+
e.error = "Not logged in.";
|
|
67
|
+
e.res = resData;
|
|
68
|
+
e.originalError = retryErr;
|
|
69
|
+
throw e;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Several requests can discover an expired session at the same time when
|
|
74
|
+
// many users interact with the bot. They must share one recovery attempt;
|
|
75
|
+
// rejecting all followers as "already in progress" creates an auth storm
|
|
76
|
+
// and can make the MQTT session look logged out.
|
|
77
|
+
if (ctx._autoLoginPromise) {
|
|
78
|
+
let ok;
|
|
79
|
+
try {
|
|
80
|
+
ok = await ctx._autoLoginPromise;
|
|
81
|
+
} catch (autoLoginErr) {
|
|
82
|
+
const e = new Error("Not logged in. Auto login failed.");
|
|
83
|
+
e.error = "Not logged in.";
|
|
84
|
+
e.res = resData;
|
|
85
|
+
e.originalError = autoLoginErr;
|
|
86
|
+
throw e;
|
|
87
|
+
}
|
|
88
|
+
if (!ok) {
|
|
89
|
+
const e = new Error("Not logged in. Auto login failed.");
|
|
90
|
+
e.error = "Not logged in.";
|
|
91
|
+
e.res = resData;
|
|
92
|
+
throw e;
|
|
93
|
+
}
|
|
94
|
+
return retryAfterLogin();
|
|
22
95
|
}
|
|
96
|
+
|
|
23
97
|
if (typeof ctx.performAutoLogin !== "function") {
|
|
24
98
|
const e = new Error("Not logged in. Auto login function not available.");
|
|
25
99
|
e.error = "Not logged in.";
|
|
@@ -31,68 +105,16 @@ function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
|
31
105
|
logger("Login session expired, attempting auto login...", "warn");
|
|
32
106
|
emit("sessionExpired", { res: resData });
|
|
33
107
|
|
|
108
|
+
const autoLoginPromise = Promise.resolve().then(() => ctx.performAutoLogin());
|
|
109
|
+
ctx._autoLoginPromise = autoLoginPromise;
|
|
110
|
+
|
|
34
111
|
try {
|
|
35
|
-
const ok = await
|
|
112
|
+
const ok = await autoLoginPromise;
|
|
36
113
|
if (ok) {
|
|
37
114
|
logger("Auto login successful! Retrying request...", "info");
|
|
38
115
|
emit("autoLoginSuccess", { res: resData });
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (resConfig) {
|
|
42
|
-
const url = buildUrl(resConfig);
|
|
43
|
-
const method = String(resConfig?.method || "GET").toUpperCase();
|
|
44
|
-
const ctype = String(headerOf(resConfig?.headers, "content-type") || "").toLowerCase();
|
|
45
|
-
const isMultipart = ctype.includes("multipart/form-data");
|
|
46
|
-
const payload = resConfig?.data;
|
|
47
|
-
const params = resConfig?.params;
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
let newData;
|
|
51
|
-
if (method === "GET") {
|
|
52
|
-
newData = await http.get(url, ctx.jar, params || null, ctx.globalOptions, ctx);
|
|
53
|
-
} else if (isMultipart) {
|
|
54
|
-
newData = await http.postFormData(url, ctx.jar, payload, params, ctx.globalOptions, ctx);
|
|
55
|
-
} else {
|
|
56
|
-
newData = await http.post(url, ctx.jar, payload, ctx.globalOptions, ctx);
|
|
57
|
-
}
|
|
58
|
-
return await parseAndCheckLogin(ctx, http)(newData);
|
|
59
|
-
} catch (retryErr) {
|
|
60
|
-
if (
|
|
61
|
-
retryErr?.code === "ERR_INVALID_CHAR" ||
|
|
62
|
-
(retryErr?.message && retryErr.message.includes("Invalid character in header"))
|
|
63
|
-
) {
|
|
64
|
-
logger(
|
|
65
|
-
`Auto login retry failed: Invalid header detected. Error: ${retryErr.message}`,
|
|
66
|
-
"error"
|
|
67
|
-
);
|
|
68
|
-
const e = new Error("Not logged in. Auto login retry failed due to invalid header.");
|
|
69
|
-
e.error = "Not logged in.";
|
|
70
|
-
e.res = resData;
|
|
71
|
-
e.originalError = retryErr;
|
|
72
|
-
throw e;
|
|
73
|
-
}
|
|
74
|
-
logger(
|
|
75
|
-
`Auto login retry failed: ${
|
|
76
|
-
retryErr && retryErr.message ? retryErr.message : String(retryErr)
|
|
77
|
-
}`,
|
|
78
|
-
"error"
|
|
79
|
-
);
|
|
80
|
-
const e = new Error("Not logged in. Auto login retry failed.");
|
|
81
|
-
e.error = "Not logged in.";
|
|
82
|
-
e.res = resData;
|
|
83
|
-
e.originalError = retryErr;
|
|
84
|
-
throw e;
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
const e = new Error(
|
|
88
|
-
"Not logged in. Auto login successful but cannot retry request."
|
|
89
|
-
);
|
|
90
|
-
e.error = "Not logged in.";
|
|
91
|
-
e.res = resData;
|
|
92
|
-
throw e;
|
|
93
|
-
}
|
|
116
|
+
return retryAfterLogin();
|
|
94
117
|
} else {
|
|
95
|
-
ctx.auto_login = false;
|
|
96
118
|
const e = new Error("Not logged in. Auto login failed.");
|
|
97
119
|
e.error = "Not logged in.";
|
|
98
120
|
e.res = resData;
|
|
@@ -100,7 +122,6 @@ function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
|
100
122
|
throw e;
|
|
101
123
|
}
|
|
102
124
|
} catch (autoLoginErr) {
|
|
103
|
-
ctx.auto_login = false;
|
|
104
125
|
if (autoLoginErr.error === "Not logged in.") {
|
|
105
126
|
throw autoLoginErr;
|
|
106
127
|
}
|
|
@@ -116,6 +137,11 @@ function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
|
116
137
|
e.originalError = autoLoginErr;
|
|
117
138
|
emit("autoLoginFailed", { error: e, res: resData });
|
|
118
139
|
throw e;
|
|
140
|
+
} finally {
|
|
141
|
+
ctx.auto_login = false;
|
|
142
|
+
if (ctx._autoLoginPromise === autoLoginPromise) {
|
|
143
|
+
ctx._autoLoginPromise = null;
|
|
144
|
+
}
|
|
119
145
|
}
|
|
120
146
|
};
|
|
121
147
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const TEXT_EFFECTS = Object.freeze({
|
|
4
|
+
hearts: "1",
|
|
5
|
+
heart: "1",
|
|
6
|
+
love: "1",
|
|
7
|
+
gift: "2",
|
|
8
|
+
present: "2",
|
|
9
|
+
box: "2",
|
|
10
|
+
giftbox: "2",
|
|
11
|
+
fire: "3",
|
|
12
|
+
flame: "3",
|
|
13
|
+
hot: "3",
|
|
14
|
+
confetti: "4",
|
|
15
|
+
confecti: "4",
|
|
16
|
+
party: "4",
|
|
17
|
+
celebrate: "4",
|
|
18
|
+
celebration: "4",
|
|
19
|
+
sparkles: "5",
|
|
20
|
+
sparkle: "5",
|
|
21
|
+
star: "5",
|
|
22
|
+
stars: "5",
|
|
23
|
+
magic: "5"
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const EFFECT_PROPERTIES = [
|
|
27
|
+
"textEffect",
|
|
28
|
+
"text_effect",
|
|
29
|
+
"text_effect_id",
|
|
30
|
+
"textEffectId",
|
|
31
|
+
"textEffectID",
|
|
32
|
+
"effect",
|
|
33
|
+
"effect_id",
|
|
34
|
+
"effectID",
|
|
35
|
+
"text_effect_name",
|
|
36
|
+
"effectName",
|
|
37
|
+
"effect_name"
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
function toTextEffectId(effect) {
|
|
41
|
+
if (effect === null || effect === undefined) return null;
|
|
42
|
+
if (typeof effect === "number" && Number.isFinite(effect)) return String(effect);
|
|
43
|
+
if (typeof effect !== "string") return null;
|
|
44
|
+
|
|
45
|
+
const value = effect.trim();
|
|
46
|
+
if (!value) return null;
|
|
47
|
+
return TEXT_EFFECTS[value.toLowerCase()] || value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getTextEffectId(message) {
|
|
51
|
+
if (!message || typeof message !== "object") return null;
|
|
52
|
+
for (const property of EFFECT_PROPERTIES) {
|
|
53
|
+
if (message[property] !== undefined && message[property] !== null) {
|
|
54
|
+
const effectID = toTextEffectId(message[property]);
|
|
55
|
+
if (effectID) return effectID;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const rawIDs = Array.isArray(message.text_effect_ids)
|
|
60
|
+
? message.text_effect_ids
|
|
61
|
+
: [message.text_effect_ids];
|
|
62
|
+
for (const rawID of rawIDs) {
|
|
63
|
+
const effectID = toTextEffectId(rawID);
|
|
64
|
+
if (effectID) return effectID;
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getTextEffectIds(message) {
|
|
70
|
+
if (!message || typeof message !== "object") return [];
|
|
71
|
+
const rawIDs = Array.isArray(message.text_effect_ids)
|
|
72
|
+
? message.text_effect_ids
|
|
73
|
+
: [message.text_effect_ids];
|
|
74
|
+
return rawIDs
|
|
75
|
+
.map(toTextEffectId)
|
|
76
|
+
.filter(Boolean);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
EFFECT_PROPERTIES,
|
|
81
|
+
TEXT_EFFECTS,
|
|
82
|
+
getTextEffectId,
|
|
83
|
+
getTextEffectIds,
|
|
84
|
+
toTextEffectId
|
|
85
|
+
};
|