File size: 7,981 Bytes
e192d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
var utils = require("../utils");
var log = require("npmlog");
var bluebird = require("bluebird");

module.exports = function (defaultFuncs, api, ctx) {
	function uploadAttachment(attachments, callback) {
		callback = callback || function () { };
		var uploads = [];

		// create an array of promises
		for (var i = 0; i < attachments.length; i++) {
			if (!utils.isReadableStream(attachments[i])) {
				throw {
					error:
						"Attachment should be a readable stream and not " +
						utils.getType(attachments[i]) +
						"."
				};
			}

			var form = {
				upload_1024: attachments[i],
				voice_clip: "true"
			};

			uploads.push(
				defaultFuncs
					.postFormData(
						"https://upload.facebook.com/ajax/mercury/upload.php",
						ctx.jar,
						form,
						{}
					)
					.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
					.then(function (resData) {
						if (resData.error) {
							throw resData;
						}

						// We have to return the data unformatted unless we want to change it
						// back in sendMessage.
						return resData.payload.metadata[0];
					})
			);
		}

		// resolve all promises
		bluebird
			.all(uploads)
			.then(function (resData) {
				callback(null, resData);
			})
			.catch(function (err) {
				log.error("uploadAttachment", err);
				return callback(err);
			});
	}

	let variance = 0;
	const epoch_id = () => Math.floor(Date.now() * (4194304 + (variance = (variance + 0.1) % 5)));
	const emojiSizes = {
		small: 1,
		medium: 2,
		large: 3
	};

	function handleEmoji(msg, form, callback, cb) {
		if (msg.emojiSize != null && msg.emoji == null) {
			return callback({ error: "emoji property is empty" });
		}
		if (msg.emoji) {
			if (!msg.emojiSize) {
				msg.emojiSize = "small";
			}
			if (
				msg.emojiSize !== "small" &&
				msg.emojiSize !== "medium" &&
				msg.emojiSize !== "large" &&
				(isNaN(msg.emojiSize) || msg.emojiSize < 1 || msg.emojiSize > 3)
			) {
				return callback({ error: "emojiSize property is invalid" });
			}

			form.payload.tasks[0].payload.send_type = 1;
			form.payload.tasks[0].payload.text = msg.emoji;
			form.payload.tasks[0].payload.hot_emoji_size = !isNaN(msg.emojiSize) ? msg.emojiSize : emojiSizes[msg.emojiSize];
		}
		cb();
	}

	function handleSticker(msg, form, callback, cb) {
		if (msg.sticker) {
			form.payload.tasks[0].payload.send_type = 2;
			form.payload.tasks[0].payload.sticker_id = msg.sticker;
		}
		cb();
	}

	function handleAttachment(msg, form, callback, cb) {
		if (msg.attachment) {
			form.payload.tasks[0].payload.send_type = 3;
			form.payload.tasks[0].payload.attachment_fbids = [];
			if (form.payload.tasks[0].payload.text == "")
				form.payload.tasks[0].payload.text = null;
			if (utils.getType(msg.attachment) !== "Array") {
				msg.attachment = [msg.attachment];
			}

			uploadAttachment(msg.attachment, function (err, files) {
				if (err) {
					return callback(err);
				}

				files.forEach(function (file) {
					var key = Object.keys(file);
					var type = key[0]; // image_id, file_id, etc
					form.payload.tasks[0].payload.attachment_fbids.push(file[type]); // push the id
				});
				cb();
			});
		} else {
			cb();
		}
	}


	function handleMention(msg, form, callback, cb) {
		if (msg.mentions) {
			form.payload.tasks[0].payload.send_type = 1;

			const arrayIds = [];
			const arrayOffsets = [];
			const arrayLengths = [];
			const mention_types = [];

			for (let i = 0; i < msg.mentions.length; i++) {
				const mention = msg.mentions[i];

				const tag = mention.tag;
				if (typeof tag !== "string") {
					return callback({ error: "Mention tags must be strings." });
				}

				const offset = msg.body.indexOf(tag, mention.fromIndex || 0);

				if (offset < 0) {
					log.warn(
						"handleMention",
						'Mention for "' + tag + '" not found in message string.'
					);
				}

				if (mention.id == null) {
					log.warn("handleMention", "Mention id should be non-null.");
				}

				const id = mention.id || 0;
				arrayIds.push(id);
				arrayOffsets.push(offset);
				arrayLengths.push(tag.length);
				mention_types.push("p");
			}

			form.payload.tasks[0].payload.mention_data = {
				mention_ids: arrayIds.join(","),
				mention_offsets: arrayOffsets.join(","),
				mention_lengths: arrayLengths.join(","),
				mention_types: mention_types.join(",")
			};
		}
		cb();
	}

	function handleLocation(msg, form, callback, cb) {
		// this is not working yet
		if (msg.location) {
			if (msg.location.latitude == null || msg.location.longitude == null) {
				return callback({ error: "location property needs both latitude and longitude" });
			}

			form.payload.tasks[0].payload.send_type = 1;
			form.payload.tasks[0].payload.location_data = {
				coordinates: {
					latitude: msg.location.latitude,
					longitude: msg.location.longitude
				},
				is_current_location: !!msg.location.current,
				is_live_location: !!msg.location.live
			};
		}

		cb();
	}

	function send(form, threadID, callback, replyToMessage) {
		if (replyToMessage) {
			form.payload.tasks[0].payload.reply_metadata = {
				reply_source_id: replyToMessage,
				reply_source_type: 1,
				reply_type: 0
			};
		}
		const mqttClient = ctx.mqttClient;
		form.payload.tasks.forEach((task) => {
			task.payload = JSON.stringify(task.payload);
		});
		form.payload = JSON.stringify(form.payload);
		console.log(global.jsonStringifyColor(form, null, 2));

		return mqttClient.publish("/ls_req", JSON.stringify(form), function (err, data) {
			if (err) {
				console.error('Error publishing message: ', err);
				callback(err);
			} else {
				console.log('Message published successfully with data: ', data);
				callback(null, data);
			}
		});
	}

	return function sendMessageMqtt(msg, threadID, callback, replyToMessage) {
		if (
			!callback &&
			(utils.getType(threadID) === "Function" ||
				utils.getType(threadID) === "AsyncFunction")
		) {
			return threadID({ error: "Pass a threadID as a second argument." });
		}
		if (
			!replyToMessage &&
			utils.getType(callback) === "String"
		) {
			replyToMessage = callback;
			callback = function () { };
		}


		if (!callback) {
			callback = function (err, friendList) {
			};
		}

		var msgType = utils.getType(msg);
		var threadIDType = utils.getType(threadID);
		var messageIDType = utils.getType(replyToMessage);

		if (msgType !== "String" && msgType !== "Object") {
			return callback({
				error:
					"Message should be of type string or object and not " + msgType + "."
			});
		}

		if (msgType === "String") {
			msg = { body: msg };
		}

		const timestamp = Date.now();
		// get full date time
		const epoch = timestamp << 22;
		//const otid = epoch + 0; // TODO replace with randomInt(0, 2**22)
		const otid = epoch + Math.floor(Math.random() * 4194304);

		const form = {
			app_id: "2220391788200892",
			payload: {
				tasks: [
					{
						label: "46",
						payload: {
							thread_id: threadID.toString(),
							otid: otid.toString(),
							source: 0,
							send_type: 1,
							sync_group: 1,
							text: msg.body != null && msg.body != undefined ? msg.body.toString() : "",
							initiating_source: 1,
							skip_url_preview_gen: 0
						},
						queue_name: threadID.toString(),
						task_id: 0,
						failure_count: null
					},
					{
						label: "21",
						payload: {
							thread_id: threadID.toString(),
							last_read_watermark_ts: Date.now(),
							sync_group: 1
						},
						queue_name: threadID.toString(),
						task_id: 1,
						failure_count: null
					}
				],
				epoch_id: epoch_id(),
				version_id: "6120284488008082",
				data_trace_id: null
			},
			request_id: 1,
			type: 3
		};

		handleEmoji(msg, form, callback, function () {
			handleLocation(msg, form, callback, function () {
				handleMention(msg, form, callback, function () {
					handleSticker(msg, form, callback, function () {
						handleAttachment(msg, form, callback, function () {
							send(form, threadID, callback, replyToMessage);
						});
					});
				});
			});
		});
	};
};