File size: 1,919 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
"use strict";

const utils = require("../utils");
const log = require("npmlog");

module.exports = function (defaultFuncs, api, ctx) {
	return async function markAsRead(threadID, read, callback) {
		if (utils.getType(read) === 'Function' || utils.getType(read) === 'AsyncFunction') {
			callback = read;
			read = true;
		}
		if (read == undefined) {
			read = true;
		}

		if (!callback) {
			callback = () => { };
		}

		const form = {};

		if (typeof ctx.globalOptions.pageID !== 'undefined') {
			form["source"] = "PagesManagerMessagesInterface";
			form["request_user_id"] = ctx.globalOptions.pageID;
			form["ids[" + threadID + "]"] = read;
			form["watermarkTimestamp"] = new Date().getTime();
			form["shouldSendReadReceipt"] = true;
			form["commerce_last_message_type"] = "";
			//form["titanOriginatedThreadId"] = utils.generateThreadingID(ctx.clientID);

			let resData;
			try {
				resData = await (
					defaultFuncs
						.post(
							"https://www.facebook.com/ajax/mercury/change_read_status.php",
							ctx.jar,
							form
						)
						.then(utils.saveCookies(ctx.jar))
						.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
				);
			} catch (e) {
				callback(e);
				return e;
			}

			if (resData.error) {
				const err = resData.error;
				log.error("markAsRead", err);
				if (utils.getType(err) == "Object" && err.error === "Not logged in.") {
					ctx.loggedIn = false;
				}
				callback(err);
				return err;
			}

			callback();
			return null;
		} else {
			try {
				if (ctx.mqttClient) {
					const err = await new Promise(r => ctx.mqttClient.publish("/mark_thread", JSON.stringify({
						threadID,
						mark: "read",
						state: read
					}), { qos: 1, retain: false }, r));
					if (err) throw err;
				} else {
					throw {
						error: "You can only use this function after you start listening."
					};
				}
			} catch (e) {
				callback(e);
				return e;
			}
		}
	};
};