@dongdev/fca-unofficial 0.0.5 → 0.0.6
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/index.js +142 -102
- package/package.json +4 -3
- package/src/getThreadList.js +175 -224
- package/src/listenMqtt.js +230 -138
- package/utils.js +71 -40
package/src/getThreadList.js
CHANGED
@@ -3,239 +3,190 @@
|
|
3
3
|
const utils = require("../utils");
|
4
4
|
const log = require("npmlog");
|
5
5
|
|
6
|
-
function
|
7
|
-
|
8
|
-
|
9
|
-
eventCreatorID: reminder.lightweight_event_creator.id,
|
10
|
-
time: reminder.time,
|
11
|
-
eventType: reminder.lightweight_event_type.toLowerCase(),
|
12
|
-
locationName: reminder.location_name,
|
13
|
-
// @TODO verify this
|
14
|
-
locationCoordinates: reminder.location_coordinates,
|
15
|
-
locationPage: reminder.location_page,
|
16
|
-
eventStatus: reminder.lightweight_event_status.toLowerCase(),
|
17
|
-
note: reminder.note,
|
18
|
-
repeatMode: reminder.repeat_mode.toLowerCase(),
|
19
|
-
eventTitle: reminder.event_title,
|
20
|
-
triggerMessage: reminder.trigger_message,
|
21
|
-
secondsToNotifyBefore: reminder.seconds_to_notify_before,
|
22
|
-
allowsRsvp: reminder.allows_rsvp,
|
23
|
-
relatedEvent: reminder.related_event,
|
24
|
-
members: reminder.event_reminder_members.edges.map(function (member) {
|
25
|
-
return {
|
26
|
-
memberID: member.node.id,
|
27
|
-
state: member.guest_list_state.toLowerCase()
|
28
|
-
};
|
29
|
-
})
|
30
|
-
};
|
6
|
+
function createProfileUrl(url, username, id) {
|
7
|
+
if (url) return url;
|
8
|
+
return "https://www.facebook.com/" + (username || utils.formatID(id.toString()));
|
31
9
|
}
|
32
10
|
|
33
|
-
function
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
11
|
+
function formatParticipants(participants) {
|
12
|
+
return participants.edges.map((p) => {
|
13
|
+
p = p.node.messaging_actor;
|
14
|
+
switch (p["__typename"]) {
|
15
|
+
case "User":
|
16
|
+
return {
|
17
|
+
accountType: p["__typename"],
|
18
|
+
userID: utils.formatID(p.id.toString()), // do we need .toString()? when it is not a string?
|
19
|
+
name: p.name,
|
20
|
+
shortName: p.short_name,
|
21
|
+
gender: p.gender,
|
22
|
+
url: p.url, // how about making it profileURL
|
23
|
+
profilePicture: p.big_image_src.uri,
|
24
|
+
username: (p.username || null),
|
25
|
+
// TODO: maybe better names for these?
|
26
|
+
isViewerFriend: p.is_viewer_friend, // true/false
|
27
|
+
isMessengerUser: p.is_messenger_user, // true/false
|
28
|
+
isVerified: p.is_verified, // true/false
|
29
|
+
isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
|
30
|
+
isViewerCoworker: p.is_viewer_coworker, // true/false
|
31
|
+
isEmployee: p.is_employee // null? when it is something other? can someone check?
|
32
|
+
};
|
33
|
+
case "Page":
|
34
|
+
return {
|
35
|
+
accountType: p["__typename"],
|
36
|
+
userID: utils.formatID(p.id.toString()), // or maybe... pageID?
|
37
|
+
name: p.name,
|
38
|
+
url: p.url,
|
39
|
+
profilePicture: p.big_image_src.uri,
|
40
|
+
username: (p.username || null),
|
41
|
+
// uhm... better names maybe?
|
42
|
+
acceptsMessengerUserFeedback: p.accepts_messenger_user_feedback, // true/false
|
43
|
+
isMessengerUser: p.is_messenger_user, // true/false
|
44
|
+
isVerified: p.is_verified, // true/false
|
45
|
+
isMessengerPlatformBot: p.is_messenger_platform_bot, // true/false
|
46
|
+
isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
|
47
|
+
};
|
48
|
+
case "ReducedMessagingActor":
|
49
|
+
case "UnavailableMessagingActor":
|
50
|
+
return {
|
51
|
+
accountType: p["__typename"],
|
52
|
+
userID: utils.formatID(p.id.toString()),
|
53
|
+
name: p.name,
|
54
|
+
url: createProfileUrl(p.url, p.username, p.id), // in this case p.url is null all the time
|
55
|
+
profilePicture: p.big_image_src.uri, // in this case it is default facebook photo, we could determine gender using it
|
56
|
+
username: (p.username || null), // maybe we could use it to generate profile URL?
|
57
|
+
isMessageBlockedByViewer: p.is_message_blocked_by_viewer, // true/false
|
58
|
+
};
|
59
|
+
default:
|
60
|
+
log.warn("getThreadList", "Found participant with unsupported typename. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues\n" + JSON.stringify(p, null, 2));
|
61
|
+
return {
|
62
|
+
accountType: p["__typename"],
|
63
|
+
userID: utils.formatID(p.id.toString()),
|
64
|
+
name: p.name || `[unknown ${p["__typename"]}]`, // probably it will always be something... but fallback to [unknown], just in case
|
65
|
+
};
|
66
|
+
}
|
67
|
+
});
|
68
|
+
}
|
55
69
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
id: d.node.messaging_actor.id,
|
62
|
-
name: d.node.messaging_actor.name,
|
63
|
-
firstName: d.node.messaging_actor.short_name,
|
64
|
-
vanity: d.node.messaging_actor.username,
|
65
|
-
url: d.node.messaging_actor.url,
|
66
|
-
thumbSrc: d.node.messaging_actor.big_image_src.uri,
|
67
|
-
profileUrl: d.node.messaging_actor.big_image_src.uri,
|
68
|
-
gender: d.node.messaging_actor.gender,
|
69
|
-
type: d.node.messaging_actor.__typename,
|
70
|
-
isFriend: d.node.messaging_actor.is_viewer_friend,
|
71
|
-
isBirthday: !!d.node.messaging_actor.is_birthday //not sure?
|
72
|
-
})),
|
73
|
-
unreadCount: messageThread.unread_count,
|
74
|
-
messageCount: messageThread.messages_count,
|
75
|
-
timestamp: messageThread.updated_time_precise,
|
76
|
-
muteUntil: messageThread.mute_until,
|
77
|
-
isGroup: messageThread.thread_type == "GROUP",
|
78
|
-
isSubscribed: messageThread.is_viewer_subscribed,
|
79
|
-
isArchived: messageThread.has_viewer_archived,
|
80
|
-
folder: messageThread.folder,
|
81
|
-
cannotReplyReason: messageThread.cannot_reply_reason,
|
82
|
-
eventReminders: messageThread.event_reminders
|
83
|
-
? messageThread.event_reminders.nodes.map(formatEventReminders)
|
84
|
-
: null,
|
85
|
-
emoji: messageThread.customization_info
|
86
|
-
? messageThread.customization_info.emoji
|
87
|
-
: null,
|
88
|
-
color:
|
89
|
-
messageThread.customization_info &&
|
90
|
-
messageThread.customization_info.outgoing_bubble_color
|
91
|
-
? messageThread.customization_info.outgoing_bubble_color.slice(2)
|
92
|
-
: null,
|
93
|
-
threadTheme: messageThread.thread_theme,
|
94
|
-
nicknames:
|
95
|
-
messageThread.customization_info &&
|
96
|
-
messageThread.customization_info.participant_customizations
|
97
|
-
? messageThread.customization_info.participant_customizations.reduce(
|
98
|
-
function (res, val) {
|
99
|
-
if (val.nickname) res[val.participant_id] = val.nickname;
|
100
|
-
return res;
|
101
|
-
},
|
102
|
-
{}
|
103
|
-
)
|
104
|
-
: {},
|
105
|
-
adminIDs: messageThread.thread_admins,
|
106
|
-
approvalMode: Boolean(messageThread.approval_mode),
|
107
|
-
approvalQueue: messageThread.group_approval_queue.nodes.map(a => ({
|
108
|
-
inviterID: a.inviter.id,
|
109
|
-
requesterID: a.requester.id,
|
110
|
-
timestamp: a.request_timestamp,
|
111
|
-
request_source: a.request_source // @Undocumented
|
112
|
-
})),
|
70
|
+
// "FF8C0077" -> "8C0077"
|
71
|
+
function formatColor(color) {
|
72
|
+
if (color && color.match(/^(?:[0-9a-fA-F]{8})$/g)) return color.slice(2);
|
73
|
+
return color;
|
74
|
+
}
|
113
75
|
|
114
|
-
|
115
|
-
|
116
|
-
mentionsMuteMode: messageThread.mentions_mute_mode.toLowerCase(),
|
117
|
-
isPinProtected: messageThread.is_pin_protected,
|
118
|
-
relatedPageThread: messageThread.related_page_thread,
|
76
|
+
function getThreadName(t) {
|
77
|
+
if (t.name || t.thread_key.thread_fbid) return t.name;
|
119
78
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
serverTimestamp: messageThread.updated_time_precise,
|
126
|
-
imageSrc: messageThread.image ? messageThread.image.uri : null,
|
127
|
-
isCanonicalUser: messageThread.is_canonical_neo_user,
|
128
|
-
isCanonical: messageThread.thread_type != "GROUP",
|
129
|
-
recipientsLoadable: true,
|
130
|
-
hasEmailParticipant: false,
|
131
|
-
readOnly: false,
|
132
|
-
canReply: messageThread.cannot_reply_reason == null,
|
133
|
-
lastMessageTimestamp: messageThread.last_message
|
134
|
-
? messageThread.last_message.timestamp_precise
|
135
|
-
: null,
|
136
|
-
lastMessageType: "message",
|
137
|
-
lastReadTimestamp: lastReadTimestamp,
|
138
|
-
threadType: messageThread.thread_type == "GROUP" ? 2 : 1,
|
79
|
+
for (let po of t.all_participants.edges) {
|
80
|
+
let p = po.node;
|
81
|
+
if (p.messaging_actor.id === t.thread_key.other_user_id) return p.messaging_actor.name;
|
82
|
+
}
|
83
|
+
}
|
139
84
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
85
|
+
function mapNicknames(customizationInfo) {
|
86
|
+
return (customizationInfo && customizationInfo.participant_customizations) ? customizationInfo.participant_customizations.map(u => {
|
87
|
+
return {
|
88
|
+
"userID": u.participant_id,
|
89
|
+
"nickname": u.nickname
|
90
|
+
};
|
91
|
+
}) : [];
|
146
92
|
}
|
147
93
|
|
148
94
|
function formatThreadList(data) {
|
149
|
-
|
150
|
-
|
95
|
+
return data.map(t => {
|
96
|
+
let lastMessageNode = (t.last_message && t.last_message.nodes && t.last_message.nodes.length > 0) ? t.last_message.nodes[0] : null;
|
97
|
+
return {
|
98
|
+
threadID: t.thread_key ? utils.formatID(t.thread_key.thread_fbid || t.thread_key.other_user_id) : null, // shall never be null
|
99
|
+
name: getThreadName(t),
|
100
|
+
unreadCount: t.unread_count,
|
101
|
+
messageCount: t.messages_count,
|
102
|
+
imageSrc: t.image ? t.image.uri : null,
|
103
|
+
emoji: t.customization_info ? t.customization_info.emoji : null,
|
104
|
+
color: formatColor(t.customization_info ? t.customization_info.outgoing_bubble_color : null),
|
105
|
+
threadTheme: t.thread_theme,
|
106
|
+
nicknames: mapNicknames(t.customization_info),
|
107
|
+
muteUntil: t.mute_until,
|
108
|
+
participants: formatParticipants(t.all_participants),
|
109
|
+
adminIDs: t.thread_admins.map(a => a.id),
|
110
|
+
folder: t.folder,
|
111
|
+
isGroup: t.thread_type === "GROUP",
|
112
|
+
customizationEnabled: t.customization_enabled, // false for ONE_TO_ONE with Page or ReducedMessagingActor
|
113
|
+
participantAddMode: t.participant_add_mode_as_string, // "ADD" if "GROUP" and null if "ONE_TO_ONE"
|
114
|
+
montageThread: t.montage_thread ? Buffer.from(t.montage_thread.id, "base64").toString() : null, // base64 encoded string "message_thread:0000000000000000"
|
115
|
+
reactionsMuteMode: t.reactions_mute_mode,
|
116
|
+
mentionsMuteMode: t.mentions_mute_mode,
|
117
|
+
isArchived: t.has_viewer_archived,
|
118
|
+
isSubscribed: t.is_viewer_subscribed,
|
119
|
+
timestamp: t.updated_time_precise, // in miliseconds
|
120
|
+
snippet: lastMessageNode ? lastMessageNode.snippet : null,
|
121
|
+
snippetAttachments: lastMessageNode ? lastMessageNode.extensible_attachment : null, // TODO: not sure if it works
|
122
|
+
snippetSender: lastMessageNode ? utils.formatID((lastMessageNode.message_sender.messaging_actor.id || "").toString()) : null,
|
123
|
+
lastMessageTimestamp: lastMessageNode ? lastMessageNode.timestamp_precise : null, // timestamp in miliseconds
|
124
|
+
lastReadTimestamp: (t.last_read_receipt && t.last_read_receipt.nodes.length > 0)
|
125
|
+
? (t.last_read_receipt.nodes[0] ? t.last_read_receipt.nodes[0].timestamp_precise : null)
|
126
|
+
: null,
|
127
|
+
cannotReplyReason: t.cannot_reply_reason,
|
128
|
+
approvalMode: Boolean(t.approval_mode),
|
129
|
+
participantIDs: formatParticipants(t.all_participants).map(participant => participant.userID),
|
130
|
+
threadType: t.thread_type === "GROUP" ? 2 : 1, // "GROUP" or "ONE_TO_ONE"
|
131
|
+
inviteLink: {
|
132
|
+
enable: t.joinable_mode ? t.joinable_mode.mode == 1 : false,
|
133
|
+
link: t.joinable_mode ? t.joinable_mode.link : null
|
134
|
+
}
|
135
|
+
};
|
136
|
+
});
|
151
137
|
}
|
152
138
|
|
153
139
|
module.exports = function (defaultFuncs, api, ctx) {
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
}),
|
208
|
-
"batch_name": "MessengerGraphQLThreadlistFetcher"
|
209
|
-
};
|
210
|
-
|
211
|
-
defaultFuncs
|
212
|
-
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
|
213
|
-
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
214
|
-
.then((resData) => {
|
215
|
-
if (resData[resData.length - 1].error_results > 0) {
|
216
|
-
throw new utils.CustomError(resData[0].o0.errors);
|
217
|
-
}
|
218
|
-
|
219
|
-
if (resData[resData.length - 1].successful_results === 0) {
|
220
|
-
throw new utils.CustomError({ error: "getThreadList: there was no successful_results", res: resData });
|
221
|
-
}
|
222
|
-
|
223
|
-
// When we ask for threads using timestamp from the previous request,
|
224
|
-
// we are getting the last thread repeated as the first thread in this response.
|
225
|
-
// .shift() gets rid of it
|
226
|
-
// It is also the reason for increasing limit by 1 when timestamp is set
|
227
|
-
// this way user asks for 10 threads, we are asking for 11,
|
228
|
-
// but after removing the duplicated one, it is again 10
|
229
|
-
if (timestamp) {
|
230
|
-
resData[0].o0.data.viewer.message_threads.nodes.shift();
|
231
|
-
}
|
232
|
-
callback(null, formatThreadList(resData[0].o0.data.viewer.message_threads.nodes));
|
233
|
-
})
|
234
|
-
.catch((err) => {
|
235
|
-
log.error("getThreadList", err);
|
236
|
-
return callback(err);
|
237
|
-
});
|
238
|
-
|
239
|
-
return returnPromise;
|
240
|
-
};
|
241
|
-
};
|
140
|
+
return function getThreadList(limit, timestamp, tags, callback) {
|
141
|
+
if (!callback && (utils.getType(tags) === "Function" || utils.getType(tags) === "AsyncFunction")) {
|
142
|
+
callback = tags;
|
143
|
+
tags = [""];
|
144
|
+
}
|
145
|
+
if (utils.getType(limit) !== "Number" || !Number.isInteger(limit) || limit <= 0) throw { error: "getThreadList: limit must be a positive integer" };
|
146
|
+
if (utils.getType(timestamp) !== "Null" && (utils.getType(timestamp) !== "Number" || !Number.isInteger(timestamp))) throw { error: "getThreadList: timestamp must be an integer or null" };
|
147
|
+
if (utils.getType(tags) === "String") tags = [tags];
|
148
|
+
if (utils.getType(tags) !== "Array") throw { error: "getThreadList: tags must be an array" };
|
149
|
+
var resolveFunc = function () { };
|
150
|
+
var rejectFunc = function () { };
|
151
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
152
|
+
resolveFunc = resolve;
|
153
|
+
rejectFunc = reject;
|
154
|
+
});
|
155
|
+
if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
|
156
|
+
callback = function (err, data) {
|
157
|
+
if (err) return rejectFunc(err);
|
158
|
+
resolveFunc(data);
|
159
|
+
};
|
160
|
+
}
|
161
|
+
const form = {
|
162
|
+
"av": ctx.globalOptions.pageID,
|
163
|
+
"queries": JSON.stringify({
|
164
|
+
"o0": {
|
165
|
+
"doc_id": "3336396659757871",
|
166
|
+
"query_params": {
|
167
|
+
"limit": limit + (timestamp ? 1 : 0),
|
168
|
+
"before": timestamp,
|
169
|
+
"tags": tags,
|
170
|
+
"includeDeliveryReceipts": true,
|
171
|
+
"includeSeqID": false
|
172
|
+
}
|
173
|
+
}
|
174
|
+
}),
|
175
|
+
"batch_name": "MessengerGraphQLThreadlistFetcher"
|
176
|
+
};
|
177
|
+
defaultFuncs
|
178
|
+
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
|
179
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
180
|
+
.then((resData) => {
|
181
|
+
if (resData[resData.length - 1].error_results > 0) throw resData[0].o0.errors;
|
182
|
+
if (resData[resData.length - 1].successful_results === 0) throw { error: "getThreadList: there was no successful_results", res: resData };
|
183
|
+
if (timestamp) resData[0].o0.data.viewer.message_threads.nodes.shift();
|
184
|
+
callback(null, formatThreadList(resData[0].o0.data.viewer.message_threads.nodes));
|
185
|
+
})
|
186
|
+
.catch((err) => {
|
187
|
+
log.error("getThreadList", err);
|
188
|
+
return callback(err);
|
189
|
+
});
|
190
|
+
return returnPromise;
|
191
|
+
};
|
192
|
+
};
|