@cexy/hoonfca 1.0.4 → 1.0.5

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 (135) hide show
  1. package/CHANGELOG.md +181 -0
  2. package/DOCS.md +2636 -0
  3. package/LICENSE-MIT +21 -21
  4. package/README.md +632 -0
  5. package/func/checkUpdate.js +222 -0
  6. package/func/logger.js +48 -0
  7. package/index.d.ts +731 -605
  8. package/index.js +6 -774
  9. package/module/config.js +34 -0
  10. package/module/login.js +136 -0
  11. package/module/loginHelper.js +1034 -0
  12. package/module/options.js +45 -0
  13. package/package.json +54 -19
  14. package/src/api/action/addExternalModule.js +25 -0
  15. package/src/api/action/changeAvatar.js +137 -0
  16. package/src/{changeBio.js → api/action/changeBio.js} +75 -76
  17. package/src/api/action/enableAutoSaveAppState.js +73 -0
  18. package/src/{getCurrentUserID.js → api/action/getCurrentUserID.js} +7 -7
  19. package/src/{handleFriendRequest.js → api/action/handleFriendRequest.js} +57 -57
  20. package/src/api/action/logout.js +76 -0
  21. package/src/api/action/refreshFb_dtsg.js +48 -0
  22. package/src/api/action/setPostReaction.js +106 -0
  23. package/src/api/action/unfriend.js +54 -0
  24. package/src/api/http/httpGet.js +46 -0
  25. package/src/api/http/httpPost.js +52 -0
  26. package/src/api/http/postFormData.js +47 -0
  27. package/src/api/messaging/addUserToGroup.js +68 -0
  28. package/src/api/messaging/changeAdminStatus.js +122 -0
  29. package/src/api/messaging/changeArchivedStatus.js +55 -0
  30. package/src/api/messaging/changeBlockedStatus.js +48 -0
  31. package/src/api/messaging/changeGroupImage.js +90 -0
  32. package/src/api/messaging/changeNickname.js +70 -0
  33. package/src/api/messaging/changeThreadColor.js +79 -0
  34. package/src/api/messaging/changeThreadEmoji.js +106 -0
  35. package/src/{createNewGroup.js → api/messaging/createNewGroup.js} +88 -88
  36. package/src/api/messaging/createPoll.js +43 -0
  37. package/src/api/messaging/createThemeAI.js +98 -0
  38. package/src/{deleteMessage.js → api/messaging/deleteMessage.js} +56 -56
  39. package/src/{deleteThread.js → api/messaging/deleteThread.js} +56 -56
  40. package/src/api/messaging/editMessage.js +68 -0
  41. package/src/api/messaging/forwardAttachment.js +51 -0
  42. package/src/{getEmojiUrl.js → api/messaging/getEmojiUrl.js} +8 -8
  43. package/src/{getFriendsList.js → api/messaging/getFriendsList.js} +82 -84
  44. package/src/api/messaging/getMessage.js +829 -0
  45. package/src/api/messaging/getThemePictures.js +62 -0
  46. package/src/api/messaging/handleMessageRequest.js +65 -0
  47. package/src/{markAsDelivered.js → api/messaging/markAsDelivered.js} +57 -58
  48. package/src/{markAsRead.js → api/messaging/markAsRead.js} +88 -88
  49. package/src/{markAsReadAll.js → api/messaging/markAsReadAll.js} +49 -50
  50. package/src/api/messaging/markAsSeen.js +61 -0
  51. package/src/{muteThread.js → api/messaging/muteThread.js} +50 -52
  52. package/src/api/messaging/removeUserFromGroup.js +106 -0
  53. package/src/{resolvePhotoUrl.js → api/messaging/resolvePhotoUrl.js} +43 -45
  54. package/src/api/messaging/scheduler.js +264 -0
  55. package/src/{searchForThread.js → api/messaging/searchForThread.js} +52 -53
  56. package/src/api/messaging/sendMessage.js +272 -0
  57. package/src/api/messaging/sendTypingIndicator.js +67 -0
  58. package/src/api/messaging/setMessageReaction.js +76 -0
  59. package/src/api/messaging/setTitle.js +119 -0
  60. package/src/{shareContact.js → api/messaging/shareContact.js} +49 -53
  61. package/src/api/messaging/threadColors.js +128 -0
  62. package/src/api/messaging/unsendMessage.js +81 -0
  63. package/src/api/messaging/uploadAttachment.js +94 -0
  64. package/src/api/socket/core/connectMqtt.js +255 -0
  65. package/src/api/socket/core/emitAuth.js +106 -0
  66. package/src/api/socket/core/getSeqID.js +40 -0
  67. package/src/api/socket/core/getTaskResponseData.js +22 -0
  68. package/src/api/socket/core/markDelivery.js +12 -0
  69. package/src/api/socket/core/parseDelta.js +391 -0
  70. package/src/api/socket/detail/buildStream.js +208 -0
  71. package/src/api/socket/detail/constants.js +24 -0
  72. package/src/api/socket/listenMqtt.js +364 -0
  73. package/src/api/socket/middleware/index.js +216 -0
  74. package/src/{getThreadHistory.js → api/threads/getThreadHistory.js} +664 -647
  75. package/src/api/threads/getThreadInfo.js +438 -0
  76. package/src/{getThreadList.js → api/threads/getThreadList.js} +293 -292
  77. package/src/{getThreadPictures.js → api/threads/getThreadPictures.js} +78 -79
  78. package/src/{getUserID.js → api/users/getUserID.js} +65 -66
  79. package/src/api/users/getUserInfo.js +327 -0
  80. package/src/api/users/getUserInfoV2.js +134 -0
  81. package/src/core/sendReqMqtt.js +96 -0
  82. package/src/database/models/index.js +87 -0
  83. package/src/database/models/thread.js +45 -0
  84. package/src/database/models/user.js +46 -0
  85. package/src/database/threadData.js +98 -0
  86. package/src/database/userData.js +89 -0
  87. package/src/utils/client.js +320 -0
  88. package/src/utils/constants.js +23 -0
  89. package/{utils.js → src/utils/format.js} +1115 -1447
  90. package/src/utils/headers.js +115 -0
  91. package/src/utils/request.js +305 -0
  92. package/.travis.yml +0 -6
  93. package/src/addExternalModule.js +0 -23
  94. package/src/addUserToGroup.js +0 -113
  95. package/src/changeAdminStatus.js +0 -95
  96. package/src/changeApprovalMode.js +0 -79
  97. package/src/changeArchivedStatus.js +0 -55
  98. package/src/changeBlockedStatus.js +0 -47
  99. package/src/changeBlockedStatusMqtt.js +0 -86
  100. package/src/changeGroupImage.js +0 -133
  101. package/src/changeNickname.js +0 -59
  102. package/src/changeThreadColor.js +0 -71
  103. package/src/changeThreadEmoji.js +0 -55
  104. package/src/createPoll.js +0 -130
  105. package/src/editMessage.js +0 -63
  106. package/src/forwardAttachment.js +0 -60
  107. package/src/forwardMessage.js +0 -63
  108. package/src/getGroupsList.js +0 -83
  109. package/src/getMarketplace.js +0 -84
  110. package/src/getNotifications.js +0 -86
  111. package/src/getPagesList.js +0 -83
  112. package/src/getStories.js +0 -88
  113. package/src/getThreadInfo.js +0 -222
  114. package/src/getUserInfo.js +0 -128
  115. package/src/handleMessageRequest.js +0 -65
  116. package/src/httpGet.js +0 -58
  117. package/src/httpPost.js +0 -58
  118. package/src/listenMqtt.js +0 -1018
  119. package/src/logout.js +0 -75
  120. package/src/markAsSeen.js +0 -61
  121. package/src/pinMessage.js +0 -61
  122. package/src/removeUserFromGroup.js +0 -79
  123. package/src/searchUsers.js +0 -89
  124. package/src/sendComment.js +0 -161
  125. package/src/sendMessage.js +0 -457
  126. package/src/sendTypingIndicator.js +0 -179
  127. package/src/setBio.js +0 -86
  128. package/src/setMessageReaction.js +0 -186
  129. package/src/setPostReaction.js +0 -71
  130. package/src/setTheme.js +0 -313
  131. package/src/setTitle.js +0 -90
  132. package/src/shareLink.js +0 -62
  133. package/src/threadColors.js +0 -57
  134. package/src/unfriend.js +0 -53
  135. package/src/unsendMessage.js +0 -104
@@ -1,79 +1,78 @@
1
- "use strict";
2
-
3
- var utils = require("../utils");
4
- var log = require("npmlog");
5
-
6
- module.exports = function (defaultFuncs, api, ctx) {
7
- return function getThreadPictures(threadID, offset, limit, callback) {
8
- var resolveFunc = function () {};
9
- var rejectFunc = function () {};
10
- var returnPromise = new Promise(function (resolve, reject) {
11
- resolveFunc = resolve;
12
- rejectFunc = reject;
13
- });
14
-
15
- if (!callback) {
16
- callback = function (err, friendList) {
17
- if (err) {
18
- return rejectFunc(err);
19
- }
20
- resolveFunc(friendList);
21
- };
22
- }
23
-
24
- var form = {
25
- thread_id: threadID,
26
- offset: offset,
27
- limit: limit,
28
- };
29
-
30
- defaultFuncs
31
- .post(
32
- "https://www.facebook.com/ajax/messaging/attachments/sharedphotos.php",
33
- ctx.jar,
34
- form,
35
- )
36
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
37
- .then(function (resData) {
38
- if (resData.error) {
39
- throw resData;
40
- }
41
- return Promise.all(
42
- resData.payload.imagesData.map(function (image) {
43
- form = {
44
- thread_id: threadID,
45
- image_id: image.fbid,
46
- };
47
- return defaultFuncs
48
- .post(
49
- "https://www.facebook.com/ajax/messaging/attachments/sharedphotos.php",
50
- ctx.jar,
51
- form,
52
- )
53
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
54
- .then(function (resData) {
55
- if (resData.error) {
56
- throw resData;
57
- }
58
- // the response is pretty messy
59
- var queryThreadID =
60
- resData.jsmods.require[0][3][1].query_metadata.query_path[0]
61
- .message_thread;
62
- var imageData =
63
- resData.jsmods.require[0][3][1].query_results[queryThreadID]
64
- .message_images.edges[0].node.image2;
65
- return imageData;
66
- });
67
- }),
68
- );
69
- })
70
- .then(function (resData) {
71
- callback(null, resData);
72
- })
73
- .catch(function (err) {
74
- log.error("Error in getThreadPictures", err);
75
- callback(err);
76
- });
77
- return returnPromise;
78
- };
79
- };
1
+ "use strict";
2
+
3
+ const log = require("npmlog");
4
+ const { parseAndCheckLogin } = require("../../utils/client");
5
+ module.exports = function(defaultFuncs, api, ctx) {
6
+ return function getThreadPictures(threadID, offset, limit, callback) {
7
+ let resolveFunc = function() {};
8
+ let rejectFunc = function() {};
9
+ const returnPromise = new Promise(function(resolve, reject) {
10
+ resolveFunc = resolve;
11
+ rejectFunc = reject;
12
+ });
13
+
14
+ if (!callback) {
15
+ callback = function(err, friendList) {
16
+ if (err) {
17
+ return rejectFunc(err);
18
+ }
19
+ resolveFunc(friendList);
20
+ };
21
+ }
22
+
23
+ let form = {
24
+ thread_id: threadID,
25
+ offset: offset,
26
+ limit: limit
27
+ };
28
+
29
+ defaultFuncs
30
+ .post(
31
+ "https://www.facebook.com/ajax/messaging/attachments/sharedphotos.php",
32
+ ctx.jar,
33
+ form
34
+ )
35
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
36
+ .then(function(resData) {
37
+ if (resData.error) {
38
+ throw resData;
39
+ }
40
+ return Promise.all(
41
+ resData.payload.imagesData.map(function(image) {
42
+ form = {
43
+ thread_id: threadID,
44
+ image_id: image.fbid
45
+ };
46
+ return defaultFuncs
47
+ .post(
48
+ "https://www.facebook.com/ajax/messaging/attachments/sharedphotos.php",
49
+ ctx.jar,
50
+ form
51
+ )
52
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
53
+ .then(function(resData) {
54
+ if (resData.error) {
55
+ throw resData;
56
+ }
57
+ // the response is pretty messy
58
+ const queryThreadID =
59
+ resData.jsmods.require[0][3][1].query_metadata.query_path[0]
60
+ .message_thread;
61
+ const imageData =
62
+ resData.jsmods.require[0][3][1].query_results[queryThreadID]
63
+ .message_images.edges[0].node.image2;
64
+ return imageData;
65
+ });
66
+ })
67
+ );
68
+ })
69
+ .then(function(resData) {
70
+ callback(null, resData);
71
+ })
72
+ .catch(function(err) {
73
+ log.error("Error in getThreadPictures", err);
74
+ callback(err);
75
+ });
76
+ return returnPromise;
77
+ };
78
+ };
@@ -1,66 +1,65 @@
1
- "use strict";
2
-
3
- var utils = require("../utils");
4
- var log = require("npmlog");
5
-
6
- function formatData(data) {
7
- return {
8
- userID: utils.formatID(data.uid.toString()),
9
- photoUrl: data.photo,
10
- indexRank: data.index_rank,
11
- name: data.text,
12
- isVerified: data.is_verified,
13
- profileUrl: data.path,
14
- category: data.category,
15
- score: data.score,
16
- type: data.type,
17
- };
18
- }
19
-
20
- module.exports = function (defaultFuncs, api, ctx) {
21
- return function getUserID(name, callback) {
22
- var resolveFunc = function () {};
23
- var rejectFunc = function () {};
24
- var returnPromise = new Promise(function (resolve, reject) {
25
- resolveFunc = resolve;
26
- rejectFunc = reject;
27
- });
28
-
29
- if (!callback) {
30
- callback = function (err, friendList) {
31
- if (err) {
32
- return rejectFunc(err);
33
- }
34
- resolveFunc(friendList);
35
- };
36
- }
37
-
38
- var form = {
39
- value: name.toLowerCase(),
40
- viewer: ctx.userID,
41
- rsp: "search",
42
- context: "search",
43
- path: "/home.php",
44
- request_id: utils.getGUID(),
45
- };
46
-
47
- defaultFuncs
48
- .get("https://www.facebook.com/ajax/typeahead/search.php", ctx.jar, form)
49
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
50
- .then(function (resData) {
51
- if (resData.error) {
52
- throw resData;
53
- }
54
-
55
- var data = resData.payload.entries;
56
-
57
- callback(null, data.map(formatData));
58
- })
59
- .catch(function (err) {
60
- log.error("getUserID", err);
61
- return callback(err);
62
- });
63
-
64
- return returnPromise;
65
- };
66
- };
1
+ "use strict";
2
+
3
+ const log = require("npmlog");
4
+ const { formatID } = require("../../utils/format");
5
+ function formatData(data) {
6
+ return {
7
+ userID: formatID(data.uid.toString()),
8
+ photoUrl: data.photo,
9
+ indexRank: data.index_rank,
10
+ name: data.text,
11
+ isVerified: data.is_verified,
12
+ profileUrl: data.path,
13
+ category: data.category,
14
+ score: data.score,
15
+ type: data.type
16
+ };
17
+ }
18
+
19
+ module.exports = function(defaultFuncs, api, ctx) {
20
+ return function getUserID(name, callback) {
21
+ let resolveFunc = function() {};
22
+ let rejectFunc = function() {};
23
+ const returnPromise = new Promise(function(resolve, reject) {
24
+ resolveFunc = resolve;
25
+ rejectFunc = reject;
26
+ });
27
+
28
+ if (!callback) {
29
+ callback = function(err, friendList) {
30
+ if (err) {
31
+ return rejectFunc(err);
32
+ }
33
+ resolveFunc(friendList);
34
+ };
35
+ }
36
+
37
+ const form = {
38
+ value: name.toLowerCase(),
39
+ viewer: ctx.userID,
40
+ rsp: "search",
41
+ context: "search",
42
+ path: "/home.php",
43
+ request_id: ctx.clientId
44
+ };
45
+
46
+ defaultFuncs
47
+ .get("https://www.facebook.com/ajax/typeahead/search.php", ctx.jar, form)
48
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
49
+ .then(function(resData) {
50
+ if (resData.error) {
51
+ throw resData;
52
+ }
53
+
54
+ const data = resData.payload.entries;
55
+
56
+ callback(null, data.map(formatData));
57
+ })
58
+ .catch(function(err) {
59
+ log.error("getUserID", err);
60
+ return callback(err);
61
+ });
62
+
63
+ return returnPromise;
64
+ };
65
+ };
@@ -0,0 +1,327 @@
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const log = require("npmlog");
6
+ const logger = require("../../../func/logger");
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;
31
+ }
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;
48
+ }
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;
55
+ }
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 = {
137
+ queries: JSON.stringify({
138
+ o0: {
139
+ doc_id: DOC_PRIMARY,
140
+ query_params: { ids }
141
+ }
142
+ }),
143
+ batch_name: BATCH_PRIMARY
144
+ };
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;
216
+ }
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));
250
+ }
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
+ // Store interval reference for cleanup
272
+ const updateInterval = setInterval(() => {
273
+ checkAndUpdateUsers();
274
+ processQueue();
275
+ }, 10000);
276
+
277
+ // Store interval in ctx for cleanup on logout/stop
278
+ if (!ctx._userInfoIntervals) {
279
+ ctx._userInfoIntervals = [];
280
+ }
281
+ ctx._userInfoIntervals.push(updateInterval);
282
+
283
+ return function getUserInfo(idsOrId, callback) {
284
+ let resolveFunc, rejectFunc;
285
+ const returnPromise = new Promise((resolve, reject) => { resolveFunc = resolve; rejectFunc = reject; });
286
+ if (typeof callback !== "function") {
287
+ callback = (err, data) => { if (err) return rejectFunc(err); resolveFunc(data); };
288
+ }
289
+ const ids = Array.isArray(idsOrId) ? idsOrId.map(v => String(v)) : [String(idsOrId)];
290
+ Promise.all(ids.map(id => get(id).catch(() => null))).then(async cachedRows => {
291
+ const ret = {};
292
+ const needCreate = [];
293
+ for (let i = 0; i < ids.length; i++) {
294
+ const id = ids[i];
295
+ const row = cachedRows[i];
296
+ if (row?.data && row.data.id) {
297
+ ret[id] = row.data;
298
+ } else {
299
+ needCreate.push(id);
300
+ }
301
+ }
302
+ if (needCreate.length) {
303
+ const fetched = await fetchAndPersist(needCreate, true);
304
+ for (const id of needCreate) ret[id] = fetched[id] || {
305
+ id,
306
+ name: null,
307
+ firstName: null,
308
+ vanity: null,
309
+ thumbSrc: null,
310
+ profileUrl: null,
311
+ gender: null,
312
+ type: null,
313
+ isFriend: false,
314
+ isMessengerUser: null,
315
+ isMessageBlockedByViewer: false,
316
+ workInfo: null,
317
+ messengerStatus: null
318
+ };
319
+ }
320
+ return callback(null, ret);
321
+ }).catch(err => {
322
+ log.error("getUserInfo", "Error: " + (err?.message || "Unknown"));
323
+ callback(err);
324
+ });
325
+ return returnPromise;
326
+ };
327
+ };