@dongdev/fca-unofficial 1.0.19 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +1 -0
- package/CHANGELOG.md +30 -27
- package/DOCS.md +727 -592
- package/README.md +94 -97
- package/func/logger.js +112 -0
- package/html.html +474 -0
- package/index.js +1 -379
- package/module/config.js +26 -0
- package/module/login.js +45 -0
- package/module/loginHelper.js +634 -0
- package/module/options.js +49 -0
- package/package.json +6 -39
- package/src/api/action/addExternalModule.js +25 -0
- package/src/api/action/changeAvatar.js +136 -0
- package/src/api/action/changeBio.js +76 -0
- package/src/api/action/getCurrentUserID.js +7 -0
- package/src/api/action/handleFriendRequest.js +57 -0
- package/src/api/action/logout.js +75 -0
- package/src/{refreshFb_dtsg.js → api/action/refreshFb_dtsg.js} +8 -8
- package/src/api/action/setPostReaction.js +107 -0
- package/src/api/action/unfriend.js +55 -0
- package/src/api/http/httpGet.js +65 -0
- package/src/api/http/httpPost.js +65 -0
- package/src/{postFormData.js → api/http/postFormData.js} +10 -10
- package/src/api/messaging/addUserToGroup.js +69 -0
- package/src/api/messaging/changeAdminStatus.js +103 -0
- package/src/api/messaging/changeArchivedStatus.js +55 -0
- package/src/api/messaging/changeBlockedStatus.js +49 -0
- package/src/api/messaging/changeGroupImage.js +135 -0
- package/src/api/messaging/changeNickname.js +59 -0
- package/src/api/messaging/changeThreadColor.js +65 -0
- package/src/api/messaging/createNewGroup.js +88 -0
- package/src/api/messaging/createPoll.js +70 -0
- package/src/api/messaging/deleteMessage.js +56 -0
- package/src/api/messaging/deleteThread.js +56 -0
- package/src/api/messaging/forwardAttachment.js +60 -0
- package/src/api/messaging/getEmojiUrl.js +29 -0
- package/src/api/messaging/getFriendsList.js +83 -0
- package/src/api/messaging/getMessage.js +834 -0
- package/src/api/messaging/getThreadHistory.js +681 -0
- package/src/api/messaging/handleMessageRequest.js +65 -0
- package/src/api/messaging/markAsDelivered.js +57 -0
- package/src/api/messaging/markAsRead.js +88 -0
- package/src/api/messaging/markAsReadAll.js +50 -0
- package/src/api/messaging/markAsSeen.js +61 -0
- package/src/api/messaging/muteThread.js +51 -0
- package/src/api/messaging/removeUserFromGroup.js +79 -0
- package/src/api/messaging/resolvePhotoUrl.js +44 -0
- package/src/api/messaging/searchForThread.js +53 -0
- package/src/api/messaging/sendMessage.js +306 -0
- package/src/api/messaging/sendMessageMqtt.js +321 -0
- package/src/api/messaging/sendTypingIndicator.js +110 -0
- package/src/{setMessageReaction.js → api/messaging/setMessageReaction.js} +20 -20
- package/src/api/messaging/setTitle.js +90 -0
- package/src/api/messaging/shareContact.js +51 -0
- package/src/api/messaging/threadColors.js +131 -0
- package/src/api/messaging/unsendMessage.js +44 -0
- package/src/api/messaging/uploadAttachment.js +93 -0
- package/src/api/socket/detail/buildStream.js +100 -0
- package/src/{listenMqtt.js → api/socket/listenMqtt.js} +172 -256
- package/src/api/threads/changeThreadEmoji.js +55 -0
- package/src/api/threads/getThreadInfo.js +572 -0
- package/src/{getThreadList.js → api/threads/getThreadList.js} +110 -54
- package/src/api/threads/getThreadPictures.js +79 -0
- package/src/api/users/getUserID.js +66 -0
- package/src/api/users/getUserInfo.js +88 -0
- package/src/core/sendReqMqtt.js +63 -0
- package/{lib → src}/database/models/index.js +12 -10
- package/{lib → src}/database/models/thread.js +5 -5
- package/{lib → src}/database/threadData.js +19 -14
- package/src/utils/client.js +159 -0
- package/src/utils/constants.js +13 -0
- package/src/utils/format.js +60 -0
- package/src/utils/headers.js +41 -0
- package/src/utils/index.js +1497 -0
- package/src/utils/request.js +147 -0
- package/lib/logger.js +0 -96
- package/src/addExternalModule.js +0 -19
- package/src/addUserToGroup.js +0 -113
- package/src/changeAdminStatus.js +0 -79
- package/src/changeArchivedStatus.js +0 -55
- package/src/changeAvatar.js +0 -126
- package/src/changeBio.js +0 -77
- package/src/changeBlockedStatus.js +0 -47
- package/src/changeGroupImage.js +0 -132
- package/src/changeNickname.js +0 -59
- package/src/changeThreadColor.js +0 -65
- package/src/changeThreadEmoji.js +0 -55
- package/src/createNewGroup.js +0 -86
- package/src/createPoll.js +0 -71
- package/src/deleteMessage.js +0 -56
- package/src/deleteThread.js +0 -56
- package/src/forwardAttachment.js +0 -60
- package/src/getCurrentUserID.js +0 -7
- package/src/getEmojiUrl.js +0 -29
- package/src/getFriendsList.js +0 -83
- package/src/getMessage.js +0 -796
- package/src/getThreadHistory.js +0 -666
- package/src/getThreadInfo.js +0 -535
- package/src/getThreadPictures.js +0 -79
- package/src/getUserID.js +0 -66
- package/src/getUserInfo.js +0 -80
- package/src/handleFriendRequest.js +0 -61
- package/src/handleMessageRequest.js +0 -65
- package/src/httpGet.js +0 -57
- package/src/httpPost.js +0 -57
- package/src/httpPostFormData.js +0 -63
- package/src/logout.js +0 -75
- package/src/markAsDelivered.js +0 -58
- package/src/markAsRead.js +0 -80
- package/src/markAsReadAll.js +0 -50
- package/src/markAsSeen.js +0 -59
- package/src/muteThread.js +0 -52
- package/src/removeUserFromGroup.js +0 -79
- package/src/resolvePhotoUrl.js +0 -45
- package/src/searchForThread.js +0 -53
- package/src/sendMessage.js +0 -328
- package/src/sendMessageMqtt.js +0 -316
- package/src/sendTypingIndicator.js +0 -103
- package/src/setPostReaction.js +0 -109
- package/src/setTitle.js +0 -86
- package/src/shareContact.js +0 -49
- package/src/threadColors.js +0 -131
- package/src/unfriend.js +0 -52
- package/src/unsendMessage.js +0 -49
- package/src/uploadAttachment.js +0 -95
- package/utils.js +0 -1387
- /package/{lib → func}/login.js +0 -0
@@ -0,0 +1,306 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const fs = require("fs");
|
4
|
+
const path = require("path");
|
5
|
+
const { Readable } = require("stream");
|
6
|
+
const utils = require("../../utils");
|
7
|
+
const log = require("npmlog");
|
8
|
+
const allowedProperties = {
|
9
|
+
attachment: true,
|
10
|
+
url: true,
|
11
|
+
sticker: true,
|
12
|
+
emoji: true,
|
13
|
+
emojiSize: true,
|
14
|
+
body: true,
|
15
|
+
mentions: true,
|
16
|
+
location: true,
|
17
|
+
asPage: true
|
18
|
+
};
|
19
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
20
|
+
function toReadable(input) {
|
21
|
+
if (utils.isReadableStream(input)) return input;
|
22
|
+
if (Buffer.isBuffer(input)) return Readable.from(input);
|
23
|
+
if (typeof input === "string" && fs.existsSync(input) && fs.statSync(input).isFile()) return fs.createReadStream(path.resolve(input));
|
24
|
+
throw { error: "Unsupported attachment input. Use stream/buffer/filepath." };
|
25
|
+
}
|
26
|
+
function normalizeAttachments(list) {
|
27
|
+
if (!Array.isArray(list)) list = [list];
|
28
|
+
return list.map(a => {
|
29
|
+
if (Array.isArray(a) && /_id$/.test(a[0])) return a;
|
30
|
+
if (a && typeof a === "object") {
|
31
|
+
if (a.id && a.type && /_id$/.test(a.type)) return [a.type, a.id];
|
32
|
+
if (a.image_id || a.file_id || a.video_id || a.audio_id || a.gif_id) {
|
33
|
+
const k = Object.keys(a).find(x => /_id$/.test(x));
|
34
|
+
return [k, a[k]];
|
35
|
+
}
|
36
|
+
}
|
37
|
+
return toReadable(a);
|
38
|
+
});
|
39
|
+
}
|
40
|
+
function uploadAttachment(attachments, callback) {
|
41
|
+
const uploads = [];
|
42
|
+
for (let i = 0; i < attachments.length; i++) {
|
43
|
+
if (!utils.isReadableStream(attachments[i])) {
|
44
|
+
throw { error: "Attachment should be a readable stream and not " + utils.getType(attachments[i]) + "." };
|
45
|
+
}
|
46
|
+
const form = { upload_1024: attachments[i], voice_clip: "true" };
|
47
|
+
uploads.push(
|
48
|
+
defaultFuncs
|
49
|
+
.postFormData("https://upload.facebook.com/ajax/mercury/upload.php", ctx.jar, form, {}, {})
|
50
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
51
|
+
.then(resData => {
|
52
|
+
if (resData.error) throw resData;
|
53
|
+
return resData.payload.metadata[0];
|
54
|
+
})
|
55
|
+
);
|
56
|
+
}
|
57
|
+
Promise.all(uploads)
|
58
|
+
.then(resData => callback(null, resData))
|
59
|
+
.catch(err => {
|
60
|
+
log.error("uploadAttachment", err);
|
61
|
+
callback(err);
|
62
|
+
});
|
63
|
+
}
|
64
|
+
function getUrl(url, callback) {
|
65
|
+
const form = { image_height: 960, image_width: 960, uri: url };
|
66
|
+
defaultFuncs
|
67
|
+
.post("https://www.facebook.com/message_share_attachment/fromURI/", ctx.jar, form)
|
68
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
69
|
+
.then(resData => {
|
70
|
+
if (resData.error) return callback(resData);
|
71
|
+
if (!resData.payload) return callback({ error: "Invalid url" });
|
72
|
+
callback(null, resData.payload.share_data.share_params);
|
73
|
+
})
|
74
|
+
.catch(err => {
|
75
|
+
log.error("getUrl", err);
|
76
|
+
callback(err);
|
77
|
+
});
|
78
|
+
}
|
79
|
+
function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
|
80
|
+
async function postWithRetry(url, jar, form, tries = 3) {
|
81
|
+
let lastErr;
|
82
|
+
for (let i = 0; i < tries; i++) {
|
83
|
+
try {
|
84
|
+
const res = await defaultFuncs.post(url, jar, form).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
|
85
|
+
if (res && !res.error) return res;
|
86
|
+
lastErr = res;
|
87
|
+
if (res && (res.error === 1545003 || res.error === 368)) await sleep(500 * (i + 1));
|
88
|
+
else break;
|
89
|
+
} catch (e) {
|
90
|
+
lastErr = e;
|
91
|
+
await sleep(500 * (i + 1));
|
92
|
+
}
|
93
|
+
}
|
94
|
+
throw lastErr || { error: "Send failed" };
|
95
|
+
}
|
96
|
+
function applyPageAuthor(form, msg) {
|
97
|
+
const pageID = msg && msg.asPage ? msg.asPage : ctx.globalOptions.pageID;
|
98
|
+
if (!pageID) return;
|
99
|
+
form["author"] = "fbid:" + pageID;
|
100
|
+
form["specific_to_list[1]"] = "fbid:" + pageID;
|
101
|
+
form["creator_info[creatorID]"] = ctx.userID;
|
102
|
+
form["creator_info[creatorType]"] = "direct_admin";
|
103
|
+
form["creator_info[labelType]"] = "sent_message";
|
104
|
+
form["creator_info[pageID]"] = pageID;
|
105
|
+
form["request_user_id"] = pageID;
|
106
|
+
form["creator_info[profileURI]"] = "https://www.facebook.com/profile.php?id=" + ctx.userID;
|
107
|
+
}
|
108
|
+
function applyMentions(msg, form) {
|
109
|
+
if (!msg.mentions || !msg.mentions.length) return;
|
110
|
+
const body = typeof msg.body === "string" ? msg.body : "";
|
111
|
+
const emptyChar = "\u200E";
|
112
|
+
form["body"] = emptyChar + body;
|
113
|
+
let searchFrom = 0;
|
114
|
+
msg.mentions.forEach((m, i) => {
|
115
|
+
const tag = String(m.tag || "");
|
116
|
+
const from = typeof m.fromIndex === "number" ? m.fromIndex : searchFrom;
|
117
|
+
const off = Math.max(0, body.indexOf(tag, from));
|
118
|
+
form[`profile_xmd[${i}][offset]`] = off + 1;
|
119
|
+
form[`profile_xmd[${i}][length]`] = tag.length;
|
120
|
+
form[`profile_xmd[${i}][id]`] = m.id || 0;
|
121
|
+
form[`profile_xmd[${i}][type]`] = "p";
|
122
|
+
searchFrom = off + tag.length;
|
123
|
+
});
|
124
|
+
}
|
125
|
+
function finalizeHasAttachment(form) {
|
126
|
+
const keys = ["image_ids", "gif_ids", "file_ids", "video_ids", "audio_ids", "sticker_id", "shareable_attachment[share_params]"];
|
127
|
+
form.has_attachment = keys.some(k => k in form && (Array.isArray(form[k]) ? form[k].length > 0 : !!form[k]));
|
128
|
+
}
|
129
|
+
function sendContent(form, threadID, isSingleUser, messageAndOTID, callback) {
|
130
|
+
if (utils.getType(threadID) === "Array") {
|
131
|
+
for (let i = 0; i < threadID.length; i++) form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
|
132
|
+
form["specific_to_list[" + threadID.length + "]"] = "fbid:" + ctx.userID;
|
133
|
+
form["client_thread_id"] = "root:" + messageAndOTID;
|
134
|
+
log.verbose("sendMessage", "Sending message to multiple users: " + threadID);
|
135
|
+
} else {
|
136
|
+
if (isSingleUser) {
|
137
|
+
form["specific_to_list[0]"] = "fbid:" + threadID;
|
138
|
+
form["specific_to_list[1]"] = "fbid:" + ctx.userID;
|
139
|
+
form["other_user_fbid"] = threadID;
|
140
|
+
} else {
|
141
|
+
form["thread_fbid"] = threadID;
|
142
|
+
}
|
143
|
+
}
|
144
|
+
postWithRetry("https://www.facebook.com/messaging/send/", ctx.jar, form)
|
145
|
+
.then(resData => {
|
146
|
+
if (!resData) return callback({ error: "Send message failed." });
|
147
|
+
if (resData.error) {
|
148
|
+
if (resData.error === 1545012) log.warn("sendMessage", "Got error 1545012. This might mean that you're not part of the conversation " + threadID);
|
149
|
+
else log.error("sendMessage", resData);
|
150
|
+
return callback(resData);
|
151
|
+
}
|
152
|
+
let messageInfo = null;
|
153
|
+
if (resData.payload && Array.isArray(resData.payload.actions)) {
|
154
|
+
for (const v of resData.payload.actions) {
|
155
|
+
if (v && v.message_id) messageInfo = { threadID: v.thread_fbid, messageID: v.message_id, timestamp: v.timestamp };
|
156
|
+
}
|
157
|
+
}
|
158
|
+
callback(null, messageInfo);
|
159
|
+
})
|
160
|
+
.catch(err => {
|
161
|
+
log.error("sendMessage", err);
|
162
|
+
if (utils.getType(err) === "Object" && err.error === "Not logged in.") ctx.loggedIn = false;
|
163
|
+
callback(err);
|
164
|
+
});
|
165
|
+
}
|
166
|
+
function send(form, threadID, messageAndOTID, callback, isGroup) {
|
167
|
+
if (utils.getType(threadID) === "Array") return sendContent(form, threadID, false, messageAndOTID, callback);
|
168
|
+
if (utils.getType(isGroup) !== "Boolean") return sendContent(form, threadID, String(threadID).length === 15, messageAndOTID, callback);
|
169
|
+
return sendContent(form, threadID, !isGroup, messageAndOTID, callback);
|
170
|
+
}
|
171
|
+
function handleUrl(msg, form, callback, cb) {
|
172
|
+
if (msg.url) {
|
173
|
+
form["shareable_attachment[share_type]"] = "100";
|
174
|
+
getUrl(msg.url, function (err, params) {
|
175
|
+
if (err) return callback(err);
|
176
|
+
form["shareable_attachment[share_params]"] = params;
|
177
|
+
cb();
|
178
|
+
});
|
179
|
+
} else cb();
|
180
|
+
}
|
181
|
+
function handleLocation(msg, form, callback, cb) {
|
182
|
+
if (msg.location) {
|
183
|
+
if (msg.location.latitude == null || msg.location.longitude == null) return callback({ error: "location property needs both latitude and longitude" });
|
184
|
+
form["location_attachment[coordinates][latitude]"] = msg.location.latitude;
|
185
|
+
form["location_attachment[coordinates][longitude]"] = msg.location.longitude;
|
186
|
+
form["location_attachment[is_current_location]"] = !!msg.location.current;
|
187
|
+
}
|
188
|
+
cb();
|
189
|
+
}
|
190
|
+
function handleSticker(msg, form, callback, cb) {
|
191
|
+
if (msg.sticker) form["sticker_id"] = msg.sticker;
|
192
|
+
cb();
|
193
|
+
}
|
194
|
+
function handleEmoji(msg, form, callback, cb) {
|
195
|
+
if (msg.emojiSize != null && msg.emoji == null) return callback({ error: "emoji property is empty" });
|
196
|
+
if (msg.emoji) {
|
197
|
+
if (msg.emojiSize == null) msg.emojiSize = "medium";
|
198
|
+
if (msg.emojiSize !== "small" && msg.emojiSize !== "medium" && msg.emojiSize !== "large") return callback({ error: "emojiSize property is invalid" });
|
199
|
+
if (form["body"] != null && form["body"] !== "") return callback({ error: "body is not empty" });
|
200
|
+
form["body"] = msg.emoji;
|
201
|
+
form["tags[0]"] = "hot_emoji_size:" + msg.emojiSize;
|
202
|
+
}
|
203
|
+
cb();
|
204
|
+
}
|
205
|
+
function handleAttachment(msg, form, callback, cb) {
|
206
|
+
if (!msg.attachment) return cb();
|
207
|
+
form["image_ids"] = [];
|
208
|
+
form["gif_ids"] = [];
|
209
|
+
form["file_ids"] = [];
|
210
|
+
form["video_ids"] = [];
|
211
|
+
form["audio_ids"] = [];
|
212
|
+
const items = normalizeAttachments(msg.attachment);
|
213
|
+
if (items.every(e => Array.isArray(e) && /_id$/.test(e[0]))) {
|
214
|
+
items.forEach(e => form[`${e[0]}s`].push(e[1]));
|
215
|
+
return cb();
|
216
|
+
}
|
217
|
+
uploadAttachment(items, function (err, files) {
|
218
|
+
if (err) return callback(err);
|
219
|
+
files.forEach(function (file) {
|
220
|
+
const type = Object.keys(file)[0];
|
221
|
+
form[type + "s"].push(file[type]);
|
222
|
+
});
|
223
|
+
cb();
|
224
|
+
});
|
225
|
+
}
|
226
|
+
function handleMention(msg, form, callback, cb) {
|
227
|
+
try { applyMentions(msg, form); cb(); } catch (e) { callback(e); }
|
228
|
+
}
|
229
|
+
return function sendMessage(msg, threadID, callback, replyToMessage, isGroup) {
|
230
|
+
if (typeof isGroup === "undefined") isGroup = null;
|
231
|
+
if (!callback && (utils.getType(threadID) === "Function" || utils.getType(threadID) === "AsyncFunction")) return threadID({ error: "Pass a threadID as a second argument." });
|
232
|
+
if (!replyToMessage && utils.getType(callback) === "String") {
|
233
|
+
replyToMessage = callback;
|
234
|
+
callback = function () { };
|
235
|
+
}
|
236
|
+
let resolveFunc = function () { };
|
237
|
+
let rejectFunc = function () { };
|
238
|
+
const returnPromise = new Promise(function (resolve, reject) {
|
239
|
+
resolveFunc = resolve;
|
240
|
+
rejectFunc = reject;
|
241
|
+
});
|
242
|
+
if (!callback) {
|
243
|
+
callback = function (err, data) {
|
244
|
+
if (err) return rejectFunc(err);
|
245
|
+
resolveFunc(data);
|
246
|
+
};
|
247
|
+
}
|
248
|
+
const msgType = utils.getType(msg);
|
249
|
+
const threadIDType = utils.getType(threadID);
|
250
|
+
const messageIDType = utils.getType(replyToMessage);
|
251
|
+
if (msgType !== "String" && msgType !== "Object") return callback({ error: "Message should be of type string or object and not " + msgType + "." });
|
252
|
+
if (threadIDType !== "Array" && threadIDType !== "Number" && threadIDType !== "String") return callback({ error: "ThreadID should be of type number, string, or array and not " + threadIDType + "." });
|
253
|
+
if (replyToMessage && messageIDType !== "String") return callback({ error: "MessageID should be of type string and not " + threadIDType + "." });
|
254
|
+
if (msgType === "String") msg = { body: msg };
|
255
|
+
const disallowedProperties = Object.keys(msg).filter(prop => !allowedProperties[prop]);
|
256
|
+
if (disallowedProperties.length > 0) return callback({ error: "Disallowed props: `" + disallowedProperties.join(", ") + "`" });
|
257
|
+
const messageAndOTID = utils.generateOfflineThreadingID();
|
258
|
+
const form = {
|
259
|
+
client: "mercury",
|
260
|
+
action_type: "ma-type:user-generated-message",
|
261
|
+
author: "fbid:" + ctx.userID,
|
262
|
+
timestamp: Date.now(),
|
263
|
+
timestamp_absolute: "Today",
|
264
|
+
timestamp_relative: utils.generateTimestampRelative(),
|
265
|
+
timestamp_time_passed: "0",
|
266
|
+
is_unread: false,
|
267
|
+
is_cleared: false,
|
268
|
+
is_forward: false,
|
269
|
+
is_filtered_content: false,
|
270
|
+
is_filtered_content_bh: false,
|
271
|
+
is_filtered_content_account: false,
|
272
|
+
is_filtered_content_quasar: false,
|
273
|
+
is_filtered_content_invalid_app: false,
|
274
|
+
is_spoof_warning: false,
|
275
|
+
source: "source:chat:web",
|
276
|
+
"source_tags[0]": "source:chat",
|
277
|
+
body: msg.body ? msg.body.toString() : "",
|
278
|
+
html_body: false,
|
279
|
+
ui_push_phase: "V3",
|
280
|
+
status: "0",
|
281
|
+
offline_threading_id: messageAndOTID,
|
282
|
+
message_id: messageAndOTID,
|
283
|
+
threading_id: utils.generateThreadingID(ctx.clientID),
|
284
|
+
ephemeral_ttl_mode: "0",
|
285
|
+
manual_retry_cnt: "0",
|
286
|
+
signatureID: utils.getSignatureID(),
|
287
|
+
replied_to_message_id: replyToMessage
|
288
|
+
};
|
289
|
+
applyPageAuthor(form, msg);
|
290
|
+
handleLocation(msg, form, callback, () =>
|
291
|
+
handleSticker(msg, form, callback, () =>
|
292
|
+
handleAttachment(msg, form, callback, () =>
|
293
|
+
handleUrl(msg, form, callback, () =>
|
294
|
+
handleEmoji(msg, form, callback, () =>
|
295
|
+
handleMention(msg, form, callback, () => {
|
296
|
+
finalizeHasAttachment(form);
|
297
|
+
send(form, threadID, messageAndOTID, callback, isGroup);
|
298
|
+
})
|
299
|
+
)
|
300
|
+
)
|
301
|
+
)
|
302
|
+
)
|
303
|
+
);
|
304
|
+
return returnPromise;
|
305
|
+
};
|
306
|
+
};
|
@@ -0,0 +1,321 @@
|
|
1
|
+
const utils = require("../../utils");
|
2
|
+
var log = require("npmlog");
|
3
|
+
var bluebird = require("bluebird");
|
4
|
+
|
5
|
+
module.exports = function(defaultFuncs, api, ctx) {
|
6
|
+
function uploadAttachment(attachments, callback) {
|
7
|
+
callback = callback || function() {};
|
8
|
+
var uploads = [];
|
9
|
+
|
10
|
+
// create an array of promises
|
11
|
+
for (var i = 0; i < attachments.length; i++) {
|
12
|
+
if (!utils.isReadableStream(attachments[i])) {
|
13
|
+
throw {
|
14
|
+
error:
|
15
|
+
"Attachment should be a readable stream and not " +
|
16
|
+
utils.getType(attachments[i]) +
|
17
|
+
"."
|
18
|
+
};
|
19
|
+
}
|
20
|
+
|
21
|
+
var form = {
|
22
|
+
upload_1024: attachments[i],
|
23
|
+
voice_clip: "true"
|
24
|
+
};
|
25
|
+
|
26
|
+
uploads.push(
|
27
|
+
defaultFuncs
|
28
|
+
.postFormData(
|
29
|
+
"https://upload.facebook.com/ajax/mercury/upload.php",
|
30
|
+
ctx.jar,
|
31
|
+
form,
|
32
|
+
{}
|
33
|
+
)
|
34
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
35
|
+
.then(function(resData) {
|
36
|
+
if (resData.error) {
|
37
|
+
throw resData;
|
38
|
+
}
|
39
|
+
|
40
|
+
// We have to return the data unformatted unless we want to change it
|
41
|
+
// back in sendMessage.
|
42
|
+
return resData.payload.metadata[0];
|
43
|
+
})
|
44
|
+
);
|
45
|
+
}
|
46
|
+
|
47
|
+
// resolve all promises
|
48
|
+
bluebird
|
49
|
+
.all(uploads)
|
50
|
+
.then(function(resData) {
|
51
|
+
callback(null, resData);
|
52
|
+
})
|
53
|
+
.catch(function(err) {
|
54
|
+
log.error("uploadAttachment", err);
|
55
|
+
return callback(err);
|
56
|
+
});
|
57
|
+
}
|
58
|
+
|
59
|
+
let variance = 0;
|
60
|
+
const epoch_id = () =>
|
61
|
+
Math.floor(Date.now() * (4194304 + (variance = (variance + 0.1) % 5)));
|
62
|
+
const emojiSizes = {
|
63
|
+
small: 1,
|
64
|
+
medium: 2,
|
65
|
+
large: 3
|
66
|
+
};
|
67
|
+
|
68
|
+
function handleEmoji(msg, form, callback, cb) {
|
69
|
+
if (msg.emojiSize != null && msg.emoji == null) {
|
70
|
+
return callback({ error: "emoji property is empty" });
|
71
|
+
}
|
72
|
+
if (msg.emoji) {
|
73
|
+
if (!msg.emojiSize) {
|
74
|
+
msg.emojiSize = "small";
|
75
|
+
}
|
76
|
+
if (
|
77
|
+
msg.emojiSize !== "small" &&
|
78
|
+
msg.emojiSize !== "medium" &&
|
79
|
+
msg.emojiSize !== "large" &&
|
80
|
+
(isNaN(msg.emojiSize) || msg.emojiSize < 1 || msg.emojiSize > 3)
|
81
|
+
) {
|
82
|
+
return callback({ error: "emojiSize property is invalid" });
|
83
|
+
}
|
84
|
+
|
85
|
+
form.payload.tasks[0].payload.send_type = 1;
|
86
|
+
form.payload.tasks[0].payload.text = msg.emoji;
|
87
|
+
form.payload.tasks[0].payload.hot_emoji_size = !isNaN(msg.emojiSize)
|
88
|
+
? msg.emojiSize
|
89
|
+
: emojiSizes[msg.emojiSize];
|
90
|
+
}
|
91
|
+
cb();
|
92
|
+
}
|
93
|
+
|
94
|
+
function handleSticker(msg, form, callback, cb) {
|
95
|
+
if (msg.sticker) {
|
96
|
+
form.payload.tasks[0].payload.send_type = 2;
|
97
|
+
form.payload.tasks[0].payload.sticker_id = msg.sticker;
|
98
|
+
}
|
99
|
+
cb();
|
100
|
+
}
|
101
|
+
|
102
|
+
function handleAttachment(msg, form, callback, cb) {
|
103
|
+
if (msg.attachment) {
|
104
|
+
form.payload.tasks[0].payload.send_type = 3;
|
105
|
+
form.payload.tasks[0].payload.attachment_fbids = [];
|
106
|
+
if (form.payload.tasks[0].payload.text == "")
|
107
|
+
form.payload.tasks[0].payload.text = null;
|
108
|
+
if (utils.getType(msg.attachment) !== "Array") {
|
109
|
+
msg.attachment = [msg.attachment];
|
110
|
+
}
|
111
|
+
|
112
|
+
uploadAttachment(msg.attachment, function(err, files) {
|
113
|
+
if (err) {
|
114
|
+
return callback(err);
|
115
|
+
}
|
116
|
+
|
117
|
+
files.forEach(function(file) {
|
118
|
+
var key = Object.keys(file);
|
119
|
+
var type = key[0]; // image_id, file_id, etc
|
120
|
+
form.payload.tasks[0].payload.attachment_fbids.push(file[type]); // push the id
|
121
|
+
});
|
122
|
+
cb();
|
123
|
+
});
|
124
|
+
} else {
|
125
|
+
cb();
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
function handleMention(msg, form, callback, cb) {
|
130
|
+
if (msg.mentions) {
|
131
|
+
form.payload.tasks[0].payload.send_type = 1;
|
132
|
+
|
133
|
+
const arrayIds = [];
|
134
|
+
const arrayOffsets = [];
|
135
|
+
const arrayLengths = [];
|
136
|
+
const mention_types = [];
|
137
|
+
|
138
|
+
for (let i = 0; i < msg.mentions.length; i++) {
|
139
|
+
const mention = msg.mentions[i];
|
140
|
+
|
141
|
+
const tag = mention.tag;
|
142
|
+
if (typeof tag !== "string") {
|
143
|
+
return callback({ error: "Mention tags must be strings." });
|
144
|
+
}
|
145
|
+
|
146
|
+
const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
|
147
|
+
|
148
|
+
if (offset < 0) {
|
149
|
+
log.warn(
|
150
|
+
"handleMention",
|
151
|
+
'Mention for "' + tag + '" not found in message string.'
|
152
|
+
);
|
153
|
+
}
|
154
|
+
|
155
|
+
if (mention.id == null) {
|
156
|
+
log.warn("handleMention", "Mention id should be non-null.");
|
157
|
+
}
|
158
|
+
|
159
|
+
const id = mention.id || 0;
|
160
|
+
arrayIds.push(id);
|
161
|
+
arrayOffsets.push(offset);
|
162
|
+
arrayLengths.push(tag.length);
|
163
|
+
mention_types.push("p");
|
164
|
+
}
|
165
|
+
|
166
|
+
form.payload.tasks[0].payload.mention_data = {
|
167
|
+
mention_ids: arrayIds.join(","),
|
168
|
+
mention_offsets: arrayOffsets.join(","),
|
169
|
+
mention_lengths: arrayLengths.join(","),
|
170
|
+
mention_types: mention_types.join(",")
|
171
|
+
};
|
172
|
+
}
|
173
|
+
cb();
|
174
|
+
}
|
175
|
+
|
176
|
+
function handleLocation(msg, form, callback, cb) {
|
177
|
+
// this is not working yet
|
178
|
+
if (msg.location) {
|
179
|
+
if (msg.location.latitude == null || msg.location.longitude == null) {
|
180
|
+
return callback({
|
181
|
+
error: "location property needs both latitude and longitude"
|
182
|
+
});
|
183
|
+
}
|
184
|
+
|
185
|
+
form.payload.tasks[0].payload.send_type = 1;
|
186
|
+
form.payload.tasks[0].payload.location_data = {
|
187
|
+
coordinates: {
|
188
|
+
latitude: msg.location.latitude,
|
189
|
+
longitude: msg.location.longitude
|
190
|
+
},
|
191
|
+
is_current_location: !!msg.location.current,
|
192
|
+
is_live_location: !!msg.location.live
|
193
|
+
};
|
194
|
+
}
|
195
|
+
|
196
|
+
cb();
|
197
|
+
}
|
198
|
+
|
199
|
+
function send(form, threadID, callback, replyToMessage) {
|
200
|
+
if (replyToMessage) {
|
201
|
+
form.payload.tasks[0].payload.reply_metadata = {
|
202
|
+
reply_source_id: replyToMessage,
|
203
|
+
reply_source_type: 1,
|
204
|
+
reply_type: 0
|
205
|
+
};
|
206
|
+
}
|
207
|
+
const mqttClient = ctx.mqttClient;
|
208
|
+
form.payload.tasks.forEach(task => {
|
209
|
+
task.payload = JSON.stringify(task.payload);
|
210
|
+
});
|
211
|
+
form.payload = JSON.stringify(form.payload);
|
212
|
+
console.log(global.jsonStringifyColor(form, null, 2));
|
213
|
+
|
214
|
+
return mqttClient.publish("/ls_req", JSON.stringify(form), function(
|
215
|
+
err,
|
216
|
+
data
|
217
|
+
) {
|
218
|
+
if (err) {
|
219
|
+
console.error("Error publishing message: ", err);
|
220
|
+
callback(err);
|
221
|
+
} else {
|
222
|
+
console.log("Message published successfully with data: ", data);
|
223
|
+
callback(null, data);
|
224
|
+
}
|
225
|
+
});
|
226
|
+
}
|
227
|
+
|
228
|
+
return function sendMessageMqtt(msg, threadID, callback, replyToMessage) {
|
229
|
+
if (
|
230
|
+
!callback &&
|
231
|
+
(utils.getType(threadID) === "Function" ||
|
232
|
+
utils.getType(threadID) === "AsyncFunction")
|
233
|
+
) {
|
234
|
+
return threadID({ error: "Pass a threadID as a second argument." });
|
235
|
+
}
|
236
|
+
if (!replyToMessage && utils.getType(callback) === "String") {
|
237
|
+
replyToMessage = callback;
|
238
|
+
callback = function() {};
|
239
|
+
}
|
240
|
+
|
241
|
+
if (!callback) {
|
242
|
+
callback = function(err, friendList) {};
|
243
|
+
}
|
244
|
+
|
245
|
+
var msgType = utils.getType(msg);
|
246
|
+
var threadIDType = utils.getType(threadID);
|
247
|
+
var messageIDType = utils.getType(replyToMessage);
|
248
|
+
|
249
|
+
if (msgType !== "String" && msgType !== "Object") {
|
250
|
+
return callback({
|
251
|
+
error:
|
252
|
+
"Message should be of type string or object and not " + msgType + "."
|
253
|
+
});
|
254
|
+
}
|
255
|
+
|
256
|
+
if (msgType === "String") {
|
257
|
+
msg = { body: msg };
|
258
|
+
}
|
259
|
+
|
260
|
+
const timestamp = Date.now();
|
261
|
+
// get full date time
|
262
|
+
const epoch = timestamp << 22;
|
263
|
+
//const otid = epoch + 0; // TODO replace with randomInt(0, 2**22)
|
264
|
+
const otid = epoch + Math.floor(Math.random() * 4194304);
|
265
|
+
|
266
|
+
const form = {
|
267
|
+
app_id: "2220391788200892",
|
268
|
+
payload: {
|
269
|
+
tasks: [
|
270
|
+
{
|
271
|
+
label: "46",
|
272
|
+
payload: {
|
273
|
+
thread_id: threadID.toString(),
|
274
|
+
otid: otid.toString(),
|
275
|
+
source: 0,
|
276
|
+
send_type: 1,
|
277
|
+
sync_group: 1,
|
278
|
+
text:
|
279
|
+
msg.body != null && msg.body != undefined
|
280
|
+
? msg.body.toString()
|
281
|
+
: "",
|
282
|
+
initiating_source: 1,
|
283
|
+
skip_url_preview_gen: 0
|
284
|
+
},
|
285
|
+
queue_name: threadID.toString(),
|
286
|
+
task_id: 0,
|
287
|
+
failure_count: null
|
288
|
+
},
|
289
|
+
{
|
290
|
+
label: "21",
|
291
|
+
payload: {
|
292
|
+
thread_id: threadID.toString(),
|
293
|
+
last_read_watermark_ts: Date.now(),
|
294
|
+
sync_group: 1
|
295
|
+
},
|
296
|
+
queue_name: threadID.toString(),
|
297
|
+
task_id: 1,
|
298
|
+
failure_count: null
|
299
|
+
}
|
300
|
+
],
|
301
|
+
epoch_id: epoch_id(),
|
302
|
+
version_id: "6120284488008082",
|
303
|
+
data_trace_id: null
|
304
|
+
},
|
305
|
+
request_id: 1,
|
306
|
+
type: 3
|
307
|
+
};
|
308
|
+
|
309
|
+
handleEmoji(msg, form, callback, function() {
|
310
|
+
handleLocation(msg, form, callback, function() {
|
311
|
+
handleMention(msg, form, callback, function() {
|
312
|
+
handleSticker(msg, form, callback, function() {
|
313
|
+
handleAttachment(msg, form, callback, function() {
|
314
|
+
send(form, threadID, callback, replyToMessage);
|
315
|
+
});
|
316
|
+
});
|
317
|
+
});
|
318
|
+
});
|
319
|
+
});
|
320
|
+
};
|
321
|
+
};
|