@lazyneoaz/testfca 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 (120) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/COOKIE_LOGIN.md +208 -0
  3. package/LICENSE +3 -0
  4. package/README.md +492 -0
  5. package/index.js +2 -0
  6. package/package.json +120 -0
  7. package/scripts/build-go.mjs +54 -0
  8. package/scripts/detect-platform.mjs +36 -0
  9. package/scripts/download-prebuilt.mjs +119 -0
  10. package/scripts/package.mjs +6 -0
  11. package/scripts/postinstall.mjs +113 -0
  12. package/src/apis/addExternalModule.js +24 -0
  13. package/src/apis/addUserToGroup.js +108 -0
  14. package/src/apis/changeAdminStatus.js +148 -0
  15. package/src/apis/changeArchivedStatus.js +61 -0
  16. package/src/apis/changeAvatar.js +103 -0
  17. package/src/apis/changeBio.js +69 -0
  18. package/src/apis/changeBlockedStatus.js +54 -0
  19. package/src/apis/changeGroupImage.js +136 -0
  20. package/src/apis/changeThreadColor.js +116 -0
  21. package/src/apis/changeThreadEmoji.js +53 -0
  22. package/src/apis/comment.js +207 -0
  23. package/src/apis/createAITheme.js +129 -0
  24. package/src/apis/createNewGroup.js +79 -0
  25. package/src/apis/createPoll.js +73 -0
  26. package/src/apis/deleteMessage.js +52 -0
  27. package/src/apis/deleteThread.js +52 -0
  28. package/src/apis/e2ee.js +170 -0
  29. package/src/apis/editMessage.js +78 -0
  30. package/src/apis/emoji.js +124 -0
  31. package/src/apis/fetchThemeData.js +82 -0
  32. package/src/apis/follow.js +81 -0
  33. package/src/apis/forwardMessage.js +52 -0
  34. package/src/apis/friend.js +243 -0
  35. package/src/apis/gcmember.js +122 -0
  36. package/src/apis/gcname.js +123 -0
  37. package/src/apis/gcrule.js +119 -0
  38. package/src/apis/getAccess.js +111 -0
  39. package/src/apis/getBotInfo.js +88 -0
  40. package/src/apis/getBotInitialData.js +43 -0
  41. package/src/apis/getFriendsList.js +79 -0
  42. package/src/apis/getMessage.js +423 -0
  43. package/src/apis/getTheme.js +95 -0
  44. package/src/apis/getThemeInfo.js +116 -0
  45. package/src/apis/getThreadHistory.js +239 -0
  46. package/src/apis/getThreadInfo.js +267 -0
  47. package/src/apis/getThreadList.js +232 -0
  48. package/src/apis/getThreadPictures.js +58 -0
  49. package/src/apis/getUserID.js +117 -0
  50. package/src/apis/getUserInfo.js +513 -0
  51. package/src/apis/getUserInfoV2.js +146 -0
  52. package/src/apis/handleMessageRequest.js +50 -0
  53. package/src/apis/httpGet.js +63 -0
  54. package/src/apis/httpPost.js +89 -0
  55. package/src/apis/httpPostFormData.js +69 -0
  56. package/src/apis/listenMqtt.js +1236 -0
  57. package/src/apis/listenSpeed.js +179 -0
  58. package/src/apis/logout.js +93 -0
  59. package/src/apis/markAsDelivered.js +47 -0
  60. package/src/apis/markAsRead.js +115 -0
  61. package/src/apis/markAsReadAll.js +40 -0
  62. package/src/apis/markAsSeen.js +70 -0
  63. package/src/apis/mqttDeltaValue.js +250 -0
  64. package/src/apis/muteThread.js +45 -0
  65. package/src/apis/nickname.js +132 -0
  66. package/src/apis/notes.js +163 -0
  67. package/src/apis/pinMessage.js +150 -0
  68. package/src/apis/produceMetaTheme.js +180 -0
  69. package/src/apis/realtime.js +182 -0
  70. package/src/apis/removeUserFromGroup.js +117 -0
  71. package/src/apis/resolvePhotoUrl.js +58 -0
  72. package/src/apis/searchForThread.js +154 -0
  73. package/src/apis/sendMessage.js +346 -0
  74. package/src/apis/sendMessageMqtt.js +248 -0
  75. package/src/apis/sendTypingIndicator.js +105 -0
  76. package/src/apis/setMessageReaction.js +38 -0
  77. package/src/apis/setMessageReactionMqtt.js +61 -0
  78. package/src/apis/setThreadTheme.js +260 -0
  79. package/src/apis/setThreadThemeMqtt.js +94 -0
  80. package/src/apis/share.js +107 -0
  81. package/src/apis/shareContact.js +66 -0
  82. package/src/apis/stickers.js +257 -0
  83. package/src/apis/story.js +181 -0
  84. package/src/apis/theme.js +233 -0
  85. package/src/apis/unfriend.js +47 -0
  86. package/src/apis/unsendMessage.js +25 -0
  87. package/src/database/appStateBackup.js +298 -0
  88. package/src/database/models/index.js +56 -0
  89. package/src/database/models/thread.js +31 -0
  90. package/src/database/models/user.js +32 -0
  91. package/src/database/threadData.js +101 -0
  92. package/src/database/userData.js +90 -0
  93. package/src/e2ee/bridge.js +275 -0
  94. package/src/e2ee/index.js +60 -0
  95. package/src/engine/client.js +95 -0
  96. package/src/engine/models/buildAPI.js +152 -0
  97. package/src/engine/models/loginHelper.js +574 -0
  98. package/src/engine/models/setOptions.js +88 -0
  99. package/src/types/index.d.ts +574 -0
  100. package/src/utils/antiSuspension.js +529 -0
  101. package/src/utils/auth-helpers.js +149 -0
  102. package/src/utils/autoReLogin.js +336 -0
  103. package/src/utils/axios.js +436 -0
  104. package/src/utils/cache.js +54 -0
  105. package/src/utils/clients.js +282 -0
  106. package/src/utils/constants.js +410 -0
  107. package/src/utils/formatters/data/formatAttachment.js +370 -0
  108. package/src/utils/formatters/data/formatDelta.js +109 -0
  109. package/src/utils/formatters/index.js +159 -0
  110. package/src/utils/formatters/value/formatCookie.js +91 -0
  111. package/src/utils/formatters/value/formatDate.js +36 -0
  112. package/src/utils/formatters/value/formatID.js +16 -0
  113. package/src/utils/formatters.js +1373 -0
  114. package/src/utils/headers.js +235 -0
  115. package/src/utils/index.js +153 -0
  116. package/src/utils/monitoring.js +333 -0
  117. package/src/utils/rateLimiter.js +319 -0
  118. package/src/utils/tokenRefresh.js +680 -0
  119. package/src/utils/user-agents.js +238 -0
  120. package/src/utils/validation.js +157 -0
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+ module.exports = (defaultFuncs, api, ctx) => {
6
+ return async function searchForThread(searchQuery, callback) {
7
+ let resolveFunc = () => {};
8
+ let rejectFunc = () => {};
9
+ const returnPromise = new Promise((resolve, reject) => {
10
+ resolveFunc = resolve;
11
+ rejectFunc = reject;
12
+ });
13
+
14
+ // Store original callback if provided
15
+ const originalCallback = callback;
16
+
17
+ // Always use a wrapped callback that settles the promise
18
+ callback = (err, result) => {
19
+ if (originalCallback) {
20
+ originalCallback(err, result);
21
+ }
22
+ if (err) return rejectFunc(err);
23
+ resolveFunc(result);
24
+ };
25
+
26
+ if (!searchQuery || typeof searchQuery !== 'string') {
27
+ const error = { error: "searchForThread: searchQuery parameter must be a non-empty string" };
28
+ utils.error("searchForThread", error);
29
+ callback(error);
30
+ return returnPromise;
31
+ }
32
+
33
+ try {
34
+ // Strategy 1: Use GraphQL-based getThreadList and filter locally
35
+ // This bypasses checkpoint issues entirely
36
+ utils.log("searchForThread", "Using GraphQL-based search (bypasses checkpoints)");
37
+
38
+ try {
39
+ // Use getThreadList to fetch threads from INBOX
40
+ // This ensures consistent behavior and avoids tag parameter issues
41
+ const threads = await api.getThreadList(100, null, ["INBOX"]);
42
+
43
+ if (!threads || threads.length === 0) {
44
+ utils.warn("searchForThread", "No threads available in INBOX, trying legacy method");
45
+ throw new Error("No threads available from GraphQL");
46
+ }
47
+
48
+ utils.log("searchForThread", `Retrieved ${threads.length} threads from GraphQL`);
49
+
50
+ // Filter threads by search query (case-insensitive, partial match)
51
+ const searchLower = searchQuery.toLowerCase().trim();
52
+ const matchedThreads = threads.filter(thread => {
53
+ // Search in thread name
54
+ if (thread.threadName && thread.threadName.toLowerCase().includes(searchLower)) {
55
+ return true;
56
+ }
57
+
58
+ // Search in thread ID (exact or partial match)
59
+ if (thread.threadID && thread.threadID.toString().includes(searchQuery)) {
60
+ return true;
61
+ }
62
+
63
+ // Search in participant names
64
+ if (thread.userInfo && Array.isArray(thread.userInfo)) {
65
+ return thread.userInfo.some(user =>
66
+ user.name && user.name.toLowerCase().includes(searchLower)
67
+ );
68
+ }
69
+
70
+ return false;
71
+ });
72
+
73
+ if (matchedThreads.length === 0) {
74
+ callback({
75
+ error: `Could not find thread matching "${searchQuery}".`,
76
+ details: "No threads match your search query. Try a different search term."
77
+ });
78
+ return returnPromise;
79
+ }
80
+
81
+ utils.log("searchForThread", `Found ${matchedThreads.length} matching thread(s) using GraphQL method`);
82
+ callback(null, matchedThreads);
83
+ return returnPromise;
84
+
85
+ } catch (graphqlError) {
86
+ utils.warn("searchForThread", "GraphQL method failed, falling back to legacy AJAX endpoint");
87
+ utils.error("searchForThread GraphQL error", graphqlError);
88
+
89
+ // Strategy 2: Fallback to legacy AJAX endpoint (may trigger checkpoints)
90
+ const form = {
91
+ client: "web_messenger",
92
+ query: searchQuery,
93
+ offset: 0,
94
+ limit: 21,
95
+ index: "fbid"
96
+ };
97
+
98
+ const res = await defaultFuncs.post(
99
+ "https://www.facebook.com/ajax/mercury/search_threads.php",
100
+ ctx.jar,
101
+ form
102
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
103
+
104
+ if (!res) {
105
+ const error = {
106
+ error: "Account checkpoint required - searchForThread is restricted until verification",
107
+ details: "Please verify your account on facebook.com. This function requires additional permissions.",
108
+ errorCode: 1357004,
109
+ errorType: 'CHECKPOINT'
110
+ };
111
+ callback(error);
112
+ return returnPromise;
113
+ }
114
+
115
+ if (res.error) {
116
+ throw res;
117
+ }
118
+
119
+ // Support both legacy payload.threads (object map) and newer payload.mercury_payload.threads (array)
120
+ let threadsData = res.payload?.mercury_payload?.threads || res.payload?.threads;
121
+
122
+ if (!threadsData) {
123
+ callback({
124
+ error: `Could not find thread "${searchQuery}".`,
125
+ details: "The thread may not exist or access may be restricted."
126
+ });
127
+ return returnPromise;
128
+ }
129
+
130
+ // Convert legacy object format to array if needed
131
+ if (!Array.isArray(threadsData)) {
132
+ threadsData = Object.values(threadsData);
133
+ }
134
+
135
+ const threads = threadsData.map(utils.formatThread);
136
+ utils.log("searchForThread", `Found ${threads.length} thread(s) using legacy AJAX method`);
137
+ callback(null, threads);
138
+ }
139
+ } catch (err) {
140
+ // Enhanced error handling for checkpoint errors
141
+ if (err.errorCode === 1357004 || err.errorType === 'CHECKPOINT') {
142
+ err.error = "Account checkpoint required - searchForThread is restricted until verification";
143
+ err.friendlyMessage = "Your account requires verification on facebook.com before using search features";
144
+ } else if (err.error && typeof err.error === 'string' && err.error.includes('checkpoint')) {
145
+ err.friendlyMessage = "Account checkpoint required - searchForThread is restricted until verification";
146
+ }
147
+
148
+ utils.error("searchForThread", err);
149
+ callback(err);
150
+ }
151
+
152
+ return returnPromise;
153
+ };
154
+ };
@@ -0,0 +1,346 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+ const { globalAntiSuspension } = require('../utils/antiSuspension');
5
+
6
+ const allowedProperties = {
7
+ attachment: true,
8
+ url: true,
9
+ sticker: true,
10
+ emoji: true,
11
+ emojiSize: true,
12
+ body: true,
13
+ mentions: true,
14
+ location: true,
15
+ };
16
+
17
+ module.exports = (defaultFuncs, api, ctx) => {
18
+ function getThreadCache() {
19
+ if (!ctx.threadTypeCache) ctx.threadTypeCache = Object.create(null);
20
+ return ctx.threadTypeCache;
21
+ }
22
+
23
+ async function isGroupThread(threadID, explicitIsGroup) {
24
+ if (utils.getType(explicitIsGroup) === "Boolean") return !!explicitIsGroup;
25
+ const tid = threadID.toString();
26
+ const cache = getThreadCache();
27
+ if (Object.prototype.hasOwnProperty.call(cache, tid)) return !!cache[tid];
28
+ try {
29
+ const info = await api.getThreadInfo(tid);
30
+ cache[tid] = !!info.isGroup;
31
+ return !!info.isGroup;
32
+ } catch (_) {
33
+ const fallback = tid.length >= 16;
34
+ cache[tid] = fallback;
35
+ return fallback;
36
+ }
37
+ }
38
+
39
+ function detectAttachmentType(attachment) {
40
+ const path = attachment.path || '';
41
+ const ext = path.toLowerCase().split('.').pop();
42
+
43
+ const audioTypes = ['mp3', 'wav', 'aac', 'm4a', 'ogg', 'opus', 'flac'];
44
+ const videoTypes = ['mp4', 'mov', 'avi', 'mkv', 'webm', 'wmv', 'flv'];
45
+ const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'];
46
+
47
+ if (audioTypes.includes(ext)) return { voice_clip: "true" };
48
+ if (videoTypes.includes(ext)) return { video: "true" };
49
+ if (imageTypes.includes(ext)) return { image: "true" };
50
+ return { file: "true" };
51
+ }
52
+
53
+ async function uploadSingleAttachment(attachment, threadIDHint) {
54
+ if (!utils.isReadableStream(attachment)) {
55
+ throw new Error("Attachment should be a readable stream and not " + utils.getType(attachment) + ".");
56
+ }
57
+ const uploadType = detectAttachmentType(attachment);
58
+ const oksir = await defaultFuncs.postFormData(
59
+ "https://upload.facebook.com/ajax/mercury/upload.php",
60
+ ctx.jar,
61
+ { upload_1024: attachment, ...uploadType },
62
+ {},
63
+ { ...ctx, requestThreadID: threadIDHint }
64
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
65
+
66
+ if (oksir.error) throw new Error(JSON.stringify(oksir));
67
+ return oksir.payload.metadata[0];
68
+ }
69
+
70
+ async function uploadAttachment(attachments, threadIDHint) {
71
+ const CONCURRENT_UPLOADS = 3;
72
+ const uploads = [];
73
+ for (let i = 0; i < attachments.length; i += CONCURRENT_UPLOADS) {
74
+ const batch = attachments.slice(i, i + CONCURRENT_UPLOADS);
75
+ const results = await Promise.all(batch.map(a => uploadSingleAttachment(a, threadIDHint)));
76
+ uploads.push(...results);
77
+ if (i + CONCURRENT_UPLOADS < attachments.length) {
78
+ await globalAntiSuspension.addSmartDelay();
79
+ }
80
+ }
81
+ return uploads;
82
+ }
83
+
84
+ async function getUrl(url) {
85
+ const resData = await defaultFuncs.post(
86
+ "https://www.facebook.com/message_share_attachment/fromURI/",
87
+ ctx.jar,
88
+ { image_height: 960, image_width: 960, uri: url }
89
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
90
+ if (!resData || resData.error || !resData.payload) throw new Error("Invalid url");
91
+ return resData.payload.share_data.share_params;
92
+ }
93
+
94
+ async function sendContent(form, threadID, isSingleUser, messageAndOTID) {
95
+ if (utils.getType(threadID) === "Array") {
96
+ for (let i = 0; i < threadID.length; i++) {
97
+ form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
98
+ }
99
+ form["specific_to_list[" + threadID.length + "]"] = "fbid:" + ctx.userID;
100
+ form["client_thread_id"] = "root:" + messageAndOTID;
101
+ utils.log("sendMessage", "Sending message to multiple users: " + threadID);
102
+ } else {
103
+ if (isSingleUser) {
104
+ form["specific_to_list[0]"] = "fbid:" + threadID;
105
+ form["specific_to_list[1]"] = "fbid:" + ctx.userID;
106
+ form["other_user_fbid"] = threadID;
107
+ form["client_thread_id"] = "root:" + messageAndOTID;
108
+ } else {
109
+ form["thread_fbid"] = threadID;
110
+ }
111
+ }
112
+
113
+ if (ctx.globalOptions.pageID) {
114
+ form["author"] = "fbid:" + ctx.globalOptions.pageID;
115
+ form["specific_to_list[1]"] = "fbid:" + ctx.globalOptions.pageID;
116
+ form["creator_info[creatorID]"] = ctx.userID;
117
+ form["creator_info[creatorType]"] = "direct_admin";
118
+ form["creator_info[labelType]"] = "sent_message";
119
+ form["creator_info[pageID]"] = ctx.globalOptions.pageID;
120
+ form["request_user_id"] = ctx.globalOptions.pageID;
121
+ form["creator_info[profileURI]"] = "https://www.facebook.com/profile.php?id=" + ctx.userID;
122
+ }
123
+
124
+ const resData = await defaultFuncs.post(
125
+ "https://www.facebook.com/messaging/send/",
126
+ ctx.jar,
127
+ form,
128
+ { ...ctx, requestThreadID: threadID }
129
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
130
+
131
+ if (!resData) throw new Error("Send message failed.");
132
+ if (resData.error) {
133
+ if (resData.error === 1545012) {
134
+ utils.warn("sendMessage", "Got error 1545012. This might mean that you're not part of the conversation " + threadID);
135
+ }
136
+ globalAntiSuspension.detectSuspensionSignal(String(resData.error) + ' ' + JSON.stringify(resData));
137
+ throw new Error(JSON.stringify(resData));
138
+ }
139
+
140
+ const messageInfo = resData.payload.actions.reduce((p, v) => {
141
+ return { threadID: v.thread_fbid, messageID: v.message_id, timestamp: v.timestamp } || p;
142
+ }, null);
143
+ return messageInfo;
144
+ }
145
+
146
+ return async (msg, threadID, callback, replyToMessage, isGroup) => {
147
+ if (!callback && (utils.getType(threadID) === "Function" || utils.getType(threadID) === "AsyncFunction")) {
148
+ throw new Error("Pass a threadID as a second argument.");
149
+ }
150
+ if (!replyToMessage && utils.getType(callback) === "String") {
151
+ replyToMessage = callback;
152
+ callback = undefined;
153
+ }
154
+
155
+ // E2EE routing: msg = { body, e2ee: true, chatJid: 'uid@msgr', replyToId?, replyToSenderJid? }
156
+ if (utils.getType(msg) === "Object" && msg.e2ee === true) {
157
+ if (!msg.chatJid) throw new Error("E2EE sendMessage requires msg.chatJid (e.g. '61234@msgr')");
158
+ if (!api.e2ee || !api.e2ee.isAvailable()) throw new Error("E2EE bridge not available. Build with: npm run build:go");
159
+ const status = api.e2ee.isConnected();
160
+ if (!status.connected || !status.e2eeConnected) throw new Error("E2EE not connected. Call api.e2ee.connect() and api.e2ee.connectE2EE() first.");
161
+ return api.e2ee.sendE2EEMessage(msg.chatJid, msg.body || '', msg.replyToId, msg.replyToSenderJid);
162
+ }
163
+
164
+ let resolveFunc = () => {};
165
+ let rejectFunc = () => {};
166
+ let returnPromise = new Promise((resolve, reject) => {
167
+ resolveFunc = resolve;
168
+ rejectFunc = reject;
169
+ });
170
+
171
+ if (!callback) {
172
+ callback = (err, data) => {
173
+ if (err) return rejectFunc(err);
174
+ resolveFunc(data);
175
+ };
176
+ }
177
+
178
+ let msgType = utils.getType(msg);
179
+ let threadIDType = utils.getType(threadID);
180
+ let messageIDType = utils.getType(replyToMessage);
181
+
182
+ if (msgType !== "String" && msgType !== "Object") {
183
+ return callback(new Error("Message should be of type string or object and not " + msgType + "."));
184
+ }
185
+ if (threadIDType !== "Array" && threadIDType !== "Number" && threadIDType !== "String") {
186
+ return callback(new Error("ThreadID should be of type number, string, or array and not " + threadIDType + "."));
187
+ }
188
+ if (replyToMessage && messageIDType !== 'String') {
189
+ return callback(new Error("MessageID should be of type string and not " + messageIDType + "."));
190
+ }
191
+
192
+ if (!ctx.validator.isValidMessage(msg)) {
193
+ return callback(new Error("Invalid message content"));
194
+ }
195
+ const threadIDs = Array.isArray(threadID) ? threadID : [threadID];
196
+ if (!ctx.validator.validateIDArray(threadIDs, ctx.validator.isValidThreadID)) {
197
+ return callback(new Error("Invalid thread ID(s)"));
198
+ }
199
+
200
+ if (msgType === "String") msg = { body: msg };
201
+
202
+ let disallowedProperties = Object.keys(msg).filter(prop => !allowedProperties[prop]);
203
+ if (disallowedProperties.length > 0) {
204
+ return callback(new Error("Dissallowed props: `" + disallowedProperties.join(", ") + "`"));
205
+ }
206
+
207
+ try {
208
+ let messageAndOTID = utils.generateOfflineThreadingID();
209
+ let form = {
210
+ client: "mercury",
211
+ action_type: "ma-type:user-generated-message",
212
+ author: "fbid:" + ctx.userID,
213
+ timestamp: Date.now(),
214
+ timestamp_absolute: "Today",
215
+ timestamp_relative: utils.generateTimestampRelative(),
216
+ timestamp_time_passed: "0",
217
+ is_unread: false,
218
+ is_cleared: false,
219
+ is_forward: false,
220
+ is_filtered_content: false,
221
+ is_filtered_content_bh: false,
222
+ is_filtered_content_account: false,
223
+ is_filtered_content_quasar: false,
224
+ is_filtered_content_invalid_app: false,
225
+ is_spoof_warning: false,
226
+ source: "source:chat:web",
227
+ "source_tags[0]": "source:chat",
228
+ ...(msg.body && { body: msg.body }),
229
+ html_body: false,
230
+ ui_push_phase: "V3",
231
+ status: "0",
232
+ offline_threading_id: messageAndOTID,
233
+ message_id: messageAndOTID,
234
+ threading_id: utils.generateThreadingID(ctx.clientID),
235
+ "ephemeral_ttl_mode:": "0",
236
+ manual_retry_cnt: "0",
237
+ has_attachment: !!(msg.attachment || msg.url || msg.sticker),
238
+ signatureID: utils.getSignatureID(),
239
+ ...(replyToMessage && { replied_to_message_id: replyToMessage })
240
+ };
241
+
242
+ if (msg.location) {
243
+ if (!msg.location.latitude || !msg.location.longitude) {
244
+ return callback(new Error("location property needs both latitude and longitude"));
245
+ }
246
+ form["location_attachment[coordinates][latitude]"] = msg.location.latitude;
247
+ form["location_attachment[coordinates][longitude]"] = msg.location.longitude;
248
+ form["location_attachment[is_current_location]"] = !!msg.location.current;
249
+ }
250
+ if (msg.sticker) form["sticker_id"] = msg.sticker;
251
+ if (msg.attachment) {
252
+ form.image_ids = [];
253
+ form.gif_ids = [];
254
+ form.file_ids = [];
255
+ form.video_ids = [];
256
+ form.audio_ids = [];
257
+ if (utils.getType(msg.attachment) !== "Array") msg.attachment = [msg.attachment];
258
+ const files = await uploadAttachment(msg.attachment, threadID);
259
+ files.forEach(file => {
260
+ const type = Object.keys(file)[0];
261
+ form["" + type + "s"].push(file[type]);
262
+ });
263
+ }
264
+ if (msg.url) {
265
+ form["shareable_attachment[share_type]"] = "100";
266
+ const params = await getUrl(msg.url);
267
+ form["shareable_attachment[share_params]"] = params;
268
+ }
269
+ if (msg.emoji) {
270
+ if (!msg.emojiSize) msg.emojiSize = "medium";
271
+ if (msg.emojiSize !== "small" && msg.emojiSize !== "medium" && msg.emojiSize !== "large") {
272
+ return callback(new Error("emojiSize property is invalid"));
273
+ }
274
+ if (form.body && form.body !== "") return callback(new Error("body is not empty"));
275
+ form.body = msg.emoji;
276
+ form["tags[0]"] = "hot_emoji_size:" + msg.emojiSize;
277
+ }
278
+ if (msg.mentions) {
279
+ for (let i = 0; i < msg.mentions.length; i++) {
280
+ const mention = msg.mentions[i];
281
+ const tag = mention.tag;
282
+ if (typeof tag !== "string") return callback(new Error("Mention tags must be strings."));
283
+ const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
284
+ if (offset < 0) utils.warn("handleMention", 'Mention for "' + tag + '" not found in message string.');
285
+ if (!mention.id) utils.warn("handleMention", "Mention id should be non-null.");
286
+ const id = mention.id || 0;
287
+ const emptyChar = '\u200E';
288
+ form["body"] = emptyChar + msg.body;
289
+ form["profile_xmd[" + i + "][offset]"] = offset + 1;
290
+ form["profile_xmd[" + i + "][length]"] = tag.length;
291
+ form["profile_xmd[" + i + "][id]"] = id;
292
+ form["profile_xmd[" + i + "][type]"] = "p";
293
+ }
294
+ }
295
+
296
+ const isSingleUser = !(await isGroupThread(threadID, isGroup));
297
+
298
+ await globalAntiSuspension.prepareBeforeMessage(threadID, msg.body || '');
299
+
300
+ let typingStarted = false;
301
+ let typingTimeout;
302
+ const shouldSimulateTyping = ctx.globalOptions && ctx.globalOptions.simulateTyping && api.sendTypingIndicator;
303
+ if (shouldSimulateTyping) {
304
+ try {
305
+ await api.sendTypingIndicator(true, threadID);
306
+ typingStarted = true;
307
+
308
+ const msgLen = (msg.body || '').length;
309
+ const typingMs = await globalAntiSuspension.simulateTyping(threadID, msgLen);
310
+ await new Promise(resolve => setTimeout(resolve, typingMs));
311
+
312
+ typingTimeout = setTimeout(() => {
313
+ if (typingStarted) {
314
+ try { api.sendTypingIndicator(false, threadID); } catch (_) {}
315
+ typingStarted = false;
316
+ }
317
+ }, 10000);
318
+ } catch (_) {}
319
+ }
320
+
321
+ try {
322
+ const result = await sendContent(form, threadID, isSingleUser, messageAndOTID);
323
+ callback(null, result);
324
+ } catch (primaryErr) {
325
+ if (api.sendMessageMqtt) {
326
+ try {
327
+ const mqttRes = await api.sendMessageMqtt(msg, threadID, replyToMessage);
328
+ callback(null, mqttRes);
329
+ } catch (fallbackErr) {
330
+ callback(primaryErr);
331
+ }
332
+ } else {
333
+ callback(primaryErr);
334
+ }
335
+ } finally {
336
+ if (typingTimeout) clearTimeout(typingTimeout);
337
+ if (typingStarted) {
338
+ try { await api.sendTypingIndicator(false, threadID); } catch (_) {}
339
+ }
340
+ }
341
+ } catch (err) {
342
+ callback(err);
343
+ }
344
+ return returnPromise;
345
+ };
346
+ };