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

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

module.exports = function (defaultFuncs, api, ctx) {
	/**
	 * Refreshes the fb_dtsg and jazoest values.
	 * @param {Function} callback
	 * @returns {Promise}
	 * @description if you don't update the value of fb_dtsg and jazoest for a long time an error "Please try closing and re-opening your browser window" will appear
	 * @description you should refresh it every 48h or less
	 */
	return function refreshFb_dtsg(obj, callback) {
		let resolveFunc = function () { };
		let rejectFunc = function () { };
		const returnPromise = new Promise(function (resolve, reject) {
			resolveFunc = resolve;
			rejectFunc = reject;
		});

		if (utils.getType(obj) === "Function" || utils.getType(obj) === "AsyncFunction") {
			callback = obj;
			obj = {};
		}

		if (!obj) {
			obj = {};
		}

		if (utils.getType(obj) !== "Object") {
			throw new utils.CustomError("the first parameter must be an object or a callback function");
		}

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

		if (Object.keys(obj).length == 0) {
			utils
				.get('https://m.facebook.com/', ctx.jar, null, ctx.globalOptions, { noRef: true })
				.then(function (resData) {
					const html = resData.body;
					const fb_dtsg = utils.getFrom(html, 'name="fb_dtsg" value="', '"');
					const jazoest = utils.getFrom(html, 'name="jazoest" value="', '"');
					if (!fb_dtsg) {
						throw new utils.CustomError("Could not find fb_dtsg in HTML after requesting https://www.facebook.com/");
					}
					ctx.fb_dtsg = fb_dtsg;
					ctx.jazoest = jazoest;
					callback(null, {
						data: {
							fb_dtsg: fb_dtsg,
							jazoest: jazoest
						},
						message: "refreshed fb_dtsg and jazoest"
					});
				})
				.catch(function (err) {
					log.error("refreshFb_dtsg", err);
					return callback(err);
				});
		}
		else {
			Object.keys(obj).forEach(function (key) {
				ctx[key] = obj[key];
			});
			callback(null, {
				data: obj,
				message: "refreshed " + Object.keys(obj).join(", ")
			});
		}

		return returnPromise;
	};
};