@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.
Files changed (115) hide show
  1. package/.gitattributes +2 -0
  2. package/.github/workflows/nodejs.yml +26 -0
  3. package/.github/workflows/npmpublish.yml +30 -0
  4. package/.travis.yml +6 -0
  5. package/CHANGELOG.md +2 -0
  6. package/DOCS.md +1932 -0
  7. package/LICENSE-MIT +21 -0
  8. package/README.md +198 -0
  9. package/jest.config.js +10 -0
  10. package/package.json +86 -0
  11. package/pnpm-workspace.yaml +3 -0
  12. package/src/actions/addExternalModule.js +34 -0
  13. package/src/actions/addUserToGroup.js +113 -0
  14. package/src/actions/changeAdminStatus.js +79 -0
  15. package/src/actions/changeArchivedStatus.js +55 -0
  16. package/src/actions/changeBio.js +77 -0
  17. package/src/actions/changeBlockedStatus.js +47 -0
  18. package/src/actions/changeGroupImage.js +129 -0
  19. package/src/actions/changeNickname.js +59 -0
  20. package/src/actions/changeThreadColor.js +71 -0
  21. package/src/actions/changeThreadEmoji.js +55 -0
  22. package/src/actions/createNewGroup.js +86 -0
  23. package/src/actions/createPoll.js +71 -0
  24. package/src/actions/deleteMessage.js +56 -0
  25. package/src/actions/deleteThread.js +56 -0
  26. package/src/actions/editMessage.js +81 -0
  27. package/src/actions/forwardAttachment.js +60 -0
  28. package/src/actions/getAvatarUser.js +77 -0
  29. package/src/actions/getCurrentUserID.js +11 -0
  30. package/src/actions/getEmojiUrl.js +37 -0
  31. package/src/actions/getFriendsList.js +84 -0
  32. package/src/actions/getMessage.js +833 -0
  33. package/src/actions/getRegion.js +11 -0
  34. package/src/actions/getThreadHistory.js +702 -0
  35. package/src/actions/getThreadHistoryDeprecated.js +93 -0
  36. package/src/actions/getThreadInfo.js +265 -0
  37. package/src/actions/getThreadInfoDeprecated.js +80 -0
  38. package/src/actions/getThreadList.js +274 -0
  39. package/src/actions/getThreadListDeprecated.js +91 -0
  40. package/src/actions/getThreadPictures.js +79 -0
  41. package/src/actions/getUserID.js +66 -0
  42. package/src/actions/getUserInfo.js +92 -0
  43. package/src/actions/handleFriendRequest.js +61 -0
  44. package/src/actions/handleMessageRequest.js +65 -0
  45. package/src/actions/httpGet.js +70 -0
  46. package/src/actions/httpPost.js +70 -0
  47. package/src/actions/index.js +69 -0
  48. package/src/actions/listenMqtt.js +818 -0
  49. package/src/actions/logout.js +136 -0
  50. package/src/actions/markAsDelivered.js +58 -0
  51. package/src/actions/markAsRead.js +92 -0
  52. package/src/actions/markAsReadAll.js +50 -0
  53. package/src/actions/markAsSeen.js +59 -0
  54. package/src/actions/muteThread.js +52 -0
  55. package/src/actions/pinMessage.js +86 -0
  56. package/src/actions/refreshFb_dtsg.js +89 -0
  57. package/src/actions/removeUserFromGroup.js +79 -0
  58. package/src/actions/resolvePhotoUrl.js +45 -0
  59. package/src/actions/searchForThread.js +53 -0
  60. package/src/actions/searchStickers.js +53 -0
  61. package/src/actions/sendMessage.js +490 -0
  62. package/src/actions/sendMessageMqtt.js +235 -0
  63. package/src/actions/sendTypingIndicator.js +59 -0
  64. package/src/actions/setMessageReaction.js +140 -0
  65. package/src/actions/setMessageReactionMqtt.js +72 -0
  66. package/src/actions/setPostReaction.js +76 -0
  67. package/src/actions/setTitle.js +86 -0
  68. package/src/actions/stopListenMqtt.js +63 -0
  69. package/src/actions/threadColors.js +57 -0
  70. package/src/actions/unfriend.js +52 -0
  71. package/src/actions/unsendMessage.js +61 -0
  72. package/src/index.js +659 -0
  73. package/src/service/uploadAttachment.js +318 -0
  74. package/src/utils/base-parts/auth.js +286 -0
  75. package/src/utils/base-parts/formatters.js +751 -0
  76. package/src/utils/base-parts/identity.js +169 -0
  77. package/src/utils/base-parts/network.js +159 -0
  78. package/src/utils/base-parts/parsing.js +53 -0
  79. package/src/utils/base-parts/type.js +9 -0
  80. package/src/utils/base.js +18 -0
  81. package/src/utils/defaults.js +10 -0
  82. package/src/utils/format-attachments.js +7 -0
  83. package/src/utils/format-core.js +8 -0
  84. package/src/utils/format-events.js +11 -0
  85. package/src/utils/format-messages.js +10 -0
  86. package/src/utils/format-presence.js +9 -0
  87. package/src/utils/format-threads.js +8 -0
  88. package/src/utils/formatters.js +18 -0
  89. package/src/utils/identity.js +12 -0
  90. package/src/utils/index.js +15 -0
  91. package/src/utils/network.js +12 -0
  92. package/src/utils/parsing.js +11 -0
  93. package/src/utils.js +3 -0
  94. package/test/config.env.example +30 -0
  95. package/test/data/shareAttach.js +146 -0
  96. package/test/data/something.mov +0 -0
  97. package/test/data/test.png +0 -0
  98. package/test/data/test.txt +7 -0
  99. package/test/example-appstate.txt +11 -0
  100. package/test/integration/actions.appstate.integration.test.js +233 -0
  101. package/test/integration/api.integration.test.js +387 -0
  102. package/test/integration/env-test-config.js +46 -0
  103. package/test/integration/login.appstate.integration.test.js +47 -0
  104. package/test/integration/page.integration.test.js +139 -0
  105. package/test/jest.setup.js +54 -0
  106. package/test/unit/actions/actions.basic.test.js +116 -0
  107. package/test/unit/actions/actions.registry.test.js +36 -0
  108. package/test/unit/index/index.test.js +109 -0
  109. package/test/unit/utils/base-parts/auth.test.js +92 -0
  110. package/test/unit/utils/base-parts/formatters.test.js +53 -0
  111. package/test/unit/utils/base-parts/identity.test.js +43 -0
  112. package/test/unit/utils/base-parts/parsing.test.js +38 -0
  113. package/test/unit/utils/base-parts/type.test.js +23 -0
  114. package/test/unit/utils/base.test.js +35 -0
  115. package/vangbanlanhat-fca-unofficial-1.4.2.tgz +0 -0
@@ -0,0 +1,274 @@
1
+ "use strict";
2
+
3
+ const utils = require("../utils");
4
+ const log = require("npmlog");
5
+
6
+ function createProfileUrl(url, username, id) {
7
+ if (url) return url;
8
+ return "https://www.facebook.com/" + (username || utils.formatID(id.toString()));
9
+ }
10
+
11
+ function formatParticipants(participants) {
12
+ var edges = participants && Array.isArray(participants.edges)
13
+ ? participants.edges
14
+ : [];
15
+
16
+ return edges.map((p)=>{
17
+ p = p && p.node ? p.node.messaging_actor : null;
18
+ if (!p) return null;
19
+ switch (p["__typename"]) {
20
+ case "User":
21
+ return {
22
+ accountType: p["__typename"],
23
+ userID: utils.formatID(p.id.toString()), // do we need .toString()? when it is not a string?
24
+ name: p.name,
25
+ shortName: p.short_name,
26
+ gender: p.gender,
27
+ url: p.url, // how about making it profileURL
28
+ profilePicture: p.big_image_src ? p.big_image_src.uri : null,
29
+ username: (p.username||null),
30
+ // TODO: maybe better names for these?
31
+ isViewerFriend: p.is_viewer_friend, // true/false
32
+ isMessengerUser: p.is_messenger_user, // true/false
33
+ isVerified: p.is_verified, // true/false
34
+ isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
35
+ isViewerCoworker: p.is_viewer_coworker, // true/false
36
+ isEmployee: p.is_employee // null? when it is something other? can someone check?
37
+ };
38
+ case "Page":
39
+ return {
40
+ accountType: p["__typename"],
41
+ userID: utils.formatID(p.id.toString()), // or maybe... pageID?
42
+ name: p.name,
43
+ url: p.url,
44
+ profilePicture: p.big_image_src ? p.big_image_src.uri : null,
45
+ username: (p.username||null),
46
+ // uhm... better names maybe?
47
+ acceptsMessengerUserFeedback: p.accepts_messenger_user_feedback, // true/false
48
+ isMessengerUser: p.is_messenger_user, // true/false
49
+ isVerified: p.is_verified, // true/false
50
+ isMessengerPlatformBot: p.is_messenger_platform_bot, // true/false
51
+ isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
52
+ };
53
+ case "ReducedMessagingActor":
54
+ return {
55
+ accountType: p["__typename"],
56
+ userID: utils.formatID(p.id.toString()),
57
+ name: p.name,
58
+ url: createProfileUrl(p.url, p.username, p.id), // in this case p.url is null all the time
59
+ profilePicture: p.big_image_src ? p.big_image_src.uri : null, // in this case it is default facebook photo, we could determine gender using it
60
+ username: (p.username||null), // maybe we could use it to generate profile URL?
61
+ isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
62
+ };
63
+ case "UnavailableMessagingActor":
64
+ return {
65
+ accountType: p["__typename"],
66
+ userID: utils.formatID(p.id.toString()),
67
+ name: p.name, // "Facebook User" in user's language
68
+ url: createProfileUrl(p.url, p.username, p.id), // in this case p.url is null all the time
69
+ profilePicture: p.big_image_src ? p.big_image_src.uri : null, // default male facebook photo
70
+ username: (p.username||null), // maybe we could use it to generate profile URL?
71
+ isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
72
+ };
73
+ default:
74
+ log.warn("getThreadList", "Found participant with unsupported typename. Please open an issue at https://github.com/VangBanLaNhat/fca-unofficial/issues\n" + JSON.stringify(p, null, 2));
75
+ return {
76
+ accountType: p["__typename"],
77
+ userID: utils.formatID(p.id.toString()),
78
+ name: p.name || `[unknown ${p["__typename"]}]`, // probably it will always be something... but fallback to [unknown], just in case
79
+ };
80
+ }
81
+ }).filter(Boolean);
82
+ }
83
+
84
+ // "FF8C0077" -> "8C0077"
85
+ function formatColor(color) {
86
+ if (color && color.match(/^(?:[0-9a-fA-F]{8})$/g)) {
87
+ return color.slice(2);
88
+ }
89
+ return color;
90
+ }
91
+
92
+ function getThreadName(t) {
93
+ if (!t || !t.thread_key) return null;
94
+ if (t.name || t.thread_key.thread_fbid) return t.name;
95
+
96
+ var edges = t.all_participants && Array.isArray(t.all_participants.edges)
97
+ ? t.all_participants.edges
98
+ : [];
99
+
100
+ for (let po of edges) {
101
+ let p = po.node;
102
+ if (p.messaging_actor.id === t.thread_key.other_user_id) return p.messaging_actor.name;
103
+ }
104
+ }
105
+
106
+ function mapNicknames(customizationInfo) {
107
+ return (customizationInfo && customizationInfo.participant_customizations) ? customizationInfo.participant_customizations.map(u => {
108
+ return {
109
+ "userID": u.participant_id,
110
+ "nickname": u.nickname
111
+ };
112
+ }):[];
113
+ }
114
+
115
+ function formatThreadList(data) {
116
+ if (!Array.isArray(data)) return [];
117
+
118
+ return data.map(t => {
119
+ if (!t) return null;
120
+ var participants = formatParticipants(t.all_participants);
121
+ let lastMessageNode = (t.last_message&&t.last_message.nodes&&t.last_message.nodes.length>0)?t.last_message.nodes[0]:null;
122
+ return {
123
+ threadID: t.thread_key?utils.formatID(t.thread_key.thread_fbid || t.thread_key.other_user_id):null, // shall never be null
124
+ name: getThreadName(t),
125
+ unreadCount: t.unread_count,
126
+ messageCount: t.messages_count,
127
+ imageSrc: t.image?t.image.uri:null,
128
+ emoji: t.customization_info?t.customization_info.emoji:null,
129
+ color: formatColor(t.customization_info?t.customization_info.outgoing_bubble_color:null),
130
+ nicknames: mapNicknames(t.customization_info),
131
+ muteUntil: t.mute_until,
132
+ participants: participants,
133
+ adminIDs: Array.isArray(t.thread_admins) ? t.thread_admins.map(a => a.id) : [],
134
+ folder: t.folder,
135
+ isGroup: t.thread_type === "GROUP",
136
+ // rtc_call_data: t.rtc_call_data, // TODO: format and document this
137
+ // isPinProtected: t.is_pin_protected, // feature from future? always false (2018-04-04)
138
+ customizationEnabled: t.customization_enabled, // false for ONE_TO_ONE with Page or ReducedMessagingActor
139
+ participantAddMode: t.participant_add_mode_as_string, // "ADD" if "GROUP" and null if "ONE_TO_ONE"
140
+ montageThread: t.montage_thread?Buffer.from(t.montage_thread.id,"base64").toString():null, // base64 encoded string "message_thread:0000000000000000"
141
+ // it is not userID nor any other ID known to me...
142
+ // can somebody inspect it? where is it used?
143
+ // probably Messenger Day uses it
144
+ reactionsMuteMode: t.reactions_mute_mode,
145
+ mentionsMuteMode: t.mentions_mute_mode,
146
+ isArchived: t.has_viewer_archived,
147
+ isSubscribed: t.is_viewer_subscribed,
148
+ timestamp: t.updated_time_precise, // in miliseconds
149
+ // isCanonicalUser: t.is_canonical_neo_user, // is it always false?
150
+ // TODO: how about putting snippet in another object? current implementation does not handle every possibile message type etc.
151
+ snippet: lastMessageNode?lastMessageNode.snippet:null,
152
+ snippetAttachments: lastMessageNode?lastMessageNode.extensible_attachment:null, // TODO: not sure if it works
153
+ snippetSender: lastMessageNode?utils.formatID((lastMessageNode.message_sender.messaging_actor.id || "").toString()):null,
154
+ lastMessageTimestamp: lastMessageNode?lastMessageNode.timestamp_precise:null, // timestamp in miliseconds
155
+ lastReadTimestamp: (t.last_read_receipt&&t.last_read_receipt.nodes.length>0)
156
+ ? (t.last_read_receipt.nodes[0]?t.last_read_receipt.nodes[0].timestamp_precise:null)
157
+ : null, // timestamp in miliseconds
158
+ cannotReplyReason: t.cannot_reply_reason, // TODO: inspect possible values
159
+ approvalMode: Boolean(t.approval_mode),
160
+
161
+ // @Legacy
162
+ participantIDs: participants.map(participant => participant.userID),
163
+ threadType: t.thread_type === "GROUP" ? 2 : 1 // "GROUP" or "ONE_TO_ONE"
164
+ };
165
+ }).filter(Boolean);
166
+ }
167
+
168
+ module.exports = function(defaultFuncs, api, ctx) {
169
+ return function getThreadList(limit, timestamp, tags, callback) {
170
+ if (!callback && (utils.getType(tags) === "Function" || utils.getType(tags) === "AsyncFunction")) {
171
+ callback = tags;
172
+ tags = [""];
173
+ }
174
+ if (utils.getType(limit) !== "Number" || !Number.isInteger(limit) || limit <= 0) {
175
+ throw {error: "getThreadList: limit must be a positive integer"};
176
+ }
177
+ if (utils.getType(timestamp) !== "Null" &&
178
+ (utils.getType(timestamp) !== "Number" || !Number.isInteger(timestamp))) {
179
+ throw {error: "getThreadList: timestamp must be an integer or null"};
180
+ }
181
+ if (utils.getType(tags) === "String") {
182
+ tags = [tags];
183
+ }
184
+ if (utils.getType(tags) !== "Array") {
185
+ throw {error: "getThreadList: tags must be an array"};
186
+ }
187
+
188
+ var resolveFunc = function(){};
189
+ var rejectFunc = function(){};
190
+ var returnPromise = new Promise(function (resolve, reject) {
191
+ resolveFunc = resolve;
192
+ rejectFunc = reject;
193
+ });
194
+
195
+ if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
196
+ callback = function (err, data) {
197
+ if (err) {
198
+ return rejectFunc(err);
199
+ }
200
+ resolveFunc(data);
201
+ };
202
+ }
203
+
204
+ const form = {
205
+ "av": ctx.globalOptions.pageID,
206
+ "queries": JSON.stringify({
207
+ "o0": {
208
+ // This doc_id was valid on 2020-07-20
209
+ "doc_id": "3336396659757871",
210
+ "query_params": {
211
+ "limit": limit+(timestamp?1:0),
212
+ "before": timestamp,
213
+ "tags": tags,
214
+ "includeDeliveryReceipts": true,
215
+ "includeSeqID": false
216
+ }
217
+ }
218
+ }),
219
+ "batch_name": "MessengerGraphQLThreadlistFetcher"
220
+ };
221
+
222
+ defaultFuncs
223
+ .post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
224
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
225
+ .then((resData) => {
226
+ if (!Array.isArray(resData) || resData.length === 0) {
227
+ throw {error: "getThreadList: invalid graphql response", res: resData};
228
+ }
229
+
230
+ if (resData[resData.length - 1].error_results > 0) {
231
+ throw resData[0].o0.errors;
232
+ }
233
+
234
+ if (resData[resData.length - 1].successful_results === 0) {
235
+ throw {error: "getThreadList: there was no successful_results", res: resData};
236
+ }
237
+
238
+ // When we ask for threads using timestamp from the previous request,
239
+ // we are getting the last thread repeated as the first thread in this response.
240
+ // .shift() gets rid of it
241
+ // It is also the reason for increasing limit by 1 when timestamp is set
242
+ // this way user asks for 10 threads, we are asking for 11,
243
+ // but after removing the duplicated one, it is again 10
244
+ if (timestamp) {
245
+ var nodes =
246
+ resData[0] && resData[0].o0 && resData[0].o0.data && resData[0].o0.data.viewer &&
247
+ resData[0].o0.data.viewer.message_threads
248
+ ? resData[0].o0.data.viewer.message_threads.nodes
249
+ : null;
250
+ if (Array.isArray(nodes)) {
251
+ nodes.shift();
252
+ }
253
+ }
254
+
255
+ var threadNodes =
256
+ resData[0] && resData[0].o0 && resData[0].o0.data && resData[0].o0.data.viewer &&
257
+ resData[0].o0.data.viewer.message_threads
258
+ ? resData[0].o0.data.viewer.message_threads.nodes
259
+ : null;
260
+
261
+ if (!Array.isArray(threadNodes)) {
262
+ throw {error: "getThreadList: missing thread nodes", res: resData[0]};
263
+ }
264
+
265
+ callback(null, formatThreadList(threadNodes));
266
+ })
267
+ .catch((err) => {
268
+ log.error("getThreadList", err);
269
+ return callback(err);
270
+ });
271
+
272
+ return returnPromise;
273
+ };
274
+ };
@@ -0,0 +1,91 @@
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 getThreadList(start, end, type, callback) {
8
+ if (utils.getType(callback) === "Undefined") {
9
+ if (utils.getType(end) !== "Number") {
10
+ throw {
11
+ error: "Please pass a number as a second argument."
12
+ };
13
+ } else if (
14
+ utils.getType(type) === "Function" ||
15
+ utils.getType(type) === "AsyncFunction"
16
+ ) {
17
+ callback = type;
18
+ type = "inbox"; //default to inbox
19
+ } else if (utils.getType(type) === "Undefined") {
20
+ type = "inbox";
21
+ } else if (utils.getType(type) !== "String") {
22
+ throw {
23
+ error:
24
+ "Please pass a String as a third argument. Your options are: inbox, pending, and archived"
25
+ };
26
+ }
27
+ }
28
+
29
+ var resolveFunc = function(){};
30
+ var rejectFunc = function(){};
31
+ var returnPromise = new Promise(function (resolve, reject) {
32
+ resolveFunc = resolve;
33
+ rejectFunc = reject;
34
+ });
35
+
36
+ if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
37
+ callback = function (err, data) {
38
+ if (err) {
39
+ return rejectFunc(err);
40
+ }
41
+ resolveFunc(data);
42
+ };
43
+ }
44
+
45
+ if (type === "archived") {
46
+ type = "action:archived";
47
+ } else if (type !== "inbox" && type !== "pending" && type !== "other") {
48
+ throw {
49
+ error:
50
+ "type can only be one of the following: inbox, pending, archived, other"
51
+ };
52
+ }
53
+
54
+ if (end <= start) end = start + 20;
55
+
56
+ var form = {
57
+ client: "mercury"
58
+ };
59
+
60
+ form[type + "[offset]"] = start;
61
+ form[type + "[limit]"] = end - start;
62
+
63
+ if (ctx.globalOptions.pageID) {
64
+ form.request_user_id = ctx.globalOptions.pageID;
65
+ }
66
+
67
+ defaultFuncs
68
+ .post(
69
+ "https://www.facebook.com/ajax/mercury/threadlist_info.php",
70
+ ctx.jar,
71
+ form
72
+ )
73
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
74
+ .then(function(resData) {
75
+ if (resData.error) {
76
+ throw resData;
77
+ }
78
+ log.verbose("getThreadList", JSON.stringify(resData.payload.threads));
79
+ return callback(
80
+ null,
81
+ (resData.payload.threads || []).map(utils.formatThread)
82
+ );
83
+ })
84
+ .catch(function(err) {
85
+ log.error("getThreadList", err);
86
+ return callback(err);
87
+ });
88
+
89
+ return returnPromise;
90
+ };
91
+ };
@@ -0,0 +1,79 @@
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 getThreadPictures(threadID, offset, limit, 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
+ var form = {
25
+ thread_id: threadID,
26
+ offset: offset,
27
+ limit: limit
28
+ };
29
+
30
+ defaultFuncs
31
+ .post(
32
+ "https://www.facebook.com/ajax/messaging/attachments/sharedphotos.php",
33
+ ctx.jar,
34
+ form
35
+ )
36
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
37
+ .then(function(resData) {
38
+ if (resData.error) {
39
+ throw resData;
40
+ }
41
+ return Promise.all(
42
+ resData.payload.imagesData.map(function(image) {
43
+ form = {
44
+ thread_id: threadID,
45
+ image_id: image.fbid
46
+ };
47
+ return defaultFuncs
48
+ .post(
49
+ "https://www.facebook.com/ajax/messaging/attachments/sharedphotos.php",
50
+ ctx.jar,
51
+ form
52
+ )
53
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
54
+ .then(function(resData) {
55
+ if (resData.error) {
56
+ throw resData;
57
+ }
58
+ // the response is pretty messy
59
+ var queryThreadID =
60
+ resData.jsmods.require[0][3][1].query_metadata.query_path[0]
61
+ .message_thread;
62
+ var imageData =
63
+ resData.jsmods.require[0][3][1].query_results[queryThreadID]
64
+ .message_images.edges[0].node.image2;
65
+ return imageData;
66
+ });
67
+ })
68
+ );
69
+ })
70
+ .then(function(resData) {
71
+ callback(null, resData);
72
+ })
73
+ .catch(function(err) {
74
+ log.error("Error in getThreadPictures", err);
75
+ callback(err);
76
+ });
77
+ return returnPromise;
78
+ };
79
+ };
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+
3
+ var utils = require("../utils");
4
+ var log = require("npmlog");
5
+
6
+ function formatData(data) {
7
+ return {
8
+ userID: utils.formatID(data.uid.toString()),
9
+ photoUrl: data.photo,
10
+ indexRank: data.index_rank,
11
+ name: data.text,
12
+ isVerified: data.is_verified,
13
+ profileUrl: data.path,
14
+ category: data.category,
15
+ score: data.score,
16
+ type: data.type
17
+ };
18
+ }
19
+
20
+ module.exports = function(defaultFuncs, api, ctx) {
21
+ return function getUserID(name, callback) {
22
+ var resolveFunc = function(){};
23
+ var rejectFunc = function(){};
24
+ var returnPromise = new Promise(function (resolve, reject) {
25
+ resolveFunc = resolve;
26
+ rejectFunc = reject;
27
+ });
28
+
29
+ if (!callback) {
30
+ callback = function (err, friendList) {
31
+ if (err) {
32
+ return rejectFunc(err);
33
+ }
34
+ resolveFunc(friendList);
35
+ };
36
+ }
37
+
38
+ var form = {
39
+ value: name.toLowerCase(),
40
+ viewer: ctx.userID,
41
+ rsp: "search",
42
+ context: "search",
43
+ path: "/home.php",
44
+ request_id: utils.getGUID()
45
+ };
46
+
47
+ defaultFuncs
48
+ .get("https://www.facebook.com/ajax/typeahead/search.php", ctx.jar, form)
49
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
50
+ .then(function(resData) {
51
+ if (resData.error) {
52
+ throw resData;
53
+ }
54
+
55
+ var data = resData.payload.entries;
56
+
57
+ callback(null, data.map(formatData));
58
+ })
59
+ .catch(function(err) {
60
+ log.error("getUserID", err);
61
+ return callback(err);
62
+ });
63
+
64
+ return returnPromise;
65
+ };
66
+ };
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+
3
+ var utils = require("../utils");
4
+ var log = require("npmlog");
5
+
6
+ function formatData(data) {
7
+ var retObj = {};
8
+
9
+ if (!data || utils.getType(data) !== "Object") {
10
+ return retObj;
11
+ }
12
+
13
+ for (var prop in data) {
14
+ // eslint-disable-next-line no-prototype-builtins
15
+ if (data.hasOwnProperty(prop)) {
16
+ var innerObj = data[prop];
17
+ if (!innerObj) {
18
+ continue;
19
+ }
20
+ retObj[prop] = {
21
+ name: innerObj.name,
22
+ firstName: innerObj.firstName,
23
+ vanity: innerObj.vanity,
24
+ thumbSrc: innerObj.thumbSrc,
25
+ profileUrl: innerObj.uri,
26
+ gender: innerObj.gender,
27
+ type: innerObj.type,
28
+ isFriend: innerObj.is_friend,
29
+ isBirthday: !!innerObj.is_birthday
30
+ };
31
+ }
32
+ }
33
+
34
+ return retObj;
35
+ }
36
+
37
+ module.exports = function(defaultFuncs, api, ctx) {
38
+ return function getUserInfo(id, callback) {
39
+ var resolveFunc = function(){};
40
+ var rejectFunc = function(){};
41
+ var returnPromise = new Promise(function (resolve, reject) {
42
+ resolveFunc = resolve;
43
+ rejectFunc = reject;
44
+ });
45
+
46
+ if (!callback) {
47
+ callback = function (err, friendList) {
48
+ if (err) {
49
+ return rejectFunc(err);
50
+ }
51
+ resolveFunc(friendList);
52
+ };
53
+ }
54
+
55
+ if (utils.getType(id) !== "Array") {
56
+ id = [id];
57
+ }
58
+
59
+ var form = {};
60
+ id.map(function(v, i) {
61
+ form["ids[" + i + "]"] = v;
62
+ });
63
+ defaultFuncs
64
+ .post("https://www.facebook.com/chat/user_info/", ctx.jar, form)
65
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
66
+ .then(function(resData) {
67
+ if (resData.error) {
68
+ throw resData;
69
+ }
70
+
71
+ var profiles =
72
+ resData && resData.payload && resData.payload.profiles
73
+ ? resData.payload.profiles
74
+ : null;
75
+
76
+ if (!profiles) {
77
+ throw {
78
+ error: "getUserInfo: missing profiles in response payload",
79
+ res: resData
80
+ };
81
+ }
82
+
83
+ return callback(null, formatData(profiles));
84
+ })
85
+ .catch(function(err) {
86
+ log.error("getUserInfo", err);
87
+ return callback(err);
88
+ });
89
+
90
+ return returnPromise;
91
+ };
92
+ };
@@ -0,0 +1,61 @@
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 handleFriendRequest(userID, accept, callback) {
8
+ if (utils.getType(accept) !== "Boolean") {
9
+ throw {
10
+ error: "Please pass a boolean as a second argument."
11
+ };
12
+ }
13
+
14
+ var resolveFunc = function(){};
15
+ var rejectFunc = function(){};
16
+ var returnPromise = new Promise(function (resolve, reject) {
17
+ resolveFunc = resolve;
18
+ rejectFunc = reject;
19
+ });
20
+
21
+ if (!callback) {
22
+ callback = function (err, friendList) {
23
+ if (err) {
24
+ return rejectFunc(err);
25
+ }
26
+ resolveFunc(friendList);
27
+ };
28
+ }
29
+
30
+ var form = {
31
+ viewer_id: ctx.userID,
32
+ "frefs[0]": "jwl",
33
+ floc: "friend_center_requests",
34
+ ref: "/reqs.php",
35
+ action: (accept ? "confirm" : "reject")
36
+ };
37
+
38
+ defaultFuncs
39
+ .post(
40
+ "https://www.facebook.com/requests/friends/ajax/",
41
+ ctx.jar,
42
+ form
43
+ )
44
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
45
+ .then(function(resData) {
46
+ if (resData.payload.err) {
47
+ throw {
48
+ err: resData.payload.err
49
+ };
50
+ }
51
+
52
+ return callback();
53
+ })
54
+ .catch(function(err) {
55
+ log.error("handleFriendRequest", err);
56
+ return callback(err);
57
+ });
58
+
59
+ return returnPromise;
60
+ };
61
+ };