Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Add changeAdminStatus #658

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [`login`](#login)
* [`api.addUserToGroup`](#addUserToGroup)
* [`api.changeAdminStatus`](#changeAdminStatus)
* [`api.changeArchivedStatus`](#changeArchivedStatus)
* [`api.changeBlockedStatus`](#changeBlockedStatus)
* [`api.changeGroupImage`](#changeGroupImage)
Expand Down Expand Up @@ -173,6 +174,34 @@ __Arguments__

---------------------------------------

<a name="changeAdminStatus"></a>
### 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);
});
});
```

---------------------------------------

<a name="changeArchivedStatus"></a>
### api.changeArchivedStatus(threadOrThreads, archive[, callback])

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function buildAPI(globalOptions, html, jar) {
'changeGroupImage',
'changeThreadColor',
'changeThreadEmoji',
'changeAdminStatus',
'changeNickname',
'createPoll',
'deleteMessage',
Expand Down
75 changes: 75 additions & 0 deletions src/changeAdminStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"use strict";

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

module.exports = function(defaultFuncs, api, ctx) {
return function changeAdminStatus(userIDs, makeAdmin, threadID, callback) {
if (
utils.getType(userIDs) !== "String" &&
utils.getType(userIDs) !== "Array"
) {
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 form = {
thread_fbid: threadID,
add: makeAdmin
};

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 && resData.error === 1357031) {
throw {
error: "Cannot alter admin status: this thread is not a group chat."
};
}

if (resData.error) {
throw resData;
}

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