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

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

module.exports = function (defaultFuncs, api, ctx) {
	return function createNewGroup(participantIDs, groupTitle, callback) {
		if (utils.getType(groupTitle) == "Function") {
			callback = groupTitle;
			groupTitle = null;
		}

		if (utils.getType(participantIDs) !== "Array") {
			throw { error: "createNewGroup: participantIDs should be an array." };
		}

		if (participantIDs.length < 2) {
			throw { error: "createNewGroup: participantIDs should have at least 2 IDs." };
		}

		let resolveFunc = function () { };
		let rejectFunc = function () { };
		const returnPromise = new Promise(function (resolve, reject) {
			resolveFunc = resolve;
			rejectFunc = reject;
		});

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

		const pids = [];
		for (const n in participantIDs) {
			pids.push({
				fbid: participantIDs[n]
			});
		}
		pids.push({ fbid: ctx.i_userID || ctx.userID });

		const form = {
			fb_api_caller_class: "RelayModern",
			fb_api_req_friendly_name: "MessengerGroupCreateMutation",
			av: ctx.i_userID || ctx.userID,
			//This doc_id is valid as of January 11th, 2020
			doc_id: "577041672419534",
			variables: JSON.stringify({
				input: {
					entry_point: "jewel_new_group",
					actor_id: ctx.i_userID || ctx.userID,
					participants: pids,
					client_mutation_id: Math.round(Math.random() * 1024).toString(),
					thread_settings: {
						name: groupTitle,
						joinable_mode: "PRIVATE",
						thread_image_fbid: null
					}
				}
			})
		};

		defaultFuncs
			.post(
				"https://www.facebook.com/api/graphql/",
				ctx.jar,
				form
			)
			.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
			.then(function (resData) {
				if (resData.errors) {
					throw resData;
				}
				return callback(null, resData.data.messenger_group_thread_create.thread.thread_key.thread_fbid);
			})
			.catch(function (err) {
				log.error("createNewGroup", err);
				return callback(err);
			});

		return returnPromise;
	};
};