From e19568dd21c6854644bba1c019684218731b3316 Mon Sep 17 00:00:00 2001 From: Cameron Bernhardt Date: Wed, 13 Jun 2018 17:16:00 -0700 Subject: [PATCH 1/3] Add changeAdminStatus --- DOCS.md | 29 +++++++++++++++ README.md | 1 + index.js | 1 + src/changeAdminStatus.js | 78 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 src/changeAdminStatus.js diff --git a/DOCS.md b/DOCS.md index eccb7288..7f1e9eb2 100644 --- a/DOCS.md +++ b/DOCS.md @@ -2,6 +2,7 @@ * [`login`](#login) * [`api.addUserToGroup`](#addUserToGroup) +* [`api.changeAdminStatus`](#changeAdminStatus) * [`api.changeArchivedStatus`](#changeArchivedStatus) * [`api.changeBlockedStatus`](#changeBlockedStatus) * [`api.changeGroupImage`](#changeGroupImage) @@ -173,6 +174,34 @@ __Arguments__ --------------------------------------- + +### api.changeAdminStatus(userIDs, makeAdmin, threadID[, callback]) + +Given a userID, or an array of userIDs, will set the admin status of the user(s) to `makeAdmin`. + +__Arguments__ +* `userIDs`: The id(s) of users you wish to admin/unadmin (either a string indicating a user's ID or an array of them). +* `makeAdmin`: Boolean indicating whether the user(s) should be promoted to admin (`true`) or demoted to a regular user (`false`). +* `callback(err)`: A callback called when the query is done (either with an error or null). + +__Example__ + +```js +const fs = require("fs"); +const login = require("facebook-chat-api"); + +login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => { + if(err) return console.error(err); + + // Makes user 000000000000000 an admin in thread 111111111111111 + api.changeAdminStatus("000000000000000", true, "111111111111111", (err) => { + if(err) return console.error(err); + }); +}); +``` + +--------------------------------------- + ### api.changeArchivedStatus(threadOrThreads, archive[, callback]) diff --git a/README.md b/README.md index 7dbadbdc..b720e20b 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Result: * [`login`](DOCS.md#login) * [`api.addUserToGroup`](DOCS.md#addUserToGroup) +* [`api.changeAdminStatus`](DOCS.md#changeAdminStatus) * [`api.changeArchivedStatus`](DOCS.md#changeArchivedStatus) * [`api.changeBlockedStatus`](DOCS.md#changeBlockedStatus) * [`api.changeGroupImage`](DOCS.md#changeGroupImage) diff --git a/index.js b/index.js index ffd759d6..8d9cb66f 100644 --- a/index.js +++ b/index.js @@ -82,6 +82,7 @@ function buildAPI(globalOptions, html, jar) { 'changeGroupImage', 'changeThreadColor', 'changeThreadEmoji', + 'changeAdminStatus', 'changeNickname', 'createPoll', 'deleteMessage', diff --git a/src/changeAdminStatus.js b/src/changeAdminStatus.js new file mode 100644 index 00000000..65d37538 --- /dev/null +++ b/src/changeAdminStatus.js @@ -0,0 +1,78 @@ +"use strict"; + +var utils = require("../utils"); +var log = require("npmlog"); + +module.exports = function(defaultFuncs, api, ctx) { + return function changeAdminStatus(userIDs, makeAdmin, threadID, callback) { + if ( + !callback && + (utils.getType(makeAdmin) === "AsyncFunction" || + utils.getType(makeAdmin) === "AsyncFunction" || + utils.getType(threadID) === "Function" || + utils.getType(threadID) === "AsyncFunction") + ) { + throw { error: "please pass makeAdmin and threadID as the second/third arguments." }; + } + + if (utils.getType(userIDs) === "String") { + userIDs = [userIDs]; + } + + if (!callback) { + callback = function() {}; + } + + var messageAndOTID = utils.generateOfflineThreadingID(); + var form = { + client: "mercury", + action_type: "ma-type:log-message", + author: "fbid:" + ctx.userID, + author_email: "", + coordinates: "", + 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_spoof_warning: false, + source: "source:chat:web", + "source_tags[0]": "source:chat", + status: "0", + offline_threading_id: messageAndOTID, + message_id: messageAndOTID, + threading_id: utils.generateThreadingID(ctx.clientID), + manual_retry_cnt: "0", + thread_fbid: threadID, + add: makeAdmin, + thread_id: threadID, + log_message_type: "log:thread-name" + }; + + for (var i = 0; i < userIDs.length; i++) { + form["admin_ids[" + i + "]"] = userIDs[i]; + } + + defaultFuncs + .post("https://www.messenger.com/messaging/save_admins/", ctx.jar, form) + .then(utils.parseAndCheckLogin(ctx, defaultFuncs)) + .then(function(resData) { + if (resData.error && resData.error === 1976004) { + throw { error: "Cannot alter admin status: you are not an admin." }; + } + + if (resData.error) { + throw resData; + } + + return callback(); + }) + .catch(function(err) { + log.error("changeAdminStatus", err); + return callback(err); + }); + }; +}; From bc1597ab80f27d0a53ae6ebfde51598203d24a92 Mon Sep 17 00:00:00 2001 From: Cameron Bernhardt Date: Wed, 13 Jun 2018 17:20:45 -0700 Subject: [PATCH 2/3] Add error handling for changing status in non-group chats --- src/changeAdminStatus.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/changeAdminStatus.js b/src/changeAdminStatus.js index 65d37538..4307dee4 100644 --- a/src/changeAdminStatus.js +++ b/src/changeAdminStatus.js @@ -64,6 +64,10 @@ module.exports = function(defaultFuncs, api, ctx) { throw { error: "Cannot alter admin status: you are not an admin." }; } + if (resData.error && resData.error === 1357031) { + throw { error: "Cannot alter admin status: this thread is not a group chat." }; + } + if (resData.error) { throw resData; } From 64f37680b902751864134e120ab436df3abd399c Mon Sep 17 00:00:00 2001 From: Cameron Bernhardt Date: Thu, 14 Jun 2018 23:29:37 -0700 Subject: [PATCH 3/3] Clean up --- src/changeAdminStatus.js | 59 ++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/changeAdminStatus.js b/src/changeAdminStatus.js index 4307dee4..ee1c432b 100644 --- a/src/changeAdminStatus.js +++ b/src/changeAdminStatus.js @@ -6,50 +6,41 @@ var log = require("npmlog"); module.exports = function(defaultFuncs, api, ctx) { return function changeAdminStatus(userIDs, makeAdmin, threadID, callback) { if ( - !callback && - (utils.getType(makeAdmin) === "AsyncFunction" || - utils.getType(makeAdmin) === "AsyncFunction" || - utils.getType(threadID) === "Function" || - utils.getType(threadID) === "AsyncFunction") + utils.getType(userIDs) !== "String" && + utils.getType(userIDs) !== "Array" ) { - throw { error: "please pass makeAdmin and threadID as the second/third arguments." }; + throw { error: "userIDs must be a string or array" }; + } + + if (utils.getType(makeAdmin) !== "Boolean") { + throw { error: "makeAdmin must be a boolean" }; + } + + if (utils.getType(threadID) !== "String") { + throw { error: "threadID must be a string" }; } if (utils.getType(userIDs) === "String") { userIDs = [userIDs]; } + if ( + callback && + !( + utils.getType(callback) === "Function" || + utils.getType(callback) === "AsyncFunction" + ) + ) { + throw { error: "callback must be a function" }; + } + if (!callback) { callback = function() {}; } - var messageAndOTID = utils.generateOfflineThreadingID(); var form = { - client: "mercury", - action_type: "ma-type:log-message", - author: "fbid:" + ctx.userID, - author_email: "", - coordinates: "", - 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_spoof_warning: false, - source: "source:chat:web", - "source_tags[0]": "source:chat", - status: "0", - offline_threading_id: messageAndOTID, - message_id: messageAndOTID, - threading_id: utils.generateThreadingID(ctx.clientID), - manual_retry_cnt: "0", thread_fbid: threadID, - add: makeAdmin, - thread_id: threadID, - log_message_type: "log:thread-name" + add: makeAdmin }; for (var i = 0; i < userIDs.length; i++) { @@ -65,7 +56,9 @@ module.exports = function(defaultFuncs, api, ctx) { } if (resData.error && resData.error === 1357031) { - throw { error: "Cannot alter admin status: this thread is not a group chat." }; + throw { + error: "Cannot alter admin status: this thread is not a group chat." + }; } if (resData.error) { @@ -79,4 +72,4 @@ module.exports = function(defaultFuncs, api, ctx) { return callback(err); }); }; -}; +}; \ No newline at end of file