@vangbanlanhat/fca-unofficial 1.4.2
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 +2 -0
- package/.github/workflows/nodejs.yml +26 -0
- package/.github/workflows/npmpublish.yml +30 -0
- package/.travis.yml +6 -0
- package/CHANGELOG.md +2 -0
- package/DOCS.md +1932 -0
- package/LICENSE-MIT +21 -0
- package/README.md +198 -0
- package/jest.config.js +10 -0
- package/package.json +86 -0
- package/pnpm-workspace.yaml +3 -0
- package/src/actions/addExternalModule.js +34 -0
- package/src/actions/addUserToGroup.js +113 -0
- package/src/actions/changeAdminStatus.js +79 -0
- package/src/actions/changeArchivedStatus.js +55 -0
- package/src/actions/changeBio.js +77 -0
- package/src/actions/changeBlockedStatus.js +47 -0
- package/src/actions/changeGroupImage.js +129 -0
- package/src/actions/changeNickname.js +59 -0
- package/src/actions/changeThreadColor.js +71 -0
- package/src/actions/changeThreadEmoji.js +55 -0
- package/src/actions/createNewGroup.js +86 -0
- package/src/actions/createPoll.js +71 -0
- package/src/actions/deleteMessage.js +56 -0
- package/src/actions/deleteThread.js +56 -0
- package/src/actions/editMessage.js +81 -0
- package/src/actions/forwardAttachment.js +60 -0
- package/src/actions/getAvatarUser.js +77 -0
- package/src/actions/getCurrentUserID.js +11 -0
- package/src/actions/getEmojiUrl.js +37 -0
- package/src/actions/getFriendsList.js +84 -0
- package/src/actions/getMessage.js +833 -0
- package/src/actions/getRegion.js +11 -0
- package/src/actions/getThreadHistory.js +702 -0
- package/src/actions/getThreadHistoryDeprecated.js +93 -0
- package/src/actions/getThreadInfo.js +265 -0
- package/src/actions/getThreadInfoDeprecated.js +80 -0
- package/src/actions/getThreadList.js +274 -0
- package/src/actions/getThreadListDeprecated.js +91 -0
- package/src/actions/getThreadPictures.js +79 -0
- package/src/actions/getUserID.js +66 -0
- package/src/actions/getUserInfo.js +92 -0
- package/src/actions/handleFriendRequest.js +61 -0
- package/src/actions/handleMessageRequest.js +65 -0
- package/src/actions/httpGet.js +70 -0
- package/src/actions/httpPost.js +70 -0
- package/src/actions/index.js +69 -0
- package/src/actions/listenMqtt.js +818 -0
- package/src/actions/logout.js +136 -0
- package/src/actions/markAsDelivered.js +58 -0
- package/src/actions/markAsRead.js +92 -0
- package/src/actions/markAsReadAll.js +50 -0
- package/src/actions/markAsSeen.js +59 -0
- package/src/actions/muteThread.js +52 -0
- package/src/actions/pinMessage.js +86 -0
- package/src/actions/refreshFb_dtsg.js +89 -0
- package/src/actions/removeUserFromGroup.js +79 -0
- package/src/actions/resolvePhotoUrl.js +45 -0
- package/src/actions/searchForThread.js +53 -0
- package/src/actions/searchStickers.js +53 -0
- package/src/actions/sendMessage.js +490 -0
- package/src/actions/sendMessageMqtt.js +235 -0
- package/src/actions/sendTypingIndicator.js +59 -0
- package/src/actions/setMessageReaction.js +140 -0
- package/src/actions/setMessageReactionMqtt.js +72 -0
- package/src/actions/setPostReaction.js +76 -0
- package/src/actions/setTitle.js +86 -0
- package/src/actions/stopListenMqtt.js +63 -0
- package/src/actions/threadColors.js +57 -0
- package/src/actions/unfriend.js +52 -0
- package/src/actions/unsendMessage.js +61 -0
- package/src/index.js +659 -0
- package/src/service/uploadAttachment.js +318 -0
- package/src/utils/base-parts/auth.js +286 -0
- package/src/utils/base-parts/formatters.js +751 -0
- package/src/utils/base-parts/identity.js +169 -0
- package/src/utils/base-parts/network.js +159 -0
- package/src/utils/base-parts/parsing.js +53 -0
- package/src/utils/base-parts/type.js +9 -0
- package/src/utils/base.js +18 -0
- package/src/utils/defaults.js +10 -0
- package/src/utils/format-attachments.js +7 -0
- package/src/utils/format-core.js +8 -0
- package/src/utils/format-events.js +11 -0
- package/src/utils/format-messages.js +10 -0
- package/src/utils/format-presence.js +9 -0
- package/src/utils/format-threads.js +8 -0
- package/src/utils/formatters.js +18 -0
- package/src/utils/identity.js +12 -0
- package/src/utils/index.js +15 -0
- package/src/utils/network.js +12 -0
- package/src/utils/parsing.js +11 -0
- package/src/utils.js +3 -0
- package/test/config.env.example +30 -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-appstate.txt +11 -0
- package/test/integration/actions.appstate.integration.test.js +233 -0
- package/test/integration/api.integration.test.js +387 -0
- package/test/integration/env-test-config.js +46 -0
- package/test/integration/login.appstate.integration.test.js +47 -0
- package/test/integration/page.integration.test.js +139 -0
- package/test/jest.setup.js +54 -0
- package/test/unit/actions/actions.basic.test.js +116 -0
- package/test/unit/actions/actions.registry.test.js +36 -0
- package/test/unit/index/index.test.js +109 -0
- package/test/unit/utils/base-parts/auth.test.js +92 -0
- package/test/unit/utils/base-parts/formatters.test.js +53 -0
- package/test/unit/utils/base-parts/identity.test.js +43 -0
- package/test/unit/utils/base-parts/parsing.test.js +38 -0
- package/test/unit/utils/base-parts/type.test.js +23 -0
- package/test/unit/utils/base.test.js +35 -0
- package/vangbanlanhat-fca-unofficial-1.4.2.tgz +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var utils = require("../utils");
|
|
4
|
+
var log = require("npmlog");
|
|
5
|
+
|
|
6
|
+
module.exports = function(defaultFuncs, api, ctx) {
|
|
7
|
+
return function resolvePhotoUrl(photoID, callback) {
|
|
8
|
+
var resolveFunc = function(){};
|
|
9
|
+
var rejectFunc = function(){};
|
|
10
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
|
11
|
+
resolveFunc = resolve;
|
|
12
|
+
rejectFunc = reject;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
if (!callback) {
|
|
16
|
+
callback = function (err, friendList) {
|
|
17
|
+
if (err) {
|
|
18
|
+
return rejectFunc(err);
|
|
19
|
+
}
|
|
20
|
+
resolveFunc(friendList);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
defaultFuncs
|
|
25
|
+
.get("https://www.facebook.com/mercury/attachments/photo", ctx.jar, {
|
|
26
|
+
photo_id: photoID
|
|
27
|
+
})
|
|
28
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
|
29
|
+
.then(resData => {
|
|
30
|
+
if (resData.error) {
|
|
31
|
+
throw resData;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var photoUrl = resData.jsmods.require[0][3][0];
|
|
35
|
+
|
|
36
|
+
return callback(null, photoUrl);
|
|
37
|
+
})
|
|
38
|
+
.catch(err => {
|
|
39
|
+
log.error("resolvePhotoUrl", err);
|
|
40
|
+
return callback(err);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return returnPromise;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var utils = require("../utils");
|
|
4
|
+
|
|
5
|
+
module.exports = function(defaultFuncs, api, ctx) {
|
|
6
|
+
return function searchForThread(name, callback) {
|
|
7
|
+
var resolveFunc = function(){};
|
|
8
|
+
var rejectFunc = function(){};
|
|
9
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
|
10
|
+
resolveFunc = resolve;
|
|
11
|
+
rejectFunc = reject;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if (!callback) {
|
|
15
|
+
callback = function (err, friendList) {
|
|
16
|
+
if (err) {
|
|
17
|
+
return rejectFunc(err);
|
|
18
|
+
}
|
|
19
|
+
resolveFunc(friendList);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var tmpForm = {
|
|
24
|
+
client: "web_messenger",
|
|
25
|
+
query: name,
|
|
26
|
+
offset: 0,
|
|
27
|
+
limit: 21,
|
|
28
|
+
index: "fbid"
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
defaultFuncs
|
|
32
|
+
.post(
|
|
33
|
+
"https://www.facebook.com/ajax/mercury/search_threads.php",
|
|
34
|
+
ctx.jar,
|
|
35
|
+
tmpForm
|
|
36
|
+
)
|
|
37
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
|
38
|
+
.then(function(resData) {
|
|
39
|
+
if (resData.error) {
|
|
40
|
+
throw resData;
|
|
41
|
+
}
|
|
42
|
+
if (!resData.payload.mercury_payload.threads) {
|
|
43
|
+
return callback({ error: "Could not find thread `" + name + "`." });
|
|
44
|
+
}
|
|
45
|
+
return callback(
|
|
46
|
+
null,
|
|
47
|
+
resData.payload.mercury_payload.threads.map(utils.formatThread)
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return returnPromise;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var utils = require('../utils.js');
|
|
5
|
+
var log = require('npmlog');
|
|
6
|
+
|
|
7
|
+
module.exports = function (http, api, ctx) {
|
|
8
|
+
function formatData(res) {
|
|
9
|
+
return {
|
|
10
|
+
id: res.node.id,
|
|
11
|
+
image: res.node.image,
|
|
12
|
+
package: res.node.pack != null ? {
|
|
13
|
+
name: res.node.pack.name,
|
|
14
|
+
id: res.node.pack.id
|
|
15
|
+
} : {},
|
|
16
|
+
label: res.node.label
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return function searchStickers(query = '', callback) {
|
|
21
|
+
var cb;
|
|
22
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
|
23
|
+
cb = function (error, data) {
|
|
24
|
+
data ? resolve(data) : reject(error);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (typeof callback == 'function') cb = callback;
|
|
29
|
+
|
|
30
|
+
var form = {
|
|
31
|
+
fb_api_req_friendly_name: 'StickersFlyoutTagSelectorQuery',
|
|
32
|
+
variables: JSON.stringify({
|
|
33
|
+
stickerWidth: 64,
|
|
34
|
+
stickerHeight: 64,
|
|
35
|
+
stickerInterface: 'messages',
|
|
36
|
+
query
|
|
37
|
+
}),
|
|
38
|
+
doc_id: '4642836929159953'
|
|
39
|
+
}
|
|
40
|
+
http
|
|
41
|
+
.post('https://www.facebook.com/api/graphql/', ctx.jar, form)
|
|
42
|
+
.then(utils.parseAndCheckLogin(ctx, http))
|
|
43
|
+
.then(function (res) {
|
|
44
|
+
return cb(null, res.data.sticker_search.sticker_results.edges.map(formatData));
|
|
45
|
+
})
|
|
46
|
+
.catch(function (err) {
|
|
47
|
+
log.error('searchStickers', err);
|
|
48
|
+
return cb(err);
|
|
49
|
+
});
|
|
50
|
+
return returnPromise;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var utils = require("../utils");
|
|
4
|
+
var log = require("npmlog");
|
|
5
|
+
var path = require("path");
|
|
6
|
+
|
|
7
|
+
var createUploadAttachment = require("../service/uploadAttachment");
|
|
8
|
+
|
|
9
|
+
var allowedProperties = {
|
|
10
|
+
attachment: true,
|
|
11
|
+
url: true,
|
|
12
|
+
sticker: true,
|
|
13
|
+
emoji: true,
|
|
14
|
+
emojiSize: true,
|
|
15
|
+
body: true,
|
|
16
|
+
mentions: true,
|
|
17
|
+
location: true,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
21
|
+
var sendMessageUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3";
|
|
22
|
+
|
|
23
|
+
var uploadAttachment = createUploadAttachment(defaultFuncs, ctx, log);
|
|
24
|
+
|
|
25
|
+
function streamToBuffer(stream) {
|
|
26
|
+
return new Promise(function (resolve, reject) {
|
|
27
|
+
var chunks = [];
|
|
28
|
+
stream.on("data", function (chunk) {
|
|
29
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
30
|
+
});
|
|
31
|
+
stream.on("end", function () {
|
|
32
|
+
resolve(Buffer.concat(chunks));
|
|
33
|
+
});
|
|
34
|
+
stream.on("error", reject);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function detectMediaFromAttachment(attachment) {
|
|
39
|
+
var filename = attachment && typeof attachment.path === "string"
|
|
40
|
+
? path.basename(attachment.path)
|
|
41
|
+
: "file.bin";
|
|
42
|
+
var lower = filename.toLowerCase();
|
|
43
|
+
|
|
44
|
+
if (/\.(jpe?g|png|gif|webp)$/.test(lower)) {
|
|
45
|
+
return { mediaType: "image", filename: filename };
|
|
46
|
+
}
|
|
47
|
+
if (/\.(mp4|mov|mkv|webm)$/.test(lower)) {
|
|
48
|
+
return { mediaType: "video", filename: filename };
|
|
49
|
+
}
|
|
50
|
+
if (/\.(mp3|m4a|aac|ogg|wav|flac)$/.test(lower)) {
|
|
51
|
+
return { mediaType: "audio", filename: filename };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { mediaType: "document", filename: filename };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function getUrl(url, callback) {
|
|
58
|
+
var form = {
|
|
59
|
+
image_height: 960,
|
|
60
|
+
image_width: 960,
|
|
61
|
+
uri: url
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
defaultFuncs
|
|
65
|
+
.post(
|
|
66
|
+
"https://www.facebook.com/message_share_attachment/fromURI/",
|
|
67
|
+
ctx.jar,
|
|
68
|
+
form
|
|
69
|
+
)
|
|
70
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
|
71
|
+
.then(function (resData) {
|
|
72
|
+
if (resData.error) {
|
|
73
|
+
return callback(resData);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!resData.payload) {
|
|
77
|
+
return callback({ error: "Invalid url" });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
callback(null, resData.payload.share_data.share_params);
|
|
81
|
+
})
|
|
82
|
+
.catch(function (err) {
|
|
83
|
+
log.error("getUrl", err);
|
|
84
|
+
return callback(err);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function sendContent(form, threadID, isSingleUser, messageAndOTID, callback) {
|
|
89
|
+
// There are three cases here:
|
|
90
|
+
//threadID is of type array, where we're starting a new group chat with users
|
|
91
|
+
// specified in the array.
|
|
92
|
+
//User is sending a message to a specific user.
|
|
93
|
+
// No additional form params and the message goes to an existing group chat.
|
|
94
|
+
if (utils.getType(threadID) === "Array") {
|
|
95
|
+
for (var i = 0; i < threadID.length; i++) {
|
|
96
|
+
form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
|
|
97
|
+
}
|
|
98
|
+
form["specific_to_list[" + threadID.length + "]"] = "fbid:" + ctx.userID;
|
|
99
|
+
form["client_thread_id"] = "root:" + messageAndOTID;
|
|
100
|
+
log.info("sendMessage", "Sending message to multiple users: " + threadID);
|
|
101
|
+
} else {
|
|
102
|
+
// This means that threadID is the id of a user, and the chat
|
|
103
|
+
// is a single person chat
|
|
104
|
+
if (isSingleUser) {
|
|
105
|
+
form["specific_to_list[0]"] = "fbid:" + threadID;
|
|
106
|
+
form["specific_to_list[1]"] = "fbid:" + ctx.userID;
|
|
107
|
+
form["other_user_fbid"] = threadID;
|
|
108
|
+
} else {
|
|
109
|
+
form["thread_fbid"] = threadID;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (ctx.globalOptions.pageID) {
|
|
114
|
+
form["author"] = "fbid:" + ctx.globalOptions.pageID;
|
|
115
|
+
form["specific_to_list[1]"] = "fbid:" + ctx.globalOptions.pageID;
|
|
116
|
+
form["creator_info[creatorID]"] = ctx.userID;
|
|
117
|
+
form["creator_info[creatorType]"] = "direct_admin";
|
|
118
|
+
form["creator_info[labelType]"] = "sent_message";
|
|
119
|
+
form["creator_info[pageID]"] = ctx.globalOptions.pageID;
|
|
120
|
+
form["request_user_id"] = ctx.globalOptions.pageID;
|
|
121
|
+
form["creator_info[profileURI]"] =
|
|
122
|
+
"https://www.facebook.com/profile.php?id=" + ctx.userID;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
defaultFuncs
|
|
126
|
+
.post(
|
|
127
|
+
"https://www.facebook.com/messaging/send/",
|
|
128
|
+
ctx.jar,
|
|
129
|
+
form,
|
|
130
|
+
null,
|
|
131
|
+
{
|
|
132
|
+
customUserAgent: sendMessageUserAgent,
|
|
133
|
+
Referer: "https://www.facebook.com/",
|
|
134
|
+
Origin: "https://www.facebook.com",
|
|
135
|
+
Connection: "keep-alive",
|
|
136
|
+
"Sec-Fetch-Site": "same-origin",
|
|
137
|
+
"Sec-Fetch-User": "?1"
|
|
138
|
+
}
|
|
139
|
+
)
|
|
140
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
|
141
|
+
.then(function (resData) {
|
|
142
|
+
if (!resData) {
|
|
143
|
+
return callback({ error: "Send message failed." });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (resData.error) {
|
|
147
|
+
if (resData.error === 1545012) {
|
|
148
|
+
log.warn(
|
|
149
|
+
"sendMessage",
|
|
150
|
+
"Got error 1545012. This might mean that you're not part of the conversation " +
|
|
151
|
+
threadID
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return callback(resData);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
var messageInfo = resData.payload.actions.reduce(function (p, v) {
|
|
158
|
+
return (
|
|
159
|
+
{
|
|
160
|
+
threadID: v.thread_fbid,
|
|
161
|
+
messageID: v.message_id,
|
|
162
|
+
timestamp: v.timestamp
|
|
163
|
+
} || p
|
|
164
|
+
);
|
|
165
|
+
}, null);
|
|
166
|
+
|
|
167
|
+
return callback(null, messageInfo);
|
|
168
|
+
})
|
|
169
|
+
.catch(function (err) {
|
|
170
|
+
log.error("sendMessage", err);
|
|
171
|
+
if (utils.getType(err) == "Object" && err.error === "Not logged in.") {
|
|
172
|
+
ctx.loggedIn = false;
|
|
173
|
+
}
|
|
174
|
+
return callback(err);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function send(form, threadID, messageAndOTID, callback, isGroup) {
|
|
179
|
+
// We're doing a query to this to check if the given id is the id of
|
|
180
|
+
// a user or of a group chat. The form will be different depending
|
|
181
|
+
// on that.
|
|
182
|
+
if (utils.getType(threadID) === "Array") {
|
|
183
|
+
sendContent(form, threadID, false, messageAndOTID, callback);
|
|
184
|
+
} else {
|
|
185
|
+
if (utils.getType(isGroup) != "Boolean") {
|
|
186
|
+
// Match ws3 behavior: avoid extra lookup call that can fail on some sessions.
|
|
187
|
+
sendContent(form, threadID, threadID.length <= 15, messageAndOTID, callback);
|
|
188
|
+
} else {
|
|
189
|
+
sendContent(form, threadID, !isGroup, messageAndOTID, callback);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function handleUrl(msg, form, callback, cb) {
|
|
195
|
+
if (msg.url) {
|
|
196
|
+
form["shareable_attachment[share_type]"] = "100";
|
|
197
|
+
getUrl(msg.url, function (err, params) {
|
|
198
|
+
if (err) {
|
|
199
|
+
return callback(err);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
form["shareable_attachment[share_params]"] = params;
|
|
203
|
+
cb();
|
|
204
|
+
});
|
|
205
|
+
} else {
|
|
206
|
+
cb();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function handleLocation(msg, form, callback, cb) {
|
|
211
|
+
if (msg.location) {
|
|
212
|
+
if (msg.location.latitude == null || msg.location.longitude == null) {
|
|
213
|
+
return callback({ error: "location property needs both latitude and longitude" });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
form["location_attachment[coordinates][latitude]"] = msg.location.latitude;
|
|
217
|
+
form["location_attachment[coordinates][longitude]"] = msg.location.longitude;
|
|
218
|
+
form["location_attachment[is_current_location]"] = !!msg.location.current;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
cb();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function handleSticker(msg, form, callback, cb) {
|
|
225
|
+
if (msg.sticker) {
|
|
226
|
+
form["sticker_id"] = msg.sticker;
|
|
227
|
+
}
|
|
228
|
+
cb();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function handleEmoji(msg, form, callback, cb) {
|
|
232
|
+
if (msg.emojiSize != null && msg.emoji == null) {
|
|
233
|
+
return callback({ error: "emoji property is empty" });
|
|
234
|
+
}
|
|
235
|
+
if (msg.emoji) {
|
|
236
|
+
if (msg.emojiSize == null) {
|
|
237
|
+
msg.emojiSize = "medium";
|
|
238
|
+
}
|
|
239
|
+
if (
|
|
240
|
+
msg.emojiSize != "small" &&
|
|
241
|
+
msg.emojiSize != "medium" &&
|
|
242
|
+
msg.emojiSize != "large"
|
|
243
|
+
) {
|
|
244
|
+
return callback({ error: "emojiSize property is invalid" });
|
|
245
|
+
}
|
|
246
|
+
if (form["body"] != null && form["body"] != "") {
|
|
247
|
+
return callback({ error: "body is not empty" });
|
|
248
|
+
}
|
|
249
|
+
form["body"] = msg.emoji;
|
|
250
|
+
form["tags[0]"] = "hot_emoji_size:" + msg.emojiSize;
|
|
251
|
+
}
|
|
252
|
+
cb();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function handleAttachment(msg, form, callback, cb) {
|
|
256
|
+
if (msg.attachment) {
|
|
257
|
+
form["image_ids"] = [];
|
|
258
|
+
form["gif_ids"] = [];
|
|
259
|
+
form["file_ids"] = [];
|
|
260
|
+
form["video_ids"] = [];
|
|
261
|
+
form["audio_ids"] = [];
|
|
262
|
+
|
|
263
|
+
if (utils.getType(msg.attachment) !== "Array") {
|
|
264
|
+
msg.attachment = [msg.attachment];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
uploadAttachment(msg.attachment, function (err, files) {
|
|
268
|
+
if (err) {
|
|
269
|
+
return callback(err);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
files.forEach(function (file) {
|
|
273
|
+
if (!file || typeof file !== "object") {
|
|
274
|
+
log.warn("uploadAttachment", "Skipping empty attachment metadata entry.");
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
var type = ["image_id", "gif_id", "file_id", "video_id", "audio_id"].find(
|
|
278
|
+
function (candidate) {
|
|
279
|
+
return file[candidate] != null;
|
|
280
|
+
}
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
if (!type) {
|
|
284
|
+
var key = Object.keys(file);
|
|
285
|
+
if (!key.length) {
|
|
286
|
+
log.warn("uploadAttachment", "Skipping attachment metadata entry without id key.");
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
type = key[0]; // fallback for legacy/unknown metadata shape
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (!Array.isArray(form["" + type + "s"])) {
|
|
293
|
+
log.warn("uploadAttachment", "Unsupported attachment id type: " + type);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (file[type] == null) {
|
|
298
|
+
log.warn("uploadAttachment", "Skipping attachment metadata entry with empty id value.");
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
form["" + type + "s"].push(file[type]); // push the id
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
var uploadedIdCount =
|
|
306
|
+
form["image_ids"].length +
|
|
307
|
+
form["gif_ids"].length +
|
|
308
|
+
form["file_ids"].length +
|
|
309
|
+
form["video_ids"].length +
|
|
310
|
+
form["audio_ids"].length;
|
|
311
|
+
|
|
312
|
+
if (uploadedIdCount === 0) {
|
|
313
|
+
return callback({ error: "Attachment upload failed: no attachment id returned." });
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
cb();
|
|
317
|
+
});
|
|
318
|
+
} else {
|
|
319
|
+
cb();
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function handleMention(msg, form, callback, cb) {
|
|
324
|
+
if (msg.mentions) {
|
|
325
|
+
for (let i = 0; i < msg.mentions.length; i++) {
|
|
326
|
+
const mention = msg.mentions[i];
|
|
327
|
+
|
|
328
|
+
const tag = mention.tag;
|
|
329
|
+
if (typeof tag !== "string") {
|
|
330
|
+
return callback({ error: "Mention tags must be strings." });
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
|
|
334
|
+
|
|
335
|
+
if (offset < 0) {
|
|
336
|
+
log.warn(
|
|
337
|
+
"handleMention",
|
|
338
|
+
'Mention for "' + tag + '" not found in message string.'
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (mention.id == null) {
|
|
343
|
+
log.warn("handleMention", "Mention id should be non-null.");
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const id = mention.id || 0;
|
|
347
|
+
const emptyChar = "\u200E";
|
|
348
|
+
form["body"] = emptyChar + msg.body;
|
|
349
|
+
form["profile_xmd[" + i + "][offset]"] = offset + 1;
|
|
350
|
+
form["profile_xmd[" + i + "][length]"] = tag.length;
|
|
351
|
+
form["profile_xmd[" + i + "][id]"] = id;
|
|
352
|
+
form["profile_xmd[" + i + "][type]"] = "p";
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
cb();
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return function sendMessage(msg, threadID, callback, replyToMessage, isGroup) {
|
|
359
|
+
typeof isGroup == "undefined" ? isGroup = null : "";
|
|
360
|
+
if (
|
|
361
|
+
!callback &&
|
|
362
|
+
(utils.getType(threadID) === "Function" ||
|
|
363
|
+
utils.getType(threadID) === "AsyncFunction")
|
|
364
|
+
) {
|
|
365
|
+
return threadID({ error: "Pass a threadID as a second argument." });
|
|
366
|
+
}
|
|
367
|
+
if (
|
|
368
|
+
!replyToMessage &&
|
|
369
|
+
utils.getType(callback) === "String"
|
|
370
|
+
) {
|
|
371
|
+
replyToMessage = callback;
|
|
372
|
+
callback = function () { };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
var resolveFunc = function(){};
|
|
376
|
+
var rejectFunc = function(){};
|
|
377
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
|
378
|
+
resolveFunc = resolve;
|
|
379
|
+
rejectFunc = reject;
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
if (!callback) {
|
|
383
|
+
callback = function (err, friendList) {
|
|
384
|
+
if (err) {
|
|
385
|
+
return rejectFunc(err);
|
|
386
|
+
}
|
|
387
|
+
resolveFunc(friendList);
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
var msgType = utils.getType(msg);
|
|
392
|
+
var threadIDType = utils.getType(threadID);
|
|
393
|
+
var messageIDType = utils.getType(replyToMessage);
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
if (msgType !== "String" && msgType !== "Object") {
|
|
397
|
+
return callback({
|
|
398
|
+
error:
|
|
399
|
+
"Message should be of type string or object and not " + msgType + "."
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Changing this to accomodate an array of users
|
|
404
|
+
if (
|
|
405
|
+
threadIDType !== "Array" &&
|
|
406
|
+
threadIDType !== "Number" &&
|
|
407
|
+
threadIDType !== "String"
|
|
408
|
+
) {
|
|
409
|
+
return callback({
|
|
410
|
+
error:
|
|
411
|
+
"ThreadID should be of type number, string, or array and not " +
|
|
412
|
+
threadIDType +
|
|
413
|
+
"."
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (replyToMessage && messageIDType !== 'String') {
|
|
418
|
+
return callback({
|
|
419
|
+
error:
|
|
420
|
+
"MessageID should be of type string and not " +
|
|
421
|
+
threadIDType +
|
|
422
|
+
"."
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (msgType === "String") {
|
|
427
|
+
msg = { body: msg };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
var disallowedProperties = Object.keys(msg).filter(
|
|
431
|
+
prop => !allowedProperties[prop]
|
|
432
|
+
);
|
|
433
|
+
if (disallowedProperties.length > 0) {
|
|
434
|
+
return callback({
|
|
435
|
+
error: "Dissallowed props: `" + disallowedProperties.join(", ") + "`"
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
var messageAndOTID = utils.generateOfflineThreadingID();
|
|
440
|
+
|
|
441
|
+
var form = {
|
|
442
|
+
client: "mercury",
|
|
443
|
+
action_type: "ma-type:user-generated-message",
|
|
444
|
+
author: "fbid:" + ctx.userID,
|
|
445
|
+
timestamp: Date.now(),
|
|
446
|
+
timestamp_absolute: "Today",
|
|
447
|
+
timestamp_relative: utils.generateTimestampRelative(),
|
|
448
|
+
timestamp_time_passed: "0",
|
|
449
|
+
is_unread: false,
|
|
450
|
+
is_cleared: false,
|
|
451
|
+
is_forward: false,
|
|
452
|
+
is_filtered_content: false,
|
|
453
|
+
is_filtered_content_bh: false,
|
|
454
|
+
is_filtered_content_account: false,
|
|
455
|
+
is_filtered_content_quasar: false,
|
|
456
|
+
is_filtered_content_invalid_app: false,
|
|
457
|
+
is_spoof_warning: false,
|
|
458
|
+
source: "source:chat:web",
|
|
459
|
+
"source_tags[0]": "source:chat",
|
|
460
|
+
body: msg.body ? msg.body.toString() : "",
|
|
461
|
+
html_body: false,
|
|
462
|
+
ui_push_phase: "V3",
|
|
463
|
+
status: "0",
|
|
464
|
+
offline_threading_id: messageAndOTID,
|
|
465
|
+
message_id: messageAndOTID,
|
|
466
|
+
threading_id: utils.generateThreadingID(ctx.clientID),
|
|
467
|
+
"ephemeral_ttl_mode:": "0",
|
|
468
|
+
manual_retry_cnt: "0",
|
|
469
|
+
has_attachment: !!(msg.attachment || msg.url || msg.sticker),
|
|
470
|
+
signatureID: utils.getSignatureID(),
|
|
471
|
+
replied_to_message_id: replyToMessage
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
handleLocation(msg, form, callback, () =>
|
|
475
|
+
handleSticker(msg, form, callback, () =>
|
|
476
|
+
handleAttachment(msg, form, callback, () =>
|
|
477
|
+
handleUrl(msg, form, callback, () =>
|
|
478
|
+
handleEmoji(msg, form, callback, () =>
|
|
479
|
+
handleMention(msg, form, callback, () =>
|
|
480
|
+
send(form, threadID, messageAndOTID, callback, isGroup)
|
|
481
|
+
)
|
|
482
|
+
)
|
|
483
|
+
)
|
|
484
|
+
)
|
|
485
|
+
)
|
|
486
|
+
);
|
|
487
|
+
|
|
488
|
+
return returnPromise;
|
|
489
|
+
};
|
|
490
|
+
};
|