@cexy/hoonfca 1.0.3 → 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.
- package/CHANGELOG.md +181 -0
- package/DOCS.md +2636 -0
- package/LICENSE-MIT +21 -21
- package/README.md +632 -0
- package/func/checkUpdate.js +222 -0
- package/func/logger.js +48 -0
- package/index.d.ts +731 -605
- package/index.js +6 -777
- package/module/config.js +34 -0
- package/module/login.js +136 -0
- package/module/loginHelper.js +1034 -0
- package/module/options.js +45 -0
- package/package.json +53 -22
- package/src/api/action/addExternalModule.js +25 -0
- package/src/api/action/changeAvatar.js +137 -0
- package/src/{changeBio.js → api/action/changeBio.js} +75 -76
- package/src/api/action/enableAutoSaveAppState.js +73 -0
- package/src/{getCurrentUserID.js → api/action/getCurrentUserID.js} +7 -7
- package/src/{handleFriendRequest.js → api/action/handleFriendRequest.js} +57 -57
- package/src/api/action/logout.js +76 -0
- package/src/api/action/refreshFb_dtsg.js +48 -0
- package/src/api/action/setPostReaction.js +106 -0
- package/src/api/action/unfriend.js +54 -0
- package/src/api/http/httpGet.js +46 -0
- package/src/api/http/httpPost.js +52 -0
- package/src/api/http/postFormData.js +47 -0
- package/src/api/messaging/addUserToGroup.js +68 -0
- package/src/api/messaging/changeAdminStatus.js +122 -0
- package/src/api/messaging/changeArchivedStatus.js +55 -0
- package/src/api/messaging/changeBlockedStatus.js +48 -0
- package/src/api/messaging/changeGroupImage.js +90 -0
- package/src/api/messaging/changeNickname.js +70 -0
- package/src/api/messaging/changeThreadColor.js +79 -0
- package/src/api/messaging/changeThreadEmoji.js +106 -0
- package/src/{createNewGroup.js → api/messaging/createNewGroup.js} +88 -88
- package/src/api/messaging/createPoll.js +43 -0
- package/src/api/messaging/createThemeAI.js +98 -0
- package/src/{deleteMessage.js → api/messaging/deleteMessage.js} +56 -56
- package/src/{deleteThread.js → api/messaging/deleteThread.js} +56 -56
- package/src/api/messaging/editMessage.js +68 -0
- package/src/api/messaging/forwardAttachment.js +51 -0
- package/src/{getEmojiUrl.js → api/messaging/getEmojiUrl.js} +8 -8
- package/src/{getFriendsList.js → api/messaging/getFriendsList.js} +82 -84
- package/src/api/messaging/getMessage.js +829 -0
- package/src/api/messaging/getThemePictures.js +62 -0
- package/src/api/messaging/handleMessageRequest.js +65 -0
- package/src/{markAsDelivered.js → api/messaging/markAsDelivered.js} +57 -58
- package/src/{markAsRead.js → api/messaging/markAsRead.js} +88 -88
- package/src/{markAsReadAll.js → api/messaging/markAsReadAll.js} +49 -50
- package/src/api/messaging/markAsSeen.js +61 -0
- package/src/{muteThread.js → api/messaging/muteThread.js} +50 -52
- package/src/api/messaging/removeUserFromGroup.js +106 -0
- package/src/{resolvePhotoUrl.js → api/messaging/resolvePhotoUrl.js} +43 -45
- package/src/api/messaging/scheduler.js +264 -0
- package/src/{searchForThread.js → api/messaging/searchForThread.js} +52 -53
- package/src/api/messaging/sendMessage.js +272 -0
- package/src/api/messaging/sendTypingIndicator.js +67 -0
- package/src/api/messaging/setMessageReaction.js +76 -0
- package/src/api/messaging/setTitle.js +119 -0
- package/src/{shareContact.js → api/messaging/shareContact.js} +49 -53
- package/src/api/messaging/threadColors.js +128 -0
- package/src/api/messaging/unsendMessage.js +81 -0
- package/src/api/messaging/uploadAttachment.js +94 -0
- package/src/api/socket/core/connectMqtt.js +255 -0
- package/src/api/socket/core/emitAuth.js +106 -0
- package/src/api/socket/core/getSeqID.js +40 -0
- package/src/api/socket/core/getTaskResponseData.js +22 -0
- package/src/api/socket/core/markDelivery.js +12 -0
- package/src/api/socket/core/parseDelta.js +391 -0
- package/src/api/socket/detail/buildStream.js +208 -0
- package/src/api/socket/detail/constants.js +24 -0
- package/src/api/socket/listenMqtt.js +364 -0
- package/src/api/socket/middleware/index.js +216 -0
- package/src/{getThreadHistory.js → api/threads/getThreadHistory.js} +664 -647
- package/src/api/threads/getThreadInfo.js +438 -0
- package/src/{getThreadList.js → api/threads/getThreadList.js} +293 -292
- package/src/{getThreadPictures.js → api/threads/getThreadPictures.js} +78 -79
- package/src/{getUserID.js → api/users/getUserID.js} +65 -66
- package/src/api/users/getUserInfo.js +327 -0
- package/src/api/users/getUserInfoV2.js +134 -0
- package/src/core/sendReqMqtt.js +96 -0
- package/src/database/models/index.js +87 -0
- package/src/database/models/thread.js +45 -0
- package/src/database/models/user.js +46 -0
- package/src/database/threadData.js +98 -0
- package/src/database/userData.js +89 -0
- package/src/utils/client.js +320 -0
- package/src/utils/constants.js +23 -0
- package/{utils.js → src/utils/format.js} +1115 -1447
- package/src/utils/headers.js +115 -0
- package/src/utils/request.js +305 -0
- package/.travis.yml +0 -6
- package/src/addExternalModule.js +0 -23
- package/src/addUserToGroup.js +0 -113
- package/src/changeAdminStatus.js +0 -95
- package/src/changeApprovalMode.js +0 -79
- package/src/changeArchivedStatus.js +0 -55
- package/src/changeBlockedStatus.js +0 -47
- package/src/changeBlockedStatusMqtt.js +0 -86
- package/src/changeGroupImage.js +0 -133
- package/src/changeNickname.js +0 -59
- package/src/changeThreadColor.js +0 -71
- package/src/changeThreadEmoji.js +0 -55
- package/src/createPoll.js +0 -130
- package/src/editMessage.js +0 -63
- package/src/forwardAttachment.js +0 -60
- package/src/forwardMessage.js +0 -63
- package/src/getGroupsList.js +0 -83
- package/src/getMarketplace.js +0 -84
- package/src/getNotifications.js +0 -86
- package/src/getPagesList.js +0 -83
- package/src/getStories.js +0 -88
- package/src/getThreadInfo.js +0 -222
- package/src/getUserInfo.js +0 -128
- package/src/handleMessageRequest.js +0 -65
- package/src/httpGet.js +0 -58
- package/src/httpPost.js +0 -58
- package/src/listenMqtt.js +0 -1018
- package/src/logout.js +0 -75
- package/src/markAsSeen.js +0 -61
- package/src/pinMessage.js +0 -61
- package/src/removeUserFromGroup.js +0 -79
- package/src/searchUsers.js +0 -89
- package/src/sendComment.js +0 -161
- package/src/sendMessage.js +0 -457
- package/src/sendTypingIndicator.js +0 -179
- package/src/setBio.js +0 -86
- package/src/setMessageReaction.js +0 -186
- package/src/setPostReaction.js +0 -71
- package/src/setTheme.js +0 -313
- package/src/setTitle.js +0 -90
- package/src/shareLink.js +0 -62
- package/src/threadColors.js +0 -57
- package/src/unfriend.js +0 -53
- package/src/unsendMessage.js +0 -104
|
@@ -1,79 +1,78 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
)
|
|
36
|
-
.then(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
)
|
|
53
|
-
.then(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
.
|
|
49
|
-
.then(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
+
};
|