fca-nino 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of fca-nino might be problematic. Click here for more details.
- package/.gitattributes +2 -0
- package/.travis.yml +6 -0
- package/CHANGELOG.md +2 -0
- package/DOCS.md +1738 -0
- package/LICENSE-MIT +21 -0
- package/README.md +219 -0
- package/index.js +541 -0
- package/package.json +73 -0
- package/replit.nix +5 -0
- package/src/Screenshot.js +83 -0
- package/src/addExternalModule.js +15 -0
- package/src/addUserToGroup.js +77 -0
- package/src/changeAdminStatus.js +47 -0
- package/src/changeArchivedStatus.js +41 -0
- package/src/changeAvt.js +85 -0
- package/src/changeBio.js +65 -0
- package/src/changeBlockedStatus.js +36 -0
- package/src/changeGroupImage.js +106 -0
- package/src/changeNickname.js +45 -0
- package/src/changeThreadColor.js +61 -0
- package/src/changeThreadEmoji.js +41 -0
- package/src/createNewGroup.js +70 -0
- package/src/createPoll.js +59 -0
- package/src/deleteMessage.js +44 -0
- package/src/deleteThread.js +42 -0
- package/src/forwardAttachment.js +47 -0
- package/src/getCurrentUserID.js +7 -0
- package/src/getEmojiUrl.js +27 -0
- package/src/getFriendsList.js +73 -0
- package/src/getThreadHistory.js +537 -0
- package/src/getThreadHistoryDeprecated.js +71 -0
- package/src/getThreadInfo.js +171 -0
- package/src/getThreadInfoDeprecated.js +56 -0
- package/src/getThreadList.js +213 -0
- package/src/getThreadListDeprecated.js +46 -0
- package/src/getThreadPictures.js +59 -0
- package/src/getUserID.js +61 -0
- package/src/getUserInfo.js +66 -0
- package/src/handleFriendRequest.js +46 -0
- package/src/handleMessageRequest.js +47 -0
- package/src/httpGet.js +49 -0
- package/src/httpPost.js +48 -0
- package/src/listenMqtt.js +701 -0
- package/src/logout.js +68 -0
- package/src/markAsDelivered.js +47 -0
- package/src/markAsRead.js +70 -0
- package/src/markAsReadAll.js +40 -0
- package/src/markAsSeen.js +48 -0
- package/src/muteThread.js +45 -0
- package/src/removeUserFromGroup.js +45 -0
- package/src/resolvePhotoUrl.js +36 -0
- package/src/searchForThread.js +42 -0
- package/src/sendMessage.js +328 -0
- package/src/sendTypingIndicator.js +70 -0
- package/src/setMessageReaction.js +109 -0
- package/src/setPostReaction.js +102 -0
- package/src/setTitle.js +70 -0
- package/src/shareContact.js +46 -0
- package/src/threadColors.js +41 -0
- package/src/unfriend.js +42 -0
- package/src/unsendMessage.js +39 -0
- package/test/data/shareAttach.js +146 -0
- package/test/data/something.mov +0 -0
- package/test/data/test.png +0 -0
- package/test/data/test.txt +7 -0
- package/test/example-config.json +18 -0
- package/test/test-page.js +140 -0
- package/test/test.js +385 -0
- package/utils.js +1196 -0
@@ -0,0 +1,701 @@
|
|
1
|
+
/* eslint-disable no-redeclare */
|
2
|
+
"use strict";
|
3
|
+
var utils = require("../utils");
|
4
|
+
var log = require("npmlog");
|
5
|
+
var mqtt = require('mqtt');
|
6
|
+
var websocket = require('websocket-stream');
|
7
|
+
var HttpsProxyAgent = require('https-proxy-agent');
|
8
|
+
const EventEmitter = require('events');
|
9
|
+
|
10
|
+
var identity = function () { };
|
11
|
+
var form = {};
|
12
|
+
var getSeqID = function () { };
|
13
|
+
|
14
|
+
var topics = [
|
15
|
+
"/legacy_web",
|
16
|
+
"/webrtc",
|
17
|
+
"/rtc_multi",
|
18
|
+
"/onevc",
|
19
|
+
"/br_sr", //Notification
|
20
|
+
//Need to publish /br_sr right after this
|
21
|
+
"/sr_res",
|
22
|
+
"/t_ms",
|
23
|
+
"/thread_typing",
|
24
|
+
"/orca_typing_notifications",
|
25
|
+
"/notify_disconnect",
|
26
|
+
//Need to publish /messenger_sync_create_queue right after this
|
27
|
+
"/orca_presence",
|
28
|
+
//Will receive /sr_res right here.
|
29
|
+
|
30
|
+
"/inbox",
|
31
|
+
"/mercury",
|
32
|
+
"/messaging_events",
|
33
|
+
"/orca_message_notifications",
|
34
|
+
"/pp",
|
35
|
+
"/webrtc_response",
|
36
|
+
];
|
37
|
+
|
38
|
+
function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
|
39
|
+
//Don't really know what this does but I think it's for the active state?
|
40
|
+
//TODO: Move to ctx when implemented
|
41
|
+
var chatOn = ctx.globalOptions.online;
|
42
|
+
var foreground = false;
|
43
|
+
|
44
|
+
var sessionID = Math.floor(Math.random() * 9007199254740991) + 1;
|
45
|
+
var username = {
|
46
|
+
u: ctx.userID,
|
47
|
+
s: sessionID,
|
48
|
+
chat_on: chatOn,
|
49
|
+
fg: foreground,
|
50
|
+
d: utils.getGUID(),
|
51
|
+
ct: "websocket",
|
52
|
+
//App id from facebook
|
53
|
+
aid: "219994525426954",
|
54
|
+
mqtt_sid: "",
|
55
|
+
cp: 3,
|
56
|
+
ecp: 10,
|
57
|
+
st: [],
|
58
|
+
pm: [],
|
59
|
+
dc: "",
|
60
|
+
no_auto_fg: true,
|
61
|
+
gas: null,
|
62
|
+
pack: []
|
63
|
+
};
|
64
|
+
var cookies = ctx.jar.getCookies("https://www.facebook.com").join("; ");
|
65
|
+
|
66
|
+
var host;
|
67
|
+
if (ctx.mqttEndpoint) host = `${ctx.mqttEndpoint}&sid=${sessionID}`;
|
68
|
+
else if (ctx.region) host = `wss://edge-chat.facebook.com/chat?region=${ctx.region.toLocaleLowerCase()}&sid=${sessionID}`;
|
69
|
+
else host = `wss://edge-chat.facebook.com/chat?sid=${sessionID}`;
|
70
|
+
|
71
|
+
var options = {
|
72
|
+
clientId: "mqttwsclient",
|
73
|
+
protocolId: 'MQIsdp',
|
74
|
+
protocolVersion: 3,
|
75
|
+
username: JSON.stringify(username),
|
76
|
+
clean: true,
|
77
|
+
wsOptions: {
|
78
|
+
headers: {
|
79
|
+
'Cookie': cookies,
|
80
|
+
'Origin': 'https://www.facebook.com',
|
81
|
+
'User-Agent': ctx.globalOptions.userAgent,
|
82
|
+
'Referer': 'https://www.facebook.com/',
|
83
|
+
'Host': new URL(host).hostname //'edge-chat.facebook.com'
|
84
|
+
},
|
85
|
+
origin: 'https://www.facebook.com',
|
86
|
+
protocolVersion: 13
|
87
|
+
},
|
88
|
+
keepalive: 10,
|
89
|
+
reschedulePings: false
|
90
|
+
};
|
91
|
+
|
92
|
+
if (typeof ctx.globalOptions.proxy != "undefined") {
|
93
|
+
var agent = new HttpsProxyAgent(ctx.globalOptions.proxy);
|
94
|
+
options.wsOptions.agent = agent;
|
95
|
+
}
|
96
|
+
|
97
|
+
ctx.mqttClient = new mqtt.Client(_ => websocket(host, options.wsOptions), options);
|
98
|
+
|
99
|
+
global. mqttClient = ctx.mqttClient;
|
100
|
+
|
101
|
+
mqttClient.on('error', function (err) {
|
102
|
+
log.error("listenMqtt", err);
|
103
|
+
mqttClient.end();
|
104
|
+
if (ctx.globalOptions.autoReconnect) getSeqID();
|
105
|
+
else globalCallback({ type: "stop_listen", error: "Connection refused: Server unavailable" }, null);
|
106
|
+
});
|
107
|
+
|
108
|
+
mqttClient.on('connect', function () {
|
109
|
+
topics.forEach(topicsub => mqttClient.subscribe(topicsub));
|
110
|
+
|
111
|
+
var topic;
|
112
|
+
var queue = {
|
113
|
+
sync_api_version: 10,
|
114
|
+
max_deltas_able_to_process: 1000,
|
115
|
+
delta_batch_size: 500,
|
116
|
+
encoding: "JSON",
|
117
|
+
entity_fbid: ctx.userID,
|
118
|
+
};
|
119
|
+
|
120
|
+
if (ctx.syncToken) {
|
121
|
+
topic = "/messenger_sync_get_diffs";
|
122
|
+
queue.last_seq_id = ctx.lastSeqId;
|
123
|
+
queue.sync_token = ctx.syncToken;
|
124
|
+
}
|
125
|
+
else {
|
126
|
+
topic = "/messenger_sync_create_queue";
|
127
|
+
queue.initial_titan_sequence_id = ctx.lastSeqId;
|
128
|
+
queue.device_params = null;
|
129
|
+
}
|
130
|
+
|
131
|
+
mqttClient.publish(topic, JSON.stringify(queue), { qos: 1, retain: false });
|
132
|
+
|
133
|
+
var rTimeout = setTimeout(function () {
|
134
|
+
mqttClient.end();
|
135
|
+
getSeqID();
|
136
|
+
}, 5000);
|
137
|
+
|
138
|
+
ctx.tmsWait = function () {
|
139
|
+
clearTimeout(rTimeout);
|
140
|
+
ctx.globalOptions.emitReady ? globalCallback({
|
141
|
+
type: "ready",
|
142
|
+
error: null
|
143
|
+
}) : "";
|
144
|
+
delete ctx.tmsWait;
|
145
|
+
};
|
146
|
+
});
|
147
|
+
|
148
|
+
mqttClient.on('message', function (topic, message, _packet) {
|
149
|
+
try {
|
150
|
+
var jsonMessage = JSON.parse(message);
|
151
|
+
}
|
152
|
+
catch (ex) {
|
153
|
+
return log.error("listenMqtt", ex);
|
154
|
+
}
|
155
|
+
if (topic === "/t_ms") {
|
156
|
+
if (ctx.tmsWait && typeof ctx.tmsWait == "function") ctx.tmsWait();
|
157
|
+
|
158
|
+
if (jsonMessage.firstDeltaSeqId && jsonMessage.syncToken) {
|
159
|
+
ctx.lastSeqId = jsonMessage.firstDeltaSeqId;
|
160
|
+
ctx.syncToken = jsonMessage.syncToken;
|
161
|
+
}
|
162
|
+
|
163
|
+
if (jsonMessage.lastIssuedSeqId) ctx.lastSeqId = parseInt(jsonMessage.lastIssuedSeqId);
|
164
|
+
|
165
|
+
//If it contains more than 1 delta
|
166
|
+
for (var i in jsonMessage.deltas) {
|
167
|
+
var delta = jsonMessage.deltas[i];
|
168
|
+
parseDelta(defaultFuncs, api, ctx, globalCallback, { "delta": delta });
|
169
|
+
}
|
170
|
+
}
|
171
|
+
else if (topic === "/thread_typing" || topic === "/orca_typing_notifications") {
|
172
|
+
var typ = {
|
173
|
+
type: "typ",
|
174
|
+
isTyping: !!jsonMessage.state,
|
175
|
+
from: jsonMessage.sender_fbid.toString(),
|
176
|
+
threadID: utils.formatID((jsonMessage.thread || jsonMessage.sender_fbid).toString())
|
177
|
+
};
|
178
|
+
(function () { globalCallback(null, typ); })();
|
179
|
+
}
|
180
|
+
else if (topic === "/orca_presence") {
|
181
|
+
if (!ctx.globalOptions.updatePresence) {
|
182
|
+
for (var i in jsonMessage.list) {
|
183
|
+
var data = jsonMessage.list[i];
|
184
|
+
var userID = data["u"];
|
185
|
+
|
186
|
+
var presence = {
|
187
|
+
type: "presence",
|
188
|
+
userID: userID.toString(),
|
189
|
+
//Convert to ms
|
190
|
+
timestamp: data["l"] * 1000,
|
191
|
+
statuses: data["p"]
|
192
|
+
};
|
193
|
+
(function () { globalCallback(null, presence); })();
|
194
|
+
}
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
});
|
199
|
+
|
200
|
+
mqttClient.on('close', function () {
|
201
|
+
//(function () { globalCallback("Connection closed."); })();
|
202
|
+
// client.end();
|
203
|
+
});
|
204
|
+
}
|
205
|
+
|
206
|
+
function parseDelta(defaultFuncs, api, ctx, globalCallback, v) {
|
207
|
+
if (v.delta.class == "NewMessage") {
|
208
|
+
//Not tested for pages
|
209
|
+
if (ctx.globalOptions.pageID && ctx.globalOptions.pageID != v.queue) return;
|
210
|
+
|
211
|
+
(function resolveAttachmentUrl(i) {
|
212
|
+
if (i == (v.delta.attachments || []).length) {
|
213
|
+
let fmtMsg;
|
214
|
+
try {
|
215
|
+
fmtMsg = utils.formatDeltaMessage(v);
|
216
|
+
} catch (err) {
|
217
|
+
return globalCallback({
|
218
|
+
error: "Problem parsing message object.",
|
219
|
+
detail: err,
|
220
|
+
res: v,
|
221
|
+
type: "parse_error"
|
222
|
+
});
|
223
|
+
}
|
224
|
+
if (fmtMsg) {
|
225
|
+
if (ctx.globalOptions.autoMarkDelivery) {
|
226
|
+
markDelivery(ctx, api, fmtMsg.threadID, fmtMsg.messageID);
|
227
|
+
}
|
228
|
+
}
|
229
|
+
return !ctx.globalOptions.selfListen &&
|
230
|
+
(fmtMsg.senderID === ctx.i_userID || fmtMsg.senderID === ctx.userID) ?
|
231
|
+
undefined :
|
232
|
+
(function () { globalCallback(null, fmtMsg); })();
|
233
|
+
} else {
|
234
|
+
if (v.delta.attachments[i].mercury.attach_type == "photo") {
|
235
|
+
api.resolvePhotoUrl(
|
236
|
+
v.delta.attachments[i].fbid,
|
237
|
+
(err, url) => {
|
238
|
+
if (!err)
|
239
|
+
v.delta.attachments[
|
240
|
+
i
|
241
|
+
].mercury.metadata.url = url;
|
242
|
+
return resolveAttachmentUrl(i + 1);
|
243
|
+
}
|
244
|
+
);
|
245
|
+
} else {
|
246
|
+
return resolveAttachmentUrl(i + 1);
|
247
|
+
}
|
248
|
+
}
|
249
|
+
})(0);
|
250
|
+
}
|
251
|
+
|
252
|
+
if (v.delta.class == "ClientPayload") {
|
253
|
+
var clientPayload = utils.decodeClientPayload(v.delta.payload);
|
254
|
+
if (clientPayload && clientPayload.deltas) {
|
255
|
+
for (var i in clientPayload.deltas) {
|
256
|
+
var delta = clientPayload.deltas[i];
|
257
|
+
if (delta.deltaMessageReaction && !!ctx.globalOptions.listenEvents) {
|
258
|
+
(function () {
|
259
|
+
globalCallback(null, {
|
260
|
+
type: "message_reaction",
|
261
|
+
threadID: (delta.deltaMessageReaction.threadKey.threadFbId ? delta.deltaMessageReaction.threadKey.threadFbId : delta.deltaMessageReaction.threadKey.otherUserFbId).toString(),
|
262
|
+
messageID: delta.deltaMessageReaction.messageId,
|
263
|
+
reaction: delta.deltaMessageReaction.reaction,
|
264
|
+
senderID: delta.deltaMessageReaction.senderId.toString(),
|
265
|
+
userID: delta.deltaMessageReaction.userId.toString()
|
266
|
+
});
|
267
|
+
})();
|
268
|
+
}
|
269
|
+
else if (delta.deltaRecallMessageData && !!ctx.globalOptions.listenEvents) {
|
270
|
+
(function () {
|
271
|
+
globalCallback(null, {
|
272
|
+
type: "message_unsend",
|
273
|
+
threadID: (delta.deltaRecallMessageData.threadKey.threadFbId ? delta.deltaRecallMessageData.threadKey.threadFbId : delta.deltaRecallMessageData.threadKey.otherUserFbId).toString(),
|
274
|
+
messageID: delta.deltaRecallMessageData.messageID,
|
275
|
+
senderID: delta.deltaRecallMessageData.senderID.toString(),
|
276
|
+
deletionTimestamp: delta.deltaRecallMessageData.deletionTimestamp,
|
277
|
+
timestamp: delta.deltaRecallMessageData.timestamp
|
278
|
+
});
|
279
|
+
})();
|
280
|
+
}
|
281
|
+
else if (delta.deltaMessageReply) {
|
282
|
+
//Mention block - #1
|
283
|
+
var mdata = delta.deltaMessageReply.message === undefined ? [] :
|
284
|
+
delta.deltaMessageReply.message.data === undefined ? [] :
|
285
|
+
delta.deltaMessageReply.message.data.prng === undefined ? [] :
|
286
|
+
JSON.parse(delta.deltaMessageReply.message.data.prng);
|
287
|
+
var m_id = mdata.map(u => u.i);
|
288
|
+
var m_offset = mdata.map(u => u.o);
|
289
|
+
var m_length = mdata.map(u => u.l);
|
290
|
+
|
291
|
+
var mentions = {};
|
292
|
+
|
293
|
+
for (var i = 0; i < m_id.length; i++) mentions[m_id[i]] = (delta.deltaMessageReply.message.body || "").substring(m_offset[i], m_offset[i] + m_length[i]);
|
294
|
+
//Mention block - 1#
|
295
|
+
var callbackToReturn = {
|
296
|
+
type: "message_reply",
|
297
|
+
threadID: (delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.message.messageMetadata.threadKey.otherUserFbId).toString(),
|
298
|
+
messageID: delta.deltaMessageReply.message.messageMetadata.messageId,
|
299
|
+
senderID: delta.deltaMessageReply.message.messageMetadata.actorFbId.toString(),
|
300
|
+
attachments: (delta.deltaMessageReply.message.attachments || []).map(function (att) {
|
301
|
+
var mercury = JSON.parse(att.mercuryJSON);
|
302
|
+
Object.assign(att, mercury);
|
303
|
+
return att;
|
304
|
+
}).map(att => {
|
305
|
+
var x;
|
306
|
+
try {
|
307
|
+
x = utils._formatAttachment(att);
|
308
|
+
}
|
309
|
+
catch (ex) {
|
310
|
+
x = att;
|
311
|
+
x.error = ex;
|
312
|
+
x.type = "unknown";
|
313
|
+
}
|
314
|
+
return x;
|
315
|
+
}),
|
316
|
+
args: (delta.deltaMessageReply.message.body || "").trim().split(/\s+/),
|
317
|
+
body: (delta.deltaMessageReply.message.body || ""),
|
318
|
+
isGroup: !!delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId,
|
319
|
+
mentions: mentions,
|
320
|
+
timestamp: delta.deltaMessageReply.message.messageMetadata.timestamp,
|
321
|
+
participantIDs: (delta.deltaMessageReply.message.messageMetadata.cid.canonicalParticipantFbids || delta.deltaMessageReply.message.participants || []).map(e => e.toString())
|
322
|
+
};
|
323
|
+
|
324
|
+
if (delta.deltaMessageReply.repliedToMessage) {
|
325
|
+
//Mention block - #2
|
326
|
+
mdata = delta.deltaMessageReply.repliedToMessage === undefined ? [] :
|
327
|
+
delta.deltaMessageReply.repliedToMessage.data === undefined ? [] :
|
328
|
+
delta.deltaMessageReply.repliedToMessage.data.prng === undefined ? [] :
|
329
|
+
JSON.parse(delta.deltaMessageReply.repliedToMessage.data.prng);
|
330
|
+
m_id = mdata.map(u => u.i);
|
331
|
+
m_offset = mdata.map(u => u.o);
|
332
|
+
m_length = mdata.map(u => u.l);
|
333
|
+
|
334
|
+
var rmentions = {};
|
335
|
+
|
336
|
+
for (var i = 0; i < m_id.length; i++) rmentions[m_id[i]] = (delta.deltaMessageReply.repliedToMessage.body || "").substring(m_offset[i], m_offset[i] + m_length[i]);
|
337
|
+
//Mention block - 2#
|
338
|
+
callbackToReturn.messageReply = {
|
339
|
+
threadID: (delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId ? delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.otherUserFbId).toString(),
|
340
|
+
messageID: delta.deltaMessageReply.repliedToMessage.messageMetadata.messageId,
|
341
|
+
senderID: delta.deltaMessageReply.repliedToMessage.messageMetadata.actorFbId.toString(),
|
342
|
+
attachments: delta.deltaMessageReply.repliedToMessage.attachments.map(function (att) {
|
343
|
+
var mercury = JSON.parse(att.mercuryJSON);
|
344
|
+
Object.assign(att, mercury);
|
345
|
+
return att;
|
346
|
+
}).map(att => {
|
347
|
+
var x;
|
348
|
+
try {
|
349
|
+
x = utils._formatAttachment(att);
|
350
|
+
}
|
351
|
+
catch (ex) {
|
352
|
+
x = att;
|
353
|
+
x.error = ex;
|
354
|
+
x.type = "unknown";
|
355
|
+
}
|
356
|
+
return x;
|
357
|
+
}),
|
358
|
+
args: (delta.deltaMessageReply.repliedToMessage.body || "").trim().split(/\s+/),
|
359
|
+
body: delta.deltaMessageReply.repliedToMessage.body || "",
|
360
|
+
isGroup: !!delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId,
|
361
|
+
mentions: rmentions,
|
362
|
+
timestamp: delta.deltaMessageReply.repliedToMessage.messageMetadata.timestamp
|
363
|
+
};
|
364
|
+
}
|
365
|
+
else if (delta.deltaMessageReply.replyToMessageId) {
|
366
|
+
return defaultFuncs
|
367
|
+
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, {
|
368
|
+
"av": ctx.globalOptions.pageID,
|
369
|
+
"queries": JSON.stringify({
|
370
|
+
"o0": {
|
371
|
+
//Using the same doc_id as forcedFetch
|
372
|
+
"doc_id": "2848441488556444",
|
373
|
+
"query_params": {
|
374
|
+
"thread_and_message_id": {
|
375
|
+
"thread_id": callbackToReturn.threadID,
|
376
|
+
"message_id": delta.deltaMessageReply.replyToMessageId.id,
|
377
|
+
}
|
378
|
+
}
|
379
|
+
}
|
380
|
+
})
|
381
|
+
})
|
382
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
383
|
+
.then((resData) => {
|
384
|
+
if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
|
385
|
+
if (resData[resData.length - 1].successful_results === 0) throw { error: "forcedFetch: there was no successful_results", res: resData };
|
386
|
+
var fetchData = resData[0].o0.data.message;
|
387
|
+
var mobj = {};
|
388
|
+
for (var n in fetchData.message.ranges) mobj[fetchData.message.ranges[n].entity.id] = (fetchData.message.text || "").substr(fetchData.message.ranges[n].offset, fetchData.message.ranges[n].length);
|
389
|
+
|
390
|
+
callbackToReturn.messageReply = {
|
391
|
+
threadID: callbackToReturn.threadID,
|
392
|
+
messageID: fetchData.message_id,
|
393
|
+
senderID: fetchData.message_sender.id.toString(),
|
394
|
+
attachments: fetchData.message.blob_attachment.map(att => {
|
395
|
+
var x;
|
396
|
+
try {
|
397
|
+
x = utils._formatAttachment({ blob_attachment: att });
|
398
|
+
}
|
399
|
+
catch (ex) {
|
400
|
+
x = att;
|
401
|
+
x.error = ex;
|
402
|
+
x.type = "unknown";
|
403
|
+
}
|
404
|
+
return x;
|
405
|
+
}),
|
406
|
+
args: (fetchData.message.text || "").trim().split(/\s+/) || [],
|
407
|
+
body: fetchData.message.text || "",
|
408
|
+
isGroup: callbackToReturn.isGroup,
|
409
|
+
mentions: mobj,
|
410
|
+
timestamp: parseInt(fetchData.timestamp_precise)
|
411
|
+
};
|
412
|
+
})
|
413
|
+
.catch(err => log.error("forcedFetch", err))
|
414
|
+
.finally(function () {
|
415
|
+
if (ctx.globalOptions.autoMarkDelivery) markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
|
416
|
+
!ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID ? undefined : (function () { globalCallback(null, callbackToReturn); })();
|
417
|
+
});
|
418
|
+
}
|
419
|
+
else callbackToReturn.delta = delta;
|
420
|
+
|
421
|
+
if (ctx.globalOptions.autoMarkDelivery) markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
|
422
|
+
|
423
|
+
return !ctx.globalOptions.selfListen && callbackToReturn.senderID === ctx.userID ? undefined : (function () { globalCallback(null, callbackToReturn); })();
|
424
|
+
}
|
425
|
+
}
|
426
|
+
return;
|
427
|
+
}
|
428
|
+
}
|
429
|
+
|
430
|
+
if (v.delta.class !== "NewMessage" && !ctx.globalOptions.listenEvents) return;
|
431
|
+
switch (v.delta.class) {
|
432
|
+
case "ReadReceipt":
|
433
|
+
var fmtMsg;
|
434
|
+
try {
|
435
|
+
fmtMsg = utils.formatDeltaReadReceipt(v.delta);
|
436
|
+
}
|
437
|
+
catch (err) {
|
438
|
+
return globalCallback({
|
439
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
|
440
|
+
detail: err,
|
441
|
+
res: v.delta,
|
442
|
+
type: "parse_error"
|
443
|
+
});
|
444
|
+
}
|
445
|
+
return (function () { globalCallback(null, fmtMsg); })();
|
446
|
+
case "AdminTextMessage":
|
447
|
+
switch (v.delta.type) {
|
448
|
+
case "change_thread_theme":
|
449
|
+
case "change_thread_icon":
|
450
|
+
case "change_thread_quick_reaction":
|
451
|
+
case "change_thread_nickname":
|
452
|
+
case "change_thread_admins":
|
453
|
+
case "change_thread_approval_mode":
|
454
|
+
case "group_poll":
|
455
|
+
case "messenger_call_log":
|
456
|
+
case "participant_joined_group_call":
|
457
|
+
var fmtMsg;
|
458
|
+
try {
|
459
|
+
fmtMsg = utils.formatDeltaEvent(v.delta);
|
460
|
+
}
|
461
|
+
catch (err) {
|
462
|
+
return globalCallback({
|
463
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
|
464
|
+
detail: err,
|
465
|
+
res: v.delta,
|
466
|
+
type: "parse_error"
|
467
|
+
});
|
468
|
+
}
|
469
|
+
return (function () { globalCallback(null, fmtMsg); })();
|
470
|
+
default:
|
471
|
+
return;
|
472
|
+
}
|
473
|
+
//For group images
|
474
|
+
case "ForcedFetch":
|
475
|
+
if (!v.delta.threadKey) return;
|
476
|
+
var mid = v.delta.messageId;
|
477
|
+
var tid = v.delta.threadKey.threadFbId;
|
478
|
+
if (mid && tid) {
|
479
|
+
const form = {
|
480
|
+
"av": ctx.globalOptions.pageID,
|
481
|
+
"queries": JSON.stringify({
|
482
|
+
"o0": {
|
483
|
+
//This doc_id is valid as of March 25, 2020
|
484
|
+
"doc_id": "2848441488556444",
|
485
|
+
"query_params": {
|
486
|
+
"thread_and_message_id": {
|
487
|
+
"thread_id": tid.toString(),
|
488
|
+
"message_id": mid,
|
489
|
+
}
|
490
|
+
}
|
491
|
+
}
|
492
|
+
})
|
493
|
+
};
|
494
|
+
|
495
|
+
defaultFuncs
|
496
|
+
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
|
497
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
498
|
+
.then((resData) => {
|
499
|
+
if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
|
500
|
+
|
501
|
+
if (resData[resData.length - 1].successful_results === 0) throw { error: "forcedFetch: there was no successful_results", res: resData };
|
502
|
+
|
503
|
+
var fetchData = resData[0].o0.data.message;
|
504
|
+
|
505
|
+
if (utils.getType(fetchData) == "Object") {
|
506
|
+
log.info("forcedFetch", fetchData);
|
507
|
+
switch (fetchData.__typename) {
|
508
|
+
case "ThreadImageMessage":
|
509
|
+
(!ctx.globalOptions.selfListen && fetchData.message_sender.id.toString() === ctx.userID) ||
|
510
|
+
!ctx.loggedIn ? undefined : (function () {
|
511
|
+
globalCallback(null, {
|
512
|
+
type: "change_thread_image",
|
513
|
+
threadID: utils.formatID(tid.toString()),
|
514
|
+
snippet: fetchData.snippet,
|
515
|
+
timestamp: fetchData.timestamp_precise,
|
516
|
+
author: fetchData.message_sender.id,
|
517
|
+
image: {
|
518
|
+
attachmentID: fetchData.image_with_metadata && fetchData.image_with_metadata.legacy_attachment_id,
|
519
|
+
width: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.x,
|
520
|
+
height: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.y,
|
521
|
+
url: fetchData.image_with_metadata && fetchData.image_with_metadata.preview.uri
|
522
|
+
}
|
523
|
+
});
|
524
|
+
})();
|
525
|
+
break;
|
526
|
+
case "UserMessage":
|
527
|
+
log.info("ff-Return", {
|
528
|
+
type: "message",
|
529
|
+
senderID: utils.formatID(fetchData.message_sender.id),
|
530
|
+
body: fetchData.message.text || "",
|
531
|
+
threadID: utils.formatID(tid.toString()),
|
532
|
+
messageID: fetchData.message_id,
|
533
|
+
attachments: [{
|
534
|
+
type: "share",
|
535
|
+
ID: fetchData.extensible_attachment.legacy_attachment_id,
|
536
|
+
url: fetchData.extensible_attachment.story_attachment.url,
|
537
|
+
|
538
|
+
title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
|
539
|
+
description: fetchData.extensible_attachment.story_attachment.description.text,
|
540
|
+
source: fetchData.extensible_attachment.story_attachment.source,
|
541
|
+
|
542
|
+
image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
|
543
|
+
width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
|
544
|
+
height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
|
545
|
+
playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
|
546
|
+
duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
|
547
|
+
|
548
|
+
subattachments: fetchData.extensible_attachment.subattachments,
|
549
|
+
properties: fetchData.extensible_attachment.story_attachment.properties,
|
550
|
+
}],
|
551
|
+
mentions: {},
|
552
|
+
timestamp: parseInt(fetchData.timestamp_precise),
|
553
|
+
participantIDs: (fetchData.participants || (fetchData.messageMetadata ? fetchData.messageMetadata.cid ? fetchData.messageMetadata.cid.canonicalParticipantFbids : fetchData.messageMetadata.participantIds : []) || []),
|
554
|
+
isGroup: (fetchData.message_sender.id != tid.toString())
|
555
|
+
});
|
556
|
+
globalCallback(null, {
|
557
|
+
type: "message",
|
558
|
+
senderID: utils.formatID(fetchData.message_sender.id),
|
559
|
+
body: fetchData.message.text || "",
|
560
|
+
threadID: utils.formatID(tid.toString()),
|
561
|
+
messageID: fetchData.message_id,
|
562
|
+
attachments: [{
|
563
|
+
type: "share",
|
564
|
+
ID: fetchData.extensible_attachment.legacy_attachment_id,
|
565
|
+
url: fetchData.extensible_attachment.story_attachment.url,
|
566
|
+
|
567
|
+
title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
|
568
|
+
description: fetchData.extensible_attachment.story_attachment.description.text,
|
569
|
+
source: fetchData.extensible_attachment.story_attachment.source,
|
570
|
+
|
571
|
+
image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
|
572
|
+
width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
|
573
|
+
height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
|
574
|
+
playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
|
575
|
+
duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
|
576
|
+
|
577
|
+
subattachments: fetchData.extensible_attachment.subattachments,
|
578
|
+
properties: fetchData.extensible_attachment.story_attachment.properties,
|
579
|
+
}],
|
580
|
+
mentions: {},
|
581
|
+
timestamp: parseInt(fetchData.timestamp_precise),
|
582
|
+
participantIDs: (fetchData.participants || (fetchData.messageMetadata ? fetchData.messageMetadata.cid ? fetchData.messageMetadata.cid.canonicalParticipantFbids : fetchData.messageMetadata.participantIds : []) || []),
|
583
|
+
isGroup: (fetchData.message_sender.id != tid.toString())
|
584
|
+
});
|
585
|
+
}
|
586
|
+
}
|
587
|
+
else log.error("forcedFetch", fetchData);
|
588
|
+
})
|
589
|
+
.catch((err) => log.error("forcedFetch", err));
|
590
|
+
}
|
591
|
+
break;
|
592
|
+
case "ThreadName":
|
593
|
+
case "ParticipantsAddedToGroupThread":
|
594
|
+
case "ParticipantLeftGroupThread":
|
595
|
+
var formattedEvent;
|
596
|
+
try {
|
597
|
+
formattedEvent = utils.formatDeltaEvent(v.delta);
|
598
|
+
}
|
599
|
+
catch (err) {
|
600
|
+
return globalCallback({
|
601
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
|
602
|
+
detail: err,
|
603
|
+
res: v.delta,
|
604
|
+
type: "parse_error"
|
605
|
+
});
|
606
|
+
}
|
607
|
+
return (!ctx.globalOptions.selfListen && formattedEvent.author.toString() === ctx.userID) || !ctx.loggedIn ? undefined : (function () { globalCallback(null, formattedEvent); })();
|
608
|
+
}
|
609
|
+
}
|
610
|
+
|
611
|
+
function markDelivery(ctx, api, threadID, messageID) {
|
612
|
+
if (threadID && messageID) {
|
613
|
+
api.markAsDelivered(threadID, messageID, (err) => {
|
614
|
+
if (err) log.error("markAsDelivered", err);
|
615
|
+
else {
|
616
|
+
if (ctx.globalOptions.autoMarkRead) {
|
617
|
+
api.markAsRead(threadID, (err) => {
|
618
|
+
if (err) log.error("markAsDelivered", err);
|
619
|
+
});
|
620
|
+
}
|
621
|
+
}
|
622
|
+
});
|
623
|
+
}
|
624
|
+
}
|
625
|
+
|
626
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
627
|
+
var globalCallback = identity;
|
628
|
+
getSeqID = function getSeqID() {
|
629
|
+
ctx.t_mqttCalled = false;
|
630
|
+
defaultFuncs
|
631
|
+
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
|
632
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
633
|
+
.then((resData) => {
|
634
|
+
if (utils.getType(resData) != "Array") throw { error: "Not logged in", res: resData };
|
635
|
+
if (resData && resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
|
636
|
+
if (resData[resData.length - 1].successful_results === 0) throw { error: "getSeqId: there was no successful_results", res: resData };
|
637
|
+
if (resData[0].o0.data.viewer.message_threads.sync_sequence_id) {
|
638
|
+
ctx.lastSeqId = resData[0].o0.data.viewer.message_threads.sync_sequence_id;
|
639
|
+
listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
640
|
+
}
|
641
|
+
else throw { error: "getSeqId: no sync_sequence_id found.", res: resData };
|
642
|
+
})
|
643
|
+
.catch((err) => {
|
644
|
+
log.error("getSeqId", err);
|
645
|
+
if (utils.getType(err) == "Object" && err.error === "Not logged in") ctx.loggedIn = false;
|
646
|
+
return globalCallback(err);
|
647
|
+
});
|
648
|
+
};
|
649
|
+
|
650
|
+
return function (callback) {
|
651
|
+
class MessageEmitter extends EventEmitter {
|
652
|
+
stopListening(callback) {
|
653
|
+
callback = callback || (() => { });
|
654
|
+
globalCallback = identity;
|
655
|
+
if (ctx.mqttClient) {
|
656
|
+
ctx.mqttClient.unsubscribe("/webrtc");
|
657
|
+
ctx.mqttClient.unsubscribe("/rtc_multi");
|
658
|
+
ctx.mqttClient.unsubscribe("/onevc");
|
659
|
+
ctx.mqttClient.publish("/browser_close", "{}");
|
660
|
+
ctx.mqttClient.end(false, function (...data) {
|
661
|
+
callback(data);
|
662
|
+
ctx.mqttClient = undefined;
|
663
|
+
});
|
664
|
+
}
|
665
|
+
}
|
666
|
+
}
|
667
|
+
|
668
|
+
var msgEmitter = new MessageEmitter();
|
669
|
+
globalCallback = (callback || function (error, message) {
|
670
|
+
if (error) return msgEmitter.emit("error", error);
|
671
|
+
msgEmitter.emit("message", message);
|
672
|
+
});
|
673
|
+
|
674
|
+
//Reset some stuff
|
675
|
+
if (!ctx.firstListen) ctx.lastSeqId = null;
|
676
|
+
ctx.syncToken = undefined;
|
677
|
+
ctx.t_mqttCalled = false;
|
678
|
+
|
679
|
+
//Same request as getThreadList
|
680
|
+
form = {
|
681
|
+
"av": ctx.globalOptions.pageID,
|
682
|
+
"queries": JSON.stringify({
|
683
|
+
"o0": {
|
684
|
+
"doc_id": "3336396659757871",
|
685
|
+
"query_params": {
|
686
|
+
"limit": 1,
|
687
|
+
"before": null,
|
688
|
+
"tags": ["INBOX"],
|
689
|
+
"includeDeliveryReceipts": false,
|
690
|
+
"includeSeqID": true
|
691
|
+
}
|
692
|
+
}
|
693
|
+
})
|
694
|
+
};
|
695
|
+
|
696
|
+
if (!ctx.firstListen || !ctx.lastSeqId) getSeqID();
|
697
|
+
else listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
698
|
+
ctx.firstListen = false;
|
699
|
+
return msgEmitter;
|
700
|
+
};
|
701
|
+
};
|