@lazyneoaz/metachat 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 (122) hide show
  1. package/LICENSE +3 -0
  2. package/README.md +199 -0
  3. package/index.js +2 -0
  4. package/package.json +86 -0
  5. package/src/apis/addExternalModule.js +24 -0
  6. package/src/apis/addUserToGroup.js +108 -0
  7. package/src/apis/changeAdminStatus.js +148 -0
  8. package/src/apis/changeArchivedStatus.js +61 -0
  9. package/src/apis/changeAvatar.js +103 -0
  10. package/src/apis/changeBio.js +69 -0
  11. package/src/apis/changeBlockedStatus.js +54 -0
  12. package/src/apis/changeGroupImage.js +136 -0
  13. package/src/apis/changeThreadColor.js +116 -0
  14. package/src/apis/changeThreadEmoji.js +53 -0
  15. package/src/apis/comment.js +207 -0
  16. package/src/apis/createAITheme.js +129 -0
  17. package/src/apis/createNewGroup.js +79 -0
  18. package/src/apis/createPoll.js +73 -0
  19. package/src/apis/deleteMessage.js +44 -0
  20. package/src/apis/deleteThread.js +52 -0
  21. package/src/apis/editMessage.js +70 -0
  22. package/src/apis/emoji.js +124 -0
  23. package/src/apis/enableAutoSaveAppState.js +69 -0
  24. package/src/apis/fetchThemeData.js +113 -0
  25. package/src/apis/follow.js +81 -0
  26. package/src/apis/forwardAttachment.js +195 -0
  27. package/src/apis/forwardMessage.js +52 -0
  28. package/src/apis/friend.js +243 -0
  29. package/src/apis/gcmember.js +122 -0
  30. package/src/apis/gcname.js +123 -0
  31. package/src/apis/gcrule.js +119 -0
  32. package/src/apis/getAccess.js +111 -0
  33. package/src/apis/getBotInfo.js +88 -0
  34. package/src/apis/getBotInitialData.js +43 -0
  35. package/src/apis/getEmojiUrl.js +40 -0
  36. package/src/apis/getFriendsList.js +79 -0
  37. package/src/apis/getMessage.js +423 -0
  38. package/src/apis/getTheme.js +123 -0
  39. package/src/apis/getThemeInfo.js +116 -0
  40. package/src/apis/getThemePictures.js +87 -0
  41. package/src/apis/getThreadColors.js +119 -0
  42. package/src/apis/getThreadHistory.js +239 -0
  43. package/src/apis/getThreadInfo.js +271 -0
  44. package/src/apis/getThreadList.js +236 -0
  45. package/src/apis/getThreadPictures.js +58 -0
  46. package/src/apis/getUserID.js +117 -0
  47. package/src/apis/getUserInfo.js +513 -0
  48. package/src/apis/getUserInfoV2.js +146 -0
  49. package/src/apis/handleFriendRequest.js +66 -0
  50. package/src/apis/handleMessageRequest.js +50 -0
  51. package/src/apis/httpGet.js +63 -0
  52. package/src/apis/httpPost.js +89 -0
  53. package/src/apis/httpPostFormData.js +69 -0
  54. package/src/apis/listenMqtt.js +1081 -0
  55. package/src/apis/listenSpeed.js +178 -0
  56. package/src/apis/logout.js +63 -0
  57. package/src/apis/markAsDelivered.js +47 -0
  58. package/src/apis/markAsRead.js +82 -0
  59. package/src/apis/markAsReadAll.js +40 -0
  60. package/src/apis/markAsSeen.js +70 -0
  61. package/src/apis/mqttDeltaValue.js +252 -0
  62. package/src/apis/muteThread.js +45 -0
  63. package/src/apis/nickname.js +132 -0
  64. package/src/apis/notes.js +163 -0
  65. package/src/apis/pinMessage.js +150 -0
  66. package/src/apis/produceMetaTheme.js +160 -0
  67. package/src/apis/realtime.js +182 -0
  68. package/src/apis/refreshFb_dtsg.js +94 -0
  69. package/src/apis/removeUserFromGroup.js +117 -0
  70. package/src/apis/resolvePhotoUrl.js +58 -0
  71. package/src/apis/scheduler.js +129 -0
  72. package/src/apis/searchForThread.js +154 -0
  73. package/src/apis/sendEffect.js +311 -0
  74. package/src/apis/sendMessage.js +341 -0
  75. package/src/apis/sendMessageMqtt.js +271 -0
  76. package/src/apis/sendTypingIndicator.js +74 -0
  77. package/src/apis/setMessageReaction.js +27 -0
  78. package/src/apis/setMessageReactionMqtt.js +61 -0
  79. package/src/apis/setPostReaction.js +118 -0
  80. package/src/apis/setThreadTheme.js +210 -0
  81. package/src/apis/setThreadThemeMqtt.js +94 -0
  82. package/src/apis/setTitle.js +26 -0
  83. package/src/apis/share.js +106 -0
  84. package/src/apis/shareContact.js +66 -0
  85. package/src/apis/stickers.js +257 -0
  86. package/src/apis/story.js +181 -0
  87. package/src/apis/theme.js +233 -0
  88. package/src/apis/unfriend.js +47 -0
  89. package/src/apis/unsendMessage.js +17 -0
  90. package/src/apis/uploadAttachment.js +87 -0
  91. package/src/database/appStateBackup.js +189 -0
  92. package/src/database/models/index.js +56 -0
  93. package/src/database/models/thread.js +31 -0
  94. package/src/database/models/user.js +32 -0
  95. package/src/database/threadData.js +101 -0
  96. package/src/database/userData.js +90 -0
  97. package/src/engine/client.js +92 -0
  98. package/src/engine/models/buildAPI.js +118 -0
  99. package/src/engine/models/loginHelper.js +492 -0
  100. package/src/engine/models/setOptions.js +88 -0
  101. package/src/types/index.d.ts +498 -0
  102. package/src/utils/antiSuspension.js +516 -0
  103. package/src/utils/auth-helpers.js +149 -0
  104. package/src/utils/autoReLogin.js +239 -0
  105. package/src/utils/axios.js +368 -0
  106. package/src/utils/cache.js +54 -0
  107. package/src/utils/clients.js +279 -0
  108. package/src/utils/constants.js +525 -0
  109. package/src/utils/formatters/data/formatAttachment.js +370 -0
  110. package/src/utils/formatters/data/formatDelta.js +109 -0
  111. package/src/utils/formatters/index.js +159 -0
  112. package/src/utils/formatters/value/formatCookie.js +91 -0
  113. package/src/utils/formatters/value/formatDate.js +36 -0
  114. package/src/utils/formatters/value/formatID.js +16 -0
  115. package/src/utils/formatters.js +1369 -0
  116. package/src/utils/headers.js +235 -0
  117. package/src/utils/index.js +153 -0
  118. package/src/utils/monitoring.js +333 -0
  119. package/src/utils/rateLimiter.js +251 -0
  120. package/src/utils/tokenRefresh.js +285 -0
  121. package/src/utils/user-agents.js +238 -0
  122. package/src/utils/validation.js +157 -0
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+ module.exports = (defaultFuncs, api, ctx) => {
6
+ return async function removeUserFromGroup(userID, threadID, callback) {
7
+ let resolveFunc = () => {};
8
+ let rejectFunc = () => {};
9
+ const returnPromise = new Promise((resolve, reject) => {
10
+ resolveFunc = resolve;
11
+ rejectFunc = reject;
12
+ });
13
+
14
+ if (!callback) {
15
+ callback = (err, result) => {
16
+ if (err) return rejectFunc(err);
17
+ resolveFunc(result);
18
+ };
19
+ }
20
+
21
+ try {
22
+ if (utils.getType(threadID) !== "Number" && utils.getType(threadID) !== "String") {
23
+ throw new Error("threadID should be of type Number or String");
24
+ }
25
+ if (utils.getType(userID) !== "Number" && utils.getType(userID) !== "String") {
26
+ throw new Error("userID should be of type Number or String");
27
+ }
28
+
29
+ if (ctx.mqttClient) {
30
+ const reqID = ++ctx.wsReqNumber;
31
+ const taskID = ++ctx.wsTaskNumber;
32
+
33
+ const payload = {
34
+ epoch_id: utils.generateOfflineThreadingID(),
35
+ tasks: [
36
+ {
37
+ failure_count: null,
38
+ label: '140',
39
+ payload: JSON.stringify({
40
+ thread_id: threadID,
41
+ contact_id: userID,
42
+ sync_group: 1
43
+ }),
44
+ queue_name: 'remove_participant_v2',
45
+ task_id: taskID
46
+ }
47
+ ],
48
+ version_id: '8798795233522156'
49
+ };
50
+
51
+ const form = JSON.stringify({
52
+ app_id: "2220391788200892",
53
+ payload: JSON.stringify(payload),
54
+ request_id: reqID,
55
+ type: 3
56
+ });
57
+
58
+ let responseHandled = false;
59
+ const handleRes = (topic, message) => {
60
+ if (topic !== "/ls_resp" || responseHandled) return;
61
+ let jsonMsg;
62
+ try {
63
+ jsonMsg = JSON.parse(message.toString());
64
+ jsonMsg.payload = JSON.parse(jsonMsg.payload);
65
+ } catch {
66
+ return;
67
+ }
68
+ if (jsonMsg.request_id !== reqID) return;
69
+ responseHandled = true;
70
+ ctx.mqttClient.removeListener("message", handleRes);
71
+ callback(null, { success: true });
72
+ resolveFunc({ success: true });
73
+ };
74
+
75
+ const timeout = setTimeout(() => {
76
+ if (!responseHandled) {
77
+ responseHandled = true;
78
+ ctx.mqttClient.removeListener("message", handleRes);
79
+ const err = new Error("MQTT request timeout");
80
+ callback(err);
81
+ rejectFunc(err);
82
+ }
83
+ }, 30000);
84
+
85
+ ctx.mqttClient.on("message", handleRes);
86
+ ctx.mqttClient.publish("/ls_req", form, { qos: 1, retain: false }, (err) => {
87
+ if (err && !responseHandled) {
88
+ responseHandled = true;
89
+ clearTimeout(timeout);
90
+ ctx.mqttClient.removeListener("message", handleRes);
91
+ callback(err);
92
+ rejectFunc(err);
93
+ }
94
+ });
95
+ } else {
96
+ const form = {
97
+ uid: userID,
98
+ tid: threadID
99
+ };
100
+
101
+ const res = await defaultFuncs.post("https://www.facebook.com/chat/remove_participants", ctx.jar, form)
102
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs));
103
+
104
+ if (!res || res.error) {
105
+ throw res || new Error("Remove from group failed");
106
+ }
107
+
108
+ callback(null, { success: true });
109
+ }
110
+ } catch (err) {
111
+ utils.error("removeUserFromGroup", err);
112
+ callback(err);
113
+ }
114
+
115
+ return returnPromise;
116
+ };
117
+ };
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+ /**
6
+ * @module resolvePhotoUrl
7
+ * @description Fetches the direct URL of a Facebook photo using its photo ID.
8
+ * @param {Object} defaultFuncs - An object containing default request functions.
9
+ * @param {Object} api - Facebook API object (unused here but kept for compatibility).
10
+ * @param {Object} ctx - Context object containing cookies (jar) and other session info.
11
+ * @returns {Function} resolvePhotoUrl - A function that takes a photo ID and optional callback, and returns a Promise resolving to the photo URL.
12
+ */
13
+ module.exports = function (defaultFuncs, api, ctx) {
14
+ /**
15
+ * @function resolvePhotoUrl
16
+ * @param {string} photoID - The ID of the Facebook photo to resolve.
17
+ * @param {Function} [callback] - Optional Node-style callback function `(err, photoUrl)`.
18
+ * @returns {Promise<string>} A Promise that resolves to the direct photo URL.
19
+ */
20
+ return function resolvePhotoUrl(photoID, 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, photoUrl) {
30
+ if (err) {
31
+ return rejectFunc(err);
32
+ }
33
+ resolveFunc(photoUrl);
34
+ };
35
+ }
36
+
37
+ defaultFuncs
38
+ .get("https://www.facebook.com/mercury/attachments/photo", ctx.jar, {
39
+ photo_id: photoID,
40
+ })
41
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
42
+ .then((resData) => {
43
+ if (resData.error) {
44
+ throw resData;
45
+ }
46
+
47
+ const photoUrl = resData.jsmods.require[0][3][0];
48
+
49
+ return callback(null, photoUrl);
50
+ })
51
+ .catch((err) => {
52
+ utils.error("resolvePhotoUrl", err);
53
+ return callback(err);
54
+ });
55
+
56
+ return returnPromise;
57
+ };
58
+ };
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+
3
+ module.exports = function (defaultFuncs, api, ctx) {
4
+ const scheduledMessages = new Map();
5
+ let nextId = 1;
6
+
7
+ function toTimestamp(when) {
8
+ if (when instanceof Date) return when.getTime();
9
+ if (typeof when === "number") return when;
10
+ if (typeof when === "string") return new Date(when).getTime();
11
+ return NaN;
12
+ }
13
+
14
+ function scheduleMessage(message, threadID, when, options) {
15
+ options = options || {};
16
+ const timestamp = toTimestamp(when);
17
+ if (isNaN(timestamp)) throw new Error("Invalid 'when'. Must be Date, number (ms timestamp), or ISO string.");
18
+ const now = Date.now();
19
+ if (timestamp <= now) throw new Error("Scheduled time must be in the future.");
20
+
21
+ const id = `scheduled_${nextId++}_${now}`;
22
+ const delay = timestamp - now;
23
+
24
+ const scheduled = {
25
+ id,
26
+ message,
27
+ threadID,
28
+ timestamp,
29
+ createdAt: now,
30
+ options: {
31
+ replyMessageID: options.replyMessageID || null,
32
+ callback: options.callback || null,
33
+ },
34
+ cancelled: false,
35
+ timeout: null,
36
+ };
37
+
38
+ scheduled.timeout = setTimeout(() => {
39
+ if (scheduled.cancelled) return;
40
+ const sendFn = api.sendMessage || api.sendMessageMqtt;
41
+ if (!sendFn) return;
42
+ Promise.resolve(
43
+ sendFn(message, threadID, scheduled.options.callback || (() => {}), scheduled.options.replyMessageID)
44
+ ).then(() => {
45
+ scheduledMessages.delete(id);
46
+ }).catch(() => {
47
+ scheduledMessages.delete(id);
48
+ });
49
+ }, delay);
50
+
51
+ scheduledMessages.set(id, scheduled);
52
+ return id;
53
+ }
54
+
55
+ function cancelScheduledMessage(id) {
56
+ const s = scheduledMessages.get(id);
57
+ if (!s || s.cancelled) return false;
58
+ clearTimeout(s.timeout);
59
+ s.cancelled = true;
60
+ scheduledMessages.delete(id);
61
+ return true;
62
+ }
63
+
64
+ function getScheduledMessage(id) {
65
+ const s = scheduledMessages.get(id);
66
+ if (!s || s.cancelled) return null;
67
+ return {
68
+ id: s.id,
69
+ message: s.message,
70
+ threadID: s.threadID,
71
+ timestamp: s.timestamp,
72
+ createdAt: s.createdAt,
73
+ options: { ...s.options },
74
+ timeUntilSend: s.timestamp - Date.now(),
75
+ };
76
+ }
77
+
78
+ function listScheduledMessages() {
79
+ const now = Date.now();
80
+ return Array.from(scheduledMessages.values())
81
+ .filter(s => !s.cancelled)
82
+ .map(s => ({
83
+ id: s.id,
84
+ message: s.message,
85
+ threadID: s.threadID,
86
+ timestamp: s.timestamp,
87
+ createdAt: s.createdAt,
88
+ options: { ...s.options },
89
+ timeUntilSend: s.timestamp - now,
90
+ }))
91
+ .sort((a, b) => a.timestamp - b.timestamp);
92
+ }
93
+
94
+ function cancelAllScheduledMessages() {
95
+ let count = 0;
96
+ for (const id of Array.from(scheduledMessages.keys())) {
97
+ if (cancelScheduledMessage(id)) count++;
98
+ }
99
+ return count;
100
+ }
101
+
102
+ function getScheduledCount() {
103
+ return scheduledMessages.size;
104
+ }
105
+
106
+ const cleanupInterval = setInterval(() => {
107
+ const now = Date.now();
108
+ for (const [id, s] of scheduledMessages.entries()) {
109
+ if (s.cancelled || s.timestamp < now) scheduledMessages.delete(id);
110
+ }
111
+ }, 5 * 60 * 1000);
112
+
113
+ function destroy() {
114
+ clearInterval(cleanupInterval);
115
+ return cancelAllScheduledMessages();
116
+ }
117
+
118
+ ctx._scheduler = { destroy };
119
+
120
+ return {
121
+ scheduleMessage,
122
+ cancelScheduledMessage,
123
+ getScheduledMessage,
124
+ listScheduledMessages,
125
+ cancelAllScheduledMessages,
126
+ getScheduledCount,
127
+ destroy,
128
+ };
129
+ };
@@ -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
+ };