@dongdev/fca-unofficial 2.0.6 → 2.0.8

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +6 -1
  2. package/func/checkUpdate.js +9 -9
  3. package/func/logger.js +40 -104
  4. package/module/login.js +4 -4
  5. package/module/loginHelper.js +3 -3
  6. package/module/options.js +5 -9
  7. package/package.json +6 -6
  8. package/src/api/action/addExternalModule.js +5 -5
  9. package/src/api/action/changeAvatar.js +11 -10
  10. package/src/api/action/changeBio.js +7 -8
  11. package/src/api/action/getCurrentUserID.js +1 -1
  12. package/src/api/action/handleFriendRequest.js +5 -5
  13. package/src/api/action/logout.js +9 -8
  14. package/src/api/action/refreshFb_dtsg.js +17 -12
  15. package/src/api/action/setPostReaction.js +10 -11
  16. package/src/api/action/unfriend.js +3 -4
  17. package/src/api/http/httpGet.js +7 -8
  18. package/src/api/http/httpPost.js +7 -8
  19. package/src/api/http/postFormData.js +6 -5
  20. package/src/api/messaging/addUserToGroup.js +0 -1
  21. package/src/api/messaging/changeAdminStatus.js +108 -89
  22. package/src/api/messaging/changeArchivedStatus.js +6 -6
  23. package/src/api/messaging/changeBlockedStatus.js +3 -4
  24. package/src/api/messaging/changeGroupImage.js +72 -117
  25. package/src/api/messaging/changeNickname.js +59 -48
  26. package/src/api/messaging/changeThreadColor.js +61 -47
  27. package/src/api/messaging/changeThreadEmoji.js +106 -0
  28. package/src/api/messaging/createNewGroup.js +5 -5
  29. package/src/api/messaging/createPoll.js +36 -63
  30. package/src/api/messaging/deleteMessage.js +4 -4
  31. package/src/api/messaging/deleteThread.js +4 -4
  32. package/src/api/messaging/forwardAttachment.js +38 -47
  33. package/src/api/messaging/getFriendsList.js +5 -6
  34. package/src/api/messaging/getMessage.js +4 -9
  35. package/src/api/messaging/handleMessageRequest.js +5 -5
  36. package/src/api/messaging/markAsDelivered.js +5 -5
  37. package/src/api/messaging/markAsRead.js +7 -7
  38. package/src/api/messaging/markAsReadAll.js +3 -4
  39. package/src/api/messaging/markAsSeen.js +7 -7
  40. package/src/api/messaging/muteThread.js +3 -4
  41. package/src/api/messaging/removeUserFromGroup.js +82 -56
  42. package/src/api/messaging/resolvePhotoUrl.js +2 -3
  43. package/src/api/messaging/searchForThread.js +2 -3
  44. package/src/api/messaging/sendMessage.js +171 -101
  45. package/src/api/messaging/sendMessageMqtt.js +14 -12
  46. package/src/api/messaging/sendTypingIndicator.js +11 -11
  47. package/src/api/messaging/setMessageReaction.js +68 -82
  48. package/src/api/messaging/setTitle.js +77 -48
  49. package/src/api/messaging/shareContact.js +2 -4
  50. package/src/api/messaging/threadColors.js +0 -3
  51. package/src/api/messaging/unsendMessage.js +74 -37
  52. package/src/api/messaging/uploadAttachment.js +11 -9
  53. package/src/api/socket/core/connectMqtt.js +180 -0
  54. package/src/api/socket/core/getSeqID.js +25 -0
  55. package/src/api/socket/core/getTaskResponseData.js +22 -0
  56. package/src/api/socket/core/markDelivery.js +12 -0
  57. package/src/api/socket/core/parseDelta.js +351 -0
  58. package/src/api/socket/detail/buildStream.js +176 -68
  59. package/src/api/socket/detail/constants.js +24 -0
  60. package/src/api/socket/listenMqtt.js +80 -1005
  61. package/src/api/{messaging → threads}/getThreadHistory.js +5 -22
  62. package/src/api/threads/getThreadInfo.js +35 -248
  63. package/src/api/threads/getThreadList.js +20 -20
  64. package/src/api/threads/getThreadPictures.js +3 -4
  65. package/src/api/users/getUserID.js +5 -6
  66. package/src/api/users/getUserInfo.js +305 -73
  67. package/src/api/users/getUserInfoV2.js +134 -0
  68. package/src/database/models/user.js +32 -0
  69. package/src/database/userData.js +89 -0
  70. package/src/utils/constants.js +12 -2
  71. package/src/utils/format.js +1051 -0
  72. package/src/utils/request.js +75 -7
  73. package/src/api/threads/changeThreadEmoji.js +0 -55
  74. package/src/utils/index.js +0 -1497
@@ -1,88 +1,320 @@
1
1
  "use strict";
2
- const { parseAndCheckLogin } = require("../../utils/client");
3
- var log = require("npmlog");
4
- module.exports = function(defaultFuncs, api, ctx) {
5
- function formatData(data) {
6
- const retObj = {};
7
- for (const actor of data.messaging_actors || []) {
8
- retObj[actor.id] = {
9
- name: actor.name,
10
- firstName: actor.short_name || null,
11
- vanity: actor.username || null,
12
- thumbSrc: actor.big_image_src?.uri || null,
13
- profileUrl: actor.url || null,
14
- gender: actor.gender || null,
15
- type: actor.__typename || null,
16
- isFriend: actor.is_viewer_friend || false,
17
- isMessengerUser: actor.is_messenger_user || false,
18
- isMessageBlockedByViewer: actor.is_message_blocked_by_viewer || false,
19
- workInfo: actor.work_info || null,
20
- messengerStatus: actor.messenger_account_status_category || null
21
- };
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const log = require("npmlog");
6
+ const logger = require("../../../func/logger.js");
7
+ const { parseAndCheckLogin } = require("../../utils/client.js");
8
+
9
+ const DOC_PRIMARY = "5009315269112105";
10
+ const BATCH_PRIMARY = "MessengerParticipantsFetcher";
11
+ const DOC_V2 = "24418640587785718";
12
+ const FRIENDLY_V2 = "CometHovercardQueryRendererQuery";
13
+ const CALLER_V2 = "RelayModern";
14
+
15
+ function toJSONMaybe(s) {
16
+ if (!s) return null;
17
+ if (typeof s === "string") {
18
+ const t = s.trim().replace(/^for\s*\(\s*;\s*;\s*\)\s*;/, "");
19
+ try { return JSON.parse(t); } catch { return null; }
20
+ }
21
+ return s;
22
+ }
23
+
24
+ function usernameFromUrl(raw) {
25
+ if (!raw) return null;
26
+ try {
27
+ const u = new URL(raw);
28
+ if (/^www\.facebook\.com$/i.test(u.hostname)) {
29
+ const seg = u.pathname.replace(/^\//, "").replace(/\/$/, "");
30
+ if (seg && !/^profile\.php$/i.test(seg) && !seg.includes("/")) return seg;
22
31
  }
23
- return retObj;
32
+ } catch { }
33
+ return null;
34
+ }
35
+
36
+ function pickMeta(u) {
37
+ let friendshipStatus = null;
38
+ let gender = null;
39
+ let shortName = u?.short_name || null;
40
+ const pa = Array.isArray(u?.primaryActions) ? u.primaryActions : [];
41
+ const sa = Array.isArray(u?.secondaryActions) ? u.secondaryActions : [];
42
+ const aFriend = pa.find(x => x?.profile_action_type === "FRIEND");
43
+ if (aFriend?.client_handler?.profile_action?.restrictable_profile_owner) {
44
+ const p = aFriend.client_handler.profile_action.restrictable_profile_owner;
45
+ friendshipStatus = p?.friendship_status || null;
46
+ gender = p?.gender || gender;
47
+ shortName = p?.short_name || shortName;
24
48
  }
25
- return function getUserInfoGraphQL(id, callback) {
26
- let resolveFunc, rejectFunc;
27
- const returnPromise = new Promise((resolve, reject) => {
28
- resolveFunc = resolve;
29
- rejectFunc = reject;
30
- });
31
- if (typeof callback !== "function") {
32
- callback = (err, data) => {
33
- if (err) return rejectFunc(err);
34
- resolveFunc(data);
35
- };
49
+ if (!gender || !shortName) {
50
+ const aBlock = sa.find(x => x?.profile_action_type === "BLOCK");
51
+ const p2 = aBlock?.client_handler?.profile_action?.profile_owner;
52
+ if (p2) {
53
+ gender = p2.gender || gender;
54
+ shortName = p2.short_name || shortName;
36
55
  }
37
- const ids = Array.isArray(id) ? id : [id];
38
- var form = {
56
+ }
57
+ return { friendshipStatus, gender, shortName };
58
+ }
59
+
60
+ function normalizeV2User(u) {
61
+ if (!u) return null;
62
+ const vanity = usernameFromUrl(u.profile_url || u.url);
63
+ const meta = pickMeta(u);
64
+ return {
65
+ id: u.id || null,
66
+ name: u.name || null,
67
+ firstName: meta.shortName || null,
68
+ vanity: vanity || u.username_for_profile || null,
69
+ thumbSrc: u.profile_picture?.uri || null,
70
+ profileUrl: u.profile_url || u.url || null,
71
+ gender: meta.gender || null,
72
+ type: "User",
73
+ isFriend: meta.friendshipStatus === "ARE_FRIENDS",
74
+ isMessengerUser: null,
75
+ isMessageBlockedByViewer: false,
76
+ workInfo: null,
77
+ messengerStatus: null
78
+ };
79
+ }
80
+
81
+ function normalizePrimaryActor(a) {
82
+ if (!a) return null;
83
+ return {
84
+ id: a.id || null,
85
+ name: a.name || null,
86
+ firstName: a.short_name || null,
87
+ vanity: a.username || null,
88
+ thumbSrc: a.big_image_src?.uri || null,
89
+ profileUrl: a.url || null,
90
+ gender: a.gender || null,
91
+ type: a.__typename || null,
92
+ isFriend: !!a.is_viewer_friend,
93
+ isMessengerUser: !!a.is_messenger_user,
94
+ isMessageBlockedByViewer: !!a.is_message_blocked_by_viewer,
95
+ workInfo: a.work_info || null,
96
+ messengerStatus: a.messenger_account_status_category || null
97
+ };
98
+ }
99
+
100
+ function mergeUserEntry(a, b) {
101
+ if (!a && !b) return null;
102
+ const x = a || {};
103
+ const y = b || {};
104
+ return {
105
+ id: x.id || y.id || null,
106
+ name: x.name || y.name || null,
107
+ firstName: x.firstName || y.firstName || null,
108
+ vanity: x.vanity || y.vanity || null,
109
+ thumbSrc: x.thumbSrc || y.thumbSrc || null,
110
+ profileUrl: x.profileUrl || y.profileUrl || null,
111
+ gender: x.gender || y.gender || null,
112
+ type: x.type || y.type || null,
113
+ isFriend: typeof x.isFriend === "boolean" ? x.isFriend : (typeof y.isFriend === "boolean" ? y.isFriend : false),
114
+ isMessengerUser: typeof x.isMessengerUser === "boolean" ? x.isMessengerUser : (typeof y.isMessengerUser === "boolean" ? y.isMessengerUser : null),
115
+ isMessageBlockedByViewer: typeof x.isMessageBlockedByViewer === "boolean" ? x.isMessageBlockedByViewer : (typeof y.isMessageBlockedByViewer === "boolean" ? y.isMessageBlockedByViewer : false),
116
+ workInfo: x.workInfo || y.workInfo || null,
117
+ messengerStatus: x.messengerStatus || y.messengerStatus || null
118
+ };
119
+ }
120
+
121
+ const queue = [];
122
+ let isProcessingQueue = false;
123
+ const processingUsers = new Set();
124
+ const queuedUsers = new Set();
125
+ const cooldown = new Map();
126
+
127
+ module.exports = function (defaultFuncs, api, ctx) {
128
+ const dbFiles = fs.readdirSync(path.join(__dirname, "../../database")).filter(f => path.extname(f) === ".js").reduce((acc, file) => {
129
+ acc[path.basename(file, ".js")] = require(path.join(__dirname, "../../database", file))(api);
130
+ return acc;
131
+ }, {});
132
+ const { userData } = dbFiles;
133
+ const { create, get, update, getAll } = userData;
134
+
135
+ async function fetchPrimary(ids) {
136
+ const form = {
39
137
  queries: JSON.stringify({
40
138
  o0: {
41
- doc_id: "5009315269112105",
42
- query_params: {
43
- ids: ids
44
- }
139
+ doc_id: DOC_PRIMARY,
140
+ query_params: { ids }
45
141
  }
46
142
  }),
47
- batch_name: "MessengerParticipantsFetcher"
143
+ batch_name: BATCH_PRIMARY
48
144
  };
49
- defaultFuncs
50
- .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
51
- .then(parseAndCheckLogin(ctx, defaultFuncs))
52
- .then(function(resData) {
53
- if (!resData || resData.length === 0) {
54
- throw new Error("Empty response from server");
55
- }
56
- if (resData.error) {
57
- throw resData.error;
58
- }
59
- const response = resData[0];
60
- console.log(response);
61
- if (!response || !response.o0) {
62
- throw new Error("Invalid response format");
145
+ const resData = await defaultFuncs.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form).then(parseAndCheckLogin(ctx, defaultFuncs));
146
+ if (!resData || resData.length === 0) throw new Error("Empty response");
147
+ const first = resData[0];
148
+ if (!first || !first.o0) throw new Error("Invalid batch payload");
149
+ if (first.o0.errors && first.o0.errors.length) throw new Error(first.o0.errors[0].message || "GraphQL error");
150
+ const result = first.o0.data;
151
+ if (!result || !Array.isArray(result.messaging_actors)) return {};
152
+ const out = {};
153
+ for (const actor of result.messaging_actors) {
154
+ const n = normalizePrimaryActor(actor);
155
+ if (n?.id) out[n.id] = n;
156
+ }
157
+ return out;
158
+ }
159
+
160
+ async function fetchV2One(uid) {
161
+ const av = String(ctx?.userID || "");
162
+ const variablesObj = {
163
+ actionBarRenderLocation: "WWW_COMET_HOVERCARD",
164
+ context: "DEFAULT",
165
+ entityID: String(uid),
166
+ scale: 1,
167
+ __relay_internal__pv__WorkCometIsEmployeeGKProviderrelayprovider: false
168
+ };
169
+ const form = {
170
+ av,
171
+ fb_api_caller_class: CALLER_V2,
172
+ fb_api_req_friendly_name: FRIENDLY_V2,
173
+ server_timestamps: true,
174
+ doc_id: DOC_V2,
175
+ variables: JSON.stringify(variablesObj)
176
+ };
177
+ const raw = await defaultFuncs.post("https://www.facebook.com/api/graphql/", null, form).then(parseAndCheckLogin(ctx, defaultFuncs));
178
+ const parsed = toJSONMaybe(raw) ?? raw;
179
+ const root = Array.isArray(parsed) ? parsed[0] : parsed;
180
+ const user = root?.data?.node?.comet_hovercard_renderer?.user || null;
181
+ const n = normalizeV2User(user);
182
+ return n && n.id ? { [n.id]: n } : {};
183
+ }
184
+
185
+ async function upsertUser(id, entry) {
186
+ try {
187
+ const existing = await get(id);
188
+ if (existing) {
189
+ await update(id, { data: entry });
190
+ } else {
191
+ await create(id, { data: entry });
192
+ }
193
+ } catch (e) {
194
+ logger(`user upsert ${id} error: ${e?.message || e}`, "warn");
195
+ }
196
+ }
197
+
198
+ async function fetchAndPersist(ids, creating = false) {
199
+ const result = {};
200
+ try {
201
+ const primary = await fetchPrimary(ids);
202
+ for (const id of ids) result[id] = primary[id] || null;
203
+ } catch (e) {
204
+ logger(`primary fetch error: ${e?.message || e}`, "warn");
205
+ }
206
+ if (creating) {
207
+ const needFallback = ids.filter(id => !result[id]);
208
+ if (needFallback.length) {
209
+ const tasks = needFallback.map(id => fetchV2One(id).catch(() => ({})));
210
+ const r = await Promise.allSettled(tasks);
211
+ for (let i = 0; i < needFallback.length; i++) {
212
+ const id = needFallback[i];
213
+ const ok = r[i].status === "fulfilled" ? r[i].value : {};
214
+ const n = ok[id] || null;
215
+ result[id] = n || null;
63
216
  }
64
- if (response.o0.errors && response.o0.errors.length > 0) {
65
- throw new Error(response.o0.errors[0].message || "GraphQL error");
217
+ }
218
+ }
219
+ for (const id of ids) {
220
+ const merged = result[id] || null;
221
+ if (merged) await upsertUser(id, merged);
222
+ }
223
+ return result;
224
+ }
225
+
226
+ async function refreshAUser(id) {
227
+ try {
228
+ const out = await fetchAndPersist([id], false);
229
+ if (!out[id]) cooldown.set(id, Date.now() + 5 * 60 * 1000);
230
+ } catch (e) {
231
+ cooldown.set(id, Date.now() + 5 * 60 * 1000);
232
+ logger(`refresh user ${id} error: ${e?.message || e}`, "warn");
233
+ } finally {
234
+ queuedUsers.delete(id);
235
+ }
236
+ }
237
+
238
+ async function checkAndUpdateUsers() {
239
+ try {
240
+ const all = await getAll("userID");
241
+ const now = Date.now();
242
+ for (const row of all) {
243
+ const id = row.userID;
244
+ const cd = cooldown.get(id);
245
+ if (cd && now < cd) continue;
246
+ const lastUpdated = new Date(row.updatedAt).getTime();
247
+ if ((now - lastUpdated) / (1000 * 60) > 10 && !queuedUsers.has(id)) {
248
+ queuedUsers.add(id);
249
+ queue.push(() => refreshAUser(id));
66
250
  }
67
- const result = response.o0.data;
68
- if (
69
- !result ||
70
- !result.messaging_actors ||
71
- result.messaging_actors.length === 0
72
- ) {
73
- log.warn("getUserInfo", "No user data found for the provided ID(s)");
74
- return callback(null, {});
251
+ }
252
+ } catch (e) {
253
+ logger(`checkAndUpdateUsers error: ${e?.message || e}`, "error");
254
+ }
255
+ }
256
+
257
+ async function processQueue() {
258
+ if (isProcessingQueue) return;
259
+ isProcessingQueue = true;
260
+ while (queue.length > 0) {
261
+ const task = queue.shift();
262
+ try {
263
+ await task();
264
+ } catch (e) {
265
+ logger(`user queue error: ${e?.message || e}`, "error");
266
+ }
267
+ }
268
+ isProcessingQueue = false;
269
+ }
270
+
271
+ setInterval(() => {
272
+ checkAndUpdateUsers();
273
+ processQueue();
274
+ }, 10000);
275
+
276
+ return function getUserInfo(idsOrId, callback) {
277
+ let resolveFunc, rejectFunc;
278
+ const returnPromise = new Promise((resolve, reject) => { resolveFunc = resolve; rejectFunc = reject; });
279
+ if (typeof callback !== "function") {
280
+ callback = (err, data) => { if (err) return rejectFunc(err); resolveFunc(data); };
281
+ }
282
+ const ids = Array.isArray(idsOrId) ? idsOrId.map(v => String(v)) : [String(idsOrId)];
283
+ Promise.all(ids.map(id => get(id).catch(() => null))).then(async cachedRows => {
284
+ const ret = {};
285
+ const needCreate = [];
286
+ for (let i = 0; i < ids.length; i++) {
287
+ const id = ids[i];
288
+ const row = cachedRows[i];
289
+ if (row?.data && row.data.id) {
290
+ ret[id] = row.data;
291
+ } else {
292
+ needCreate.push(id);
75
293
  }
76
- const formattedData = formatData(result);
77
- return callback(null, formattedData);
78
- })
79
- .catch(err => {
80
- log.error(
81
- "getUserInfoGraphQL",
82
- "Error: " + (err.message || "Unknown error occurred")
83
- );
84
- callback(err);
85
- });
294
+ }
295
+ if (needCreate.length) {
296
+ const fetched = await fetchAndPersist(needCreate, true);
297
+ for (const id of needCreate) ret[id] = fetched[id] || {
298
+ id,
299
+ name: null,
300
+ firstName: null,
301
+ vanity: null,
302
+ thumbSrc: null,
303
+ profileUrl: null,
304
+ gender: null,
305
+ type: null,
306
+ isFriend: false,
307
+ isMessengerUser: null,
308
+ isMessageBlockedByViewer: false,
309
+ workInfo: null,
310
+ messengerStatus: null
311
+ };
312
+ }
313
+ return callback(null, ret);
314
+ }).catch(err => {
315
+ log.error("getUserInfo", "Error: " + (err?.message || "Unknown"));
316
+ callback(err);
317
+ });
86
318
  return returnPromise;
87
319
  };
88
320
  };
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+
3
+ const { parseAndCheckLogin } = require("../../utils/client.js");
4
+ const logger = require("../../../func/logger.js");
5
+ const DEFAULT_DOC_ID = "24418640587785718";
6
+ const DEFAULT_FRIENDLY_NAME = "CometHovercardQueryRendererQuery";
7
+ const DEFAULT_CALLER_CLASS = "RelayModern";
8
+
9
+ function toJSONMaybe(s) {
10
+ if (!s) return null;
11
+ if (typeof s === "string") {
12
+ const t = s.trim().replace(/^for\s*\(\s*;\s*;\s*\)\s*;/, "");
13
+ try { return JSON.parse(t); } catch { return null; }
14
+ }
15
+ return s;
16
+ }
17
+
18
+ function usernameFromUrl(raw) {
19
+ if (!raw) return null;
20
+ try {
21
+ const u = new URL(raw);
22
+ if (/^www\.facebook\.com$/i.test(u.hostname)) {
23
+ const seg = u.pathname.replace(/^\//, "").replace(/\/$/, "");
24
+ if (seg && !/^profile\.php$/i.test(seg) && !seg.includes("/")) return seg;
25
+ }
26
+ } catch { }
27
+ return null;
28
+ }
29
+
30
+ function pickMeta(u) {
31
+ let friendshipStatus = null;
32
+ let gender = null;
33
+ let shortName = u?.short_name || null;
34
+ const pa = Array.isArray(u?.primaryActions) ? u.primaryActions : [];
35
+ const sa = Array.isArray(u?.secondaryActions) ? u.secondaryActions : [];
36
+ const aFriend = pa.find(x => x?.profile_action_type === "FRIEND");
37
+ if (aFriend?.client_handler?.profile_action?.restrictable_profile_owner) {
38
+ const p = aFriend.client_handler.profile_action.restrictable_profile_owner;
39
+ friendshipStatus = p?.friendship_status || null;
40
+ gender = p?.gender || gender;
41
+ shortName = p?.short_name || shortName;
42
+ }
43
+ if (!gender || !shortName) {
44
+ const aBlock = sa.find(x => x?.profile_action_type === "BLOCK");
45
+ const p2 = aBlock?.client_handler?.profile_action?.profile_owner;
46
+ if (p2) {
47
+ gender = p2.gender || gender;
48
+ shortName = p2.short_name || shortName;
49
+ }
50
+ }
51
+ return { friendshipStatus, gender, shortName };
52
+ }
53
+
54
+ function normalizeUser(u) {
55
+ if (!u) return null;
56
+ const vanity = usernameFromUrl(u.profile_url || u.url);
57
+ const meta = pickMeta(u);
58
+ return {
59
+ id: u.id || null,
60
+ name: u.name || null,
61
+ username: vanity || u.username_for_profile || null,
62
+ profileUrl: u.profile_url || u.url || null,
63
+ url: u.url || null,
64
+ isVerified: !!u.is_verified,
65
+ isMemorialized: !!u.is_visibly_memorialized,
66
+ avatar: u.profile_picture?.uri || null,
67
+ shortName: meta.shortName || null,
68
+ gender: meta.gender || null,
69
+ friendshipStatus: meta.friendshipStatus || null
70
+ };
71
+ }
72
+
73
+ function toRetObjEntry(nu) {
74
+ return {
75
+ name: nu?.name || null,
76
+ firstName: nu?.shortName || null,
77
+ vanity: nu?.username || null,
78
+ thumbSrc: nu?.avatar || null,
79
+ profileUrl: nu?.profileUrl || null,
80
+ gender: nu?.gender || null,
81
+ type: "User",
82
+ isFriend: nu?.friendshipStatus === "ARE_FRIENDS",
83
+ isMessengerUser: null,
84
+ isMessageBlockedByViewer: false,
85
+ workInfo: null,
86
+ messengerStatus: null
87
+ };
88
+ }
89
+
90
+ module.exports = function (defaultFuncs, api, ctx) {
91
+ async function fetchOne(uid) {
92
+ const av = String(ctx?.userID || "");
93
+ const variablesObj = {
94
+ actionBarRenderLocation: "WWW_COMET_HOVERCARD",
95
+ context: "DEFAULT",
96
+ entityID: String(uid),
97
+ scale: 1,
98
+ __relay_internal__pv__WorkCometIsEmployeeGKProviderrelayprovider: false
99
+ };
100
+ const form = {
101
+ av,
102
+ fb_api_caller_class: DEFAULT_CALLER_CLASS,
103
+ fb_api_req_friendly_name: DEFAULT_FRIENDLY_NAME,
104
+ server_timestamps: true,
105
+ doc_id: DEFAULT_DOC_ID,
106
+ variables: JSON.stringify(variablesObj)
107
+ };
108
+ const raw = await defaultFuncs.post("https://www.facebook.com/api/graphql/", null, form).then(parseAndCheckLogin(ctx, defaultFuncs));
109
+ const parsed = toJSONMaybe(raw) ?? raw;
110
+ const root = Array.isArray(parsed) ? parsed[0] : parsed;
111
+ const user = root?.data?.node?.comet_hovercard_renderer?.user || null;
112
+ return normalizeUser(user);
113
+ }
114
+
115
+ return function getUserInfoV2(idOrList, callback) {
116
+ let resolveFunc, rejectFunc;
117
+ const returnPromise = new Promise((resolve, reject) => { resolveFunc = resolve; rejectFunc = reject; });
118
+ if (typeof callback !== "function") {
119
+ callback = (err, data) => { if (err) return rejectFunc(err); resolveFunc(data); };
120
+ }
121
+ const ids = Array.isArray(idOrList) ? idOrList.map(v => String(v)) : [String(idOrList)];
122
+ Promise.allSettled(ids.map(fetchOne))
123
+ .then(results => {
124
+ const retObj = {};
125
+ for (let i = 0; i < ids.length; i++) {
126
+ const nu = results[i].status === "fulfilled" ? results[i].value : null;
127
+ retObj[ids[i]] = toRetObjEntry(nu);
128
+ }
129
+ return callback(null, retObj);
130
+ })
131
+ .catch(err => { logger("getUserInfoV2" + err, "error"); callback(err); });
132
+ return returnPromise;
133
+ };
134
+ };
@@ -0,0 +1,32 @@
1
+ module.exports = function (sequelize) {
2
+ const { Model, DataTypes } = require("sequelize");
3
+
4
+ class User extends Model { }
5
+
6
+ User.init(
7
+ {
8
+ num: {
9
+ type: DataTypes.INTEGER,
10
+ allowNull: false,
11
+ autoIncrement: true,
12
+ primaryKey: true
13
+ },
14
+ userID: {
15
+ type: DataTypes.STRING,
16
+ allowNull: false,
17
+ unique: true
18
+ },
19
+ data: {
20
+ type: DataTypes.JSONB,
21
+ allowNull: true
22
+ }
23
+ },
24
+ {
25
+ sequelize,
26
+ modelName: "User",
27
+ timestamps: true
28
+ }
29
+ );
30
+
31
+ return User;
32
+ };
@@ -0,0 +1,89 @@
1
+ const { User } = require("./models");
2
+
3
+ const validateUserID = userID => {
4
+ if (typeof userID !== "string" && typeof userID !== "number") {
5
+ throw new Error("Invalid userID: must be a string or number.");
6
+ }
7
+ return String(userID);
8
+ };
9
+ const validateData = data => {
10
+ if (!data || typeof data !== "object" || Array.isArray(data)) {
11
+ throw new Error("Invalid data: must be a non-empty object.");
12
+ }
13
+ };
14
+
15
+ module.exports = function (bot) {
16
+ return {
17
+ async create(userID, data) {
18
+ try {
19
+ userID = validateUserID(userID);
20
+ validateData(data);
21
+ let user = await User.findOne({ where: { userID } });
22
+ if (user) return { user: user.get(), created: false };
23
+ const payload = Object.prototype.hasOwnProperty.call(data, "data") ? data : { data };
24
+ user = await User.create({ userID, ...payload });
25
+ return { user: user.get(), created: true };
26
+ } catch (error) {
27
+ throw new Error(`Failed to create user: ${error.message}`);
28
+ }
29
+ },
30
+
31
+ async get(userID) {
32
+ try {
33
+ userID = validateUserID(userID);
34
+ const user = await User.findOne({ where: { userID } });
35
+ return user ? user.get() : null;
36
+ } catch (error) {
37
+ throw new Error(`Failed to get user: ${error.message}`);
38
+ }
39
+ },
40
+
41
+ async update(userID, data) {
42
+ try {
43
+ userID = validateUserID(userID);
44
+ validateData(data);
45
+ const payload = Object.prototype.hasOwnProperty.call(data, "data") ? data : { data };
46
+ const user = await User.findOne({ where: { userID } });
47
+ if (user) {
48
+ await user.update(payload);
49
+ return { user: user.get(), created: false };
50
+ } else {
51
+ const newUser = await User.create({ userID, ...payload });
52
+ return { user: newUser.get(), created: true };
53
+ }
54
+ } catch (error) {
55
+ throw new Error(`Failed to update user: ${error.message}`);
56
+ }
57
+ },
58
+
59
+ async del(userID) {
60
+ try {
61
+ if (!userID) throw new Error("userID is required and cannot be undefined");
62
+ userID = validateUserID(userID);
63
+ const result = await User.destroy({ where: { userID } });
64
+ if (result === 0) throw new Error("No user found with the specified userID");
65
+ return result;
66
+ } catch (error) {
67
+ throw new Error(`Failed to delete user: ${error.message}`);
68
+ }
69
+ },
70
+
71
+ async delAll() {
72
+ try {
73
+ return await User.destroy({ where: {} });
74
+ } catch (error) {
75
+ throw new Error(`Failed to delete all users: ${error.message}`);
76
+ }
77
+ },
78
+
79
+ async getAll(keys = null) {
80
+ try {
81
+ const attributes = typeof keys === "string" ? [keys] : Array.isArray(keys) ? keys : undefined;
82
+ const users = await User.findAll({ attributes });
83
+ return users.map(u => u.get());
84
+ } catch (error) {
85
+ throw new Error(`Failed to get all users: ${error.message}`);
86
+ }
87
+ }
88
+ };
89
+ };
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
-
2
+ const { getType } = require("./format");
3
+ const stream = require("stream");
3
4
  function getFrom(html, a, b) {
4
5
  const i = html.indexOf(a);
5
6
  if (i < 0) return;
@@ -7,7 +8,16 @@ function getFrom(html, a, b) {
7
8
  const j = html.indexOf(b, start);
8
9
  return j < 0 ? undefined : html.slice(start, j);
9
10
  }
11
+ function isReadableStream(obj) {
12
+ return (
13
+ obj instanceof stream.Stream &&
14
+ (getType(obj._read) === "Function" ||
15
+ getType(obj._read) === "AsyncFunction") &&
16
+ getType(obj._readableState) === "Object"
17
+ );
18
+ }
10
19
 
11
20
  module.exports = {
12
- getFrom
21
+ getFrom,
22
+ isReadableStream
13
23
  };