node-ainzfb-new 1.7.9-uOw19mf → 1.7.10-11

Sign up to get free protection for your applications and to get access to all the features.
package/utils.js CHANGED
@@ -10,55 +10,61 @@ var querystring = require("querystring");
10
10
  var url = require("url");
11
11
 
12
12
  function setProxy(url) {
13
- if (typeof url == undefined) return request = bluebird.promisify(require("request").defaults({ jar: true }));
14
- return request = bluebird.promisify(require("request").defaults({ jar: true, proxy: url }));
13
+ if (typeof url == undefined)
14
+ return (request = bluebird.promisify(
15
+ require("request").defaults({ jar: true })
16
+ ));
17
+ return (request = bluebird.promisify(
18
+ require("request").defaults({ jar: true, proxy: url })
19
+ ));
15
20
  }
16
21
 
17
22
  function getHeaders(url, options, ctx, customHeader) {
18
- var headers = {
19
- "Content-Type": "application/x-www-form-urlencoded",
20
- Referer: "https://www.facebook.com/",
21
- Host: url.replace("https://", "").split("/")[0],
22
- Origin: "https://www.facebook.com",
23
- "User-Agent": options.userAgent,
24
- Connection: "keep-alive",
25
- "sec-fetch-site": 'same-origin',
26
- "sec-fetch-mode": 'cors'
27
- };
28
- if (customHeader) Object.assign(headers, customHeader);
29
-
30
- if (ctx && ctx.region) headers["X-MSGR-Region"] = ctx.region;
31
-
32
- return headers;
23
+ var headers = {
24
+ "Content-Type": "application/x-www-form-urlencoded",
25
+ Referer: "https://www.facebook.com/",
26
+ Host: url.replace("https://", "").split("/")[0],
27
+ Origin: "https://www.facebook.com",
28
+ "User-Agent": options.userAgent,
29
+ Connection: "keep-alive",
30
+ "sec-fetch-site": "same-origin",
31
+ "sec-fetch-mode": "cors"
32
+ };
33
+ if (customHeader) Object.assign(headers, customHeader);
34
+
35
+ if (ctx && ctx.region) headers["X-MSGR-Region"] = ctx.region;
36
+
37
+ return headers;
33
38
  }
34
39
 
35
40
  function isReadableStream(obj) {
36
- return (
37
- obj instanceof stream.Stream &&
38
- (getType(obj._read) === "Function" ||
39
- getType(obj._read) === "AsyncFunction") &&
40
- getType(obj._readableState) === "Object"
41
- );
41
+ return (
42
+ obj instanceof stream.Stream &&
43
+ (getType(obj._read) === "Function" ||
44
+ getType(obj._read) === "AsyncFunction") &&
45
+ getType(obj._readableState) === "Object"
46
+ );
42
47
  }
43
48
 
44
49
  function get(url, jar, qs, options, ctx) {
45
- // I'm still confused about this
46
- if (getType(qs) === "Object")
47
- for (var prop in qs)
48
- if (qs.hasOwnProperty(prop) && getType(qs[prop]) === "Object") qs[prop] = JSON.stringify(qs[prop]);
49
- var op = {
50
- headers: getHeaders(url, options, ctx),
51
- timeout: 60000,
52
- qs: qs,
53
- url: url,
54
- method: "GET",
55
- jar: jar,
56
- gzip: true
57
- };
58
-
59
- return request(op).then(function(res) {
60
- return res[0];
61
- });
50
+ // I'm still confused about this
51
+ if (getType(qs) === "Object")
52
+ for (var prop in qs)
53
+ if (qs.hasOwnProperty(prop) && getType(qs[prop]) === "Object")
54
+ qs[prop] = JSON.stringify(qs[prop]);
55
+ var op = {
56
+ headers: getHeaders(url, options, ctx),
57
+ timeout: 60000,
58
+ qs: qs,
59
+ url: url,
60
+ method: "GET",
61
+ jar: jar,
62
+ gzip: true
63
+ };
64
+
65
+ return request(op).then(function(res) {
66
+ return res[0];
67
+ });
62
68
  }
63
69
 
64
70
  /*function getv2(url, jar, qs, options, ctx) {
@@ -82,21 +88,21 @@ function get(url, jar, qs, options, ctx) {
82
88
  }*/
83
89
 
84
90
  function post(url, jar, form, options, ctx, customHeader) {
85
- let headers = getHeaders(url, options);
86
- headers['sec-fetch-site'] = 'same-origin';
87
- var op = {
88
- headers: headers,
89
- timeout: 60000,
90
- url: url,
91
- method: "POST",
92
- form: form,
93
- jar: jar,
94
- gzip: true
95
- };
96
-
97
- return request(op).then(function(res) {
98
- return res[0];
99
- });
91
+ let headers = getHeaders(url, options);
92
+ headers["sec-fetch-site"] = "same-origin";
93
+ var op = {
94
+ headers: headers,
95
+ timeout: 60000,
96
+ url: url,
97
+ method: "POST",
98
+ form: form,
99
+ jar: jar,
100
+ gzip: true
101
+ };
102
+
103
+ return request(op).then(function(res) {
104
+ return res[0];
105
+ });
100
106
  }
101
107
 
102
108
  /*function postv2(url, jar, form, options, ctx, customHeader) {
@@ -118,809 +124,1021 @@ function post(url, jar, form, options, ctx, customHeader) {
118
124
  }*/
119
125
 
120
126
  function postFormData(url, jar, form, qs, options, ctx) {
121
- var headers = getHeaders(url, options, ctx);
122
- headers["Content-Type"] = "multipart/form-data";
123
- var op = {
124
- headers: headers,
125
- timeout: 60000,
126
- url: url,
127
- method: "POST",
128
- formData: form,
129
- qs: qs,
130
- jar: jar,
131
- gzip: true
132
- };
133
-
134
- return request(op).then(function(res) {
135
- return res[0];
136
- });
127
+ var headers = getHeaders(url, options, ctx);
128
+ headers["Content-Type"] = "multipart/form-data";
129
+ var op = {
130
+ headers: headers,
131
+ timeout: 60000,
132
+ url: url,
133
+ method: "POST",
134
+ formData: form,
135
+ qs: qs,
136
+ jar: jar,
137
+ gzip: true
138
+ };
139
+
140
+ return request(op).then(function(res) {
141
+ return res[0];
142
+ });
137
143
  }
138
144
 
139
145
  function padZeros(val, len) {
140
- val = String(val);
141
- len = len || 2;
142
- while (val.length < len) val = "0" + val;
143
- return val;
146
+ val = String(val);
147
+ len = len || 2;
148
+ while (val.length < len) val = "0" + val;
149
+ return val;
144
150
  }
145
151
 
146
152
  function generateThreadingID(clientID) {
147
- var k = Date.now();
148
- var l = Math.floor(Math.random() * 4294967295);
149
- var m = clientID;
150
- return "<" + k + ":" + l + "-" + m + "@mail.projektitan.com>";
153
+ var k = Date.now();
154
+ var l = Math.floor(Math.random() * 4294967295);
155
+ var m = clientID;
156
+ return "<" + k + ":" + l + "-" + m + "@mail.projektitan.com>";
157
+ }
158
+
159
+ function getCurrentTimestamp() {
160
+ const date = new Date();
161
+ const unixTime = date.getTime();
162
+ return unixTime;
151
163
  }
152
164
 
153
165
  function binaryToDecimal(data) {
154
- var ret = "";
155
- while (data !== "0") {
156
- var end = 0;
157
- var fullName = "";
158
- var i = 0;
159
- for (; i < data.length; i++) {
160
- end = 2 * end + parseInt(data[i], 10);
161
- if (end >= 10) {
162
- fullName += "1";
163
- end -= 10;
164
- } else fullName += "0";
165
- }
166
- ret = end.toString() + ret;
167
- data = fullName.slice(fullName.indexOf("1"));
166
+ var ret = "";
167
+ while (data !== "0") {
168
+ var end = 0;
169
+ var fullName = "";
170
+ var i = 0;
171
+ for (; i < data.length; i++) {
172
+ end = 2 * end + parseInt(data[i], 10);
173
+ if (end >= 10) {
174
+ fullName += "1";
175
+ end -= 10;
176
+ } else fullName += "0";
168
177
  }
169
- return ret;
178
+ ret = end.toString() + ret;
179
+ data = fullName.slice(fullName.indexOf("1"));
180
+ }
181
+ return ret;
170
182
  }
171
183
 
172
184
  function generateOfflineThreadingID() {
173
- var ret = Date.now();
174
- var value = Math.floor(Math.random() * 4294967295);
175
- var str = ("0000000000000000000000" + value.toString(2)).slice(-22);
176
- var msgs = ret.toString(2) + str;
177
- return binaryToDecimal(msgs);
185
+ var ret = Date.now();
186
+ var value = Math.floor(Math.random() * 4294967295);
187
+ var str = ("0000000000000000000000" + value.toString(2)).slice(-22);
188
+ var msgs = ret.toString(2) + str;
189
+ return binaryToDecimal(msgs);
178
190
  }
179
191
 
180
192
  var h;
181
193
  var i = {};
182
194
  var j = {
183
- _: "%",
184
- A: "%2",
185
- B: "000",
186
- C: "%7d",
187
- D: "%7b%22",
188
- E: "%2c%22",
189
- F: "%22%3a",
190
- G: "%2c%22ut%22%3a1",
191
- H: "%2c%22bls%22%3a",
192
- I: "%2c%22n%22%3a%22%",
193
- J: "%22%3a%7b%22i%22%3a0%7d",
194
- K: "%2c%22pt%22%3a0%2c%22vis%22%3a",
195
- L: "%2c%22ch%22%3a%7b%22h%22%3a%22",
196
- M: "%7b%22v%22%3a2%2c%22time%22%3a1",
197
- N: ".channel%22%2c%22sub%22%3a%5b",
198
- O: "%2c%22sb%22%3a1%2c%22t%22%3a%5b",
199
- P: "%2c%22ud%22%3a100%2c%22lc%22%3a0",
200
- Q: "%5d%2c%22f%22%3anull%2c%22uct%22%3a",
201
- R: ".channel%22%2c%22sub%22%3a%5b1%5d",
202
- S: "%22%2c%22m%22%3a0%7d%2c%7b%22i%22%3a",
203
- T: "%2c%22blc%22%3a1%2c%22snd%22%3a1%2c%22ct%22%3a",
204
- U: "%2c%22blc%22%3a0%2c%22snd%22%3a1%2c%22ct%22%3a",
205
- V: "%2c%22blc%22%3a0%2c%22snd%22%3a0%2c%22ct%22%3a",
206
- W: "%2c%22s%22%3a0%2c%22blo%22%3a0%7d%2c%22bl%22%3a%7b%22ac%22%3a",
207
- X: "%2c%22ri%22%3a0%7d%2c%22state%22%3a%7b%22p%22%3a0%2c%22ut%22%3a1",
208
- Y: "%2c%22pt%22%3a0%2c%22vis%22%3a1%2c%22bls%22%3a0%2c%22blc%22%3a0%2c%22snd%22%3a1%2c%22ct%22%3a",
209
- Z: "%2c%22sb%22%3a1%2c%22t%22%3a%5b%5d%2c%22f%22%3anull%2c%22uct%22%3a0%2c%22s%22%3a0%2c%22blo%22%3a0%7d%2c%22bl%22%3a%7b%22ac%22%3a"
195
+ _: "%",
196
+ A: "%2",
197
+ B: "000",
198
+ C: "%7d",
199
+ D: "%7b%22",
200
+ E: "%2c%22",
201
+ F: "%22%3a",
202
+ G: "%2c%22ut%22%3a1",
203
+ H: "%2c%22bls%22%3a",
204
+ I: "%2c%22n%22%3a%22%",
205
+ J: "%22%3a%7b%22i%22%3a0%7d",
206
+ K: "%2c%22pt%22%3a0%2c%22vis%22%3a",
207
+ L: "%2c%22ch%22%3a%7b%22h%22%3a%22",
208
+ M: "%7b%22v%22%3a2%2c%22time%22%3a1",
209
+ N: ".channel%22%2c%22sub%22%3a%5b",
210
+ O: "%2c%22sb%22%3a1%2c%22t%22%3a%5b",
211
+ P: "%2c%22ud%22%3a100%2c%22lc%22%3a0",
212
+ Q: "%5d%2c%22f%22%3anull%2c%22uct%22%3a",
213
+ R: ".channel%22%2c%22sub%22%3a%5b1%5d",
214
+ S: "%22%2c%22m%22%3a0%7d%2c%7b%22i%22%3a",
215
+ T: "%2c%22blc%22%3a1%2c%22snd%22%3a1%2c%22ct%22%3a",
216
+ U: "%2c%22blc%22%3a0%2c%22snd%22%3a1%2c%22ct%22%3a",
217
+ V: "%2c%22blc%22%3a0%2c%22snd%22%3a0%2c%22ct%22%3a",
218
+ W: "%2c%22s%22%3a0%2c%22blo%22%3a0%7d%2c%22bl%22%3a%7b%22ac%22%3a",
219
+ X: "%2c%22ri%22%3a0%7d%2c%22state%22%3a%7b%22p%22%3a0%2c%22ut%22%3a1",
220
+ Y:
221
+ "%2c%22pt%22%3a0%2c%22vis%22%3a1%2c%22bls%22%3a0%2c%22blc%22%3a0%2c%22snd%22%3a1%2c%22ct%22%3a",
222
+ Z:
223
+ "%2c%22sb%22%3a1%2c%22t%22%3a%5b%5d%2c%22f%22%3anull%2c%22uct%22%3a0%2c%22s%22%3a0%2c%22blo%22%3a0%7d%2c%22bl%22%3a%7b%22ac%22%3a"
210
224
  };
211
225
  (function() {
212
- var l = [];
213
- for (var m in j) {
214
- i[j[m]] = m;
215
- l.push(j[m]);
216
- }
217
- l.reverse();
218
- h = new RegExp(l.join("|"), "g");
226
+ var l = [];
227
+ for (var m in j) {
228
+ i[j[m]] = m;
229
+ l.push(j[m]);
230
+ }
231
+ l.reverse();
232
+ h = new RegExp(l.join("|"), "g");
219
233
  })();
220
234
 
221
235
  function presenceEncode(str) {
222
- return encodeURIComponent(str)
223
- .replace(/([_A-Z])|%../g, function(m, n) {
224
- return n ? "%" + n.charCodeAt(0).toString(16) : m;
225
- })
226
- .toLowerCase()
227
- .replace(h, function(m) {
228
- return i[m];
229
- });
236
+ return encodeURIComponent(str)
237
+ .replace(/([_A-Z])|%../g, function(m, n) {
238
+ return n ? "%" + n.charCodeAt(0).toString(16) : m;
239
+ })
240
+ .toLowerCase()
241
+ .replace(h, function(m) {
242
+ return i[m];
243
+ });
230
244
  }
231
245
 
232
246
  // eslint-disable-next-line no-unused-vars
233
247
  function presenceDecode(str) {
234
- return decodeURIComponent(
235
- str.replace(/[_A-Z]/g, function(m) {
236
- return j[m];
237
- })
238
- );
248
+ return decodeURIComponent(
249
+ str.replace(/[_A-Z]/g, function(m) {
250
+ return j[m];
251
+ })
252
+ );
239
253
  }
240
254
 
241
255
  function generatePresence(userID) {
242
- var time = Date.now();
243
- return (
244
- "E" +
245
- presenceEncode(
246
- JSON.stringify({
247
- v: 3,
248
- time: parseInt(time / 1000, 10),
249
- user: userID,
250
- state: {
251
- ut: 0,
252
- t2: [],
253
- lm2: null,
254
- uct2: time,
255
- tr: null,
256
- tw: Math.floor(Math.random() * 4294967295) + 1,
257
- at: time
258
- },
259
- ch: {
260
- ["p_" + userID]: 0
261
- }
262
- })
263
- )
264
- );
256
+ var time = Date.now();
257
+ return (
258
+ "E" +
259
+ presenceEncode(
260
+ JSON.stringify({
261
+ v: 3,
262
+ time: parseInt(time / 1000, 10),
263
+ user: userID,
264
+ state: {
265
+ ut: 0,
266
+ t2: [],
267
+ lm2: null,
268
+ uct2: time,
269
+ tr: null,
270
+ tw: Math.floor(Math.random() * 4294967295) + 1,
271
+ at: time
272
+ },
273
+ ch: {
274
+ ["p_" + userID]: 0
275
+ }
276
+ })
277
+ )
278
+ );
265
279
  }
266
280
 
267
281
  function generateAccessiblityCookie() {
268
- var time = Date.now();
269
- return encodeURIComponent(
270
- JSON.stringify({
271
- sr: 0,
272
- "sr-ts": time,
273
- jk: 0,
274
- "jk-ts": time,
275
- kb: 0,
276
- "kb-ts": time,
277
- hcm: 0,
278
- "hcm-ts": time
279
- })
280
- );
282
+ var time = Date.now();
283
+ return encodeURIComponent(
284
+ JSON.stringify({
285
+ sr: 0,
286
+ "sr-ts": time,
287
+ jk: 0,
288
+ "jk-ts": time,
289
+ kb: 0,
290
+ "kb-ts": time,
291
+ hcm: 0,
292
+ "hcm-ts": time
293
+ })
294
+ );
281
295
  }
282
296
 
283
297
  function getGUID() {
298
+ /** @type {number} */
299
+ var sectionLength = Date.now();
300
+ /** @type {string} */
301
+ var id = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
302
+ /** @type {number} */
303
+ var r = Math.floor((sectionLength + Math.random() * 16) % 16);
284
304
  /** @type {number} */
285
- var sectionLength = Date.now();
305
+ sectionLength = Math.floor(sectionLength / 16);
286
306
  /** @type {string} */
287
- var id = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
288
- /** @type {number} */
289
- var r = Math.floor((sectionLength + Math.random() * 16) % 16);
290
- /** @type {number} */
291
- sectionLength = Math.floor(sectionLength / 16);
292
- /** @type {string} */
293
- var _guid = (c == "x" ? r : (r & 7) | 8).toString(16);
294
- return _guid;
295
- });
296
- return id;
307
+ var _guid = (c == "x" ? r : (r & 7) | 8).toString(16);
308
+ return _guid;
309
+ });
310
+ return id;
297
311
  }
298
312
 
299
313
  function _formatAttachment(attachment1, attachment2) {
300
- // TODO: THIS IS REALLY BAD
301
- // This is an attempt at fixing Facebook's inconsistencies. Sometimes they give us
302
- // two attachment objects, but sometimes only one. They each contain part of the
303
- // data that you'd want so we merge them for convenience.
304
- // Instead of having a bunch of if statements guarding every access to image_data,
305
- // we set it to empty object and use the fact that it'll return undefined.
306
- attachment2 = attachment2 || { id: "", image_data: {} };
307
- attachment1 = attachment1.mercury ? attachment1.mercury : attachment1;
308
- var blob = attachment1.blob_attachment;
309
- var type =
310
- blob && blob.__typename ? blob.__typename : attachment1.attach_type;
311
- if (!type && attachment1.sticker_attachment) {
312
- type = "StickerAttachment";
313
- blob = attachment1.sticker_attachment;
314
- } else if (!type && attachment1.extensible_attachment) {
315
- if (
316
- attachment1.extensible_attachment.story_attachment &&
317
- attachment1.extensible_attachment.story_attachment.target &&
318
- attachment1.extensible_attachment.story_attachment.target.__typename &&
319
- attachment1.extensible_attachment.story_attachment.target.__typename === "MessageLocation"
320
- ) type = "MessageLocation";
321
- else type = "ExtensibleAttachment";
322
-
323
- blob = attachment1.extensible_attachment;
324
- }
325
- // TODO: Determine whether "sticker", "photo", "file" etc are still used
326
- // KEEP IN SYNC WITH getThreadHistory
327
- switch (type) {
328
- case "sticker":
329
- return {
330
- type: "sticker",
331
- ID: attachment1.metadata.stickerID.toString(),
332
- url: attachment1.url,
333
-
334
- packID: attachment1.metadata.packID.toString(),
335
- spriteUrl: attachment1.metadata.spriteURI,
336
- spriteUrl2x: attachment1.metadata.spriteURI2x,
337
- width: attachment1.metadata.width,
338
- height: attachment1.metadata.height,
339
-
340
- caption: attachment2.caption,
341
- description: attachment2.description,
342
-
343
- frameCount: attachment1.metadata.frameCount,
344
- frameRate: attachment1.metadata.frameRate,
345
- framesPerRow: attachment1.metadata.framesPerRow,
346
- framesPerCol: attachment1.metadata.framesPerCol,
347
-
348
- stickerID: attachment1.metadata.stickerID.toString(), // @Legacy
349
- spriteURI: attachment1.metadata.spriteURI, // @Legacy
350
- spriteURI2x: attachment1.metadata.spriteURI2x // @Legacy
351
- };
352
- case "file":
353
- return {
354
- type: "file",
355
- filename: attachment1.name,
356
- ID: attachment2.id.toString(),
357
- url: attachment1.url,
358
-
359
- isMalicious: attachment2.is_malicious,
360
- contentType: attachment2.mime_type,
361
-
362
- name: attachment1.name, // @Legacy
363
- mimeType: attachment2.mime_type, // @Legacy
364
- fileSize: attachment2.file_size // @Legacy
365
- };
366
- case "photo":
367
- return {
368
- type: "photo",
369
- ID: attachment1.metadata.fbid.toString(),
370
- filename: attachment1.fileName,
371
- thumbnailUrl: attachment1.thumbnail_url,
372
-
373
- previewUrl: attachment1.preview_url,
374
- previewWidth: attachment1.preview_width,
375
- previewHeight: attachment1.preview_height,
376
-
377
- largePreviewUrl: attachment1.large_preview_url,
378
- largePreviewWidth: attachment1.large_preview_width,
379
- largePreviewHeight: attachment1.large_preview_height,
380
-
381
- url: attachment1.metadata.url, // @Legacy
382
- width: attachment1.metadata.dimensions.split(",")[0], // @Legacy
383
- height: attachment1.metadata.dimensions.split(",")[1], // @Legacy
384
- name: attachment1.fileName // @Legacy
385
- };
386
- case "animated_image":
387
- return {
388
- type: "animated_image",
389
- ID: attachment2.id.toString(),
390
- filename: attachment2.filename,
391
-
392
- previewUrl: attachment1.preview_url,
393
- previewWidth: attachment1.preview_width,
394
- previewHeight: attachment1.preview_height,
395
-
396
- url: attachment2.image_data.url,
397
- width: attachment2.image_data.width,
398
- height: attachment2.image_data.height,
399
-
400
- name: attachment1.name, // @Legacy
401
- facebookUrl: attachment1.url, // @Legacy
402
- thumbnailUrl: attachment1.thumbnail_url, // @Legacy
403
- mimeType: attachment2.mime_type, // @Legacy
404
- rawGifImage: attachment2.image_data.raw_gif_image, // @Legacy
405
- rawWebpImage: attachment2.image_data.raw_webp_image, // @Legacy
406
- animatedGifUrl: attachment2.image_data.animated_gif_url, // @Legacy
407
- animatedGifPreviewUrl: attachment2.image_data.animated_gif_preview_url, // @Legacy
408
- animatedWebpUrl: attachment2.image_data.animated_webp_url, // @Legacy
409
- animatedWebpPreviewUrl: attachment2.image_data.animated_webp_preview_url // @Legacy
410
- };
411
- case "share":
412
- return {
413
- type: "share",
414
- ID: attachment1.share.share_id.toString(),
415
- url: attachment2.href,
416
-
417
- title: attachment1.share.title,
418
- description: attachment1.share.description,
419
- source: attachment1.share.source,
420
-
421
- image: attachment1.share.media.image,
422
- width: attachment1.share.media.image_size.width,
423
- height: attachment1.share.media.image_size.height,
424
- playable: attachment1.share.media.playable,
425
- duration: attachment1.share.media.duration,
426
-
427
- subattachments: attachment1.share.subattachments,
428
- properties: {},
429
-
430
- animatedImageSize: attachment1.share.media.animated_image_size, // @Legacy
431
- facebookUrl: attachment1.share.uri, // @Legacy
432
- target: attachment1.share.target, // @Legacy
433
- styleList: attachment1.share.style_list // @Legacy
434
- };
435
- case "video":
436
- return {
437
- type: "video",
438
- ID: attachment1.metadata.fbid.toString(),
439
- filename: attachment1.name,
440
-
441
- previewUrl: attachment1.preview_url,
442
- previewWidth: attachment1.preview_width,
443
- previewHeight: attachment1.preview_height,
444
-
445
- url: attachment1.url,
446
- width: attachment1.metadata.dimensions.width,
447
- height: attachment1.metadata.dimensions.height,
448
-
449
- duration: attachment1.metadata.duration,
450
- videoType: "unknown",
451
-
452
- thumbnailUrl: attachment1.thumbnail_url // @Legacy
453
- };
454
- case "error":
455
- return {
456
- type: "error",
457
-
458
- // Save error attachments because we're unsure of their format,
459
- // and whether there are cases they contain something useful for debugging.
460
- attachment1: attachment1,
461
- attachment2: attachment2
462
- };
463
- case "MessageImage":
464
- return {
465
- type: "photo",
466
- ID: blob.legacy_attachment_id,
467
- filename: blob.filename,
468
- thumbnailUrl: blob.thumbnail.uri,
469
-
470
- previewUrl: blob.preview.uri,
471
- previewWidth: blob.preview.width,
472
- previewHeight: blob.preview.height,
473
-
474
- largePreviewUrl: blob.large_preview.uri,
475
- largePreviewWidth: blob.large_preview.width,
476
- largePreviewHeight: blob.large_preview.height,
477
-
478
- url: blob.large_preview.uri, // @Legacy
479
- width: blob.original_dimensions.x, // @Legacy
480
- height: blob.original_dimensions.y, // @Legacy
481
- name: blob.filename // @Legacy
482
- };
483
- case "MessageAnimatedImage":
484
- return {
485
- type: "animated_image",
486
- ID: blob.legacy_attachment_id,
487
- filename: blob.filename,
488
-
489
- previewUrl: blob.preview_image.uri,
490
- previewWidth: blob.preview_image.width,
491
- previewHeight: blob.preview_image.height,
492
-
493
- url: blob.animated_image.uri,
494
- width: blob.animated_image.width,
495
- height: blob.animated_image.height,
496
-
497
- thumbnailUrl: blob.preview_image.uri, // @Legacy
498
- name: blob.filename, // @Legacy
499
- facebookUrl: blob.animated_image.uri, // @Legacy
500
- rawGifImage: blob.animated_image.uri, // @Legacy
501
- animatedGifUrl: blob.animated_image.uri, // @Legacy
502
- animatedGifPreviewUrl: blob.preview_image.uri, // @Legacy
503
- animatedWebpUrl: blob.animated_image.uri, // @Legacy
504
- animatedWebpPreviewUrl: blob.preview_image.uri // @Legacy
505
- };
506
- case "MessageVideo":
507
- return {
508
- type: "video",
509
- filename: blob.filename,
510
- ID: blob.legacy_attachment_id,
511
-
512
- previewUrl: blob.large_image.uri,
513
- previewWidth: blob.large_image.width,
514
- previewHeight: blob.large_image.height,
515
-
516
- url: blob.playable_url,
517
- width: blob.original_dimensions.x,
518
- height: blob.original_dimensions.y,
519
-
520
- duration: blob.playable_duration_in_ms,
521
- videoType: blob.video_type.toLowerCase(),
522
-
523
- thumbnailUrl: blob.large_image.uri // @Legacy
524
- };
525
- case "MessageAudio":
526
- return {
527
- type: "audio",
528
- filename: blob.filename,
529
- ID: blob.url_shimhash,
530
-
531
- audioType: blob.audio_type,
532
- duration: blob.playable_duration_in_ms,
533
- url: blob.playable_url,
534
-
535
- isVoiceMail: blob.is_voicemail
536
- };
537
- case "StickerAttachment":
538
- return {
539
- type: "sticker",
540
- ID: blob.id,
541
- url: blob.url,
542
-
543
- packID: blob.pack ? blob.pack.id : null,
544
- spriteUrl: blob.sprite_image,
545
- spriteUrl2x: blob.sprite_image_2x,
546
- width: blob.width,
547
- height: blob.height,
548
-
549
- caption: blob.label,
550
- description: blob.label,
551
-
552
- frameCount: blob.frame_count,
553
- frameRate: blob.frame_rate,
554
- framesPerRow: blob.frames_per_row,
555
- framesPerCol: blob.frames_per_column,
556
-
557
- stickerID: blob.id, // @Legacy
558
- spriteURI: blob.sprite_image, // @Legacy
559
- spriteURI2x: blob.sprite_image_2x // @Legacy
560
- };
561
- case "MessageLocation":
562
- var urlAttach = blob.story_attachment.url;
563
- var mediaAttach = blob.story_attachment.media;
564
-
565
- var u = querystring.parse(url.parse(urlAttach).query).u;
566
- var where1 = querystring.parse(url.parse(u).query).where1;
567
- var address = where1.split(", ");
568
-
569
- var latitude;
570
- var longitude;
571
-
572
- try {
573
- latitude = Number.parseFloat(address[0]);
574
- longitude = Number.parseFloat(address[1]);
575
- } catch (err) {
576
- /* empty */
577
- }
314
+ // TODO: THIS IS REALLY BAD
315
+ // This is an attempt at fixing Facebook's inconsistencies. Sometimes they give us
316
+ // two attachment objects, but sometimes only one. They each contain part of the
317
+ // data that you'd want so we merge them for convenience.
318
+ // Instead of having a bunch of if statements guarding every access to image_data,
319
+ // we set it to empty object and use the fact that it'll return undefined.
320
+ attachment2 = attachment2 || { id: "", image_data: {} };
321
+ attachment1 = attachment1.mercury ? attachment1.mercury : attachment1;
322
+ var blob = attachment1.blob_attachment;
323
+ var type =
324
+ blob && blob.__typename ? blob.__typename : attachment1.attach_type;
325
+ if (!type && attachment1.sticker_attachment) {
326
+ type = "StickerAttachment";
327
+ blob = attachment1.sticker_attachment;
328
+ } else if (!type && attachment1.extensible_attachment) {
329
+ if (
330
+ attachment1.extensible_attachment.story_attachment &&
331
+ attachment1.extensible_attachment.story_attachment.target &&
332
+ attachment1.extensible_attachment.story_attachment.target.__typename &&
333
+ attachment1.extensible_attachment.story_attachment.target.__typename ===
334
+ "MessageLocation"
335
+ )
336
+ type = "MessageLocation";
337
+ else type = "ExtensibleAttachment";
338
+
339
+ blob = attachment1.extensible_attachment;
340
+ }
341
+ // TODO: Determine whether "sticker", "photo", "file" etc are still used
342
+ // KEEP IN SYNC WITH getThreadHistory
343
+ switch (type) {
344
+ case "sticker":
345
+ return {
346
+ type: "sticker",
347
+ ID: attachment1.metadata.stickerID.toString(),
348
+ url: attachment1.url,
349
+
350
+ packID: attachment1.metadata.packID.toString(),
351
+ spriteUrl: attachment1.metadata.spriteURI,
352
+ spriteUrl2x: attachment1.metadata.spriteURI2x,
353
+ width: attachment1.metadata.width,
354
+ height: attachment1.metadata.height,
355
+
356
+ caption: attachment2.caption,
357
+ description: attachment2.description,
358
+
359
+ frameCount: attachment1.metadata.frameCount,
360
+ frameRate: attachment1.metadata.frameRate,
361
+ framesPerRow: attachment1.metadata.framesPerRow,
362
+ framesPerCol: attachment1.metadata.framesPerCol,
363
+
364
+ stickerID: attachment1.metadata.stickerID.toString(), // @Legacy
365
+ spriteURI: attachment1.metadata.spriteURI, // @Legacy
366
+ spriteURI2x: attachment1.metadata.spriteURI2x // @Legacy
367
+ };
368
+ case "file":
369
+ return {
370
+ type: "file",
371
+ filename: attachment1.name,
372
+ ID: attachment2.id.toString(),
373
+ url: attachment1.url,
374
+
375
+ isMalicious: attachment2.is_malicious,
376
+ contentType: attachment2.mime_type,
377
+
378
+ name: attachment1.name, // @Legacy
379
+ mimeType: attachment2.mime_type, // @Legacy
380
+ fileSize: attachment2.file_size // @Legacy
381
+ };
382
+ case "photo":
383
+ return {
384
+ type: "photo",
385
+ ID: attachment1.metadata.fbid.toString(),
386
+ filename: attachment1.fileName,
387
+ thumbnailUrl: attachment1.thumbnail_url,
388
+
389
+ previewUrl: attachment1.preview_url,
390
+ previewWidth: attachment1.preview_width,
391
+ previewHeight: attachment1.preview_height,
392
+
393
+ largePreviewUrl: attachment1.large_preview_url,
394
+ largePreviewWidth: attachment1.large_preview_width,
395
+ largePreviewHeight: attachment1.large_preview_height,
396
+
397
+ url: attachment1.metadata.url, // @Legacy
398
+ width: attachment1.metadata.dimensions.split(",")[0], // @Legacy
399
+ height: attachment1.metadata.dimensions.split(",")[1], // @Legacy
400
+ name: attachment1.fileName // @Legacy
401
+ };
402
+ case "animated_image":
403
+ return {
404
+ type: "animated_image",
405
+ ID: attachment2.id.toString(),
406
+ filename: attachment2.filename,
407
+
408
+ previewUrl: attachment1.preview_url,
409
+ previewWidth: attachment1.preview_width,
410
+ previewHeight: attachment1.preview_height,
411
+
412
+ url: attachment2.image_data.url,
413
+ width: attachment2.image_data.width,
414
+ height: attachment2.image_data.height,
415
+
416
+ name: attachment1.name, // @Legacy
417
+ facebookUrl: attachment1.url, // @Legacy
418
+ thumbnailUrl: attachment1.thumbnail_url, // @Legacy
419
+ mimeType: attachment2.mime_type, // @Legacy
420
+ rawGifImage: attachment2.image_data.raw_gif_image, // @Legacy
421
+ rawWebpImage: attachment2.image_data.raw_webp_image, // @Legacy
422
+ animatedGifUrl: attachment2.image_data.animated_gif_url, // @Legacy
423
+ animatedGifPreviewUrl: attachment2.image_data.animated_gif_preview_url, // @Legacy
424
+ animatedWebpUrl: attachment2.image_data.animated_webp_url, // @Legacy
425
+ animatedWebpPreviewUrl: attachment2.image_data.animated_webp_preview_url // @Legacy
426
+ };
427
+ case "share":
428
+ return {
429
+ type: "share",
430
+ ID: attachment1.share.share_id.toString(),
431
+ url: attachment2.href,
432
+
433
+ title: attachment1.share.title,
434
+ description: attachment1.share.description,
435
+ source: attachment1.share.source,
436
+
437
+ image: attachment1.share.media.image,
438
+ width: attachment1.share.media.image_size.width,
439
+ height: attachment1.share.media.image_size.height,
440
+ playable: attachment1.share.media.playable,
441
+ duration: attachment1.share.media.duration,
442
+
443
+ subattachments: attachment1.share.subattachments,
444
+ properties: {},
445
+
446
+ animatedImageSize: attachment1.share.media.animated_image_size, // @Legacy
447
+ facebookUrl: attachment1.share.uri, // @Legacy
448
+ target: attachment1.share.target, // @Legacy
449
+ styleList: attachment1.share.style_list // @Legacy
450
+ };
451
+ case "video":
452
+ return {
453
+ type: "video",
454
+ ID: attachment1.metadata.fbid.toString(),
455
+ filename: attachment1.name,
456
+
457
+ previewUrl: attachment1.preview_url,
458
+ previewWidth: attachment1.preview_width,
459
+ previewHeight: attachment1.preview_height,
460
+
461
+ url: attachment1.url,
462
+ width: attachment1.metadata.dimensions.width,
463
+ height: attachment1.metadata.dimensions.height,
464
+
465
+ duration: attachment1.metadata.duration,
466
+ videoType: "unknown",
467
+
468
+ thumbnailUrl: attachment1.thumbnail_url // @Legacy
469
+ };
470
+ case "error":
471
+ return {
472
+ type: "error",
473
+
474
+ // Save error attachments because we're unsure of their format,
475
+ // and whether there are cases they contain something useful for debugging.
476
+ attachment1: attachment1,
477
+ attachment2: attachment2
478
+ };
479
+ case "MessageImage":
480
+ return {
481
+ type: "photo",
482
+ ID: blob.legacy_attachment_id,
483
+ filename: blob.filename,
484
+ thumbnailUrl: blob.thumbnail.uri,
485
+
486
+ previewUrl: blob.preview.uri,
487
+ previewWidth: blob.preview.width,
488
+ previewHeight: blob.preview.height,
489
+
490
+ largePreviewUrl: blob.large_preview.uri,
491
+ largePreviewWidth: blob.large_preview.width,
492
+ largePreviewHeight: blob.large_preview.height,
493
+
494
+ url: blob.large_preview.uri, // @Legacy
495
+ width: blob.original_dimensions.x, // @Legacy
496
+ height: blob.original_dimensions.y, // @Legacy
497
+ name: blob.filename // @Legacy
498
+ };
499
+ case "MessageAnimatedImage":
500
+ return {
501
+ type: "animated_image",
502
+ ID: blob.legacy_attachment_id,
503
+ filename: blob.filename,
504
+
505
+ previewUrl: blob.preview_image.uri,
506
+ previewWidth: blob.preview_image.width,
507
+ previewHeight: blob.preview_image.height,
508
+
509
+ url: blob.animated_image.uri,
510
+ width: blob.animated_image.width,
511
+ height: blob.animated_image.height,
512
+
513
+ thumbnailUrl: blob.preview_image.uri, // @Legacy
514
+ name: blob.filename, // @Legacy
515
+ facebookUrl: blob.animated_image.uri, // @Legacy
516
+ rawGifImage: blob.animated_image.uri, // @Legacy
517
+ animatedGifUrl: blob.animated_image.uri, // @Legacy
518
+ animatedGifPreviewUrl: blob.preview_image.uri, // @Legacy
519
+ animatedWebpUrl: blob.animated_image.uri, // @Legacy
520
+ animatedWebpPreviewUrl: blob.preview_image.uri // @Legacy
521
+ };
522
+ case "MessageVideo":
523
+ return {
524
+ type: "video",
525
+ filename: blob.filename,
526
+ ID: blob.legacy_attachment_id,
527
+
528
+ previewUrl: blob.large_image.uri,
529
+ previewWidth: blob.large_image.width,
530
+ previewHeight: blob.large_image.height,
531
+
532
+ url: blob.playable_url,
533
+ width: blob.original_dimensions.x,
534
+ height: blob.original_dimensions.y,
535
+
536
+ duration: blob.playable_duration_in_ms,
537
+ videoType: blob.video_type.toLowerCase(),
538
+
539
+ thumbnailUrl: blob.large_image.uri // @Legacy
540
+ };
541
+ case "MessageAudio":
542
+ return {
543
+ type: "audio",
544
+ filename: blob.filename,
545
+ ID: blob.url_shimhash,
546
+
547
+ audioType: blob.audio_type,
548
+ duration: blob.playable_duration_in_ms,
549
+ url: blob.playable_url,
550
+
551
+ isVoiceMail: blob.is_voicemail
552
+ };
553
+ case "StickerAttachment":
554
+ return {
555
+ type: "sticker",
556
+ ID: blob.id,
557
+ url: blob.url,
558
+
559
+ packID: blob.pack ? blob.pack.id : null,
560
+ spriteUrl: blob.sprite_image,
561
+ spriteUrl2x: blob.sprite_image_2x,
562
+ width: blob.width,
563
+ height: blob.height,
564
+
565
+ caption: blob.label,
566
+ description: blob.label,
567
+
568
+ frameCount: blob.frame_count,
569
+ frameRate: blob.frame_rate,
570
+ framesPerRow: blob.frames_per_row,
571
+ framesPerCol: blob.frames_per_column,
572
+
573
+ stickerID: blob.id, // @Legacy
574
+ spriteURI: blob.sprite_image, // @Legacy
575
+ spriteURI2x: blob.sprite_image_2x // @Legacy
576
+ };
577
+ case "MessageLocation":
578
+ var urlAttach = blob.story_attachment.url;
579
+ var mediaAttach = blob.story_attachment.media;
580
+
581
+ var u = querystring.parse(url.parse(urlAttach).query).u;
582
+ var where1 = querystring.parse(url.parse(u).query).where1;
583
+ var address = where1.split(", ");
584
+
585
+ var latitude;
586
+ var longitude;
578
587
 
579
- var imageUrl;
580
- var width;
581
- var height;
588
+ try {
589
+ latitude = Number.parseFloat(address[0]);
590
+ longitude = Number.parseFloat(address[1]);
591
+ } catch (err) {
592
+ /* empty */
593
+ }
582
594
 
583
- if (mediaAttach && mediaAttach.image) {
584
- imageUrl = mediaAttach.image.uri;
585
- width = mediaAttach.image.width;
586
- height = mediaAttach.image.height;
587
- }
595
+ var imageUrl;
596
+ var width;
597
+ var height;
588
598
 
589
- return {
590
- type: "location",
591
- ID: blob.legacy_attachment_id,
592
- latitude: latitude,
593
- longitude: longitude,
594
- image: imageUrl,
595
- width: width,
596
- height: height,
597
- url: u || urlAttach,
598
- address: where1,
599
-
600
- facebookUrl: blob.story_attachment.url, // @Legacy
601
- target: blob.story_attachment.target, // @Legacy
602
- styleList: blob.story_attachment.style_list // @Legacy
603
- };
604
- case "ExtensibleAttachment":
605
- return {
606
- type: "share",
607
- ID: blob.legacy_attachment_id,
608
- url: blob.story_attachment.url,
609
-
610
- title: blob.story_attachment.title_with_entities.text,
611
- description: blob.story_attachment.description &&
612
- blob.story_attachment.description.text,
613
- source: blob.story_attachment.source ? blob.story_attachment.source.text : null,
614
-
615
- image: blob.story_attachment.media &&
616
- blob.story_attachment.media.image &&
617
- blob.story_attachment.media.image.uri,
618
- width: blob.story_attachment.media &&
619
- blob.story_attachment.media.image &&
620
- blob.story_attachment.media.image.width,
621
- height: blob.story_attachment.media &&
622
- blob.story_attachment.media.image &&
623
- blob.story_attachment.media.image.height,
624
- playable: blob.story_attachment.media &&
625
- blob.story_attachment.media.is_playable,
626
- duration: blob.story_attachment.media &&
627
- blob.story_attachment.media.playable_duration_in_ms,
628
- playableUrl: blob.story_attachment.media == null ? null : blob.story_attachment.media.playable_url,
629
-
630
- subattachments: blob.story_attachment.subattachments,
631
- properties: blob.story_attachment.properties.reduce(function(obj, cur) {
632
- obj[cur.key] = cur.value.text;
633
- return obj;
634
- }, {}),
635
-
636
- facebookUrl: blob.story_attachment.url, // @Legacy
637
- target: blob.story_attachment.target, // @Legacy
638
- styleList: blob.story_attachment.style_list // @Legacy
639
- };
640
- case "MessageFile":
641
- return {
642
- type: "file",
643
- filename: blob.filename,
644
- ID: blob.message_file_fbid,
645
-
646
- url: blob.url,
647
- isMalicious: blob.is_malicious,
648
- contentType: blob.content_type,
649
-
650
- name: blob.filename,
651
- mimeType: "",
652
- fileSize: -1
653
- };
654
- default:
655
- throw new Error(
656
- "unrecognized attach_file of type " +
657
- type +
658
- "`" +
659
- JSON.stringify(attachment1, null, 4) +
660
- " attachment2: " +
661
- JSON.stringify(attachment2, null, 4) +
662
- "`"
663
- );
664
- }
599
+ if (mediaAttach && mediaAttach.image) {
600
+ imageUrl = mediaAttach.image.uri;
601
+ width = mediaAttach.image.width;
602
+ height = mediaAttach.image.height;
603
+ }
604
+
605
+ return {
606
+ type: "location",
607
+ ID: blob.legacy_attachment_id,
608
+ latitude: latitude,
609
+ longitude: longitude,
610
+ image: imageUrl,
611
+ width: width,
612
+ height: height,
613
+ url: u || urlAttach,
614
+ address: where1,
615
+
616
+ facebookUrl: blob.story_attachment.url, // @Legacy
617
+ target: blob.story_attachment.target, // @Legacy
618
+ styleList: blob.story_attachment.style_list // @Legacy
619
+ };
620
+ case "ExtensibleAttachment":
621
+ return {
622
+ type: "share",
623
+ ID: blob.legacy_attachment_id,
624
+ url: blob.story_attachment.url,
625
+
626
+ title: blob.story_attachment.title_with_entities.text,
627
+ description:
628
+ blob.story_attachment.description &&
629
+ blob.story_attachment.description.text,
630
+ source: blob.story_attachment.source
631
+ ? blob.story_attachment.source.text
632
+ : null,
633
+
634
+ image:
635
+ blob.story_attachment.media &&
636
+ blob.story_attachment.media.image &&
637
+ blob.story_attachment.media.image.uri,
638
+ width:
639
+ blob.story_attachment.media &&
640
+ blob.story_attachment.media.image &&
641
+ blob.story_attachment.media.image.width,
642
+ height:
643
+ blob.story_attachment.media &&
644
+ blob.story_attachment.media.image &&
645
+ blob.story_attachment.media.image.height,
646
+ playable:
647
+ blob.story_attachment.media &&
648
+ blob.story_attachment.media.is_playable,
649
+ duration:
650
+ blob.story_attachment.media &&
651
+ blob.story_attachment.media.playable_duration_in_ms,
652
+ playableUrl:
653
+ blob.story_attachment.media == null
654
+ ? null
655
+ : blob.story_attachment.media.playable_url,
656
+
657
+ subattachments: blob.story_attachment.subattachments,
658
+ properties: blob.story_attachment.properties.reduce(function(obj, cur) {
659
+ obj[cur.key] = cur.value.text;
660
+ return obj;
661
+ }, {}),
662
+
663
+ facebookUrl: blob.story_attachment.url, // @Legacy
664
+ target: blob.story_attachment.target, // @Legacy
665
+ styleList: blob.story_attachment.style_list // @Legacy
666
+ };
667
+ case "MessageFile":
668
+ return {
669
+ type: "file",
670
+ filename: blob.filename,
671
+ ID: blob.message_file_fbid,
672
+
673
+ url: blob.url,
674
+ isMalicious: blob.is_malicious,
675
+ contentType: blob.content_type,
676
+
677
+ name: blob.filename,
678
+ mimeType: "",
679
+ fileSize: -1
680
+ };
681
+ default:
682
+ throw new Error(
683
+ "unrecognized attach_file of type " +
684
+ type +
685
+ "`" +
686
+ JSON.stringify(attachment1, null, 4) +
687
+ " attachment2: " +
688
+ JSON.stringify(attachment2, null, 4) +
689
+ "`"
690
+ );
691
+ }
665
692
  }
666
693
 
667
694
  function formatAttachment(attachments, attachmentIds, attachmentMap, shareMap) {
668
- attachmentMap = shareMap || attachmentMap;
669
- return attachments ?
670
- attachments.map(function(val, i) {
671
- if (!attachmentMap ||
672
- !attachmentIds ||
673
- !attachmentMap[attachmentIds[i]]
674
- ) {
675
- return _formatAttachment(val);
676
- }
677
- return _formatAttachment(val, attachmentMap[attachmentIds[i]]);
678
- }) : [];
695
+ attachmentMap = shareMap || attachmentMap;
696
+ return attachments
697
+ ? attachments.map(function(val, i) {
698
+ if (
699
+ !attachmentMap ||
700
+ !attachmentIds ||
701
+ !attachmentMap[attachmentIds[i]]
702
+ ) {
703
+ return _formatAttachment(val);
704
+ }
705
+ return _formatAttachment(val, attachmentMap[attachmentIds[i]]);
706
+ })
707
+ : [];
679
708
  }
680
709
 
681
710
  function formatDeltaMessage(m) {
682
- var md = m.delta.messageMetadata;
683
- var mdata =
684
- m.delta.data === undefined ? [] :
685
- m.delta.data.prng === undefined ? [] :
686
- JSON.parse(m.delta.data.prng);
687
- var m_id = mdata.map(u => u.i);
688
- var m_offset = mdata.map(u => u.o);
689
- var m_length = mdata.map(u => u.l);
690
- var mentions = {};
691
- var body = m.delta.body || "";
692
- var args = body == "" ? [] : body.trim().split(/\s+/);
693
- for (var i = 0; i < m_id.length; i++) mentions[m_id[i]] = m.delta.body.substring(m_offset[i], m_offset[i] + m_length[i]);
694
-
695
- return {
696
- type: "message",
697
- senderID: formatID(md.actorFbId.toString()),
698
- threadID: formatID((md.threadKey.threadFbId || md.threadKey.otherUserFbId).toString()),
699
- messageID: md.messageId,
700
- args: args,
701
- body: body,
702
- attachments: (m.delta.attachments || []).map(v => _formatAttachment(v)),
703
- mentions: mentions,
704
- timestamp: md.timestamp,
705
- isGroup: !!md.threadKey.threadFbId,
706
- participantIDs: m.delta.participants || []
707
- };
711
+ var md = m.delta.messageMetadata;
712
+ var mdata =
713
+ m.delta.data === undefined
714
+ ? []
715
+ : m.delta.data.prng === undefined ? [] : JSON.parse(m.delta.data.prng);
716
+ var m_id = mdata.map(u => u.i);
717
+ var m_offset = mdata.map(u => u.o);
718
+ var m_length = mdata.map(u => u.l);
719
+ var mentions = {};
720
+ var body = m.delta.body || "";
721
+ var args = body == "" ? [] : body.trim().split(/\s+/);
722
+ for (var i = 0; i < m_id.length; i++)
723
+ mentions[m_id[i]] = m.delta.body.substring(
724
+ m_offset[i],
725
+ m_offset[i] + m_length[i]
726
+ );
727
+
728
+ return {
729
+ type: "message",
730
+ senderID: formatID(md.actorFbId.toString()),
731
+ threadID: formatID(
732
+ (md.threadKey.threadFbId || md.threadKey.otherUserFbId).toString()
733
+ ),
734
+ messageID: md.messageId,
735
+ args: args,
736
+ body: body,
737
+ attachments: (m.delta.attachments || []).map(v => _formatAttachment(v)),
738
+ mentions: mentions,
739
+ timestamp: md.timestamp,
740
+ isGroup: !!md.threadKey.threadFbId,
741
+ participantIDs: m.delta.participants || []
742
+ };
708
743
  }
709
744
 
710
745
  function formatID(id) {
711
- if (id != undefined && id != null) return id.replace(/(fb)?id[:.]/, "");
712
- else return id;
746
+ if (id != undefined && id != null) return id.replace(/(fb)?id[:.]/, "");
747
+ else return id;
713
748
  }
714
749
 
715
750
  function formatMessage(m) {
716
- var originalMessage = m.message ? m.message : m;
717
- var obj = {
718
- type: "message",
719
- senderName: originalMessage.sender_name,
720
- senderID: formatID(originalMessage.sender_fbid.toString()),
721
- participantNames: originalMessage.group_thread_info ? originalMessage.group_thread_info.participant_names : [originalMessage.sender_name.split(" ")[0]],
722
- participantIDs: originalMessage.group_thread_info ?
723
- originalMessage.group_thread_info.participant_ids.map(function(v) {
724
- return formatID(v.toString());
725
- }) : [formatID(originalMessage.sender_fbid)],
726
- body: originalMessage.body || "",
727
- threadID: formatID((originalMessage.thread_fbid || originalMessage.other_user_fbid).toString()),
728
- threadName: originalMessage.group_thread_info ? originalMessage.group_thread_info.name : originalMessage.sender_name,
729
- location: originalMessage.coordinates ? originalMessage.coordinates : null,
730
- messageID: originalMessage.mid ? originalMessage.mid.toString() : originalMessage.message_id,
731
- attachments: formatAttachment(
732
- originalMessage.attachments,
733
- originalMessage.attachmentIds,
734
- originalMessage.attachment_map,
735
- originalMessage.share_map
736
- ),
737
- timestamp: originalMessage.timestamp,
738
- timestampAbsolute: originalMessage.timestamp_absolute,
739
- timestampRelative: originalMessage.timestamp_relative,
740
- timestampDatetime: originalMessage.timestamp_datetime,
741
- tags: originalMessage.tags,
742
- reactions: originalMessage.reactions ? originalMessage.reactions : [],
743
- isUnread: originalMessage.is_unread
744
- };
745
-
746
- if (m.type === "pages_messaging") obj.pageID = m.realtime_viewer_fbid.toString();
747
- obj.isGroup = obj.participantIDs.length > 2;
748
-
749
- return obj;
751
+ var originalMessage = m.message ? m.message : m;
752
+ var obj = {
753
+ type: "message",
754
+ senderName: originalMessage.sender_name,
755
+ senderID: formatID(originalMessage.sender_fbid.toString()),
756
+ participantNames: originalMessage.group_thread_info
757
+ ? originalMessage.group_thread_info.participant_names
758
+ : [originalMessage.sender_name.split(" ")[0]],
759
+ participantIDs: originalMessage.group_thread_info
760
+ ? originalMessage.group_thread_info.participant_ids.map(function(v) {
761
+ return formatID(v.toString());
762
+ })
763
+ : [formatID(originalMessage.sender_fbid)],
764
+ body: originalMessage.body || "",
765
+ threadID: formatID(
766
+ (
767
+ originalMessage.thread_fbid || originalMessage.other_user_fbid
768
+ ).toString()
769
+ ),
770
+ threadName: originalMessage.group_thread_info
771
+ ? originalMessage.group_thread_info.name
772
+ : originalMessage.sender_name,
773
+ location: originalMessage.coordinates ? originalMessage.coordinates : null,
774
+ messageID: originalMessage.mid
775
+ ? originalMessage.mid.toString()
776
+ : originalMessage.message_id,
777
+ attachments: formatAttachment(
778
+ originalMessage.attachments,
779
+ originalMessage.attachmentIds,
780
+ originalMessage.attachment_map,
781
+ originalMessage.share_map
782
+ ),
783
+ timestamp: originalMessage.timestamp,
784
+ timestampAbsolute: originalMessage.timestamp_absolute,
785
+ timestampRelative: originalMessage.timestamp_relative,
786
+ timestampDatetime: originalMessage.timestamp_datetime,
787
+ tags: originalMessage.tags,
788
+ reactions: originalMessage.reactions ? originalMessage.reactions : [],
789
+ isUnread: originalMessage.is_unread
790
+ };
791
+
792
+ if (m.type === "pages_messaging")
793
+ obj.pageID = m.realtime_viewer_fbid.toString();
794
+ obj.isGroup = obj.participantIDs.length > 2;
795
+
796
+ return obj;
750
797
  }
751
798
 
752
799
  function formatEvent(m) {
753
- var originalMessage = m.message ? m.message : m;
754
- var logMessageType = originalMessage.log_message_type;
755
- var logMessageData;
756
- if (logMessageType === "log:generic-admin-text") {
757
- logMessageData = originalMessage.log_message_data.untypedData;
758
- logMessageType = getAdminTextMessageType(originalMessage.log_message_data.message_type);
759
- } else logMessageData = originalMessage.log_message_data;
760
-
761
- return Object.assign(formatMessage(originalMessage), {
762
- type: "event",
763
- logMessageType: logMessageType,
764
- logMessageData: logMessageData,
765
- logMessageBody: originalMessage.log_message_body
766
- });
800
+ var originalMessage = m.message ? m.message : m;
801
+ var logMessageType = originalMessage.log_message_type;
802
+ var logMessageData;
803
+ if (logMessageType === "log:generic-admin-text") {
804
+ logMessageData = originalMessage.log_message_data.untypedData;
805
+ logMessageType = getAdminTextMessageType(
806
+ originalMessage.log_message_data.message_type
807
+ );
808
+ } else logMessageData = originalMessage.log_message_data;
809
+
810
+ return Object.assign(formatMessage(originalMessage), {
811
+ type: "event",
812
+ logMessageType: logMessageType,
813
+ logMessageData: logMessageData,
814
+ logMessageBody: originalMessage.log_message_body
815
+ });
767
816
  }
768
817
 
769
818
  function formatHistoryMessage(m) {
770
- switch (m.action_type) {
771
- case "ma-type:log-message":
772
- return formatEvent(m);
773
- default:
774
- return formatMessage(m);
775
- }
819
+ switch (m.action_type) {
820
+ case "ma-type:log-message":
821
+ return formatEvent(m);
822
+ default:
823
+ return formatMessage(m);
824
+ }
776
825
  }
777
826
 
778
827
  // Get a more readable message type for AdminTextMessages
779
828
  function getAdminTextMessageType(m) {
780
- switch (m.type) {
781
- case "change_thread_theme":
782
- return "log:thread-color";
783
- case "change_thread_icon":
784
- return "log:thread-icon";
785
- case "change_thread_nickname":
786
- return "log:user-nickname";
787
- case "change_thread_admins":
788
- return "log:thread-admins";
789
- case "group_poll":
790
- return "log:thread-poll";
791
- case "change_thread_approval_mode":
792
- return "log:thread-approval-mode";
793
- case "messenger_call_log":
794
- case "participant_joined_group_call":
795
- return "log:thread-call";
796
- }
829
+ switch (m.type) {
830
+ case "change_thread_theme":
831
+ return "log:thread-color";
832
+ case "change_thread_icon":
833
+ return "log:thread-icon";
834
+ case "change_thread_nickname":
835
+ return "log:user-nickname";
836
+ case "change_thread_admins":
837
+ return "log:thread-admins";
838
+ case "group_poll":
839
+ return "log:thread-poll";
840
+ case "change_thread_approval_mode":
841
+ return "log:thread-approval-mode";
842
+ case "messenger_call_log":
843
+ case "participant_joined_group_call":
844
+ return "log:thread-call";
845
+ }
797
846
  }
798
847
 
799
848
  function formatDeltaEvent(m) {
800
- var { updateData,getData,hasData } = require('./Extra/ExtraGetThread');
801
- var logMessageType;
802
- var logMessageData;
849
+ var { updateData, getData, hasData } = require("./Extra/ExtraGetThread");
850
+ var logMessageType;
851
+ var logMessageData;
803
852
 
804
- switch (m.class) {
853
+ switch (m.class) {
805
854
  case "AdminTextMessage":
806
- logMessageType = getAdminTextMessageType(m);
807
- logMessageData = m.untypedData;
808
- break;
855
+ logMessageType = getAdminTextMessageType(m);
856
+ logMessageData = m.untypedData;
857
+ break;
809
858
  case "ThreadName":
810
- logMessageType = "log:thread-name";
811
- logMessageData = { name: m.name };
812
- break;
859
+ logMessageType = "log:thread-name";
860
+ logMessageData = { name: m.name };
861
+ break;
813
862
  case "ParticipantsAddedToGroupThread":
814
- logMessageType = "log:subscribe";
815
- logMessageData = { addedParticipants: m.addedParticipants };
816
- break;
863
+ logMessageType = "log:subscribe";
864
+ logMessageData = { addedParticipants: m.addedParticipants };
865
+ break;
817
866
  case "ParticipantLeftGroupThread":
818
- logMessageType = "log:unsubscribe";
819
- logMessageData = { leftParticipantFbId: m.leftParticipantFbId };
820
- break;
867
+ logMessageType = "log:unsubscribe";
868
+ logMessageData = { leftParticipantFbId: m.leftParticipantFbId };
869
+ break;
821
870
  case "UserLocation": {
822
- logMessageType = "log:user-location";
823
- logMessageData = {
824
- Image: m.attachments[0].mercury.extensible_attachment.story_attachment.media.image,
825
- Location: m.attachments[0].mercury.extensible_attachment.story_attachment.target.location_title,
826
- coordinates: m.attachments[0].mercury.extensible_attachment.story_attachment.target.coordinate,
827
- url: m.attachments[0].mercury.extensible_attachment.story_attachment.url
828
- };
871
+ logMessageType = "log:user-location";
872
+ logMessageData = {
873
+ Image:
874
+ m.attachments[0].mercury.extensible_attachment.story_attachment.media
875
+ .image,
876
+ Location:
877
+ m.attachments[0].mercury.extensible_attachment.story_attachment.target
878
+ .location_title,
879
+ coordinates:
880
+ m.attachments[0].mercury.extensible_attachment.story_attachment.target
881
+ .coordinate,
882
+ url: m.attachments[0].mercury.extensible_attachment.story_attachment.url
883
+ };
829
884
  }
830
- }
831
- switch (hasData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()))) {
885
+ }
886
+ switch (hasData(
887
+ formatID(
888
+ (
889
+ m.messageMetadata.threadKey.threadFbId ||
890
+ m.messageMetadata.threadKey.otherUserFbId
891
+ ).toString()
892
+ )
893
+ )) {
832
894
  case true: {
833
- switch (logMessageType) {
834
- case "log:thread-color": {
835
- let x = getData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
836
- x.emoji = (logMessageData.theme_emoji || x.emoji);
837
- x.color = (logMessageData['theme_color'] || x.color);
838
- updateData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),x);
839
- }
840
- break;
841
- case "log:thread-icon": {
842
- let x = getData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
843
- x.emoji = (logMessageData['thread_icon'] || x.emoji);
844
- updateData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),x);
845
- }
846
- break;
847
- case "log:user-nickname": {
848
- let x = getData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
849
- x.nicknames[logMessageData.participant_id] = (logMessageData.nickname.length == 0 ? x.userInfo.find(i => i.id == String(logMessageData.participant_id)).name : logMessageData.nickname);
850
- updateData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),x);
851
- }
852
- break;
853
- case "log:thread-admins": {
854
- let x = getData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
855
- switch (logMessageData.ADMIN_EVENT) {
856
- case "add_admin": {
857
- x.adminIDs.push({ id: logMessageData.TARGET_ID });
858
- }
859
- break;
860
- case "remove_admin": {
861
- x.adminIDs = x.adminIDs.filter(item => item.id != logMessageData.TARGET_ID);
862
- }
863
- break;
895
+ switch (logMessageType) {
896
+ case "log:thread-color":
897
+ {
898
+ let x = getData(
899
+ formatID(
900
+ (
901
+ m.messageMetadata.threadKey.threadFbId ||
902
+ m.messageMetadata.threadKey.otherUserFbId
903
+ ).toString()
904
+ )
905
+ );
906
+ x.emoji = logMessageData.theme_emoji || x.emoji;
907
+ x.color = logMessageData["theme_color"] || x.color;
908
+ updateData(
909
+ formatID(
910
+ (
911
+ m.messageMetadata.threadKey.threadFbId ||
912
+ m.messageMetadata.threadKey.otherUserFbId
913
+ ).toString()
914
+ ),
915
+ x
916
+ );
917
+ }
918
+ break;
919
+ case "log:thread-icon":
920
+ {
921
+ let x = getData(
922
+ formatID(
923
+ (
924
+ m.messageMetadata.threadKey.threadFbId ||
925
+ m.messageMetadata.threadKey.otherUserFbId
926
+ ).toString()
927
+ )
928
+ );
929
+ x.emoji = logMessageData["thread_icon"] || x.emoji;
930
+ updateData(
931
+ formatID(
932
+ (
933
+ m.messageMetadata.threadKey.threadFbId ||
934
+ m.messageMetadata.threadKey.otherUserFbId
935
+ ).toString()
936
+ ),
937
+ x
938
+ );
939
+ }
940
+ break;
941
+ case "log:user-nickname":
942
+ {
943
+ let x = getData(
944
+ formatID(
945
+ (
946
+ m.messageMetadata.threadKey.threadFbId ||
947
+ m.messageMetadata.threadKey.otherUserFbId
948
+ ).toString()
949
+ )
950
+ );
951
+ x.nicknames[logMessageData.participant_id] =
952
+ logMessageData.nickname.length == 0
953
+ ? x.userInfo.find(
954
+ i => i.id == String(logMessageData.participant_id)
955
+ ).name
956
+ : logMessageData.nickname;
957
+ updateData(
958
+ formatID(
959
+ (
960
+ m.messageMetadata.threadKey.threadFbId ||
961
+ m.messageMetadata.threadKey.otherUserFbId
962
+ ).toString()
963
+ ),
964
+ x
965
+ );
966
+ }
967
+ break;
968
+ case "log:thread-admins":
969
+ {
970
+ let x = getData(
971
+ formatID(
972
+ (
973
+ m.messageMetadata.threadKey.threadFbId ||
974
+ m.messageMetadata.threadKey.otherUserFbId
975
+ ).toString()
976
+ )
977
+ );
978
+ switch (logMessageData.ADMIN_EVENT) {
979
+ case "add_admin":
980
+ {
981
+ x.adminIDs.push({ id: logMessageData.TARGET_ID });
864
982
  }
865
- updateData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),x);
866
- }
867
983
  break;
868
- case "log:thread-approval-mode": {
869
- let x = getData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
870
- if (x.approvalMode == true) {
871
- x.approvalMode = false;
872
- }
873
- else {
874
- x.approvalMode = true;
984
+ case "remove_admin":
985
+ {
986
+ x.adminIDs = x.adminIDs.filter(
987
+ item => item.id != logMessageData.TARGET_ID
988
+ );
875
989
  }
876
- updateData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),x);
877
- }
878
990
  break;
879
- case "log:thread-name": {
880
- let x = getData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
881
- x.threadName = (logMessageData.name || formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
882
- updateData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),x);
883
991
  }
884
- break;
885
- case "log:subscribe": {
886
- let x = getData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
887
- for (let o of logMessageData.addedParticipants) {
888
- if (x.userInfo.some(i => i.id == o.userFbId)) continue;
889
- else {
890
- x.userInfo.push({
891
- id: o.userFbId,
892
- name: o.fullName,
893
- gender: getGenderByPhysicalMethod(o.fullName)
894
- });
895
- x.participantIDs.push(o.userFbId);
896
- }
897
- }
898
- updateData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),x);
992
+ updateData(
993
+ formatID(
994
+ (
995
+ m.messageMetadata.threadKey.threadFbId ||
996
+ m.messageMetadata.threadKey.otherUserFbId
997
+ ).toString()
998
+ ),
999
+ x
1000
+ );
1001
+ }
1002
+ break;
1003
+ case "log:thread-approval-mode":
1004
+ {
1005
+ let x = getData(
1006
+ formatID(
1007
+ (
1008
+ m.messageMetadata.threadKey.threadFbId ||
1009
+ m.messageMetadata.threadKey.otherUserFbId
1010
+ ).toString()
1011
+ )
1012
+ );
1013
+ if (x.approvalMode == true) {
1014
+ x.approvalMode = false;
1015
+ } else {
1016
+ x.approvalMode = true;
899
1017
  }
900
- break;
901
- case "log:unsubscribe": {
902
- let x = getData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()));
903
- x.participantIDs = x.participantIDs.filter(item => item != logMessageData.leftParticipantFbId);
904
- x.userInfo = x.userInfo.filter(item => item.id != logMessageData.leftParticipantFbId);
905
- if (x.adminIDs.some(i => i.id == logMessageData.leftParticipantFbId)) {
906
- x.adminIDs = x.adminIDs.filter(item => item.id != logMessageData.leftParticipantFbId);
907
- }
908
- updateData(formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),x);
1018
+ updateData(
1019
+ formatID(
1020
+ (
1021
+ m.messageMetadata.threadKey.threadFbId ||
1022
+ m.messageMetadata.threadKey.otherUserFbId
1023
+ ).toString()
1024
+ ),
1025
+ x
1026
+ );
1027
+ }
1028
+ break;
1029
+ case "log:thread-name":
1030
+ {
1031
+ let x = getData(
1032
+ formatID(
1033
+ (
1034
+ m.messageMetadata.threadKey.threadFbId ||
1035
+ m.messageMetadata.threadKey.otherUserFbId
1036
+ ).toString()
1037
+ )
1038
+ );
1039
+ x.threadName =
1040
+ logMessageData.name ||
1041
+ formatID(
1042
+ (
1043
+ m.messageMetadata.threadKey.threadFbId ||
1044
+ m.messageMetadata.threadKey.otherUserFbId
1045
+ ).toString()
1046
+ );
1047
+ updateData(
1048
+ formatID(
1049
+ (
1050
+ m.messageMetadata.threadKey.threadFbId ||
1051
+ m.messageMetadata.threadKey.otherUserFbId
1052
+ ).toString()
1053
+ ),
1054
+ x
1055
+ );
1056
+ }
1057
+ break;
1058
+ case "log:subscribe":
1059
+ {
1060
+ let x = getData(
1061
+ formatID(
1062
+ (
1063
+ m.messageMetadata.threadKey.threadFbId ||
1064
+ m.messageMetadata.threadKey.otherUserFbId
1065
+ ).toString()
1066
+ )
1067
+ );
1068
+ for (let o of logMessageData.addedParticipants) {
1069
+ if (x.userInfo.some(i => i.id == o.userFbId)) continue;
1070
+ else {
1071
+ x.userInfo.push({
1072
+ id: o.userFbId,
1073
+ name: o.fullName,
1074
+ gender: getGenderByPhysicalMethod(o.fullName)
1075
+ });
1076
+ x.participantIDs.push(o.userFbId);
1077
+ }
909
1078
  }
910
- break;
911
- }
1079
+ updateData(
1080
+ formatID(
1081
+ (
1082
+ m.messageMetadata.threadKey.threadFbId ||
1083
+ m.messageMetadata.threadKey.otherUserFbId
1084
+ ).toString()
1085
+ ),
1086
+ x
1087
+ );
1088
+ }
1089
+ break;
1090
+ case "log:unsubscribe":
1091
+ {
1092
+ let x = getData(
1093
+ formatID(
1094
+ (
1095
+ m.messageMetadata.threadKey.threadFbId ||
1096
+ m.messageMetadata.threadKey.otherUserFbId
1097
+ ).toString()
1098
+ )
1099
+ );
1100
+ x.participantIDs = x.participantIDs.filter(
1101
+ item => item != logMessageData.leftParticipantFbId
1102
+ );
1103
+ x.userInfo = x.userInfo.filter(
1104
+ item => item.id != logMessageData.leftParticipantFbId
1105
+ );
1106
+ if (
1107
+ x.adminIDs.some(i => i.id == logMessageData.leftParticipantFbId)
1108
+ ) {
1109
+ x.adminIDs = x.adminIDs.filter(
1110
+ item => item.id != logMessageData.leftParticipantFbId
1111
+ );
1112
+ }
1113
+ updateData(
1114
+ formatID(
1115
+ (
1116
+ m.messageMetadata.threadKey.threadFbId ||
1117
+ m.messageMetadata.threadKey.otherUserFbId
1118
+ ).toString()
1119
+ ),
1120
+ x
1121
+ );
1122
+ }
1123
+ break;
1124
+ }
912
1125
  }
913
- }
1126
+ }
914
1127
 
915
- return {
1128
+ return {
916
1129
  type: "event",
917
- threadID: formatID((m.messageMetadata.threadKey.threadFbId || m.messageMetadata.threadKey.otherUserFbId).toString()),
1130
+ threadID: formatID(
1131
+ (
1132
+ m.messageMetadata.threadKey.threadFbId ||
1133
+ m.messageMetadata.threadKey.otherUserFbId
1134
+ ).toString()
1135
+ ),
918
1136
  logMessageType: logMessageType,
919
1137
  logMessageData: logMessageData,
920
1138
  logMessageBody: m.messageMetadata.adminText,
921
1139
  author: m.messageMetadata.actorFbId,
922
1140
  participantIDs: m.participants || []
923
- };
1141
+ };
924
1142
  }
925
1143
 
926
1144
  /**
@@ -928,399 +1146,482 @@ return {
928
1146
  */
929
1147
 
930
1148
  function formatTyp(event) {
931
- return {
932
- isTyping: !!event.st,
933
- from: event.from.toString(),
934
- threadID: formatID((event.to || event.thread_fbid || event.from).toString()),
935
- // When receiving typ indication from mobile, `from_mobile` isn't set.
936
- // If it is, we just use that value.
937
- fromMobile: event.hasOwnProperty("from_mobile") ? event.from_mobile : true,
938
- userID: (event.realtime_viewer_fbid || event.from).toString(),
939
- type: "typ"
940
- };
1149
+ return {
1150
+ isTyping: !!event.st,
1151
+ from: event.from.toString(),
1152
+ threadID: formatID(
1153
+ (event.to || event.thread_fbid || event.from).toString()
1154
+ ),
1155
+ // When receiving typ indication from mobile, `from_mobile` isn't set.
1156
+ // If it is, we just use that value.
1157
+ fromMobile: event.hasOwnProperty("from_mobile") ? event.from_mobile : true,
1158
+ userID: (event.realtime_viewer_fbid || event.from).toString(),
1159
+ type: "typ"
1160
+ };
941
1161
  }
942
1162
 
943
1163
  function formatDeltaReadReceipt(delta) {
944
- // otherUserFbId seems to be used as both the readerID and the threadID in a 1-1 chat.
945
- // In a group chat actorFbId is used for the reader and threadFbId for the thread.
946
- return {
947
- reader: (delta.threadKey.otherUserFbId || delta.actorFbId).toString(),
948
- time: delta.actionTimestampMs,
949
- threadID: formatID((delta.threadKey.otherUserFbId || delta.threadKey.threadFbId).toString()),
950
- type: "read_receipt"
951
- };
1164
+ // otherUserFbId seems to be used as both the readerID and the threadID in a 1-1 chat.
1165
+ // In a group chat actorFbId is used for the reader and threadFbId for the thread.
1166
+ return {
1167
+ reader: (delta.threadKey.otherUserFbId || delta.actorFbId).toString(),
1168
+ time: delta.actionTimestampMs,
1169
+ threadID: formatID(
1170
+ (delta.threadKey.otherUserFbId || delta.threadKey.threadFbId).toString()
1171
+ ),
1172
+ type: "read_receipt"
1173
+ };
952
1174
  }
953
1175
 
954
1176
  function formatReadReceipt(event) {
955
- return {
956
- reader: event.reader.toString(),
957
- time: event.time,
958
- threadID: formatID((event.thread_fbid || event.reader).toString()),
959
- type: "read_receipt"
960
- };
1177
+ return {
1178
+ reader: event.reader.toString(),
1179
+ time: event.time,
1180
+ threadID: formatID((event.thread_fbid || event.reader).toString()),
1181
+ type: "read_receipt"
1182
+ };
961
1183
  }
962
1184
 
963
1185
  function formatRead(event) {
964
- return {
965
- threadID: formatID(((event.chat_ids && event.chat_ids[0]) || (event.thread_fbids && event.thread_fbids[0])).toString()),
966
- time: event.timestamp,
967
- type: "read"
968
- };
1186
+ return {
1187
+ threadID: formatID(
1188
+ (
1189
+ (event.chat_ids && event.chat_ids[0]) ||
1190
+ (event.thread_fbids && event.thread_fbids[0])
1191
+ ).toString()
1192
+ ),
1193
+ time: event.timestamp,
1194
+ type: "read"
1195
+ };
969
1196
  }
970
1197
 
971
1198
  function getFrom(str, startToken, endToken) {
972
- var start = str.indexOf(startToken) + startToken.length;
973
- if (start < startToken.length) return "";
974
-
975
- var lastHalf = str.substring(start);
976
- var end = lastHalf.indexOf(endToken);
977
- if (end === -1) throw Error("Could not find endTime `" + endToken + "` in the given string.");
978
- return lastHalf.substring(0, end);
1199
+ var start = str.indexOf(startToken) + startToken.length;
1200
+ if (start < startToken.length) return "";
1201
+
1202
+ var lastHalf = str.substring(start);
1203
+ var end = lastHalf.indexOf(endToken);
1204
+ if (end === -1)
1205
+ throw Error(
1206
+ "Could not find endTime `" + endToken + "` in the given string."
1207
+ );
1208
+ return lastHalf.substring(0, end);
979
1209
  }
980
1210
 
981
1211
  function makeParsable(html) {
982
- let withoutForLoop = html.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*/, "");
983
-
984
- // (What the fuck FB, why windows style newlines?)
985
- // So sometimes FB will send us base multiple objects in the same response.
986
- // They're all valid JSON, one after the other, at the top level. We detect
987
- // that and make it parse-able by JSON.parse.
988
- // Ben - July 15th 2017
989
- //
990
- // It turns out that Facebook may insert random number of spaces before
991
- // next object begins (issue #616)
992
- // rav_kr - 2018-03-19
993
- let maybeMultipleObjects = withoutForLoop.split(/\}\r\n *\{/);
994
- if (maybeMultipleObjects.length === 1) return maybeMultipleObjects;
995
-
996
- return "[" + maybeMultipleObjects.join("},{") + "]";
1212
+ let withoutForLoop = html.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*/, "");
1213
+
1214
+ // (What the fuck FB, why windows style newlines?)
1215
+ // So sometimes FB will send us base multiple objects in the same response.
1216
+ // They're all valid JSON, one after the other, at the top level. We detect
1217
+ // that and make it parse-able by JSON.parse.
1218
+ // Ben - July 15th 2017
1219
+ //
1220
+ // It turns out that Facebook may insert random number of spaces before
1221
+ // next object begins (issue #616)
1222
+ // rav_kr - 2018-03-19
1223
+ let maybeMultipleObjects = withoutForLoop.split(/\}\r\n *\{/);
1224
+ if (maybeMultipleObjects.length === 1) return maybeMultipleObjects;
1225
+
1226
+ return "[" + maybeMultipleObjects.join("},{") + "]";
997
1227
  }
998
1228
 
999
1229
  function arrToForm(form) {
1000
- return arrayToObject(form,
1001
- function(v) {
1002
- return v.name;
1003
- },
1004
- function(v) {
1005
- return v.val;
1006
- }
1007
- );
1230
+ return arrayToObject(
1231
+ form,
1232
+ function(v) {
1233
+ return v.name;
1234
+ },
1235
+ function(v) {
1236
+ return v.val;
1237
+ }
1238
+ );
1008
1239
  }
1009
1240
 
1010
1241
  function arrayToObject(arr, getKey, getValue) {
1011
- return arr.reduce(function(acc, val) {
1012
- acc[getKey(val)] = getValue(val);
1013
- return acc;
1014
- }, {});
1242
+ return arr.reduce(function(acc, val) {
1243
+ acc[getKey(val)] = getValue(val);
1244
+ return acc;
1245
+ }, {});
1015
1246
  }
1016
1247
 
1017
1248
  function getSignatureID() {
1018
- return Math.floor(Math.random() * 2147483648).toString(16);
1249
+ return Math.floor(Math.random() * 2147483648).toString(16);
1019
1250
  }
1020
1251
 
1021
1252
  function generateTimestampRelative() {
1022
- var d = new Date();
1023
- return d.getHours() + ":" + padZeros(d.getMinutes());
1253
+ var d = new Date();
1254
+ return d.getHours() + ":" + padZeros(d.getMinutes());
1024
1255
  }
1025
1256
 
1026
1257
  function makeDefaults(html, userID, ctx) {
1027
- var reqCounter = 1;
1028
- var fb_dtsg = getFrom(html, 'name="fb_dtsg" value="', '"');
1029
-
1030
- // @Hack Ok we've done hacky things, this is definitely on top 5.
1031
- // We totally assume the object is flat and try parsing until a }.
1032
- // If it works though it's cool because we get a bunch of extra data things.
1258
+ var reqCounter = 1;
1259
+ var fb_dtsg = getFrom(html, 'name="fb_dtsg" value="', '"');
1260
+
1261
+ // @Hack Ok we've done hacky things, this is definitely on top 5.
1262
+ // We totally assume the object is flat and try parsing until a }.
1263
+ // If it works though it's cool because we get a bunch of extra data things.
1264
+ //
1265
+ // Update: we don't need this. Leaving it in in case we ever do.
1266
+ // Ben - July 15th 2017
1267
+
1268
+ // var siteData = getFrom(html, "[\"SiteData\",[],", "},");
1269
+ // try {
1270
+ // siteData = JSON.parse(siteData + "}");
1271
+ // } catch(e) {
1272
+ // log.warn("makeDefaults", "Couldn't parse SiteData. Won't have access to some variables.");
1273
+ // siteData = {};
1274
+ // }
1275
+
1276
+ var ttstamp = "2";
1277
+ for (var i = 0; i < fb_dtsg.length; i++) ttstamp += fb_dtsg.charCodeAt(i);
1278
+ var revision = getFrom(html, 'revision":', ",");
1279
+
1280
+ function mergeWithDefaults(obj) {
1281
+ // @TODO This is missing a key called __dyn.
1282
+ // After some investigation it seems like __dyn is some sort of set that FB
1283
+ // calls BitMap. It seems like certain responses have a "define" key in the
1284
+ // res.jsmods arrays. I think the code iterates over those and calls `set`
1285
+ // on the bitmap for each of those keys. Then it calls
1286
+ // bitmap.toCompressedString() which returns what __dyn is.
1033
1287
  //
1034
- // Update: we don't need this. Leaving it in in case we ever do.
1035
- // Ben - July 15th 2017
1036
-
1037
- // var siteData = getFrom(html, "[\"SiteData\",[],", "},");
1038
- // try {
1039
- // siteData = JSON.parse(siteData + "}");
1040
- // } catch(e) {
1041
- // log.warn("makeDefaults", "Couldn't parse SiteData. Won't have access to some variables.");
1042
- // siteData = {};
1043
- // }
1288
+ // So far the API has been working without this.
1289
+ //
1290
+ // Ben - July 15th 2017
1291
+ var newObj = {
1292
+ __user: userID,
1293
+ __req: (reqCounter++).toString(36),
1294
+ __rev: revision,
1295
+ __a: 1,
1296
+ // __af: siteData.features,
1297
+ fb_dtsg: ctx.fb_dtsg ? ctx.fb_dtsg : fb_dtsg,
1298
+ jazoest: ctx.ttstamp ? ctx.ttstamp : ttstamp
1299
+ // __spin_r: siteData.__spin_r,
1300
+ // __spin_b: siteData.__spin_b,
1301
+ // __spin_t: siteData.__spin_t,
1302
+ };
1044
1303
 
1045
- var ttstamp = "2";
1046
- for (var i = 0; i < fb_dtsg.length; i++) ttstamp += fb_dtsg.charCodeAt(i);
1047
- var revision = getFrom(html, 'revision":', ",");
1048
-
1049
- function mergeWithDefaults(obj) {
1050
- // @TODO This is missing a key called __dyn.
1051
- // After some investigation it seems like __dyn is some sort of set that FB
1052
- // calls BitMap. It seems like certain responses have a "define" key in the
1053
- // res.jsmods arrays. I think the code iterates over those and calls `set`
1054
- // on the bitmap for each of those keys. Then it calls
1055
- // bitmap.toCompressedString() which returns what __dyn is.
1056
- //
1057
- // So far the API has been working without this.
1058
- //
1059
- // Ben - July 15th 2017
1060
- var newObj = {
1061
- __user: userID,
1062
- __req: (reqCounter++).toString(36),
1063
- __rev: revision,
1064
- __a: 1,
1065
- // __af: siteData.features,
1066
- fb_dtsg: ctx.fb_dtsg ? ctx.fb_dtsg : fb_dtsg,
1067
- jazoest: ctx.ttstamp ? ctx.ttstamp : ttstamp
1068
- // __spin_r: siteData.__spin_r,
1069
- // __spin_b: siteData.__spin_b,
1070
- // __spin_t: siteData.__spin_t,
1071
- };
1304
+ // @TODO this is probably not needed.
1305
+ // Ben - July 15th 2017
1306
+ // if (siteData.be_key) {
1307
+ // newObj[siteData.be_key] = siteData.be_mode;
1308
+ // }
1309
+ // if (siteData.pkg_cohort_key) {
1310
+ // newObj[siteData.pkg_cohort_key] = siteData.pkg_cohort;
1311
+ // }
1072
1312
 
1073
- // @TODO this is probably not needed.
1074
- // Ben - July 15th 2017
1075
- // if (siteData.be_key) {
1076
- // newObj[siteData.be_key] = siteData.be_mode;
1077
- // }
1078
- // if (siteData.pkg_cohort_key) {
1079
- // newObj[siteData.pkg_cohort_key] = siteData.pkg_cohort;
1080
- // }
1081
-
1082
- if (!obj) return newObj;
1083
- for (var prop in obj)
1084
- if (obj.hasOwnProperty(prop))
1085
- if (!newObj[prop]) newObj[prop] = obj[prop];
1086
- return newObj;
1087
- }
1313
+ if (!obj) return newObj;
1314
+ for (var prop in obj)
1315
+ if (obj.hasOwnProperty(prop)) if (!newObj[prop]) newObj[prop] = obj[prop];
1316
+ return newObj;
1317
+ }
1088
1318
 
1089
- function postWithDefaults(url, jar, form, ctxx) {
1090
- return post(url, jar, mergeWithDefaults(form), ctx.globalOptions, ctxx || ctx);
1091
- }
1319
+ function postWithDefaults(url, jar, form, ctxx) {
1320
+ return post(
1321
+ url,
1322
+ jar,
1323
+ mergeWithDefaults(form),
1324
+ ctx.globalOptions,
1325
+ ctxx || ctx
1326
+ );
1327
+ }
1092
1328
 
1093
- function getWithDefaults(url, jar, qs, ctxx) {
1094
- return get(url, jar, mergeWithDefaults(qs), ctx.globalOptions, ctxx || ctx);
1095
- }
1329
+ function getWithDefaults(url, jar, qs, ctxx) {
1330
+ return get(url, jar, mergeWithDefaults(qs), ctx.globalOptions, ctxx || ctx);
1331
+ }
1096
1332
 
1097
- function postFormDataWithDefault(url, jar, form, qs, ctxx) {
1098
- return postFormData(url, jar, mergeWithDefaults(form), mergeWithDefaults(qs), ctx.globalOptions, ctxx || ctx);
1099
- }
1333
+ function postFormDataWithDefault(url, jar, form, qs, ctxx) {
1334
+ return postFormData(
1335
+ url,
1336
+ jar,
1337
+ mergeWithDefaults(form),
1338
+ mergeWithDefaults(qs),
1339
+ ctx.globalOptions,
1340
+ ctxx || ctx
1341
+ );
1342
+ }
1100
1343
 
1101
- return {
1102
- get: getWithDefaults,
1103
- post: postWithDefaults,
1104
- postFormData: postFormDataWithDefault
1105
- };
1344
+ return {
1345
+ get: getWithDefaults,
1346
+ post: postWithDefaults,
1347
+ postFormData: postFormDataWithDefault
1348
+ };
1106
1349
  }
1107
1350
 
1108
1351
  function parseAndCheckLogin(ctx, defaultFuncs, retryCount) {
1109
- if (retryCount == undefined) retryCount = 0;
1110
- return function(data) {
1111
- return bluebird.try(function() {
1112
- log.verbose("parseAndCheckLogin", data.body);
1113
- if (data.statusCode >= 500 && data.statusCode < 600) {
1114
- if (retryCount >= 5) {
1115
- throw {
1116
- error: "Request retry failed. Check the `res` and `statusCode` property on this error.",
1117
- statusCode: data.statusCode,
1118
- res: data.body
1119
- };
1120
- }
1121
- retryCount++;
1122
- var retryTime = Math.floor(Math.random() * 5000);
1123
- log.warn("parseAndCheckLogin", "Got status code " + data.statusCode + " - " + retryCount + ". attempt to retry in " + retryTime + " milliseconds...");
1124
- var url = data.request.uri.protocol + "//" + data.request.uri.hostname + data.request.uri.pathname;
1125
- if (data.request.headers["Content-Type"].split(";")[0] === "multipart/form-data") {
1126
- return bluebird.delay(retryTime).then(() => defaultFuncs.postFormData(url, ctx.jar, data.request.formData, {}))
1127
- .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1128
- } else {
1129
- return bluebird.delay(retryTime).then(() => defaultFuncs.post(url, ctx.jar, data.request.formData))
1130
- .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1131
- }
1132
- }
1133
- if (data.statusCode !== 200) throw new Error("parseAndCheckLogin got status code: " + data.statusCode + ". Bailing out of trying to parse response.");
1134
-
1135
- var res = null;
1136
- try {
1137
- res = JSON.parse(makeParsable(data.body));
1138
- } catch (e) {
1139
- throw {
1140
- error: "JSON.parse error. Check the `detail` property on this error.",
1141
- detail: e,
1142
- res: data.body
1143
- };
1144
- }
1352
+ if (retryCount == undefined) retryCount = 0;
1353
+ return function(data) {
1354
+ return bluebird.try(function() {
1355
+ log.verbose("parseAndCheckLogin", data.body);
1356
+ if (data.statusCode >= 500 && data.statusCode < 600) {
1357
+ if (retryCount >= 5) {
1358
+ throw {
1359
+ error:
1360
+ "Request retry failed. Check the `res` and `statusCode` property on this error.",
1361
+ statusCode: data.statusCode,
1362
+ res: data.body
1363
+ };
1364
+ }
1365
+ retryCount++;
1366
+ var retryTime = Math.floor(Math.random() * 5000);
1367
+ log.warn(
1368
+ "parseAndCheckLogin",
1369
+ "Got status code " +
1370
+ data.statusCode +
1371
+ " - " +
1372
+ retryCount +
1373
+ ". attempt to retry in " +
1374
+ retryTime +
1375
+ " milliseconds..."
1376
+ );
1377
+ var url =
1378
+ data.request.uri.protocol +
1379
+ "//" +
1380
+ data.request.uri.hostname +
1381
+ data.request.uri.pathname;
1382
+ if (
1383
+ data.request.headers["Content-Type"].split(";")[0] ===
1384
+ "multipart/form-data"
1385
+ ) {
1386
+ return bluebird
1387
+ .delay(retryTime)
1388
+ .then(() =>
1389
+ defaultFuncs.postFormData(url, ctx.jar, data.request.formData, {})
1390
+ )
1391
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1392
+ } else {
1393
+ return bluebird
1394
+ .delay(retryTime)
1395
+ .then(() => defaultFuncs.post(url, ctx.jar, data.request.formData))
1396
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1397
+ }
1398
+ }
1399
+ if (data.statusCode !== 200)
1400
+ throw new Error(
1401
+ "parseAndCheckLogin got status code: " +
1402
+ data.statusCode +
1403
+ ". Bailing out of trying to parse response."
1404
+ );
1145
1405
 
1146
- // In some cases the response contains only a redirect URL which should be followed
1147
- if (res.redirect && data.request.method === "GET") return defaultFuncs.get(res.redirect, ctx.jar).then(parseAndCheckLogin(ctx, defaultFuncs));
1406
+ var res = null;
1407
+ try {
1408
+ res = JSON.parse(makeParsable(data.body));
1409
+ } catch (e) {
1410
+ throw {
1411
+ error: "JSON.parse error. Check the `detail` property on this error.",
1412
+ detail: e,
1413
+ res: data.body
1414
+ };
1415
+ }
1148
1416
 
1149
- // TODO: handle multiple cookies?
1150
- if (res.jsmods && res.jsmods.require && Array.isArray(res.jsmods.require[0]) && res.jsmods.require[0][0] === "Cookie") {
1151
- res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace("_js_", "");
1152
- var cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1153
- var cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1154
- ctx.jar.setCookie(cookie, "https://www.facebook.com");
1155
- ctx.jar.setCookie(cookie2, "https://www.messenger.com");
1156
- }
1417
+ // In some cases the response contains only a redirect URL which should be followed
1418
+ if (res.redirect && data.request.method === "GET")
1419
+ return defaultFuncs
1420
+ .get(res.redirect, ctx.jar)
1421
+ .then(parseAndCheckLogin(ctx, defaultFuncs));
1422
+
1423
+ // TODO: handle multiple cookies?
1424
+ if (
1425
+ res.jsmods &&
1426
+ res.jsmods.require &&
1427
+ Array.isArray(res.jsmods.require[0]) &&
1428
+ res.jsmods.require[0][0] === "Cookie"
1429
+ ) {
1430
+ res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace(
1431
+ "_js_",
1432
+ ""
1433
+ );
1434
+ var cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1435
+ var cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1436
+ ctx.jar.setCookie(cookie, "https://www.facebook.com");
1437
+ ctx.jar.setCookie(cookie2, "https://www.messenger.com");
1438
+ }
1157
1439
 
1158
- // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1159
- // one for the next requests.
1160
- if (res.jsmods && Array.isArray(res.jsmods.require)) {
1161
- var arr = res.jsmods.require;
1162
- for (var i in arr) {
1163
- if (arr[i][0] === "DTSG" && arr[i][1] === "setToken") {
1164
- ctx.fb_dtsg = arr[i][3][0];
1165
-
1166
- // Update ttstamp since that depends on fb_dtsg
1167
- ctx.ttstamp = "2";
1168
- for (var j = 0; j < ctx.fb_dtsg.length; j++) ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
1169
- }
1170
- }
1171
- }
1440
+ // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1441
+ // one for the next requests.
1442
+ if (res.jsmods && Array.isArray(res.jsmods.require)) {
1443
+ var arr = res.jsmods.require;
1444
+ for (var i in arr) {
1445
+ if (arr[i][0] === "DTSG" && arr[i][1] === "setToken") {
1446
+ ctx.fb_dtsg = arr[i][3][0];
1447
+
1448
+ // Update ttstamp since that depends on fb_dtsg
1449
+ ctx.ttstamp = "2";
1450
+ for (var j = 0; j < ctx.fb_dtsg.length; j++)
1451
+ ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
1452
+ }
1453
+ }
1454
+ }
1172
1455
 
1173
- if (res.error === 1357001) throw { error: "Not logged in." };
1174
- return res;
1175
- });
1176
- };
1456
+ if (res.error === 1357001) throw { error: "Not logged in." };
1457
+ return res;
1458
+ });
1459
+ };
1177
1460
  }
1178
1461
 
1179
1462
  function saveCookies(jar) {
1180
- return function(res) {
1181
- var cookies = res.headers["set-cookie"] || [];
1182
- cookies.forEach(function(c) {
1183
- if (c.indexOf(".facebook.com") > -1) jar.setCookie(c, "https://www.facebook.com");
1184
- var c2 = c.replace(/domain=\.facebook\.com/, "domain=.messenger.com");
1185
- jar.setCookie(c2, "https://www.messenger.com");
1186
- });
1187
- return res;
1188
- };
1463
+ return function(res) {
1464
+ var cookies = res.headers["set-cookie"] || [];
1465
+ cookies.forEach(function(c) {
1466
+ if (c.indexOf(".facebook.com") > -1)
1467
+ jar.setCookie(c, "https://www.facebook.com");
1468
+ var c2 = c.replace(/domain=\.facebook\.com/, "domain=.messenger.com");
1469
+ jar.setCookie(c2, "https://www.messenger.com");
1470
+ });
1471
+ return res;
1472
+ };
1189
1473
  }
1190
1474
 
1191
1475
  var NUM_TO_MONTH = [
1192
- "Jan",
1193
- "Feb",
1194
- "Mar",
1195
- "Apr",
1196
- "May",
1197
- "Jun",
1198
- "Jul",
1199
- "Aug",
1200
- "Sep",
1201
- "Oct",
1202
- "Nov",
1203
- "Dec"
1476
+ "Jan",
1477
+ "Feb",
1478
+ "Mar",
1479
+ "Apr",
1480
+ "May",
1481
+ "Jun",
1482
+ "Jul",
1483
+ "Aug",
1484
+ "Sep",
1485
+ "Oct",
1486
+ "Nov",
1487
+ "Dec"
1204
1488
  ];
1205
1489
  var NUM_TO_DAY = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
1206
1490
 
1207
1491
  function formatDate(date) {
1208
- var d = date.getUTCDate();
1209
- d = d >= 10 ? d : "0" + d;
1210
- var h = date.getUTCHours();
1211
- h = h >= 10 ? h : "0" + h;
1212
- var m = date.getUTCMinutes();
1213
- m = m >= 10 ? m : "0" + m;
1214
- var s = date.getUTCSeconds();
1215
- s = s >= 10 ? s : "0" + s;
1216
- return (NUM_TO_DAY[date.getUTCDay()] + ", " + d + " " + NUM_TO_MONTH[date.getUTCMonth()] + " " + date.getUTCFullYear() + " " + h + ":" + m + ":" + s + " GMT");
1492
+ var d = date.getUTCDate();
1493
+ d = d >= 10 ? d : "0" + d;
1494
+ var h = date.getUTCHours();
1495
+ h = h >= 10 ? h : "0" + h;
1496
+ var m = date.getUTCMinutes();
1497
+ m = m >= 10 ? m : "0" + m;
1498
+ var s = date.getUTCSeconds();
1499
+ s = s >= 10 ? s : "0" + s;
1500
+ return (
1501
+ NUM_TO_DAY[date.getUTCDay()] +
1502
+ ", " +
1503
+ d +
1504
+ " " +
1505
+ NUM_TO_MONTH[date.getUTCMonth()] +
1506
+ " " +
1507
+ date.getUTCFullYear() +
1508
+ " " +
1509
+ h +
1510
+ ":" +
1511
+ m +
1512
+ ":" +
1513
+ s +
1514
+ " GMT"
1515
+ );
1217
1516
  }
1218
1517
 
1219
1518
  function formatCookie(arr, url) {
1220
- return arr[0] + "=" + arr[1] + "; Path=" + arr[3] + "; Domain=" + url + ".com";
1519
+ return (
1520
+ arr[0] + "=" + arr[1] + "; Path=" + arr[3] + "; Domain=" + url + ".com"
1521
+ );
1221
1522
  }
1222
1523
 
1223
1524
  function formatThread(data) {
1224
- return {
1225
- threadID: formatID(data.thread_fbid.toString()),
1226
- participants: data.participants.map(formatID),
1227
- participantIDs: data.participants.map(formatID),
1228
- name: data.name,
1229
- nicknames: data.custom_nickname,
1230
- snippet: data.snippet,
1231
- snippetAttachments: data.snippet_attachments,
1232
- snippetSender: formatID((data.snippet_sender || "").toString()),
1233
- unreadCount: data.unread_count,
1234
- messageCount: data.message_count,
1235
- imageSrc: data.image_src,
1236
- timestamp: data.timestamp,
1237
- serverTimestamp: data.server_timestamp, // what is this?
1238
- muteUntil: data.mute_until,
1239
- isCanonicalUser: data.is_canonical_user,
1240
- isCanonical: data.is_canonical,
1241
- isSubscribed: data.is_subscribed,
1242
- folder: data.folder,
1243
- isArchived: data.is_archived,
1244
- recipientsLoadable: data.recipients_loadable,
1245
- hasEmailParticipant: data.has_email_participant,
1246
- readOnly: data.read_only,
1247
- canReply: data.can_reply,
1248
- cannotReplyReason: data.cannot_reply_reason,
1249
- lastMessageTimestamp: data.last_message_timestamp,
1250
- lastReadTimestamp: data.last_read_timestamp,
1251
- lastMessageType: data.last_message_type,
1252
- emoji: data.custom_like_icon,
1253
- color: data.custom_color,
1254
- adminIDs: data.admin_ids,
1255
- threadType: data.thread_type
1256
- };
1525
+ return {
1526
+ threadID: formatID(data.thread_fbid.toString()),
1527
+ participants: data.participants.map(formatID),
1528
+ participantIDs: data.participants.map(formatID),
1529
+ name: data.name,
1530
+ nicknames: data.custom_nickname,
1531
+ snippet: data.snippet,
1532
+ snippetAttachments: data.snippet_attachments,
1533
+ snippetSender: formatID((data.snippet_sender || "").toString()),
1534
+ unreadCount: data.unread_count,
1535
+ messageCount: data.message_count,
1536
+ imageSrc: data.image_src,
1537
+ timestamp: data.timestamp,
1538
+ serverTimestamp: data.server_timestamp, // what is this?
1539
+ muteUntil: data.mute_until,
1540
+ isCanonicalUser: data.is_canonical_user,
1541
+ isCanonical: data.is_canonical,
1542
+ isSubscribed: data.is_subscribed,
1543
+ folder: data.folder,
1544
+ isArchived: data.is_archived,
1545
+ recipientsLoadable: data.recipients_loadable,
1546
+ hasEmailParticipant: data.has_email_participant,
1547
+ readOnly: data.read_only,
1548
+ canReply: data.can_reply,
1549
+ cannotReplyReason: data.cannot_reply_reason,
1550
+ lastMessageTimestamp: data.last_message_timestamp,
1551
+ lastReadTimestamp: data.last_read_timestamp,
1552
+ lastMessageType: data.last_message_type,
1553
+ emoji: data.custom_like_icon,
1554
+ color: data.custom_color,
1555
+ adminIDs: data.admin_ids,
1556
+ threadType: data.thread_type
1557
+ };
1257
1558
  }
1258
1559
 
1259
1560
  function getType(obj) {
1260
- return Object.prototype.toString.call(obj).slice(8, -1);
1561
+ return Object.prototype.toString.call(obj).slice(8, -1);
1261
1562
  }
1262
1563
 
1263
1564
  function formatProxyPresence(presence, userID) {
1264
- if (presence.lat === undefined || presence.p === undefined) return null;
1265
- return {
1266
- type: "presence",
1267
- timestamp: presence.lat * 1000,
1268
- userID: userID || '',
1269
- statuses: presence.p
1270
- };
1565
+ if (presence.lat === undefined || presence.p === undefined) return null;
1566
+ return {
1567
+ type: "presence",
1568
+ timestamp: presence.lat * 1000,
1569
+ userID: userID || "",
1570
+ statuses: presence.p
1571
+ };
1271
1572
  }
1272
1573
 
1273
1574
  function formatPresence(presence, userID) {
1274
- return {
1275
- type: "presence",
1276
- timestamp: presence.la * 1000,
1277
- userID: userID || '',
1278
- statuses: presence.a
1279
- };
1575
+ return {
1576
+ type: "presence",
1577
+ timestamp: presence.la * 1000,
1578
+ userID: userID || "",
1579
+ statuses: presence.a
1580
+ };
1280
1581
  }
1281
1582
 
1282
1583
  function decodeClientPayload(payload) {
1283
- /*
1584
+ /*
1284
1585
  Special function which Client using to "encode" clients JSON payload
1285
1586
  */
1286
- function Utf8ArrayToStr(array) {
1287
- var out, i, len, c;
1288
- var char2, char3;
1289
- out = "";
1290
- len = array.length;
1291
- i = 0;
1292
- while (i < len) {
1293
- c = array[i++];
1294
- switch (c >> 4) {
1295
- case 0:
1296
- case 1:
1297
- case 2:
1298
- case 3:
1299
- case 4:
1300
- case 5:
1301
- case 6:
1302
- case 7:
1303
- out += String.fromCharCode(c);
1304
- break;
1305
- case 12:
1306
- case 13:
1307
- char2 = array[i++];
1308
- out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
1309
- break;
1310
- case 14:
1311
- char2 = array[i++];
1312
- char3 = array[i++];
1313
- out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
1314
- break;
1315
- }
1316
- }
1317
- return out;
1587
+ function Utf8ArrayToStr(array) {
1588
+ var out, i, len, c;
1589
+ var char2, char3;
1590
+ out = "";
1591
+ len = array.length;
1592
+ i = 0;
1593
+ while (i < len) {
1594
+ c = array[i++];
1595
+ switch (c >> 4) {
1596
+ case 0:
1597
+ case 1:
1598
+ case 2:
1599
+ case 3:
1600
+ case 4:
1601
+ case 5:
1602
+ case 6:
1603
+ case 7:
1604
+ out += String.fromCharCode(c);
1605
+ break;
1606
+ case 12:
1607
+ case 13:
1608
+ char2 = array[i++];
1609
+ out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
1610
+ break;
1611
+ case 14:
1612
+ char2 = array[i++];
1613
+ char3 = array[i++];
1614
+ out += String.fromCharCode(
1615
+ ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
1616
+ );
1617
+ break;
1618
+ }
1318
1619
  }
1319
- return JSON.parse(Utf8ArrayToStr(payload));
1620
+ return out;
1621
+ }
1622
+ return JSON.parse(Utf8ArrayToStr(payload));
1320
1623
  }
1321
1624
 
1322
-
1323
-
1324
1625
  /*function getAppState(jar) {
1325
1626
  try {
1326
1627
  var appstate = jar.getCookies("https://www.facebook.com").concat(jar.getCookies("https://facebook.com")).concat(jar.getCookies("https://www.messenger.com"));
@@ -1345,55 +1646,75 @@ function decodeClientPayload(payload) {
1345
1646
  }
1346
1647
  }*/
1347
1648
  function getAppState(jar) {
1348
- const prettyMilliseconds = require('pretty-ms');
1349
- var appstate = jar.getCookies("https://www.facebook.com").concat(jar.getCookies("https://facebook.com")).concat(jar.getCookies("https://www.messenger.com"));
1350
- var StateCrypt = require('./StateCrypt');
1351
- var logger = require('./logger');
1352
- logger('Encrypted Appstate Successfully!', '[ FCA-SUS ]');
1353
- logger(`Process Done: ${prettyMilliseconds(Date.now() - process.env.startTime)}`, '[ FCA-SUS ]');
1354
- if (process.env['FBKEY']) {
1355
- return StateCrypt.encryptState(JSON.stringify(appstate),process.env['FBKEY']);
1356
- }
1357
- else return appstate;
1649
+ const prettyMilliseconds = require("pretty-ms");
1650
+ var appstate = jar
1651
+ .getCookies("https://www.facebook.com")
1652
+ .concat(jar.getCookies("https://facebook.com"))
1653
+ .concat(jar.getCookies("https://www.messenger.com"));
1654
+ var StateCrypt = require("./StateCrypt");
1655
+ var logger = require("./logger");
1656
+ logger("Encrypted Appstate Successfully!", "[ FCA-SUS ]");
1657
+
1658
+ let processDuration = 0;
1659
+ if (process.env.startTime) {
1660
+ processDuration = Date.now() - parseInt(process.env.startTime);
1661
+ }
1662
+
1663
+ if (isFinite(processDuration)) {
1664
+ logger(
1665
+ `Process Done: ${prettyMilliseconds(processDuration)}`,
1666
+ "[ FCA-SUS ]"
1667
+ );
1668
+ } else {
1669
+ logger(`Process Done: Unable to calculate duration`, "[ FCA-SUS ]");
1670
+ }
1671
+
1672
+ if (process.env["FBKEY"]) {
1673
+ return StateCrypt.encryptState(
1674
+ JSON.stringify(appstate),
1675
+ process.env["FBKEY"]
1676
+ );
1677
+ } else return appstate;
1358
1678
  }
1359
1679
 
1360
1680
  module.exports = {
1361
- isReadableStream:isReadableStream,
1362
- get,
1363
- post,
1364
- postFormData,
1365
- generateThreadingID,
1366
- generateOfflineThreadingID,
1367
- getGUID,
1368
- getFrom:getFrom,
1369
- makeParsable,
1370
- arrToForm,
1371
- getSignatureID,
1372
- getJar: request.jar,
1373
- generateTimestampRelative,
1374
- makeDefaults,
1375
- parseAndCheckLogin,
1376
- saveCookies,
1377
- getType,
1378
- _formatAttachment,
1379
- formatHistoryMessage,
1380
- formatID,
1381
- formatMessage,
1382
- formatDeltaEvent,
1383
- formatDeltaMessage,
1384
- formatProxyPresence,
1385
- formatPresence,
1386
- formatTyp,
1387
- formatDeltaReadReceipt,
1388
- formatCookie,
1389
- formatThread,
1390
- formatReadReceipt,
1391
- formatRead,
1392
- generatePresence,
1393
- generateAccessiblityCookie,
1394
- formatDate,
1395
- decodeClientPayload,
1396
- getAppState,
1397
- getAdminTextMessageType,
1398
- setProxy
1399
- };
1681
+ isReadableStream: isReadableStream,
1682
+ get,
1683
+ post,
1684
+ postFormData,
1685
+ generateThreadingID,
1686
+ generateOfflineThreadingID,
1687
+ getGUID,
1688
+ getFrom: getFrom,
1689
+ makeParsable,
1690
+ arrToForm,
1691
+ getSignatureID,
1692
+ getJar: request.jar,
1693
+ generateTimestampRelative,
1694
+ makeDefaults,
1695
+ parseAndCheckLogin,
1696
+ saveCookies,
1697
+ getType,
1698
+ _formatAttachment,
1699
+ formatHistoryMessage,
1700
+ formatID,
1701
+ formatMessage,
1702
+ formatDeltaEvent,
1703
+ formatDeltaMessage,
1704
+ formatProxyPresence,
1705
+ formatPresence,
1706
+ formatTyp,
1707
+ formatDeltaReadReceipt,
1708
+ formatCookie,
1709
+ formatThread,
1710
+ formatReadReceipt,
1711
+ formatRead,
1712
+ generatePresence,
1713
+ generateAccessiblityCookie,
1714
+ formatDate,
1715
+ decodeClientPayload,
1716
+ getAppState,
1717
+ getAdminTextMessageType,
1718
+ setProxy,
1719
+ getCurrentTimestamp
1720
+ };