alicezetion 1.7.8 → 1.7.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. package/.cache/replit/__replit_disk_meta.json +1 -1
  2. package/index.js +568 -490
  3. package/leiamnash/chat.js +452 -0
  4. package/leiamnash/listenMqtt.js +843 -0
  5. package/leiamnash/react.js +109 -0
  6. package/leiamnash/refreshFb_dtsg.js +81 -0
  7. package/leiamnash/seen.js +40 -0
  8. package/package.json +8 -15
  9. package/utils.js +1338 -1047
  10. package/src/listen.js +0 -553
  11. package/src/listenMqtt-Test.js +0 -687
  12. package/src/listenMqtt.js +0 -677
  13. package/test/data/shareAttach.js +0 -146
  14. package/test/data/something.mov +0 -0
  15. package/test/data/test.png +0 -0
  16. package/test/data/test.txt +0 -7
  17. package/test/example-config.json +0 -18
  18. package/test/test-page.js +0 -140
  19. package/test/test.js +0 -385
  20. /package/{src → leiamnash}/addExternalModule.js +0 -0
  21. /package/{src → leiamnash}/addUserToGroup.js +0 -0
  22. /package/{src → leiamnash}/changeAdminStatus.js +0 -0
  23. /package/{src → leiamnash}/changeArchivedStatus.js +0 -0
  24. /package/{src → leiamnash}/changeBio.js +0 -0
  25. /package/{src → leiamnash}/changeBlockedStatus.js +0 -0
  26. /package/{src → leiamnash}/changeGroupImage.js +0 -0
  27. /package/{src → leiamnash}/changeNickname.js +0 -0
  28. /package/{src → leiamnash}/changeThreadColor.js +0 -0
  29. /package/{src → leiamnash}/changeThreadEmoji.js +0 -0
  30. /package/{src → leiamnash}/createNewGroup.js +0 -0
  31. /package/{src → leiamnash}/createPoll.js +0 -0
  32. /package/{src → leiamnash}/deleteMessage.js +0 -0
  33. /package/{src → leiamnash}/deleteThread.js +0 -0
  34. /package/{src → leiamnash}/forwardAttachment.js +0 -0
  35. /package/{src → leiamnash}/forwardMessage.js +0 -0
  36. /package/{src → leiamnash}/getCurrentUserID.js +0 -0
  37. /package/{src → leiamnash}/getEmojiUrl.js +0 -0
  38. /package/{src → leiamnash}/getFriendsList.js +0 -0
  39. /package/{src → leiamnash}/getThreadHistory.js +0 -0
  40. /package/{src → leiamnash}/getThreadHistoryDeprecated.js +0 -0
  41. /package/{src → leiamnash}/getThreadInfo.js +0 -0
  42. /package/{src → leiamnash}/getThreadInfoDeprecated.js +0 -0
  43. /package/{src → leiamnash}/getThreadList.js +0 -0
  44. /package/{src → leiamnash}/getThreadListDeprecated.js +0 -0
  45. /package/{src → leiamnash}/getThreadPictures.js +0 -0
  46. /package/{src → leiamnash}/getUserID.js +0 -0
  47. /package/{src → leiamnash}/getUserInfo.js +0 -0
  48. /package/{src → leiamnash}/handleFriendRequest.js +0 -0
  49. /package/{src → leiamnash}/handleMessageRequest.js +0 -0
  50. /package/{src → leiamnash}/httpGet.js +0 -0
  51. /package/{src → leiamnash}/httpPost.js +0 -0
  52. /package/{src → leiamnash}/logout.js +0 -0
  53. /package/{src → leiamnash}/markAsDelivered.js +0 -0
  54. /package/{src → leiamnash}/markAsRead.js +0 -0
  55. /package/{src/seen.js → leiamnash/markAsReadAll.js} +0 -0
  56. /package/{src → leiamnash}/markAsSeen.js +0 -0
  57. /package/{src → leiamnash}/muteThread.js +0 -0
  58. /package/{src → leiamnash}/removeUserFromGroup.js +0 -0
  59. /package/{src → leiamnash}/resolvePhotoUrl.js +0 -0
  60. /package/{src → leiamnash}/searchForThread.js +0 -0
  61. /package/{src/chat.js → leiamnash/sendMessage.js} +0 -0
  62. /package/{src/leiamnash.js → leiamnash/sendTypingIndicator.js} +0 -0
  63. /package/{src/react.js → leiamnash/setMessageReaction.js} +0 -0
  64. /package/{src → leiamnash}/setPostReaction.js +0 -0
  65. /package/{src → leiamnash}/setTitle.js +0 -0
  66. /package/{src → leiamnash}/threadColors.js +0 -0
  67. /package/{src → leiamnash}/unfriend.js +0 -0
  68. /package/{src → leiamnash}/unsendMessage.js +0 -0
@@ -0,0 +1,452 @@
1
+ "use strict";
2
+
3
+ const utils = require("../utils");
4
+ const log = require("npmlog");
5
+ const bluebird = require("bluebird");
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 = function (defaultFuncs, api, ctx) {
19
+ function uploadAttachment(attachments, callback) {
20
+ const uploads = [];
21
+
22
+ // create an array of promises
23
+ for (let i = 0; i < attachments.length; i++) {
24
+ if (!utils.isReadableStream(attachments[i])) {
25
+ throw {
26
+ error:
27
+ "Attachment should be a readable stream and not " +
28
+ utils.getType(attachments[i]) +
29
+ "."
30
+ };
31
+ }
32
+
33
+ const form = {
34
+ upload_1024: attachments[i],
35
+ voice_clip: "true"
36
+ };
37
+
38
+ uploads.push(
39
+ defaultFuncs
40
+ .postFormData(
41
+ "https://upload.facebook.com/ajax/mercury/upload.php",
42
+ ctx.jar,
43
+ form,
44
+ {}
45
+ )
46
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
47
+ .then(function (resData) {
48
+ if (resData.error) {
49
+ throw resData;
50
+ }
51
+
52
+ // We have to return the data unformatted unless we want to change it
53
+ // back in sendMessage.
54
+ return resData.payload.metadata[0];
55
+ })
56
+ );
57
+ }
58
+
59
+ // resolve all promises
60
+ bluebird
61
+ .all(uploads)
62
+ .then(function (resData) {
63
+ callback(null, resData);
64
+ })
65
+ .catch(function (err) {
66
+ log.error("uploadAttachment", err);
67
+ return callback(err);
68
+ });
69
+ }
70
+
71
+ function getUrl(url, callback) {
72
+ const form = {
73
+ image_height: 960,
74
+ image_width: 960,
75
+ uri: url
76
+ };
77
+
78
+ defaultFuncs
79
+ .post(
80
+ "https://www.facebook.com/message_share_attachment/fromURI/",
81
+ ctx.jar,
82
+ form
83
+ )
84
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
85
+ .then(function (resData) {
86
+ if (resData.error) {
87
+ return callback(resData);
88
+ }
89
+
90
+ if (!resData.payload) {
91
+ return callback({ error: "Invalid url" });
92
+ }
93
+
94
+ callback(null, resData.payload.share_data.share_params);
95
+ })
96
+ .catch(function (err) {
97
+ log.error("getUrl", err);
98
+ return callback(err);
99
+ });
100
+ }
101
+
102
+ function sendContent(form, threadID, isSingleUser, messageAndOTID, callback) {
103
+ // There are three cases here:
104
+ // 1. threadID is of type array, where we're starting a new group chat with users
105
+ // specified in the array.
106
+ // 2. User is sending a message to a specific user.
107
+ // 3. No additional form params and the message goes to an existing group chat.
108
+ if (utils.getType(threadID) === "Array") {
109
+ for (let i = 0; i < threadID.length; i++) {
110
+ form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
111
+ }
112
+ form["specific_to_list[" + threadID.length + "]"] = "fbid:" + (ctx.i_userID || ctx.userID);
113
+ form["client_thread_id"] = "root:" + messageAndOTID;
114
+ log.info("sendMessage", "Sending message to multiple users: " + threadID);
115
+ } else {
116
+ // This means that threadID is the id of a user, and the chat
117
+ // is a single person chat
118
+ if (isSingleUser) {
119
+ form["specific_to_list[0]"] = "fbid:" + threadID;
120
+ form["specific_to_list[1]"] = "fbid:" + (ctx.i_userID || ctx.userID);
121
+ form["other_user_fbid"] = threadID;
122
+ } else {
123
+ form["thread_fbid"] = threadID;
124
+ }
125
+ }
126
+
127
+ if (ctx.globalOptions.pageID) {
128
+ form["author"] = "fbid:" + ctx.globalOptions.pageID;
129
+ form["specific_to_list[1]"] = "fbid:" + ctx.globalOptions.pageID;
130
+ form["creator_info[creatorID]"] = ctx.i_userID || ctx.userID;
131
+ form["creator_info[creatorType]"] = "direct_admin";
132
+ form["creator_info[labelType]"] = "sent_message";
133
+ form["creator_info[pageID]"] = ctx.globalOptions.pageID;
134
+ form["request_user_id"] = ctx.globalOptions.pageID;
135
+ form["creator_info[profileURI]"] =
136
+ "https://www.facebook.com/profile.php?id=" + (ctx.i_userID || ctx.userID);
137
+ }
138
+
139
+ defaultFuncs
140
+ .post("https://www.facebook.com/messaging/send/", ctx.jar, form)
141
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
142
+ .then(function (resData) {
143
+ if (!resData) {
144
+ return callback({ error: "Send message failed." });
145
+ }
146
+
147
+ if (resData.error) {
148
+ if (resData.error === 1545012) {
149
+ log.warn(
150
+ "sendMessage",
151
+ "Got error 1545012. This might mean that you're not part of the conversation " +
152
+ threadID
153
+ );
154
+ }
155
+ else {
156
+ log.error("sendMessage", resData);
157
+ }
158
+ return callback(null, resData);
159
+ }
160
+
161
+ const messageInfo = resData.payload.actions.reduce(function (p, v) {
162
+ return (
163
+ {
164
+ threadID: v.thread_fbid,
165
+ messageID: v.message_id,
166
+ timestamp: v.timestamp
167
+ } || p
168
+ );
169
+ }, null);
170
+
171
+ return callback(null, messageInfo);
172
+ })
173
+ .catch(function (err) {
174
+ log.error("sendMessage", err);
175
+ if (utils.getType(err) == "Object" && err.error === "Not logged in.") {
176
+ ctx.loggedIn = false;
177
+ }
178
+ return callback(err);
179
+ });
180
+ }
181
+
182
+ function send(form, threadID, messageAndOTID, callback, isGroup) {
183
+ // We're doing a query to this to check if the given id is the id of
184
+ // a user or of a group chat. The form will be different depending
185
+ // on that.
186
+ if (utils.getType(threadID) === "Array") {
187
+ sendContent(form, threadID, false, messageAndOTID, callback);
188
+ } else {
189
+ if (utils.getType(isGroup) != "Boolean") {
190
+ // Removed the use of api.getUserInfo() in the old version to reduce account lockout
191
+ sendContent(form, threadID, threadID.toString().length < 16, messageAndOTID, callback);
192
+ } else {
193
+ sendContent(form, threadID, !isGroup, messageAndOTID, callback);
194
+ }
195
+ }
196
+ }
197
+
198
+ function handleUrl(msg, form, callback, cb) {
199
+ if (msg.url) {
200
+ form["shareable_attachment[share_type]"] = "100";
201
+ getUrl(msg.url, function (err, params) {
202
+ if (err) {
203
+ return callback(err);
204
+ }
205
+
206
+ form["shareable_attachment[share_params]"] = params;
207
+ cb();
208
+ });
209
+ } else {
210
+ cb();
211
+ }
212
+ }
213
+
214
+ function handleLocation(msg, form, callback, cb) {
215
+ if (msg.location) {
216
+ if (msg.location.latitude == null || msg.location.longitude == null) {
217
+ return callback({ error: "location property needs both latitude and longitude" });
218
+ }
219
+
220
+ form["location_attachment[coordinates][latitude]"] = msg.location.latitude;
221
+ form["location_attachment[coordinates][longitude]"] = msg.location.longitude;
222
+ form["location_attachment[is_current_location]"] = !!msg.location.current;
223
+ }
224
+
225
+ cb();
226
+ }
227
+
228
+ function handleSticker(msg, form, callback, cb) {
229
+ if (msg.sticker) {
230
+ form["sticker_id"] = msg.sticker;
231
+ }
232
+ cb();
233
+ }
234
+
235
+ function handleEmoji(msg, form, callback, cb) {
236
+ if (msg.emojiSize != null && msg.emoji == null) {
237
+ return callback({ error: "emoji property is empty" });
238
+ }
239
+ if (msg.emoji) {
240
+ if (msg.emojiSize == null) {
241
+ msg.emojiSize = "medium";
242
+ }
243
+ if (
244
+ msg.emojiSize != "small" &&
245
+ msg.emojiSize != "medium" &&
246
+ msg.emojiSize != "large"
247
+ ) {
248
+ return callback({ error: "emojiSize property is invalid" });
249
+ }
250
+ if (form["body"] != null && form["body"] != "") {
251
+ return callback({ error: "body is not empty" });
252
+ }
253
+ form["body"] = msg.emoji;
254
+ form["tags[0]"] = "hot_emoji_size:" + msg.emojiSize;
255
+ }
256
+ cb();
257
+ }
258
+
259
+ function handleAttachment(msg, form, callback, cb) {
260
+ if (msg.attachment) {
261
+ form["image_ids"] = [];
262
+ form["gif_ids"] = [];
263
+ form["file_ids"] = [];
264
+ form["video_ids"] = [];
265
+ form["audio_ids"] = [];
266
+
267
+ if (utils.getType(msg.attachment) !== "Array") {
268
+ msg.attachment = [msg.attachment];
269
+ }
270
+
271
+ uploadAttachment(msg.attachment, function (err, files) {
272
+ if (err) {
273
+ return callback(err);
274
+ }
275
+
276
+ files.forEach(function (file) {
277
+ const key = Object.keys(file);
278
+ const type = key[0]; // image_id, file_id, etc
279
+ form["" + type + "s"].push(file[type]); // push the id
280
+ });
281
+ cb();
282
+ });
283
+ } else {
284
+ cb();
285
+ }
286
+ }
287
+
288
+ function handleMention(msg, form, callback, cb) {
289
+ if (msg.mentions) {
290
+ for (let i = 0; i < msg.mentions.length; i++) {
291
+ const mention = msg.mentions[i];
292
+
293
+ const tag = mention.tag;
294
+ if (typeof tag !== "string") {
295
+ return callback({ error: "Mention tags must be strings." });
296
+ }
297
+
298
+ const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
299
+
300
+ if (offset < 0) {
301
+ log.warn(
302
+ "handleMention",
303
+ 'Mention for "' + tag + '" not found in message string.'
304
+ );
305
+ }
306
+
307
+ if (mention.id == null) {
308
+ log.warn("handleMention", "Mention id should be non-null.");
309
+ }
310
+
311
+ const id = mention.id || 0;
312
+ form["profile_xmd[" + i + "][offset]"] = offset;
313
+ form["profile_xmd[" + i + "][length]"] = tag.length;
314
+ form["profile_xmd[" + i + "][id]"] = id;
315
+ form["profile_xmd[" + i + "][type]"] = "p";
316
+ }
317
+ }
318
+ cb();
319
+ }
320
+
321
+ return function sendMessage(msg, threadID, callback, replyToMessage, isGroup) {
322
+ typeof isGroup == "undefined" ? isGroup = null : "";
323
+ if (
324
+ !callback &&
325
+ (utils.getType(threadID) === "Function" ||
326
+ utils.getType(threadID) === "AsyncFunction")
327
+ ) {
328
+ return threadID({ error: "Pass a threadID as a second argument." });
329
+ }
330
+ if (
331
+ !replyToMessage &&
332
+ utils.getType(callback) === "String"
333
+ ) {
334
+ replyToMessage = callback;
335
+ callback = function () { };
336
+ }
337
+
338
+ let resolveFunc = function () { };
339
+ let rejectFunc = function () { };
340
+ const returnPromise = new Promise(function (resolve, reject) {
341
+ resolveFunc = resolve;
342
+ rejectFunc = reject;
343
+ });
344
+
345
+ if (!callback) {
346
+ callback = function (err, friendList) {
347
+ if (err) {
348
+ return rejectFunc(err);
349
+ }
350
+ resolveFunc(friendList);
351
+ };
352
+ }
353
+
354
+ const msgType = utils.getType(msg);
355
+ const threadIDType = utils.getType(threadID);
356
+ const messageIDType = utils.getType(replyToMessage);
357
+
358
+ if (msgType !== "String" && msgType !== "Object") {
359
+ return callback({
360
+ error:
361
+ "Message should be of type string or object and not " + msgType + "."
362
+ });
363
+ }
364
+
365
+ // Changing this to accomodate an array of users
366
+ if (
367
+ threadIDType !== "Array" &&
368
+ threadIDType !== "Number" &&
369
+ threadIDType !== "String"
370
+ ) {
371
+ return callback({
372
+ error:
373
+ "ThreadID should be of type number, string, or array and not " +
374
+ threadIDType +
375
+ "."
376
+ });
377
+ }
378
+
379
+ if (replyToMessage && messageIDType !== 'String') {
380
+ return callback({
381
+ error:
382
+ "MessageID should be of type string and not " +
383
+ threadIDType +
384
+ "."
385
+ });
386
+ }
387
+
388
+ if (msgType === "String") {
389
+ msg = { body: msg };
390
+ }
391
+
392
+ const disallowedProperties = Object.keys(msg).filter(
393
+ prop => !allowedProperties[prop]
394
+ );
395
+ if (disallowedProperties.length > 0) {
396
+ return callback({
397
+ error: "Dissallowed props: `" + disallowedProperties.join(", ") + "`"
398
+ });
399
+ }
400
+
401
+ const messageAndOTID = utils.generateOfflineThreadingID();
402
+
403
+ const form = {
404
+ client: "mercury",
405
+ action_type: "ma-type:user-generated-message",
406
+ author: "fbid:" + (ctx.i_userID || ctx.userID),
407
+ timestamp: Date.now(),
408
+ timestamp_absolute: "Today",
409
+ timestamp_relative: utils.generateTimestampRelative(),
410
+ timestamp_time_passed: "0",
411
+ is_unread: false,
412
+ is_cleared: false,
413
+ is_forward: false,
414
+ is_filtered_content: false,
415
+ is_filtered_content_bh: false,
416
+ is_filtered_content_account: false,
417
+ is_filtered_content_quasar: false,
418
+ is_filtered_content_invalid_app: false,
419
+ is_spoof_warning: false,
420
+ source: "source:chat:web",
421
+ "source_tags[0]": "source:chat",
422
+ body: msg.body ? msg.body.toString() : "",
423
+ html_body: false,
424
+ ui_push_phase: "V3",
425
+ status: "0",
426
+ offline_threading_id: messageAndOTID,
427
+ message_id: messageAndOTID,
428
+ threading_id: utils.generateThreadingID(ctx.clientID),
429
+ "ephemeral_ttl_mode:": "0",
430
+ manual_retry_cnt: "0",
431
+ has_attachment: !!(msg.attachment || msg.url || msg.sticker),
432
+ signatureID: utils.getSignatureID(),
433
+ replied_to_message_id: replyToMessage
434
+ };
435
+
436
+ handleLocation(msg, form, callback, () =>
437
+ handleSticker(msg, form, callback, () =>
438
+ handleAttachment(msg, form, callback, () =>
439
+ handleUrl(msg, form, callback, () =>
440
+ handleEmoji(msg, form, callback, () =>
441
+ handleMention(msg, form, callback, () =>
442
+ send(form, threadID, messageAndOTID, callback, isGroup)
443
+ )
444
+ )
445
+ )
446
+ )
447
+ )
448
+ );
449
+
450
+ return returnPromise;
451
+ };
452
+ };