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

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

module.exports = function (defaultFuncs, api, ctx) {
	return function addUserToGroup(userID, threadID, callback) {
		let resolveFunc = function () { };
		let rejectFunc = function () { };
		const returnPromise = new Promise(function (resolve, reject) {
			resolveFunc = resolve;
			rejectFunc = reject;
		});

		if (
			!callback &&
			(utils.getType(threadID) === "Function" ||
				utils.getType(threadID) === "AsyncFunction")
		) {
			throw new utils.CustomError({ error: "please pass a threadID as a second argument." });
		}

		if (!callback) {
			callback = function (err) {
				if (err) {
					return rejectFunc(err);
				}
				resolveFunc();
			};
		}

		if (
			utils.getType(threadID) !== "Number" &&
			utils.getType(threadID) !== "String"
		) {
			throw new utils.CustomError({
				error:
					"ThreadID should be of type Number or String and not " +
					utils.getType(threadID) +
					"."
			});
		}

		if (utils.getType(userID) !== "Array") {
			userID = [userID];
		}

		const messageAndOTID = utils.generateOfflineThreadingID();
		const form = {
			client: "mercury",
			action_type: "ma-type:log-message",
			author: "fbid:" + (ctx.i_userID || ctx.userID),
			thread_id: "",
			timestamp: Date.now(),
			timestamp_absolute: "Today",
			timestamp_relative: utils.generateTimestampRelative(),
			timestamp_time_passed: "0",
			is_unread: false,
			is_cleared: false,
			is_forward: false,
			is_filtered_content: false,
			is_filtered_content_bh: false,
			is_filtered_content_account: false,
			is_spoof_warning: false,
			source: "source:chat:web",
			"source_tags[0]": "source:chat",
			log_message_type: "log:subscribe",
			status: "0",
			offline_threading_id: messageAndOTID,
			message_id: messageAndOTID,
			threading_id: utils.generateThreadingID(ctx.clientID),
			manual_retry_cnt: "0",
			thread_fbid: threadID
		};

		for (let i = 0; i < userID.length; i++) {
			if (
				utils.getType(userID[i]) !== "Number" &&
				utils.getType(userID[i]) !== "String"
			) {
				throw new utils.CustomError({
					error:
						"Elements of userID should be of type Number or String and not " +
						utils.getType(userID[i]) +
						"."
				});
			}

			form["log_message_data[added_participants][" + i + "]"] =
				"fbid:" + userID[i];
		}

		defaultFuncs
			.post("https://www.facebook.com/messaging/send/", ctx.jar, form)
			.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
			.then(function (resData) {
				if (!resData) {
					throw new utils.CustomError({ error: "Add to group failed." });
				}
				if (resData.error) {
					throw new utils.CustomError(resData);
				}

				return callback();
			})
			.catch(function (err) {
				log.error("addUserToGroup", err);
				return callback(err);
			});

		return returnPromise;
	};
};