stfca 1.0.2 → 1.0.3

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 (141) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/README.md +45 -2
  3. package/checkUpdate.js +109 -0
  4. package/index.js +460 -1
  5. package/package.json +22 -46
  6. package/src/{api/action/addExternalModule.js → addExternalModule.js} +25 -25
  7. package/src/addUserToGroup.js +115 -0
  8. package/src/changeAdminStatus.js +103 -0
  9. package/src/changeArchivedStatus.js +55 -0
  10. package/src/{api/action/changeAvatar.js → changeAvatar.js} +136 -137
  11. package/src/changeAvatarV2.js +86 -0
  12. package/src/changeAvt.js +85 -0
  13. package/src/{api/action/changeBio.js → changeBio.js} +76 -75
  14. package/src/{api/messaging/changeBlockedStatus.js → changeBlockedStatus.js} +49 -48
  15. package/src/changeBlockedStatusMqtt.js +80 -0
  16. package/src/changeCover.js +72 -0
  17. package/src/changeGroupImage.js +135 -0
  18. package/src/changeName.js +79 -0
  19. package/src/changeNickname.js +59 -0
  20. package/src/changeThreadColor.js +65 -0
  21. package/src/changeThreadEmoji.js +55 -0
  22. package/src/changeUsername.js +59 -0
  23. package/src/createCommentPost.js +230 -0
  24. package/src/{api/messaging/createNewGroup.js → createNewGroup.js} +88 -88
  25. package/src/createPoll.js +71 -0
  26. package/src/createPost.js +276 -0
  27. package/src/{api/messaging/deleteMessage.js → deleteMessage.js} +56 -56
  28. package/src/{api/messaging/deleteThread.js → deleteThread.js} +56 -56
  29. package/src/editMessage.js +68 -0
  30. package/src/editMessageOld.js +67 -0
  31. package/src/follow.js +74 -0
  32. package/src/forwardAttachment.js +60 -0
  33. package/src/getAccess.js +112 -0
  34. package/src/getAvatarUser.js +78 -0
  35. package/src/{api/action/getCurrentUserID.js → getCurrentUserID.js} +7 -7
  36. package/src/{api/messaging/getEmojiUrl.js → getEmojiUrl.js} +2 -2
  37. package/src/{api/messaging/getFriendsList.js → getFriendsList.js} +83 -82
  38. package/src/{api/messaging/getMessage.js → getMessage.js} +847 -829
  39. package/src/getRegion.js +7 -0
  40. package/src/{api/threads/getThreadHistory.js → getThreadHistory.js} +680 -664
  41. package/src/getThreadHistoryDeprecated.js +71 -0
  42. package/src/getThreadInfo.js +232 -0
  43. package/src/getThreadInfoDeprecated.js +56 -0
  44. package/src/getThreadList.js +213 -0
  45. package/src/getThreadListDeprecated.js +46 -0
  46. package/src/getThreadPictures.js +59 -0
  47. package/src/getUID.js +119 -0
  48. package/src/{api/users/getUserID.js → getUserID.js} +61 -65
  49. package/src/getUserInfo.js +66 -0
  50. package/src/handleFriendRequest.js +46 -0
  51. package/src/handleMessageRequest.js +47 -0
  52. package/src/httpGet.js +49 -0
  53. package/src/httpPost.js +48 -0
  54. package/src/listenMqtt.js +833 -0
  55. package/src/logout.js +75 -0
  56. package/src/markAsDelivered.js +47 -0
  57. package/src/markAsRead.js +70 -0
  58. package/src/markAsReadAll.js +40 -0
  59. package/src/markAsSeen.js +48 -0
  60. package/src/muteThread.js +45 -0
  61. package/src/refreshFb_dtsg.js +89 -0
  62. package/src/removeUserFromGroup.js +79 -0
  63. package/src/{api/messaging/resolvePhotoUrl.js → resolvePhotoUrl.js} +45 -43
  64. package/src/{api/messaging/searchForThread.js → searchForThread.js} +53 -52
  65. package/src/searchStickers.js +53 -0
  66. package/src/sendMessage.js +379 -0
  67. package/src/{api/messaging/sendMessageMqtt.js → sendMessageMqtt.js} +322 -323
  68. package/src/sendTypingIndicator.js +101 -0
  69. package/src/sendTypingIndicatorV2.js +28 -0
  70. package/src/setMessageReaction.js +122 -0
  71. package/src/setMessageReactionMqtt.js +62 -0
  72. package/src/{api/action/setPostReaction.js → setPostReaction.js} +112 -106
  73. package/src/setStoryReaction.js +64 -0
  74. package/src/setTitle.js +90 -0
  75. package/src/shareContact.js +110 -0
  76. package/src/shareLink.js +59 -0
  77. package/src/stopListenMqtt.js +23 -0
  78. package/src/{api/messaging/threadColors.js → threadColors.js} +131 -128
  79. package/src/{api/action/unfriend.js → unfriend.js} +52 -54
  80. package/src/unsendMessage.js +45 -0
  81. package/src/{api/messaging/uploadAttachment.js → uploadAttachment.js} +93 -95
  82. package/utils.js +2876 -0
  83. package/LICENSE-MIT +0 -4
  84. package/index.d.ts +0 -615
  85. package/module/config.js +0 -33
  86. package/module/login.js +0 -48
  87. package/module/loginHelper.js +0 -722
  88. package/module/options.js +0 -44
  89. package/src/api/action/handleFriendRequest.js +0 -57
  90. package/src/api/action/logout.js +0 -76
  91. package/src/api/action/refreshFb_dtsg.js +0 -71
  92. package/src/api/http/httpGet.js +0 -46
  93. package/src/api/http/httpPost.js +0 -52
  94. package/src/api/http/postFormData.js +0 -47
  95. package/src/api/messaging/addUserToGroup.js +0 -68
  96. package/src/api/messaging/changeAdminStatus.js +0 -122
  97. package/src/api/messaging/changeArchivedStatus.js +0 -55
  98. package/src/api/messaging/changeGroupImage.js +0 -90
  99. package/src/api/messaging/changeNickname.js +0 -70
  100. package/src/api/messaging/changeThreadColor.js +0 -79
  101. package/src/api/messaging/changeThreadEmoji.js +0 -106
  102. package/src/api/messaging/createPoll.js +0 -43
  103. package/src/api/messaging/editMessage.js +0 -68
  104. package/src/api/messaging/forwardAttachment.js +0 -51
  105. package/src/api/messaging/handleMessageRequest.js +0 -65
  106. package/src/api/messaging/markAsDelivered.js +0 -57
  107. package/src/api/messaging/markAsRead.js +0 -88
  108. package/src/api/messaging/markAsReadAll.js +0 -49
  109. package/src/api/messaging/markAsSeen.js +0 -61
  110. package/src/api/messaging/muteThread.js +0 -50
  111. package/src/api/messaging/removeUserFromGroup.js +0 -105
  112. package/src/api/messaging/sendMessage.js +0 -379
  113. package/src/api/messaging/sendTypingIndicator.js +0 -67
  114. package/src/api/messaging/setMessageReaction.js +0 -75
  115. package/src/api/messaging/setTitle.js +0 -119
  116. package/src/api/messaging/shareContact.js +0 -49
  117. package/src/api/messaging/unsendMessage.js +0 -81
  118. package/src/api/socket/core/connectMqtt.js +0 -179
  119. package/src/api/socket/core/getSeqID.js +0 -25
  120. package/src/api/socket/core/getTaskResponseData.js +0 -22
  121. package/src/api/socket/core/markDelivery.js +0 -12
  122. package/src/api/socket/core/parseDelta.js +0 -351
  123. package/src/api/socket/detail/buildStream.js +0 -208
  124. package/src/api/socket/detail/constants.js +0 -24
  125. package/src/api/socket/listenMqtt.js +0 -133
  126. package/src/api/threads/getThreadInfo.js +0 -358
  127. package/src/api/threads/getThreadList.js +0 -248
  128. package/src/api/threads/getThreadPictures.js +0 -78
  129. package/src/api/users/getUserInfo.js +0 -319
  130. package/src/api/users/getUserInfoV2.js +0 -133
  131. package/src/core/sendReqMqtt.js +0 -63
  132. package/src/database/models/index.js +0 -49
  133. package/src/database/models/thread.js +0 -31
  134. package/src/database/models/user.js +0 -32
  135. package/src/database/threadData.js +0 -98
  136. package/src/database/userData.js +0 -89
  137. package/src/utils/client.js +0 -214
  138. package/src/utils/constants.js +0 -23
  139. package/src/utils/format.js +0 -1111
  140. package/src/utils/headers.js +0 -41
  141. package/src/utils/request.js +0 -215
@@ -1,323 +1,322 @@
1
-
2
- var log = require("npmlog");
3
- var bluebird = require("bluebird");
4
- const { parseAndCheckLogin } = require("../../utils/client");
5
- const { getType } = require("../../utils/format");
6
- const { isReadableStream } = require("../../utils/constants");
7
- module.exports = function(defaultFuncs, api, ctx) {
8
- function uploadAttachment(attachments, callback) {
9
- callback = callback || function() {};
10
- var uploads = [];
11
-
12
- // create an array of promises
13
- for (var i = 0; i < attachments.length; i++) {
14
- if (!isReadableStream(attachments[i])) {
15
- throw {
16
- error:
17
- "Attachment should be a readable stream and not " +
18
- getType(attachments[i]) +
19
- "."
20
- };
21
- }
22
-
23
- var form = {
24
- upload_1024: attachments[i],
25
- voice_clip: "true"
26
- };
27
-
28
- uploads.push(
29
- defaultFuncs
30
- .postFormData(
31
- "https://upload.facebook.com/ajax/mercury/upload.php",
32
- ctx.jar,
33
- form,
34
- {}
35
- )
36
- .then(parseAndCheckLogin(ctx, defaultFuncs))
37
- .then(function(resData) {
38
- if (resData.error) {
39
- throw resData;
40
- }
41
-
42
- // We have to return the data unformatted unless we want to change it
43
- // back in sendMessage.
44
- return resData.payload.metadata[0];
45
- })
46
- );
47
- }
48
-
49
- // resolve all promises
50
- bluebird
51
- .all(uploads)
52
- .then(function(resData) {
53
- callback(null, resData);
54
- })
55
- .catch(function(err) {
56
- log.error("uploadAttachment", err);
57
- return callback(err);
58
- });
59
- }
60
-
61
- let variance = 0;
62
- const epoch_id = () =>
63
- Math.floor(Date.now() * (4194304 + (variance = (variance + 0.1) % 5)));
64
- const emojiSizes = {
65
- small: 1,
66
- medium: 2,
67
- large: 3
68
- };
69
-
70
- function handleEmoji(msg, form, callback, cb) {
71
- if (msg.emojiSize != null && msg.emoji == null) {
72
- return callback({ error: "emoji property is empty" });
73
- }
74
- if (msg.emoji) {
75
- if (!msg.emojiSize) {
76
- msg.emojiSize = "small";
77
- }
78
- if (
79
- msg.emojiSize !== "small" &&
80
- msg.emojiSize !== "medium" &&
81
- msg.emojiSize !== "large" &&
82
- (isNaN(msg.emojiSize) || msg.emojiSize < 1 || msg.emojiSize > 3)
83
- ) {
84
- return callback({ error: "emojiSize property is invalid" });
85
- }
86
-
87
- form.payload.tasks[0].payload.send_type = 1;
88
- form.payload.tasks[0].payload.text = msg.emoji;
89
- form.payload.tasks[0].payload.hot_emoji_size = !isNaN(msg.emojiSize)
90
- ? msg.emojiSize
91
- : emojiSizes[msg.emojiSize];
92
- }
93
- cb();
94
- }
95
-
96
- function handleSticker(msg, form, callback, cb) {
97
- if (msg.sticker) {
98
- form.payload.tasks[0].payload.send_type = 2;
99
- form.payload.tasks[0].payload.sticker_id = msg.sticker;
100
- }
101
- cb();
102
- }
103
-
104
- function handleAttachment(msg, form, callback, cb) {
105
- if (msg.attachment) {
106
- form.payload.tasks[0].payload.send_type = 3;
107
- form.payload.tasks[0].payload.attachment_fbids = [];
108
- if (form.payload.tasks[0].payload.text == "")
109
- form.payload.tasks[0].payload.text = null;
110
- if (getType(msg.attachment) !== "Array") {
111
- msg.attachment = [msg.attachment];
112
- }
113
-
114
- uploadAttachment(msg.attachment, function(err, files) {
115
- if (err) {
116
- return callback(err);
117
- }
118
-
119
- files.forEach(function(file) {
120
- var key = Object.keys(file);
121
- var type = key[0]; // image_id, file_id, etc
122
- form.payload.tasks[0].payload.attachment_fbids.push(file[type]); // push the id
123
- });
124
- cb();
125
- });
126
- } else {
127
- cb();
128
- }
129
- }
130
-
131
- function handleMention(msg, form, callback, cb) {
132
- if (msg.mentions) {
133
- form.payload.tasks[0].payload.send_type = 1;
134
-
135
- const arrayIds = [];
136
- const arrayOffsets = [];
137
- const arrayLengths = [];
138
- const mention_types = [];
139
-
140
- for (let i = 0; i < msg.mentions.length; i++) {
141
- const mention = msg.mentions[i];
142
-
143
- const tag = mention.tag;
144
- if (typeof tag !== "string") {
145
- return callback({ error: "Mention tags must be strings." });
146
- }
147
-
148
- const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
149
-
150
- if (offset < 0) {
151
- log.warn(
152
- "handleMention",
153
- 'Mention for "' + tag + '" not found in message string.'
154
- );
155
- }
156
-
157
- if (mention.id == null) {
158
- log.warn("handleMention", "Mention id should be non-null.");
159
- }
160
-
161
- const id = mention.id || 0;
162
- arrayIds.push(id);
163
- arrayOffsets.push(offset);
164
- arrayLengths.push(tag.length);
165
- mention_types.push("p");
166
- }
167
-
168
- form.payload.tasks[0].payload.mention_data = {
169
- mention_ids: arrayIds.join(","),
170
- mention_offsets: arrayOffsets.join(","),
171
- mention_lengths: arrayLengths.join(","),
172
- mention_types: mention_types.join(",")
173
- };
174
- }
175
- cb();
176
- }
177
-
178
- function handleLocation(msg, form, callback, cb) {
179
- // this is not working yet
180
- if (msg.location) {
181
- if (msg.location.latitude == null || msg.location.longitude == null) {
182
- return callback({
183
- error: "location property needs both latitude and longitude"
184
- });
185
- }
186
-
187
- form.payload.tasks[0].payload.send_type = 1;
188
- form.payload.tasks[0].payload.location_data = {
189
- coordinates: {
190
- latitude: msg.location.latitude,
191
- longitude: msg.location.longitude
192
- },
193
- is_current_location: !!msg.location.current,
194
- is_live_location: !!msg.location.live
195
- };
196
- }
197
-
198
- cb();
199
- }
200
-
201
- function send(form, threadID, callback, replyToMessage) {
202
- if (replyToMessage) {
203
- form.payload.tasks[0].payload.reply_metadata = {
204
- reply_source_id: replyToMessage,
205
- reply_source_type: 1,
206
- reply_type: 0
207
- };
208
- }
209
- const mqttClient = ctx.mqttClient;
210
- form.payload.tasks.forEach(task => {
211
- task.payload = JSON.stringify(task.payload);
212
- });
213
- form.payload = JSON.stringify(form.payload);
214
- console.log(global.jsonStringifyColor(form, null, 2));
215
-
216
- return mqttClient.publish("/ls_req", JSON.stringify(form), function(
217
- err,
218
- data
219
- ) {
220
- if (err) {
221
- console.error("Error publishing message: ", err);
222
- callback(err);
223
- } else {
224
- console.log("Message published successfully with data: ", data);
225
- callback(null, data);
226
- }
227
- });
228
- }
229
-
230
- return function sendMessageMqtt(msg, threadID, callback, replyToMessage) {
231
- if (
232
- !callback &&
233
- (getType(threadID) === "Function" ||
234
- getType(threadID) === "AsyncFunction")
235
- ) {
236
- return threadID({ error: "Pass a threadID as a second argument." });
237
- }
238
- if (!replyToMessage && getType(callback) === "String") {
239
- replyToMessage = callback;
240
- callback = function() {};
241
- }
242
-
243
- if (!callback) {
244
- callback = function(err, friendList) {};
245
- }
246
-
247
- var msgType = getType(msg);
248
- var threadIDType = getType(threadID);
249
- var messageIDType = getType(replyToMessage);
250
-
251
- if (msgType !== "String" && msgType !== "Object") {
252
- return callback({
253
- error:
254
- "Message should be of type string or object and not " + msgType + "."
255
- });
256
- }
257
-
258
- if (msgType === "String") {
259
- msg = { body: msg };
260
- }
261
-
262
- const timestamp = Date.now();
263
- // get full date time
264
- const epoch = timestamp << 22;
265
- //const otid = epoch + 0; // TODO replace with randomInt(0, 2**22)
266
- const otid = epoch + Math.floor(Math.random() * 4194304);
267
-
268
- const form = {
269
- app_id: "2220391788200892",
270
- payload: {
271
- tasks: [
272
- {
273
- label: "46",
274
- payload: {
275
- thread_id: threadID.toString(),
276
- otid: otid.toString(),
277
- source: 0,
278
- send_type: 1,
279
- sync_group: 1,
280
- text:
281
- msg.body != null && msg.body != undefined
282
- ? msg.body.toString()
283
- : "",
284
- initiating_source: 1,
285
- skip_url_preview_gen: 0
286
- },
287
- queue_name: threadID.toString(),
288
- task_id: 0,
289
- failure_count: null
290
- },
291
- {
292
- label: "21",
293
- payload: {
294
- thread_id: threadID.toString(),
295
- last_read_watermark_ts: Date.now(),
296
- sync_group: 1
297
- },
298
- queue_name: threadID.toString(),
299
- task_id: 1,
300
- failure_count: null
301
- }
302
- ],
303
- epoch_id: epoch_id(),
304
- version_id: "6120284488008082",
305
- data_trace_id: null
306
- },
307
- request_id: 1,
308
- type: 3
309
- };
310
-
311
- handleEmoji(msg, form, callback, function() {
312
- handleLocation(msg, form, callback, function() {
313
- handleMention(msg, form, callback, function() {
314
- handleSticker(msg, form, callback, function() {
315
- handleAttachment(msg, form, callback, function() {
316
- send(form, threadID, callback, replyToMessage);
317
- });
318
- });
319
- });
320
- });
321
- });
322
- };
323
- };
1
+ var utils = require("../utils");
2
+ // @NethWs3Dev
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
+ console.error("uploadAttachment", err);
55
+ return callback(err);
56
+ });
57
+ }
58
+
59
+ let variance = 0;
60
+ const epoch_id = () =>
61
+ Math.floor(Date.now() * (4194304 + (variance = (variance + 0.1) % 5)));
62
+ const emojiSizes = {
63
+ small: 1,
64
+ medium: 2,
65
+ large: 3,
66
+ };
67
+
68
+ function handleEmoji(msg, form, callback, cb) {
69
+ if (msg.emojiSize != null && msg.emoji == null) {
70
+ return callback({ error: "emoji property is empty" });
71
+ }
72
+ if (msg.emoji) {
73
+ if (!msg.emojiSize) {
74
+ msg.emojiSize = "small";
75
+ }
76
+ if (
77
+ msg.emojiSize !== "small" &&
78
+ msg.emojiSize !== "medium" &&
79
+ msg.emojiSize !== "large" &&
80
+ (isNaN(msg.emojiSize) || msg.emojiSize < 1 || msg.emojiSize > 3)
81
+ ) {
82
+ return callback({ error: "emojiSize property is invalid" });
83
+ }
84
+
85
+ form.payload.tasks[0].payload.send_type = 1;
86
+ form.payload.tasks[0].payload.text = msg.emoji;
87
+ form.payload.tasks[0].payload.hot_emoji_size = !isNaN(msg.emojiSize)
88
+ ? msg.emojiSize
89
+ : emojiSizes[msg.emojiSize];
90
+ }
91
+ cb();
92
+ }
93
+
94
+ function handleSticker(msg, form, callback, cb) {
95
+ if (msg.sticker) {
96
+ form.payload.tasks[0].payload.send_type = 2;
97
+ form.payload.tasks[0].payload.sticker_id = msg.sticker;
98
+ }
99
+ cb();
100
+ }
101
+
102
+ function handleAttachment(msg, form, callback, cb) {
103
+ if (msg.attachment) {
104
+ form.payload.tasks[0].payload.send_type = 3;
105
+ form.payload.tasks[0].payload.attachment_fbids = [];
106
+ if (form.payload.tasks[0].payload.text == "")
107
+ form.payload.tasks[0].payload.text = null;
108
+ if (utils.getType(msg.attachment) !== "Array") {
109
+ msg.attachment = [msg.attachment];
110
+ }
111
+
112
+ uploadAttachment(msg.attachment, function (err, files) {
113
+ if (err) {
114
+ return callback(err);
115
+ }
116
+
117
+ files.forEach(function (file) {
118
+ var key = Object.keys(file);
119
+ var type = key[0]; // image_id, file_id, etc
120
+ form.payload.tasks[0].payload.attachment_fbids.push(file[type]); // push the id
121
+ });
122
+ cb();
123
+ });
124
+ } else {
125
+ cb();
126
+ }
127
+ }
128
+
129
+ function handleMention(msg, form, callback, cb) {
130
+ if (msg.mentions) {
131
+ form.payload.tasks[0].payload.send_type = 1;
132
+
133
+ const arrayIds = [];
134
+ const arrayOffsets = [];
135
+ const arrayLengths = [];
136
+ const mention_types = [];
137
+
138
+ for (let i = 0; i < msg.mentions.length; i++) {
139
+ const mention = msg.mentions[i];
140
+
141
+ const tag = mention.tag;
142
+ if (typeof tag !== "string") {
143
+ return callback({ error: "Mention tags must be strings." });
144
+ }
145
+
146
+ const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
147
+
148
+ if (offset < 0) {
149
+ console.warn(
150
+ "handleMention",
151
+ 'Mention for "' + tag + '" not found in message string.',
152
+ );
153
+ }
154
+
155
+ if (mention.id == null) {
156
+ console.warn("handleMention", "Mention id should be non-null.");
157
+ }
158
+
159
+ const id = mention.id || 0;
160
+ arrayIds.push(id);
161
+ arrayOffsets.push(offset);
162
+ arrayLengths.push(tag.length);
163
+ mention_types.push("p");
164
+ }
165
+
166
+ form.payload.tasks[0].payload.mention_data = {
167
+ mention_ids: arrayIds.join(","),
168
+ mention_offsets: arrayOffsets.join(","),
169
+ mention_lengths: arrayLengths.join(","),
170
+ mention_types: mention_types.join(","),
171
+ };
172
+ }
173
+ cb();
174
+ }
175
+
176
+ function handleLocation(msg, form, callback, cb) {
177
+ // this is not working yet
178
+ if (msg.location) {
179
+ if (msg.location.latitude == null || msg.location.longitude == null) {
180
+ return callback({
181
+ error: "location property needs both latitude and longitude",
182
+ });
183
+ }
184
+
185
+ form.payload.tasks[0].payload.send_type = 1;
186
+ form.payload.tasks[0].payload.location_data = {
187
+ coordinates: {
188
+ latitude: msg.location.latitude,
189
+ longitude: msg.location.longitude,
190
+ },
191
+ is_current_location: !!msg.location.current,
192
+ is_live_location: !!msg.location.live,
193
+ };
194
+ }
195
+
196
+ cb();
197
+ }
198
+
199
+ function send(form, threadID, callback, replyToMessage) {
200
+ if (replyToMessage) {
201
+ form.payload.tasks[0].payload.reply_metadata = {
202
+ reply_source_id: replyToMessage,
203
+ reply_source_type: 1,
204
+ reply_type: 0,
205
+ };
206
+ }
207
+ const mqttClient = ctx.mqttClient;
208
+ form.payload.tasks.forEach((task) => {
209
+ task.payload = JSON.stringify(task.payload);
210
+ });
211
+ form.payload = JSON.stringify(form.payload);
212
+ console.log(global.jsonStringifyColor(form, null, 2));
213
+
214
+ return mqttClient.publish(
215
+ "/ls_req",
216
+ JSON.stringify(form),
217
+ function (err, data) {
218
+ if (err) {
219
+ console.error("Error publishing message: ", err);
220
+ callback(err);
221
+ } else {
222
+ console.log("Message published successfully with data: ", data);
223
+ callback(null, data);
224
+ }
225
+ },
226
+ );
227
+ }
228
+
229
+ return function sendMessageMqtt(msg, threadID, callback, replyToMessage) {
230
+ if (
231
+ !callback &&
232
+ (utils.getType(threadID) === "Function" ||
233
+ utils.getType(threadID) === "AsyncFunction")
234
+ ) {
235
+ return threadID({ error: "Pass a threadID as a second argument." });
236
+ }
237
+ if (!replyToMessage && utils.getType(callback) === "String") {
238
+ replyToMessage = callback;
239
+ callback = function () {};
240
+ }
241
+
242
+ if (!callback) {
243
+ callback = function (err, friendList) {};
244
+ }
245
+
246
+ var msgType = utils.getType(msg);
247
+ var threadIDType = utils.getType(threadID);
248
+ var messageIDType = utils.getType(replyToMessage);
249
+
250
+ if (msgType !== "String" && msgType !== "Object") {
251
+ return callback({
252
+ error:
253
+ "Message should be of type string or object and not " + msgType + ".",
254
+ });
255
+ }
256
+
257
+ if (msgType === "String") {
258
+ msg = { body: msg };
259
+ }
260
+
261
+ const timestamp = Date.now();
262
+ // get full date time
263
+ const epoch = timestamp << 22;
264
+ //const otid = epoch + 0; // TODO replace with randomInt(0, 2**22)
265
+ const otid = epoch + Math.floor(Math.random() * 4194304);
266
+
267
+ const form = {
268
+ app_id: "2220391788200892",
269
+ payload: {
270
+ tasks: [
271
+ {
272
+ label: "46",
273
+ payload: {
274
+ thread_id: threadID.toString(),
275
+ otid: otid.toString(),
276
+ source: 0,
277
+ send_type: 1,
278
+ sync_group: 1,
279
+ text:
280
+ msg.body != null && msg.body != undefined
281
+ ? msg.body.toString()
282
+ : "",
283
+ initiating_source: 1,
284
+ skip_url_preview_gen: 0,
285
+ },
286
+ queue_name: threadID.toString(),
287
+ task_id: 0,
288
+ failure_count: null,
289
+ },
290
+ {
291
+ label: "21",
292
+ payload: {
293
+ thread_id: threadID.toString(),
294
+ last_read_watermark_ts: Date.now(),
295
+ sync_group: 1,
296
+ },
297
+ queue_name: threadID.toString(),
298
+ task_id: 1,
299
+ failure_count: null,
300
+ },
301
+ ],
302
+ epoch_id: epoch_id(),
303
+ version_id: "6120284488008082",
304
+ data_trace_id: null,
305
+ },
306
+ request_id: 1,
307
+ type: 3,
308
+ };
309
+
310
+ handleEmoji(msg, form, callback, function () {
311
+ handleLocation(msg, form, callback, function () {
312
+ handleMention(msg, form, callback, function () {
313
+ handleSticker(msg, form, callback, function () {
314
+ handleAttachment(msg, form, callback, function () {
315
+ send(form, threadID, callback, replyToMessage);
316
+ });
317
+ });
318
+ });
319
+ });
320
+ });
321
+ };
322
+ };