Skip to content

Commit

Permalink
added handling for createaccount token (https://www.mediawiki.org/wik…
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Gesinn committed Oct 30, 2017
1 parent 3ca1888 commit 1e9d9ea
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const semlog = require('semlog');
const log = semlog.log;
const packageJson = require('../package.json');

Promise.config({
// Enable cancellation
cancellation: true,
});

/**
* MWBot library
*
Expand Down Expand Up @@ -52,6 +57,13 @@ class MWBot {
*/
this.editToken = false;

/**
* Bot instances createaccount token
*
* @type {boolean}
*/
this.createaccountToken = false;

/**
* Internal statistics
*
Expand Down Expand Up @@ -368,6 +380,40 @@ class MWBot {
});
}

/**
* Gets an edit token
* Requires MW 1.27+
*
* @returns {bluebird}
*/
getCreateaccountToken() {
return new Promise((resolve, reject) => {

if (this.createaccountToken) {
return resolve(this.state);
}

// MW 1.27+
this.request({
action: 'query',
meta: 'tokens',
type: 'createaccount'
}).then((response) => {
if (response.query && response.query.tokens && response.query.tokens.createaccounttoken) {
this.createaccountToken = response.query.tokens.createaccounttoken;
this.state = MWBot.merge(this.state, response.query.tokens);
return resolve(this.state);
} else {
let err = new Error('Could not get createaccount token');
err.response = response;
return reject(err) ;
}
}).catch((err) => {
return reject(err);
});
});
}

/**
* Combines Login with GetEditToken
*
Expand All @@ -382,6 +428,20 @@ class MWBot {
}


/**
* Combines Login with GetCreateaccountToken
*
* @param loginOptions
*
* @returns {bluebird}
*/
loginGetCreateaccountToken(loginOptions) {
return this.login(loginOptions).then(() => {
return this.getCreateaccountToken();
});
}


//////////////////////////////////////////
// CRUD OPERATIONS //
//////////////////////////////////////////
Expand Down

0 comments on commit 1e9d9ea

Please sign in to comment.