fca-phantom 1.0.0

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 (121) hide show
  1. package/LICENSE +58 -0
  2. package/README.md +534 -0
  3. package/index.js +35 -0
  4. package/package.json +101 -0
  5. package/phantom/core/builder/bootstrap.js +334 -0
  6. package/phantom/core/builder/config.js +78 -0
  7. package/phantom/core/builder/forge.js +113 -0
  8. package/phantom/core/builder/ignite.js +386 -0
  9. package/phantom/core/builder/options.js +61 -0
  10. package/phantom/core/engine.js +71 -0
  11. package/phantom/core/reactor.js +2 -0
  12. package/phantom/datastore/appState.js +2 -0
  13. package/phantom/datastore/appStateBackup.js +34 -0
  14. package/phantom/datastore/models/cipher/e2ee.js +48 -0
  15. package/phantom/datastore/models/cipher/vault.js +153 -0
  16. package/phantom/datastore/models/index.js +3 -0
  17. package/phantom/datastore/models/matrix/auth.js +151 -0
  18. package/phantom/datastore/models/matrix/cache.js +3 -0
  19. package/phantom/datastore/models/matrix/checker.js +2 -0
  20. package/phantom/datastore/models/matrix/clients.js +2 -0
  21. package/phantom/datastore/models/matrix/constants.js +2 -0
  22. package/phantom/datastore/models/matrix/credentials.js +2 -0
  23. package/phantom/datastore/models/matrix/cycle.js +2 -0
  24. package/phantom/datastore/models/matrix/gate.js +282 -0
  25. package/phantom/datastore/models/matrix/ghost.js +332 -0
  26. package/phantom/datastore/models/matrix/headers.js +193 -0
  27. package/phantom/datastore/models/matrix/heartbeat.js +298 -0
  28. package/phantom/datastore/models/matrix/identity.js +235 -0
  29. package/phantom/datastore/models/matrix/logger.js +271 -0
  30. package/phantom/datastore/models/matrix/monitor.js +2 -0
  31. package/phantom/datastore/models/matrix/net.js +316 -0
  32. package/phantom/datastore/models/matrix/response.js +193 -0
  33. package/phantom/datastore/models/matrix/revive.js +255 -0
  34. package/phantom/datastore/models/matrix/signals.js +2 -0
  35. package/phantom/datastore/models/matrix/store.js +263 -0
  36. package/phantom/datastore/models/matrix/telemetry.js +272 -0
  37. package/phantom/datastore/models/matrix/tools.js +93 -0
  38. package/phantom/datastore/models/matrix/transform/cookieParser.js +2 -0
  39. package/phantom/datastore/models/matrix/transform/cookies.js +114 -0
  40. package/phantom/datastore/models/matrix/transform/index.js +203 -0
  41. package/phantom/datastore/models/matrix/validator.js +157 -0
  42. package/phantom/datastore/models/types/index.d.ts +498 -0
  43. package/phantom/datastore/schema.js +167 -0
  44. package/phantom/datastore/session.js +129 -0
  45. package/phantom/datastore/threads.js +22 -0
  46. package/phantom/datastore/users.js +26 -0
  47. package/phantom/dispatch/addExternalModule.js +239 -0
  48. package/phantom/dispatch/addUserToGroup.js +161 -0
  49. package/phantom/dispatch/changeAdminStatus.js +142 -0
  50. package/phantom/dispatch/changeArchivedStatus.js +135 -0
  51. package/phantom/dispatch/changeAvatar.js +123 -0
  52. package/phantom/dispatch/changeBio.js +86 -0
  53. package/phantom/dispatch/changeBlockedStatus.js +86 -0
  54. package/phantom/dispatch/changeGroupImage.js +145 -0
  55. package/phantom/dispatch/changeThreadColor.js +172 -0
  56. package/phantom/dispatch/changeThreadEmoji.js +130 -0
  57. package/phantom/dispatch/comment.js +136 -0
  58. package/phantom/dispatch/createAITheme.js +333 -0
  59. package/phantom/dispatch/createNewGroup.js +99 -0
  60. package/phantom/dispatch/createPoll.js +148 -0
  61. package/phantom/dispatch/deleteMessage.js +131 -0
  62. package/phantom/dispatch/deleteThread.js +155 -0
  63. package/phantom/dispatch/e2ee.js +101 -0
  64. package/phantom/dispatch/editMessage.js +158 -0
  65. package/phantom/dispatch/emoji.js +143 -0
  66. package/phantom/dispatch/fetchThemeData.js +233 -0
  67. package/phantom/dispatch/follow.js +111 -0
  68. package/phantom/dispatch/forwardMessage.js +110 -0
  69. package/phantom/dispatch/friend.js +189 -0
  70. package/phantom/dispatch/gcmember.js +138 -0
  71. package/phantom/dispatch/gcname.js +131 -0
  72. package/phantom/dispatch/gcrule.js +111 -0
  73. package/phantom/dispatch/getAccess.js +109 -0
  74. package/phantom/dispatch/getBotInfo.js +81 -0
  75. package/phantom/dispatch/getBotInitialData.js +110 -0
  76. package/phantom/dispatch/getFriendsList.js +118 -0
  77. package/phantom/dispatch/getMessage.js +199 -0
  78. package/phantom/dispatch/getTheme.js +199 -0
  79. package/phantom/dispatch/getThemeInfo.js +160 -0
  80. package/phantom/dispatch/getThreadHistory.js +139 -0
  81. package/phantom/dispatch/getThreadInfo.js +153 -0
  82. package/phantom/dispatch/getThreadList.js +132 -0
  83. package/phantom/dispatch/getThreadPictures.js +93 -0
  84. package/phantom/dispatch/getUserID.js +147 -0
  85. package/phantom/dispatch/getUserInfo.js +513 -0
  86. package/phantom/dispatch/getUserInfoV2.js +146 -0
  87. package/phantom/dispatch/handleMessageRequest.js +50 -0
  88. package/phantom/dispatch/httpGet.js +63 -0
  89. package/phantom/dispatch/httpPost.js +89 -0
  90. package/phantom/dispatch/httpPostFormData.js +69 -0
  91. package/phantom/dispatch/listenMqtt.js +1236 -0
  92. package/phantom/dispatch/listenSpeed.js +179 -0
  93. package/phantom/dispatch/logout.js +93 -0
  94. package/phantom/dispatch/markAsDelivered.js +92 -0
  95. package/phantom/dispatch/markAsRead.js +119 -0
  96. package/phantom/dispatch/markAsReadAll.js +215 -0
  97. package/phantom/dispatch/markAsSeen.js +70 -0
  98. package/phantom/dispatch/mqttDeltaValue.js +278 -0
  99. package/phantom/dispatch/muteThread.js +253 -0
  100. package/phantom/dispatch/nickname.js +132 -0
  101. package/phantom/dispatch/notes.js +263 -0
  102. package/phantom/dispatch/pinMessage.js +238 -0
  103. package/phantom/dispatch/produceMetaTheme.js +335 -0
  104. package/phantom/dispatch/realtime.js +291 -0
  105. package/phantom/dispatch/removeUserFromGroup.js +248 -0
  106. package/phantom/dispatch/resolvePhotoUrl.js +217 -0
  107. package/phantom/dispatch/searchForThread.js +258 -0
  108. package/phantom/dispatch/sendMessage.js +354 -0
  109. package/phantom/dispatch/sendMessageMqtt.js +249 -0
  110. package/phantom/dispatch/sendTypingIndicator.js +206 -0
  111. package/phantom/dispatch/setMessageReaction.js +188 -0
  112. package/phantom/dispatch/setMessageReactionMqtt.js +248 -0
  113. package/phantom/dispatch/setThreadTheme.js +330 -0
  114. package/phantom/dispatch/setThreadThemeMqtt.js +207 -0
  115. package/phantom/dispatch/share.js +200 -0
  116. package/phantom/dispatch/shareContact.js +216 -0
  117. package/phantom/dispatch/stickers.js +395 -0
  118. package/phantom/dispatch/story.js +240 -0
  119. package/phantom/dispatch/theme.js +296 -0
  120. package/phantom/dispatch/unfriend.js +199 -0
  121. package/phantom/dispatch/unsendMessage.js +124 -0
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+
3
+ const utils = require('../datastore/models/matrix/tools');
4
+ const { globalShield } = require('../datastore/models/matrix/ghost');
5
+
6
+ const SEARCH_CACHE = new Map();
7
+ const SEARCH_CACHE_TTL = 60 * 1000;
8
+ const SEARCH_HISTORY = [];
9
+ const MAX_HISTORY = 200;
10
+ const INFLIGHT_SEARCHES = new Map();
11
+
12
+ async function retryOp(fn, retries = 3, base = 500) {
13
+ for (let i = 0; i < retries; i++) {
14
+ try { return await fn(); } catch (err) {
15
+ if (i === retries - 1) throw err;
16
+ const transient = /network|timeout|ECONNRESET|ETIMEDOUT|5\d\d|429/i.test(String(err?.message || err));
17
+ if (!transient) throw err;
18
+ await new Promise(r => setTimeout(r, base * Math.pow(2, i) + Math.random() * 250));
19
+ }
20
+ }
21
+ }
22
+
23
+ async function searchViaGraphQL(api, searchQuery, limit = 100) {
24
+ const threads = await api.getThreadList(limit, null, ['INBOX']);
25
+ if (!threads || threads.length === 0) throw new Error('No threads in INBOX');
26
+
27
+ const searchLower = searchQuery.toLowerCase().trim();
28
+ return threads.filter(thread => {
29
+ if (thread.threadName && thread.threadName.toLowerCase().includes(searchLower)) return true;
30
+ if (thread.threadID && thread.threadID.toString().includes(searchQuery)) return true;
31
+ if (thread.userInfo && Array.isArray(thread.userInfo)) {
32
+ return thread.userInfo.some(u => u.name && u.name.toLowerCase().includes(searchLower));
33
+ }
34
+ return false;
35
+ });
36
+ }
37
+
38
+ async function searchViaLegacy(defaultFuncs, ctx, searchQuery, options = {}) {
39
+ const { offset = 0, limit = 21 } = options;
40
+
41
+ const form = {
42
+ client: 'web_messenger',
43
+ query: searchQuery,
44
+ offset,
45
+ limit,
46
+ index: 'fbid'
47
+ };
48
+
49
+ const res = await defaultFuncs.post(
50
+ 'https://www.facebook.com/ajax/mercury/search_threads.php',
51
+ ctx.jar,
52
+ form
53
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
54
+
55
+ if (!res) throw Object.assign(new Error('Account checkpoint required — searchForThread restricted'), { errorCode: 1357004, errorType: 'CHECKPOINT' });
56
+ if (res.error) throw res;
57
+
58
+ let threadsData = res.payload?.mercury_payload?.threads || res.payload?.threads;
59
+ if (!threadsData) return [];
60
+ if (!Array.isArray(threadsData)) threadsData = Object.values(threadsData);
61
+ return threadsData.map(t => utils.formatThread ? utils.formatThread(t) : t);
62
+ }
63
+
64
+ async function searchViaGraphQLEndpoint(defaultFuncs, ctx, searchQuery) {
65
+ const form = {
66
+ fb_api_caller_class: 'RelayModern',
67
+ fb_api_req_friendly_name: 'MWChatSearchResultsRootQuery',
68
+ variables: JSON.stringify({ query: searchQuery, scale: 1, offset: 0, limit: 20 }),
69
+ doc_id: '5620028761392879'
70
+ };
71
+
72
+ const res = await defaultFuncs.post('https://www.facebook.com/api/graphql/', ctx.jar, form)
73
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs));
74
+
75
+ if (res?.errors?.length) throw new Error(JSON.stringify(res.errors));
76
+
77
+ const edges = res?.data?.viewer?.message_threads?.edges || [];
78
+ return edges.map(e => e.node || e).filter(Boolean);
79
+ }
80
+
81
+ function fuzzyScore(text, query) {
82
+ if (!text || !query) return 0;
83
+ const t = text.toLowerCase();
84
+ const q = query.toLowerCase();
85
+ if (t === q) return 1.0;
86
+ if (t.startsWith(q)) return 0.9;
87
+ if (t.includes(q)) return 0.7;
88
+ const words = q.split(/\s+/);
89
+ const matchCount = words.filter(w => t.includes(w)).length;
90
+ return matchCount / words.length * 0.5;
91
+ }
92
+
93
+ function scoreThread(thread, query) {
94
+ let maxScore = 0;
95
+ if (thread.threadName) maxScore = Math.max(maxScore, fuzzyScore(thread.threadName, query));
96
+ if (thread.threadID) {
97
+ if (thread.threadID.toString().includes(query)) maxScore = Math.max(maxScore, 0.8);
98
+ }
99
+ if (thread.userInfo && Array.isArray(thread.userInfo)) {
100
+ for (const u of thread.userInfo) {
101
+ if (u.name) maxScore = Math.max(maxScore, fuzzyScore(u.name, query));
102
+ }
103
+ }
104
+ return maxScore;
105
+ }
106
+
107
+ function recordHistory(op) {
108
+ SEARCH_HISTORY.unshift({ ...op, ts: Date.now() });
109
+ if (SEARCH_HISTORY.length > MAX_HISTORY) SEARCH_HISTORY.length = MAX_HISTORY;
110
+ }
111
+
112
+ module.exports = (defaultFuncs, api, ctx) => {
113
+ return async function searchForThread(searchQuery, options, callback) {
114
+ if (typeof options === 'function') { callback = options; options = {}; }
115
+ if (!options || typeof options !== 'object') options = {};
116
+
117
+ const {
118
+ limit = 100,
119
+ legacyLimit = 21,
120
+ legacyOffset = 0,
121
+ minScore = 0.1,
122
+ sortByScore = true,
123
+ maxResults = 50,
124
+ skipCache = false,
125
+ strategies = ['graphql', 'graphqlEndpoint', 'legacy'],
126
+ enrichResults = false
127
+ } = options;
128
+
129
+ let resolveFunc, rejectFunc;
130
+ const returnPromise = new Promise((resolve, reject) => {
131
+ resolveFunc = resolve;
132
+ rejectFunc = reject;
133
+ });
134
+
135
+ const originalCallback = callback;
136
+ callback = (err, result) => {
137
+ if (originalCallback) originalCallback(err, result);
138
+ if (err) return rejectFunc(err);
139
+ resolveFunc(result);
140
+ };
141
+
142
+ if (!searchQuery || typeof searchQuery !== 'string' || !searchQuery.trim()) {
143
+ callback({ error: 'searchForThread: searchQuery must be a non-empty string' });
144
+ return returnPromise;
145
+ }
146
+
147
+ const q = searchQuery.trim();
148
+ const cacheKey = `search_${q}_${limit}_${minScore}`;
149
+
150
+ if (!skipCache) {
151
+ const cached = SEARCH_CACHE.get(cacheKey);
152
+ if (cached && Date.now() - cached.ts < SEARCH_CACHE_TTL) {
153
+ callback(null, { ...cached.result, fromCache: true });
154
+ return returnPromise;
155
+ }
156
+ }
157
+
158
+ if (INFLIGHT_SEARCHES.has(cacheKey)) {
159
+ try {
160
+ const result = await INFLIGHT_SEARCHES.get(cacheKey);
161
+ callback(null, result);
162
+ } catch (err) {
163
+ callback(err);
164
+ }
165
+ return returnPromise;
166
+ }
167
+
168
+ const searchPromise = (async () => {
169
+ const errors = [];
170
+ let threads = null;
171
+
172
+ for (const strategy of strategies) {
173
+ if (threads && threads.length > 0) break;
174
+ try {
175
+ utils.log('searchForThread', `Trying strategy: ${strategy}`);
176
+ if (strategy === 'graphql') {
177
+ threads = await retryOp(() => searchViaGraphQL(api, q, limit));
178
+ } else if (strategy === 'graphqlEndpoint') {
179
+ threads = await retryOp(() => searchViaGraphQLEndpoint(defaultFuncs, ctx, q));
180
+ } else if (strategy === 'legacy') {
181
+ threads = await retryOp(() => searchViaLegacy(defaultFuncs, ctx, q, { offset: legacyOffset, limit: legacyLimit }));
182
+ }
183
+ } catch (err) {
184
+ errors.push({ strategy, error: err.message || String(err) });
185
+ utils.warn('searchForThread', `Strategy "${strategy}" failed:`, err.message);
186
+ }
187
+ }
188
+
189
+ if (!threads || threads.length === 0) {
190
+ return {
191
+ success: false,
192
+ threads: [],
193
+ query: q,
194
+ matchCount: 0,
195
+ errors,
196
+ timestamp: Date.now()
197
+ };
198
+ }
199
+
200
+ const scored = threads
201
+ .map(t => ({ thread: t, score: scoreThread(t, q) }))
202
+ .filter(({ score }) => score >= minScore);
203
+
204
+ if (sortByScore) scored.sort((a, b) => b.score - a.score);
205
+
206
+ const topN = scored.slice(0, maxResults).map(({ thread, score }) => ({ ...thread, _searchScore: score }));
207
+
208
+ if (topN.length === 0) {
209
+ return {
210
+ success: false,
211
+ threads: [],
212
+ query: q,
213
+ matchCount: 0,
214
+ errors,
215
+ timestamp: Date.now()
216
+ };
217
+ }
218
+
219
+ const result = {
220
+ success: true,
221
+ threads: topN,
222
+ query: q,
223
+ matchCount: topN.length,
224
+ strategiesTriedCount: strategies.length,
225
+ errors: errors.length ? errors : undefined,
226
+ timestamp: Date.now()
227
+ };
228
+
229
+ SEARCH_CACHE.set(cacheKey, { result, ts: Date.now() });
230
+ recordHistory({ query: q, matchCount: topN.length, timestamp: Date.now() });
231
+
232
+ return result;
233
+ })();
234
+
235
+ INFLIGHT_SEARCHES.set(cacheKey, searchPromise);
236
+
237
+ try {
238
+ const result = await searchPromise;
239
+ if (!result.success) {
240
+ callback({ error: `searchForThread: no matches for "${q}"`, details: result.errors });
241
+ } else {
242
+ callback(null, result);
243
+ }
244
+ } catch (err) {
245
+ utils.error('searchForThread', err);
246
+ callback(err);
247
+ } finally {
248
+ INFLIGHT_SEARCHES.delete(cacheKey);
249
+ }
250
+
251
+ return returnPromise;
252
+ };
253
+
254
+ Object.assign(module.exports, {
255
+ getHistory: (limit = 30) => SEARCH_HISTORY.slice(0, limit),
256
+ clearCache: () => { SEARCH_CACHE.clear(); return { success: true }; }
257
+ });
258
+ };
@@ -0,0 +1,354 @@
1
+ "use strict";
2
+
3
+ const utils = require('../datastore/models/matrix/tools');
4
+ const e2ee = require('../datastore/models/cipher/e2ee');
5
+ const { globalShield } = require('../datastore/models/matrix/ghost');
6
+
7
+ const allowedProperties = {
8
+ attachment: true,
9
+ url: true,
10
+ sticker: true,
11
+ emoji: true,
12
+ emojiSize: true,
13
+ body: true,
14
+ mentions: true,
15
+ location: true,
16
+ };
17
+
18
+ module.exports = (defaultFuncs, api, ctx) => {
19
+ function getThreadCache() {
20
+ if (!ctx.threadTypeCache) ctx.threadTypeCache = Object.create(null);
21
+ return ctx.threadTypeCache;
22
+ }
23
+
24
+ async function isGroupThread(threadID, explicitIsGroup) {
25
+ if (utils.getType(explicitIsGroup) === "Boolean") return !!explicitIsGroup;
26
+ const tid = threadID.toString();
27
+ const cache = getThreadCache();
28
+ if (Object.prototype.hasOwnProperty.call(cache, tid)) return !!cache[tid];
29
+ try {
30
+ const info = await api.getThreadInfo(tid);
31
+ cache[tid] = !!info.isGroup;
32
+ return !!info.isGroup;
33
+ } catch (_) {
34
+ const fallback = tid.length >= 16;
35
+ cache[tid] = fallback;
36
+ return fallback;
37
+ }
38
+ }
39
+
40
+ function detectAttachmentType(attachment) {
41
+ const path = attachment.path || '';
42
+ const ext = path.toLowerCase().split('.').pop();
43
+
44
+ const audioTypes = ['mp3', 'wav', 'aac', 'm4a', 'ogg', 'opus', 'flac'];
45
+ const videoTypes = ['mp4', 'mov', 'avi', 'mkv', 'webm', 'wmv', 'flv'];
46
+ const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'];
47
+
48
+ if (audioTypes.includes(ext)) return { voice_clip: "true" };
49
+ if (videoTypes.includes(ext)) return { video: "true" };
50
+ if (imageTypes.includes(ext)) return { image: "true" };
51
+ return { file: "true" };
52
+ }
53
+
54
+ async function uploadSingleAttachment(attachment, threadIDHint) {
55
+ if (!utils.isReadableStream(attachment)) {
56
+ throw new Error("Attachment should be a readable stream and not " + utils.getType(attachment) + ".");
57
+ }
58
+ const uploadType = detectAttachmentType(attachment);
59
+ const oksir = await defaultFuncs.postFormData(
60
+ "https://upload.facebook.com/ajax/mercury/upload.php",
61
+ ctx.jar,
62
+ { upload_1024: attachment, ...uploadType },
63
+ {},
64
+ { ...ctx, requestThreadID: threadIDHint }
65
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
66
+
67
+ if (oksir.error) throw new Error(JSON.stringify(oksir));
68
+ return oksir.payload.metadata[0];
69
+ }
70
+
71
+ async function uploadAttachment(attachments, threadIDHint) {
72
+ const CONCURRENT_UPLOADS = 3;
73
+ const uploads = [];
74
+ for (let i = 0; i < attachments.length; i += CONCURRENT_UPLOADS) {
75
+ const batch = attachments.slice(i, i + CONCURRENT_UPLOADS);
76
+ const results = await Promise.all(batch.map(a => uploadSingleAttachment(a, threadIDHint)));
77
+ uploads.push(...results);
78
+ if (i + CONCURRENT_UPLOADS < attachments.length) {
79
+ await globalShield.addSmartDelay();
80
+ }
81
+ }
82
+ return uploads;
83
+ }
84
+
85
+ async function getUrl(url) {
86
+ const resData = await defaultFuncs.post(
87
+ "https://www.facebook.com/message_share_attachment/fromURI/",
88
+ ctx.jar,
89
+ { image_height: 960, image_width: 960, uri: url }
90
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
91
+ if (!resData || resData.error || !resData.payload) throw new Error("Invalid url");
92
+ return resData.payload.share_data.share_params;
93
+ }
94
+
95
+ async function sendContent(form, threadID, isSingleUser, messageAndOTID) {
96
+ if (utils.getType(threadID) === "Array") {
97
+ for (let i = 0; i < threadID.length; i++) {
98
+ form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
99
+ }
100
+ form["specific_to_list[" + threadID.length + "]"] = "fbid:" + ctx.userID;
101
+ form["client_thread_id"] = "root:" + messageAndOTID;
102
+ utils.log("sendMessage", "Sending message to multiple users: " + threadID);
103
+ } else {
104
+ if (isSingleUser) {
105
+ form["specific_to_list[0]"] = "fbid:" + threadID;
106
+ form["specific_to_list[1]"] = "fbid:" + ctx.userID;
107
+ form["other_user_fbid"] = threadID;
108
+ form["client_thread_id"] = "root:" + messageAndOTID;
109
+ } else {
110
+ form["thread_fbid"] = threadID;
111
+ }
112
+ }
113
+
114
+ if (ctx.globalOptions.pageID) {
115
+ form["author"] = "fbid:" + ctx.globalOptions.pageID;
116
+ form["specific_to_list[1]"] = "fbid:" + ctx.globalOptions.pageID;
117
+ form["creator_info[creatorID]"] = ctx.userID;
118
+ form["creator_info[creatorType]"] = "direct_admin";
119
+ form["creator_info[labelType]"] = "sent_message";
120
+ form["creator_info[pageID]"] = ctx.globalOptions.pageID;
121
+ form["request_user_id"] = ctx.globalOptions.pageID;
122
+ form["creator_info[profileURI]"] = "https://www.facebook.com/profile.php?id=" + ctx.userID;
123
+ }
124
+
125
+ const resData = await defaultFuncs.post(
126
+ "https://www.facebook.com/messaging/send/",
127
+ ctx.jar,
128
+ form,
129
+ { ...ctx, requestThreadID: threadID }
130
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
131
+
132
+ if (!resData) throw new Error("Send message failed.");
133
+ if (resData.error) {
134
+ if (resData.error === 1545012) {
135
+ utils.warn("sendMessage", "Got error 1545012. This might mean that you're not part of the conversation " + threadID);
136
+ }
137
+ // Check for suspension signals in error
138
+ globalShield.detectSuspensionSignal(String(resData.error) + ' ' + JSON.stringify(resData));
139
+ throw new Error(JSON.stringify(resData));
140
+ }
141
+
142
+ const messageInfo = resData.payload.actions.reduce((p, v) => {
143
+ return { threadID: v.thread_fbid, messageID: v.message_id, timestamp: v.timestamp } || p;
144
+ }, null);
145
+ return messageInfo;
146
+ }
147
+
148
+ return async (msg, threadID, callback, replyToMessage, isGroup) => {
149
+ if (!callback && (utils.getType(threadID) === "Function" || utils.getType(threadID) === "AsyncFunction")) {
150
+ throw new Error("Pass a threadID as a second argument.");
151
+ }
152
+ if (!replyToMessage && utils.getType(callback) === "String") {
153
+ replyToMessage = callback;
154
+ callback = undefined;
155
+ }
156
+
157
+ let resolveFunc = () => {};
158
+ let rejectFunc = () => {};
159
+ let returnPromise = new Promise((resolve, reject) => {
160
+ resolveFunc = resolve;
161
+ rejectFunc = reject;
162
+ });
163
+
164
+ if (!callback) {
165
+ callback = (err, data) => {
166
+ if (err) return rejectFunc(err);
167
+ resolveFunc(data);
168
+ };
169
+ }
170
+
171
+ let msgType = utils.getType(msg);
172
+ let threadIDType = utils.getType(threadID);
173
+ let messageIDType = utils.getType(replyToMessage);
174
+
175
+ if (msgType !== "String" && msgType !== "Object") {
176
+ return callback(new Error("Message should be of type string or object and not " + msgType + "."));
177
+ }
178
+ if (threadIDType !== "Array" && threadIDType !== "Number" && threadIDType !== "String") {
179
+ return callback(new Error("ThreadID should be of type number, string, or array and not " + threadIDType + "."));
180
+ }
181
+ if (replyToMessage && messageIDType !== 'String') {
182
+ return callback(new Error("MessageID should be of type string and not " + messageIDType + "."));
183
+ }
184
+
185
+ if (!ctx.validator.isValidMessage(msg)) {
186
+ return callback(new Error("Invalid message content"));
187
+ }
188
+ const threadIDs = Array.isArray(threadID) ? threadID : [threadID];
189
+ if (!ctx.validator.validateIDArray(threadIDs, ctx.validator.isValidThreadID)) {
190
+ return callback(new Error("Invalid thread ID(s)"));
191
+ }
192
+
193
+ if (msgType === "String") msg = { body: msg };
194
+
195
+ let disallowedProperties = Object.keys(msg).filter(prop => !allowedProperties[prop]);
196
+ if (disallowedProperties.length > 0) {
197
+ return callback(new Error("Dissallowed props: `" + disallowedProperties.join(", ") + "`"));
198
+ }
199
+
200
+ try {
201
+ let messageAndOTID = utils.generateOfflineThreadingID();
202
+ let form = {
203
+ client: "mercury",
204
+ action_type: "ma-type:user-generated-message",
205
+ author: "fbid:" + ctx.userID,
206
+ timestamp: Date.now(),
207
+ timestamp_absolute: "Today",
208
+ timestamp_relative: utils.generateTimestampRelative(),
209
+ timestamp_time_passed: "0",
210
+ is_unread: false,
211
+ is_cleared: false,
212
+ is_forward: false,
213
+ is_filtered_content: false,
214
+ is_filtered_content_bh: false,
215
+ is_filtered_content_account: false,
216
+ is_filtered_content_quasar: false,
217
+ is_filtered_content_invalid_app: false,
218
+ is_spoof_warning: false,
219
+ source: "source:chat:web",
220
+ "source_tags[0]": "source:chat",
221
+ ...(msg.body && { body: msg.body }),
222
+ html_body: false,
223
+ ui_push_phase: "V3",
224
+ status: "0",
225
+ offline_threading_id: messageAndOTID,
226
+ message_id: messageAndOTID,
227
+ threading_id: utils.generateThreadingID(ctx.clientID),
228
+ "ephemeral_ttl_mode:": "0",
229
+ manual_retry_cnt: "0",
230
+ has_attachment: !!(msg.attachment || msg.url || msg.sticker),
231
+ signatureID: utils.getSignatureID(),
232
+ ...(replyToMessage && { replied_to_message_id: replyToMessage })
233
+ };
234
+
235
+ if (msg.location) {
236
+ if (!msg.location.latitude || !msg.location.longitude) {
237
+ return callback(new Error("location property needs both latitude and longitude"));
238
+ }
239
+ form["location_attachment[coordinates][latitude]"] = msg.location.latitude;
240
+ form["location_attachment[coordinates][longitude]"] = msg.location.longitude;
241
+ form["location_attachment[is_current_location]"] = !!msg.location.current;
242
+ }
243
+ if (msg.sticker) form["sticker_id"] = msg.sticker;
244
+ if (msg.attachment) {
245
+ form.image_ids = [];
246
+ form.gif_ids = [];
247
+ form.file_ids = [];
248
+ form.video_ids = [];
249
+ form.audio_ids = [];
250
+ if (utils.getType(msg.attachment) !== "Array") msg.attachment = [msg.attachment];
251
+ const files = await uploadAttachment(msg.attachment, threadID);
252
+ files.forEach(file => {
253
+ const type = Object.keys(file)[0];
254
+ form["" + type + "s"].push(file[type]);
255
+ });
256
+ }
257
+ if (msg.url) {
258
+ form["shareable_attachment[share_type]"] = "100";
259
+ const params = await getUrl(msg.url);
260
+ form["shareable_attachment[share_params]"] = params;
261
+ }
262
+ if (msg.emoji) {
263
+ if (!msg.emojiSize) msg.emojiSize = "medium";
264
+ if (msg.emojiSize !== "small" && msg.emojiSize !== "medium" && msg.emojiSize !== "large") {
265
+ return callback(new Error("emojiSize property is invalid"));
266
+ }
267
+ if (form.body && form.body !== "") return callback(new Error("body is not empty"));
268
+ form.body = msg.emoji;
269
+ form["tags[0]"] = "hot_emoji_size:" + msg.emojiSize;
270
+ }
271
+ if (msg.mentions) {
272
+ for (let i = 0; i < msg.mentions.length; i++) {
273
+ const mention = msg.mentions[i];
274
+ const tag = mention.tag;
275
+ if (typeof tag !== "string") return callback(new Error("Mention tags must be strings."));
276
+ const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
277
+ if (offset < 0) utils.warn("handleMention", 'Mention for "' + tag + '" not found in message string.');
278
+ if (!mention.id) utils.warn("handleMention", "Mention id should be non-null.");
279
+ const id = mention.id || 0;
280
+ const emptyChar = '\u200E';
281
+ form["body"] = emptyChar + msg.body;
282
+ form["profile_xmd[" + i + "][offset]"] = offset + 1;
283
+ form["profile_xmd[" + i + "][length]"] = tag.length;
284
+ form["profile_xmd[" + i + "][id]"] = id;
285
+ form["profile_xmd[" + i + "][type]"] = "p";
286
+ }
287
+ }
288
+
289
+ const isSingleUser = !(await isGroupThread(threadID, isGroup));
290
+
291
+ // Late E2EE encryption for plain text only
292
+ try {
293
+ const isPlainText = !!form.body && !msg.mentions && !msg.sticker && !msg.emoji && !msg.url && !msg.attachment;
294
+ if (isPlainText && e2ee.isEnabled(ctx) && e2ee.hasPeer(ctx, threadID) && !isSingleUser) {
295
+ form.body = e2ee.encrypt(ctx, threadID, form.body);
296
+ }
297
+ } catch (_) {}
298
+
299
+ // ── Optimised anti-suspension send flow ───────────────────────────────
300
+ // Step 1: enforce thread throttle (single delay — no stacking).
301
+ await globalShield.prepareBeforeMessage(threadID, msg.body || '');
302
+
303
+ // Step 2: start typing indicator BEFORE the typing delay so the delay
304
+ // is "hidden" inside the visible typing indicator — zero extra latency.
305
+ let typingStarted = false;
306
+ let typingTimeout;
307
+ const shouldSimulateTyping = ctx.globalOptions && ctx.globalOptions.simulateTyping && api.sendTypingIndicator;
308
+ if (shouldSimulateTyping) {
309
+ try {
310
+ await api.sendTypingIndicator(true, threadID);
311
+ typingStarted = true;
312
+
313
+ // Typing delay runs while the indicator is already showing.
314
+ const msgLen = (msg.body || '').length;
315
+ const typingMs = await globalShield.simulateTyping(threadID, msgLen);
316
+ await new Promise(resolve => setTimeout(resolve, typingMs));
317
+
318
+ typingTimeout = setTimeout(() => {
319
+ if (typingStarted) {
320
+ try { api.sendTypingIndicator(false, threadID); } catch (_) {}
321
+ typingStarted = false;
322
+ }
323
+ }, 10000);
324
+ } catch (_) {}
325
+ }
326
+
327
+ // Step 3: send.
328
+ try {
329
+ const result = await sendContent(form, threadID, isSingleUser, messageAndOTID);
330
+ callback(null, result);
331
+ } catch (primaryErr) {
332
+ // Fallback to MQTT for group threads or when HTTP send fails.
333
+ if (api.sendMessageMqtt) {
334
+ try {
335
+ const mqttRes = await api.sendMessageMqtt(msg, threadID, replyToMessage);
336
+ callback(null, mqttRes);
337
+ } catch (fallbackErr) {
338
+ callback(primaryErr);
339
+ }
340
+ } else {
341
+ callback(primaryErr);
342
+ }
343
+ } finally {
344
+ if (typingTimeout) clearTimeout(typingTimeout);
345
+ if (typingStarted) {
346
+ try { await api.sendTypingIndicator(false, threadID); } catch (_) {}
347
+ }
348
+ }
349
+ } catch (err) {
350
+ callback(err);
351
+ }
352
+ return returnPromise;
353
+ };
354
+ };