@dongdev/fca-unofficial 2.0.31 → 3.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.
@@ -1,323 +0,0 @@
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
- };