alicezetion 1.8.2 → 1.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. package/.cache/replit/__replit_disk_meta.json +1 -1
  2. package/.cache/replit/nix/env.json +1 -1
  3. package/index.js +210 -375
  4. package/package.json +6 -3
  5. package/replit.nix +4 -4
  6. package/src/addExternalModule.js +17 -0
  7. package/src/addUserToGroup.js +113 -0
  8. package/src/changeAdminStatus.js +79 -0
  9. package/src/changeArchivedStatus.js +55 -0
  10. package/src/changeAvatar.js +93 -0
  11. package/src/changeBio.js +77 -0
  12. package/src/changeBlockedStatus.js +47 -0
  13. package/src/changeCover.js +73 -0
  14. package/src/changeGroupImage.js +132 -0
  15. package/src/changeName.js +79 -0
  16. package/src/changeNickname.js +59 -0
  17. package/src/changeThreadColor.js +65 -0
  18. package/src/changeThreadEmoji.js +55 -0
  19. package/{leiamnash → src}/chat.js +28 -3
  20. package/src/createNewGroup.js +86 -0
  21. package/src/createPoll.js +71 -0
  22. package/src/data/getThreadInfo.json +1 -0
  23. package/src/deleteMessage.js +56 -0
  24. package/src/deleteThread.js +56 -0
  25. package/src/forwardAttachment.js +60 -0
  26. package/src/getAccess.js +112 -0
  27. package/src/getAvatarUser.js +78 -0
  28. package/src/getCurrentUserID.js +7 -0
  29. package/src/getEmojiUrl.js +29 -0
  30. package/src/getFriendsList.js +83 -0
  31. package/src/getThreadHistory.js +666 -0
  32. package/{leiamnash → src}/getThreadInfo.js +70 -54
  33. package/src/getThreadList.js +237 -0
  34. package/src/getThreadPictures.js +79 -0
  35. package/src/getUserID.js +66 -0
  36. package/src/getUserInfo.js +163 -0
  37. package/src/handleFriendRequest.js +61 -0
  38. package/src/handleMessageRequest.js +65 -0
  39. package/src/httpGet.js +57 -0
  40. package/src/httpPost.js +57 -0
  41. package/src/httpPostFormData.js +63 -0
  42. package/{leiamnash → src}/listenMqtt.js +16 -6
  43. package/src/listenNotification.js +88 -0
  44. package/src/logout.js +275 -0
  45. package/src/markAsDelivered.js +58 -0
  46. package/src/markAsRead.js +80 -0
  47. package/src/markAsReadAll.js +50 -0
  48. package/src/markAsSeen.js +59 -0
  49. package/src/muteThread.js +52 -0
  50. package/src/react.js +117 -0
  51. package/{leiamnash → src}/refreshFb_dtsg.js +1 -1
  52. package/src/removeUserFromGroup.js +79 -0
  53. package/src/resolvePhotoUrl.js +45 -0
  54. package/src/searchForThread.js +53 -0
  55. package/src/searchStickers.js +52 -0
  56. package/src/seen.js +50 -0
  57. package/src/sendMessage.js +477 -0
  58. package/src/sendMessageMqtt.js +316 -0
  59. package/src/sendTypingIndicator.js +103 -0
  60. package/src/setMessageReaction.js +117 -0
  61. package/src/setPostReaction.js +109 -0
  62. package/src/setTitle.js +86 -0
  63. package/src/threadColors.js +131 -0
  64. package/src/unfriend.js +52 -0
  65. package/src/unsendMessage.js +49 -0
  66. package/utils.js +135 -176
  67. package/.cache/replit/modules.stamp +0 -1
  68. package/leiamnash/addExternalModule.js +0 -15
  69. package/leiamnash/addUserToGroup.js +0 -77
  70. package/leiamnash/changeAdminStatus.js +0 -47
  71. package/leiamnash/changeArchivedStatus.js +0 -41
  72. package/leiamnash/changeAvatar.js +0 -127
  73. package/leiamnash/changeBio.js +0 -64
  74. package/leiamnash/changeBlockedStatus.js +0 -36
  75. package/leiamnash/changeGroupImage.js +0 -105
  76. package/leiamnash/changeNickname.js +0 -43
  77. package/leiamnash/changeThreadColor.js +0 -61
  78. package/leiamnash/changeThreadEmoji.js +0 -41
  79. package/leiamnash/createNewGroup.js +0 -70
  80. package/leiamnash/createPoll.js +0 -59
  81. package/leiamnash/deleteMessage.js +0 -44
  82. package/leiamnash/deleteThread.js +0 -42
  83. package/leiamnash/forwardAttachment.js +0 -47
  84. package/leiamnash/forwardMessage.js +0 -0
  85. package/leiamnash/getCurrentUserID.js +0 -7
  86. package/leiamnash/getEmojiUrl.js +0 -27
  87. package/leiamnash/getFriendsList.js +0 -73
  88. package/leiamnash/getInfoVideo.js +0 -134
  89. package/leiamnash/getThreadHistory.js +0 -537
  90. package/leiamnash/getThreadHistoryDeprecated.js +0 -71
  91. package/leiamnash/getThreadInfoDeprecated.js +0 -56
  92. package/leiamnash/getThreadList.js +0 -213
  93. package/leiamnash/getThreadListDeprecated.js +0 -46
  94. package/leiamnash/getThreadPictures.js +0 -59
  95. package/leiamnash/getUserID.js +0 -61
  96. package/leiamnash/getUserInfo.js +0 -66
  97. package/leiamnash/handleFriendRequest.js +0 -46
  98. package/leiamnash/handleMessageRequest.js +0 -47
  99. package/leiamnash/httpGet.js +0 -47
  100. package/leiamnash/httpPost.js +0 -47
  101. package/leiamnash/logout.js +0 -68
  102. package/leiamnash/markAsDelivered.js +0 -47
  103. package/leiamnash/markAsRead.js +0 -70
  104. package/leiamnash/markAsReadAll.js +0 -40
  105. package/leiamnash/markAsSeen.js +0 -48
  106. package/leiamnash/muteThread.js +0 -45
  107. package/leiamnash/react.js +0 -109
  108. package/leiamnash/removeUserFromGroup.js +0 -45
  109. package/leiamnash/resolvePhotoUrl.js +0 -36
  110. package/leiamnash/searchForThread.js +0 -42
  111. package/leiamnash/seen.js +0 -40
  112. package/leiamnash/sendMessage.js +0 -315
  113. package/leiamnash/sendTypingIndicator.js +0 -70
  114. package/leiamnash/setMessageReaction.js +0 -103
  115. package/leiamnash/setPostReaction.js +0 -63
  116. package/leiamnash/setTitle.js +0 -70
  117. package/leiamnash/threadColors.js +0 -41
  118. package/leiamnash/unfriend.js +0 -42
  119. package/leiamnash/unsendMessage.js +0 -39
  120. /package/{leiamnash → src}/getMessage.js +0 -0
@@ -0,0 +1,316 @@
1
+ var utils = require("../utils");
2
+ var log = require("npmlog");
3
+ var bluebird = require("bluebird");
4
+
5
+ module.exports = function (defaultFuncs, api, ctx) {
6
+ function uploadAttachment(attachments, callback) {
7
+ callback = callback || function () { };
8
+ var uploads = [];
9
+
10
+ // create an array of promises
11
+ for (var i = 0; i < attachments.length; i++) {
12
+ if (!utils.isReadableStream(attachments[i])) {
13
+ throw {
14
+ error:
15
+ "Attachment should be a readable stream and not " +
16
+ utils.getType(attachments[i]) +
17
+ "."
18
+ };
19
+ }
20
+
21
+ var form = {
22
+ upload_1024: attachments[i],
23
+ voice_clip: "true"
24
+ };
25
+
26
+ uploads.push(
27
+ defaultFuncs
28
+ .postFormData(
29
+ "https://upload.facebook.com/ajax/mercury/upload.php",
30
+ ctx.jar,
31
+ form,
32
+ {}
33
+ )
34
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
35
+ .then(function (resData) {
36
+ if (resData.error) {
37
+ throw resData;
38
+ }
39
+
40
+ // We have to return the data unformatted unless we want to change it
41
+ // back in sendMessage.
42
+ return resData.payload.metadata[0];
43
+ })
44
+ );
45
+ }
46
+
47
+ // resolve all promises
48
+ bluebird
49
+ .all(uploads)
50
+ .then(function (resData) {
51
+ callback(null, resData);
52
+ })
53
+ .catch(function (err) {
54
+ log.error("uploadAttachment", err);
55
+ return callback(err);
56
+ });
57
+ }
58
+
59
+ let variance = 0;
60
+ const epoch_id = () => Math.floor(Date.now() * (4194304 + (variance = (variance + 0.1) % 5)));
61
+ const emojiSizes = {
62
+ small: 1,
63
+ medium: 2,
64
+ large: 3
65
+ };
66
+
67
+ function handleEmoji(msg, form, callback, cb) {
68
+ if (msg.emojiSize != null && msg.emoji == null) {
69
+ return callback({ error: "emoji property is empty" });
70
+ }
71
+ if (msg.emoji) {
72
+ if (!msg.emojiSize) {
73
+ msg.emojiSize = "small";
74
+ }
75
+ if (
76
+ msg.emojiSize !== "small" &&
77
+ msg.emojiSize !== "medium" &&
78
+ msg.emojiSize !== "large" &&
79
+ (isNaN(msg.emojiSize) || msg.emojiSize < 1 || msg.emojiSize > 3)
80
+ ) {
81
+ return callback({ error: "emojiSize property is invalid" });
82
+ }
83
+
84
+ form.payload.tasks[0].payload.send_type = 1;
85
+ form.payload.tasks[0].payload.text = msg.emoji;
86
+ form.payload.tasks[0].payload.hot_emoji_size = !isNaN(msg.emojiSize) ? msg.emojiSize : emojiSizes[msg.emojiSize];
87
+ }
88
+ cb();
89
+ }
90
+
91
+ function handleSticker(msg, form, callback, cb) {
92
+ if (msg.sticker) {
93
+ form.payload.tasks[0].payload.send_type = 2;
94
+ form.payload.tasks[0].payload.sticker_id = msg.sticker;
95
+ }
96
+ cb();
97
+ }
98
+
99
+ function handleAttachment(msg, form, callback, cb) {
100
+ if (msg.attachment) {
101
+ form.payload.tasks[0].payload.send_type = 3;
102
+ form.payload.tasks[0].payload.attachment_fbids = [];
103
+ if (form.payload.tasks[0].payload.text == "")
104
+ form.payload.tasks[0].payload.text = null;
105
+ if (utils.getType(msg.attachment) !== "Array") {
106
+ msg.attachment = [msg.attachment];
107
+ }
108
+
109
+ uploadAttachment(msg.attachment, function (err, files) {
110
+ if (err) {
111
+ return callback(err);
112
+ }
113
+
114
+ files.forEach(function (file) {
115
+ var key = Object.keys(file);
116
+ var type = key[0]; // image_id, file_id, etc
117
+ form.payload.tasks[0].payload.attachment_fbids.push(file[type]); // push the id
118
+ });
119
+ cb();
120
+ });
121
+ } else {
122
+ cb();
123
+ }
124
+ }
125
+
126
+
127
+ function handleMention(msg, form, callback, cb) {
128
+ if (msg.mentions) {
129
+ form.payload.tasks[0].payload.send_type = 1;
130
+
131
+ const arrayIds = [];
132
+ const arrayOffsets = [];
133
+ const arrayLengths = [];
134
+ const mention_types = [];
135
+
136
+ for (let i = 0; i < msg.mentions.length; i++) {
137
+ const mention = msg.mentions[i];
138
+
139
+ const tag = mention.tag;
140
+ if (typeof tag !== "string") {
141
+ return callback({ error: "Mention tags must be strings." });
142
+ }
143
+
144
+ const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
145
+
146
+ if (offset < 0) {
147
+ log.warn(
148
+ "handleMention",
149
+ 'Mention for "' + tag + '" not found in message string.'
150
+ );
151
+ }
152
+
153
+ if (mention.id == null) {
154
+ log.warn("handleMention", "Mention id should be non-null.");
155
+ }
156
+
157
+ const id = mention.id || 0;
158
+ arrayIds.push(id);
159
+ arrayOffsets.push(offset);
160
+ arrayLengths.push(tag.length);
161
+ mention_types.push("p");
162
+ }
163
+
164
+ form.payload.tasks[0].payload.mention_data = {
165
+ mention_ids: arrayIds.join(","),
166
+ mention_offsets: arrayOffsets.join(","),
167
+ mention_lengths: arrayLengths.join(","),
168
+ mention_types: mention_types.join(",")
169
+ };
170
+ }
171
+ cb();
172
+ }
173
+
174
+ function handleLocation(msg, form, callback, cb) {
175
+ // this is not working yet
176
+ if (msg.location) {
177
+ if (msg.location.latitude == null || msg.location.longitude == null) {
178
+ return callback({ error: "location property needs both latitude and longitude" });
179
+ }
180
+
181
+ form.payload.tasks[0].payload.send_type = 1;
182
+ form.payload.tasks[0].payload.location_data = {
183
+ coordinates: {
184
+ latitude: msg.location.latitude,
185
+ longitude: msg.location.longitude
186
+ },
187
+ is_current_location: !!msg.location.current,
188
+ is_live_location: !!msg.location.live
189
+ };
190
+ }
191
+
192
+ cb();
193
+ }
194
+
195
+ function send(form, threadID, callback, replyToMessage) {
196
+ if (replyToMessage) {
197
+ form.payload.tasks[0].payload.reply_metadata = {
198
+ reply_source_id: replyToMessage,
199
+ reply_source_type: 1,
200
+ reply_type: 0
201
+ };
202
+ }
203
+ const mqttClient = ctx.mqttClient;
204
+ form.payload.tasks.forEach((task) => {
205
+ task.payload = JSON.stringify(task.payload);
206
+ });
207
+ form.payload = JSON.stringify(form.payload);
208
+ console.log(global.jsonStringifyColor(form, null, 2));
209
+
210
+ return mqttClient.publish("/ls_req", JSON.stringify(form), function (err, data) {
211
+ if (err) {
212
+ console.error('Error publishing message: ', err);
213
+ callback(err);
214
+ } else {
215
+ console.log('Message published successfully with data: ', data);
216
+ callback(null, data);
217
+ }
218
+ });
219
+ }
220
+
221
+ return function sendMessageMqtt(msg, threadID, callback, replyToMessage) {
222
+ if (
223
+ !callback &&
224
+ (utils.getType(threadID) === "Function" ||
225
+ utils.getType(threadID) === "AsyncFunction")
226
+ ) {
227
+ return threadID({ error: "Pass a threadID as a second argument." });
228
+ }
229
+ if (
230
+ !replyToMessage &&
231
+ utils.getType(callback) === "String"
232
+ ) {
233
+ replyToMessage = callback;
234
+ callback = function () { };
235
+ }
236
+
237
+
238
+ if (!callback) {
239
+ callback = function (err, friendList) {
240
+ };
241
+ }
242
+
243
+ var msgType = utils.getType(msg);
244
+ var threadIDType = utils.getType(threadID);
245
+ var messageIDType = utils.getType(replyToMessage);
246
+
247
+ if (msgType !== "String" && msgType !== "Object") {
248
+ return callback({
249
+ error:
250
+ "Message should be of type string or object and not " + msgType + "."
251
+ });
252
+ }
253
+
254
+ if (msgType === "String") {
255
+ msg = { body: msg };
256
+ }
257
+
258
+ const timestamp = Date.now();
259
+ // get full date time
260
+ const epoch = timestamp << 22;
261
+ //const otid = epoch + 0; // TODO replace with randomInt(0, 2**22)
262
+ const otid = epoch + Math.floor(Math.random() * 4194304);
263
+
264
+ const form = {
265
+ app_id: "2220391788200892",
266
+ payload: {
267
+ tasks: [
268
+ {
269
+ label: "46",
270
+ payload: {
271
+ thread_id: threadID.toString(),
272
+ otid: otid.toString(),
273
+ source: 0,
274
+ send_type: 1,
275
+ sync_group: 1,
276
+ text: msg.body != null && msg.body != undefined ? msg.body.toString() : "",
277
+ initiating_source: 1,
278
+ skip_url_preview_gen: 0
279
+ },
280
+ queue_name: threadID.toString(),
281
+ task_id: 0,
282
+ failure_count: null
283
+ },
284
+ {
285
+ label: "21",
286
+ payload: {
287
+ thread_id: threadID.toString(),
288
+ last_read_watermark_ts: Date.now(),
289
+ sync_group: 1
290
+ },
291
+ queue_name: threadID.toString(),
292
+ task_id: 1,
293
+ failure_count: null
294
+ }
295
+ ],
296
+ epoch_id: epoch_id(),
297
+ version_id: "6120284488008082",
298
+ data_trace_id: null
299
+ },
300
+ request_id: 1,
301
+ type: 3
302
+ };
303
+
304
+ handleEmoji(msg, form, callback, function () {
305
+ handleLocation(msg, form, callback, function () {
306
+ handleMention(msg, form, callback, function () {
307
+ handleSticker(msg, form, callback, function () {
308
+ handleAttachment(msg, form, callback, function () {
309
+ send(form, threadID, callback, replyToMessage);
310
+ });
311
+ });
312
+ });
313
+ });
314
+ });
315
+ };
316
+ };
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+
3
+ const utils = require("../utils");
4
+ const log = require("npmlog");
5
+
6
+ module.exports = function (defaultFuncs, api, ctx) {
7
+ function makeTypingIndicator(typ, threadID, callback, isGroup) {
8
+ const form = {
9
+ typ: +typ,
10
+ to: "",
11
+ source: "mercury-chat",
12
+ thread: threadID
13
+ };
14
+
15
+ // Check if thread is a single person chat or a group chat
16
+ // More info on this is in api.sendMessage
17
+ if (utils.getType(isGroup) == "Boolean") {
18
+ if (!isGroup) {
19
+ form.to = threadID;
20
+ }
21
+ defaultFuncs
22
+ .post("https://www.facebook.com/ajax/messaging/typ.php", ctx.jar, form)
23
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
24
+ .then(function (resData) {
25
+ if (resData.error) {
26
+ throw resData;
27
+ }
28
+
29
+ return callback();
30
+ })
31
+ .catch(function (err) {
32
+ log.error("sendTypingIndicator", err);
33
+ if (utils.getType(err) == "Object" && err.error === "Not logged in") {
34
+ ctx.loggedIn = false;
35
+ }
36
+ return callback(err);
37
+ });
38
+ } else {
39
+ api.getUserInfo(threadID, function (err, res) {
40
+ if (err) {
41
+ return callback(err);
42
+ }
43
+
44
+ // If id is single person chat
45
+ if (Object.keys(res).length > 0) {
46
+ form.to = threadID;
47
+ }
48
+
49
+ defaultFuncs
50
+ .post("https://www.facebook.com/ajax/messaging/typ.php", ctx.jar, form)
51
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
52
+ .then(function (resData) {
53
+ if (resData.error) {
54
+ throw resData;
55
+ }
56
+
57
+ return callback();
58
+ })
59
+ .catch(function (err) {
60
+ log.error("sendTypingIndicator", err);
61
+ if (utils.getType(err) == "Object" && err.error === "Not logged in.") {
62
+ ctx.loggedIn = false;
63
+ }
64
+ return callback(err);
65
+ });
66
+ });
67
+ }
68
+ }
69
+
70
+ return function sendTypingIndicator(threadID, callback, isGroup) {
71
+ if (
72
+ utils.getType(callback) !== "Function" &&
73
+ utils.getType(callback) !== "AsyncFunction"
74
+ ) {
75
+ if (callback) {
76
+ log.warn(
77
+ "sendTypingIndicator",
78
+ "callback is not a function - ignoring."
79
+ );
80
+ }
81
+ callback = () => { };
82
+ }
83
+
84
+ makeTypingIndicator(true, threadID, callback, isGroup);
85
+
86
+ return function end(cb) {
87
+ if (
88
+ utils.getType(cb) !== "Function" &&
89
+ utils.getType(cb) !== "AsyncFunction"
90
+ ) {
91
+ if (cb) {
92
+ log.warn(
93
+ "sendTypingIndicator",
94
+ "callback is not a function - ignoring."
95
+ );
96
+ }
97
+ cb = () => { };
98
+ }
99
+
100
+ makeTypingIndicator(false, threadID, cb, isGroup);
101
+ };
102
+ };
103
+ };
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+
3
+ const utils = require("../utils");
4
+ const log = require("npmlog");
5
+
6
+ module.exports = function (defaultFuncs, api, ctx) {
7
+ return function setMessageReaction(reaction, messageID, callback, forceCustomReaction) {
8
+ let resolveFunc = function () { };
9
+ let rejectFunc = function () { };
10
+ const returnPromise = new Promise(function (resolve, reject) {
11
+ resolveFunc = resolve;
12
+ rejectFunc = reject;
13
+ });
14
+
15
+ if (!callback) {
16
+ callback = function (err, friendList) {
17
+ if (err) {
18
+ return rejectFunc(err);
19
+ }
20
+ resolveFunc(friendList);
21
+ };
22
+ }
23
+
24
+ switch (reaction) {
25
+ case "\uD83D\uDE0D": //:heart_eyes:
26
+ case "\uD83D\uDE06": //:laughing:
27
+ case "\uD83D\uDE2E": //:open_mouth:
28
+ case "\uD83D\uDE22": //:cry:
29
+ case "\uD83D\uDE20": //:angry:
30
+ case "\uD83D\uDC4D": //:thumbsup:
31
+ case "\uD83D\uDC4E": //:thumbsdown:
32
+ case "\u2764": //:heart:
33
+ case "\uD83D\uDC97": //:glowingheart:
34
+ case "":
35
+ //valid
36
+ break;
37
+ case ":heart_eyes:":
38
+ case ":love:":
39
+ reaction = "\uD83D\uDE0D";
40
+ break;
41
+ case ":laughing:":
42
+ case ":haha:":
43
+ reaction = "\uD83D\uDE06";
44
+ break;
45
+ case ":open_mouth:":
46
+ case ":wow:":
47
+ reaction = "\uD83D\uDE2E";
48
+ break;
49
+ case ":cry:":
50
+ case ":sad:":
51
+ reaction = "\uD83D\uDE22";
52
+ break;
53
+ case ":angry:":
54
+ reaction = "\uD83D\uDE20";
55
+ break;
56
+ case ":thumbsup:":
57
+ case ":like:":
58
+ reaction = "\uD83D\uDC4D";
59
+ break;
60
+ case ":thumbsdown:":
61
+ case ":dislike:":
62
+ reaction = "\uD83D\uDC4E";
63
+ break;
64
+ case ":heart:":
65
+ reaction = "\u2764";
66
+ break;
67
+ case ":glowingheart:":
68
+ reaction = "\uD83D\uDC97";
69
+ break;
70
+ default:
71
+ if (forceCustomReaction) {
72
+ break;
73
+ }
74
+ return callback({ error: "Reaction is not a valid emoji." });
75
+ }
76
+
77
+ const variables = {
78
+ data: {
79
+ client_mutation_id: ctx.clientMutationId++,
80
+ actor_id: ctx.i_userID || ctx.userID,
81
+ action: reaction == "" ? "REMOVE_REACTION" : "ADD_REACTION",
82
+ message_id: messageID,
83
+ reaction: reaction
84
+ }
85
+ };
86
+
87
+ const qs = {
88
+ doc_id: "1491398900900362",
89
+ variables: JSON.stringify(variables),
90
+ dpr: 1
91
+ };
92
+
93
+ defaultFuncs
94
+ .postFormData(
95
+ "https://www.facebook.com/webgraphql/mutation/",
96
+ ctx.jar,
97
+ {},
98
+ qs
99
+ )
100
+ .then(utils.parseAndCheckLogin(ctx.jar, defaultFuncs))
101
+ .then(function (resData) {
102
+ if (!resData) {
103
+ throw { error: "setReaction returned empty object." };
104
+ }
105
+ if (resData.error) {
106
+ throw resData;
107
+ }
108
+ callback(null);
109
+ })
110
+ .catch(function (err) {
111
+ log.error("setReaction", err);
112
+ return callback(err);
113
+ });
114
+
115
+ return returnPromise;
116
+ };
117
+ };
@@ -0,0 +1,109 @@
1
+ /**
2
+ * @fix by NTKhang
3
+ * update as Thursday, 10 February 2022
4
+ * do not remove the author name to get more updates
5
+ */
6
+
7
+ "use strict";
8
+
9
+ const utils = require("../utils");
10
+ const log = require("npmlog");
11
+
12
+ function formatData(resData) {
13
+ return {
14
+ viewer_feedback_reaction_info: resData.feedback_react.feedback.viewer_feedback_reaction_info,
15
+ supported_reactions: resData.feedback_react.feedback.supported_reactions,
16
+ top_reactions: resData.feedback_react.feedback.top_reactions.edges,
17
+ reaction_count: resData.feedback_react.feedback.reaction_count
18
+ };
19
+ }
20
+
21
+ module.exports = function (defaultFuncs, api, ctx) {
22
+ return function setPostReaction(postID, type, callback) {
23
+ let resolveFunc = function () { };
24
+ let rejectFunc = function () { };
25
+ const returnPromise = new Promise(function (resolve, reject) {
26
+ resolveFunc = resolve;
27
+ rejectFunc = reject;
28
+ });
29
+
30
+ if (!callback) {
31
+ if (utils.getType(type) === "Function" || utils.getType(type) === "AsyncFunction") {
32
+ callback = type;
33
+ type = 0;
34
+ }
35
+ else {
36
+ callback = function (err, data) {
37
+ if (err) {
38
+ return rejectFunc(err);
39
+ }
40
+ resolveFunc(data);
41
+ };
42
+ }
43
+ }
44
+
45
+ const map = {
46
+ unlike: 0,
47
+ like: 1,
48
+ heart: 2,
49
+ love: 16,
50
+ haha: 4,
51
+ wow: 3,
52
+ sad: 7,
53
+ angry: 8
54
+ };
55
+
56
+ if (utils.getType(type) !== "Number" && utils.getType(type) === "String") {
57
+ type = map[type.toLowerCase()];
58
+ }
59
+
60
+ if (utils.getType(type) !== "Number" && utils.getType(type) !== "String") {
61
+ throw {
62
+ error: "setPostReaction: Invalid reaction type"
63
+ };
64
+ }
65
+
66
+ if (type != 0 && !type) {
67
+ throw {
68
+ error: "setPostReaction: Invalid reaction type"
69
+ };
70
+ }
71
+
72
+ const form = {
73
+ av: ctx.i_userID || ctx.userID,
74
+ fb_api_caller_class: "RelayModern",
75
+ fb_api_req_friendly_name: "CometUFIFeedbackReactMutation",
76
+ doc_id: "4769042373179384",
77
+ variables: JSON.stringify({
78
+ input: {
79
+ actor_id: ctx.i_userID || ctx.userID,
80
+ feedback_id: (new Buffer.from("feedback:" + postID)).toString("base64"),
81
+ feedback_reaction: type,
82
+ feedback_source: "OBJECT",
83
+ is_tracking_encrypted: true,
84
+ tracking: [],
85
+ session_id: "f7dd50dd-db6e-4598-8cd9-561d5002b423",
86
+ client_mutation_id: Math.round(Math.random() * 19).toString()
87
+ },
88
+ useDefaultActor: false,
89
+ scale: 3
90
+ })
91
+ };
92
+
93
+ defaultFuncs
94
+ .post("https://www.facebook.com/api/graphql/", ctx.jar, form)
95
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
96
+ .then(function (resData) {
97
+ if (resData.errors) {
98
+ throw resData;
99
+ }
100
+ return callback(null, formatData(resData.data));
101
+ })
102
+ .catch(function (err) {
103
+ log.error("setPostReaction", err);
104
+ return callback(err);
105
+ });
106
+
107
+ return returnPromise;
108
+ };
109
+ };