Skip to content

Commit

Permalink
user: add contribsGen, logsGen
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Nov 7, 2020
1 parent ef2b017 commit d7b25a9
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 7 deletions.
7 changes: 5 additions & 2 deletions build/bot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { MwnError, MwnErrorConfig } from "./error";
import type { Link, CategoryLink, FileLink, PageLink, Template, TemplateConfig, Section } from "./wikitext";
import type { ApiDeleteParams, ApiEditPageParams, ApiMoveParams, ApiParseParams, ApiPurgeParams, ApiQueryAllPagesParams, ApiQueryCategoryMembersParams, ApiQuerySearchParams, ApiRollbackParams, ApiUndeleteParams, ApiUploadParams, ApiEmailUserParams, ApiQueryRevisionsParams, ApiQueryLogEventsParams, ApiQueryBacklinkspropParams, ApiQueryCategoriesParams, ApiQueryUserContribsParams, ApiBlockParams, ApiUnblockParams } from "./api_params";
import type { recentchangeProps } from "./eventstream";
import type { LogEvent, UserContribution } from "./user";
export declare type revisionprop = "content" | "timestamp" | "user" | "comment" | "parsedcomment" | "ids" | "flags" | "size" | "tags" | "userid" | "contentmodel";
export declare type logprop = "type" | "user" | "comment" | "details" | "timestamp" | "title" | "parsedcomment" | "ids" | "tags" | "userid";
export interface RawRequestParams extends AxiosRequestConfig {
Expand Down Expand Up @@ -143,8 +144,10 @@ export interface MwnUser extends MwnTitle {
username: string;
userpage: MwnPage;
talkpage: MwnPage;
contribs(options?: ApiQueryUserContribsParams): Promise<any[]>;
logs(options?: ApiQueryLogEventsParams): Promise<any[]>;
contribs(options?: ApiQueryUserContribsParams): Promise<UserContribution[]>;
contribsGen(options?: ApiQueryUserContribsParams): AsyncGenerator<UserContribution>;
logs(options?: ApiQueryLogEventsParams): Promise<LogEvent[]>;
logsGen(options?: ApiQueryLogEventsParams): AsyncGenerator<LogEvent>;
info(props?: string | string[]): Promise<any>;
globalinfo(props?: ("groups" | "rights" | "merged" | "unattached" | "editcount")[]): Promise<any>;
sendMessage(header: string, message: string): Promise<any>;
Expand Down
29 changes: 28 additions & 1 deletion build/user.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
export {};
export declare type UserContribution = {
userid: number;
user: string;
pageid: number;
revid: number;
parentid: number;
ns: number;
title: string;
timestamp: string;
new: boolean;
minor: boolean;
top: boolean;
comment: string;
size: number;
};
export declare type LogEvent = {
logid: number;
ns: number;
title: string;
pageid: number;
logpage: number;
params: any;
type: string;
action: string;
user: string;
timestamp: string;
comment: string;
};
28 changes: 28 additions & 0 deletions build/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ module.exports = function (bot) {
...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;
}
}
}
/**
* Get user's recent log actions
* @param {Object} options - additional API options
Expand All @@ -46,6 +60,20 @@ module.exports = function (bot) {
...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;
}
}
}
/**
* Get public information about the user
* @param {Array} props - properties to fetch
Expand Down
7 changes: 5 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import type {
ApiQueryUserContribsParams, ApiBlockParams, ApiUnblockParams
} from "./api_params";
import type {recentchangeProps} from "./eventstream";
import type {LogEvent, UserContribution} from "./user";

export type revisionprop = "content" | "timestamp" | "user" | "comment" | "parsedcomment" | "ids" | "flags" |
"size" | "tags" | "userid" | "contentmodel"
Expand Down Expand Up @@ -154,8 +155,10 @@ export interface MwnUser extends MwnTitle {
talkpage: MwnPage
// get userpage(): MwnPage // XXX
// get talkpage(): MwnPage
contribs(options?: ApiQueryUserContribsParams): Promise<any[]>
logs(options?: ApiQueryLogEventsParams): Promise<any[]>
contribs(options?: ApiQueryUserContribsParams): Promise<UserContribution[]>
contribsGen(options?: ApiQueryUserContribsParams): AsyncGenerator<UserContribution>
logs(options?: ApiQueryLogEventsParams): Promise<LogEvent[]>
logsGen(options?: ApiQueryLogEventsParams): AsyncGenerator<LogEvent>
info(props?: string | string[]): Promise<any>
globalinfo(props?: ("groups"|"rights"|"merged"|"unattached"|"editcount")[]): Promise<any>
sendMessage(header: string, message: string): Promise<any>
Expand Down
63 changes: 61 additions & 2 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ import type {
ApiUnblockParams
} from "./api_params";

export type UserContribution = {
userid: number
user: string
pageid: number
revid: number
parentid: number
ns: number
title: string
timestamp: string
new: boolean
minor: boolean
top: boolean
comment: string
size: number
}

export type LogEvent = {
logid: number
ns: number
title: string
pageid: number
logpage: number
params: any
type: string
action: string
user: string
timestamp: string
comment: string
}

module.exports = function(bot: mwn) {

class User {
Expand Down Expand Up @@ -37,7 +67,7 @@ module.exports = function(bot: mwn) {
* @param {Object} options - additional API options
* @returns {Promise<Object[]>}
*/
contribs(options?: ApiQueryUserContribsParams): Promise<any[]> {
contribs(options?: ApiQueryUserContribsParams): Promise<UserContribution[]> {
return bot.request({
action: 'query',
list: 'usercontribs',
Expand All @@ -47,13 +77,27 @@ module.exports = function(bot: mwn) {
}).then(data => data.query.usercontribs);
}

async *contribsGen(options?: ApiQueryUserContribsParams): AsyncGenerator<UserContribution> {
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;
}
}
}

/**
* Get user's recent log actions
* @param {Object} options - additional API options
* @returns {Promise<Object[]>}
*/
logs(options?: ApiQueryLogEventsParams): Promise<any[]> {
logs(options?: ApiQueryLogEventsParams): Promise<LogEvent[]> {
return bot.request({
action: 'query',
list: 'logevents',
Expand All @@ -63,6 +107,21 @@ module.exports = function(bot: mwn) {
}).then(data => data.query.logevents);
}

async *logsGen(options?: ApiQueryLogEventsParams): AsyncGenerator<LogEvent> {
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;
}
}
}

/**
* Get public information about the user
* @param {Array} props - properties to fetch
Expand Down

0 comments on commit d7b25a9

Please sign in to comment.