mwn
Version:
JavaScript & TypeScript MediaWiki bot framework for Node.js
156 lines • 4.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const error_1 = require("./error");
function default_1(bot) {
class User {
/**
* @constructor
* @param {string} name - username
*/
constructor(name) {
this.username = name;
}
get userpage() {
// User: namespace name will work across all MW sites
// as it is a canonical namespace name
return new bot.Page('User:' + this.username);
}
get talkpage() {
return new bot.Page('User talk:' + this.username);
}
// XXX: should these yield rather than return?
/** @inheritDoc */
contribs(options) {
return bot
.request({
action: 'query',
list: 'usercontribs',
ucuser: this.username,
uclimit: 'max',
...options,
})
.then((data) => data.query.usercontribs);
}
async *contribsGen(options) {
let continuedQuery = bot.continuedQueryGen({
action: 'query',
list: 'usercontribs',
ucuser: this.username,
uclimit: 'max',
...options,
});
for await (let json of continuedQuery) {
for (let edit of json.query.usercontribs) {
yield edit;
}
}
}
/** @inheritDoc */
logs(options) {
return bot
.request({
action: 'query',
list: 'logevents',
leuser: this.username,
lelimit: 'max',
...options,
})
.then((data) => data.query.logevents);
}
async *logsGen(options) {
let continuedQuery = bot.continuedQueryGen({
action: 'query',
list: 'logevents',
leuser: this.username,
lelimit: 'max',
...options,
});
for await (let json of continuedQuery) {
for (let action of json.query.logevents) {
yield action;
}
}
}
/** @inheritDoc */
info(props) {
return bot
.request({
action: 'query',
list: 'users',
ususers: this.username,
usprop: props ||
'editcount|registration|blockinfo|emailable|gender|' +
'rights|groups|groupmemberships|implicitgroups',
})
.then((data) => data.query.users[0]);
}
/** @inheritDoc */
globalinfo(props) {
return bot
.request({
action: 'query',
meta: 'globaluserinfo',
guiuser: this.username,
guiprop: props || '',
})
.then((data) => {
return data.query.globaluserinfo;
});
}
/** @inheritDoc */
sendMessage(header, message) {
return this.talkpage.newSection(header, message);
}
/** @inheritDoc */
email(subject, message, options) {
return bot
.request({
action: 'emailuser',
target: this.username,
subject: subject,
text: message,
token: bot.csrfToken,
...options,
})
.then((response) => {
let data = response.emailuser;
if (data.result === 'Success') {
return data;
}
else {
return (0, error_1.rejectWithError)({
// try to get an error code and info
code: data.errors?.[0]?.code,
info: data.errors?.[0]?.info,
...data,
});
}
});
}
/** @inheritDoc */
block(options) {
return bot
.request({
action: 'block',
user: this.username,
token: bot.csrfToken,
...options,
})
.then((data) => data.block);
}
/** @inheritDoc */
unblock(options) {
return bot
.request({
action: 'unblock',
user: this.username,
token: bot.csrfToken,
...options,
})
.then((data) => data.unblock);
}
}
return User;
}
//# sourceMappingURL=user.js.map