Skip to content

Commit

Permalink
talk page modification abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Jul 13, 2022
1 parent f7c0931 commit a7cc696
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions src/wiki/TalkPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import normalizeTitle from '../util/normalizeTitle';
import decorateEditSummary from '../util/decorateEditSummary';

/**
* Options for performing edits with {@link TalkPage}.
*/
interface TalkPageEditOptions {
/**
* The <b>undecorated</b> summary to use for this edit.
*/
summary?: string;
}

/**
* Class responsible for handling talk page actions (posting messages, sections, etc.)
*/
export default class TalkPage {

talkPage: mw.Title;

/**
* @param basePage The subject/talk page to get the talk page of
*/
constructor( basePage: mw.Title | string ) {
const normalizedTitle = normalizeTitle( basePage );
if ( normalizedTitle.isTalkPage() ) {
this.talkPage = normalizedTitle;
} else if ( normalizedTitle.canHaveTalkPage() ) {
this.talkPage = normalizedTitle.getTalkPage();
} else {
throw new Error( normalizedTitle.getPrefixedText() + ' cannot have a talk page' );
}
}

/**
* Appends to the talk page. By default, two newlines are appended before the message
* provided. To disable this, set the `newLines` option to 0.
*
* No signature is appended; one must already be present in `message`.
*
* @param message The message to append
* @param options Extra option
* @param editOptions Data to be directly passed into the POST request.
*/
async append(
message: string,
options: TalkPageEditOptions & { newLines?: number; } = {},
editOptions: Record<string, any> = {}
): Promise<void> {
const finalMessage = '\n'.repeat( ( options.newLines ?? 2 ) ) + message;
await window.deputy.wiki.postWithEditToken(
Object.assign( {
// Overridable options.
redirect: true
}, editOptions, {
// Non-overridable options
action: 'edit',
title: this.talkPage.getPrefixedText(),
summary: decorateEditSummary( options.summary ),
appendtext: finalMessage
} )
);
}

/**
* Creates a new section on the talk page.
*
* No signature is appended; one must already be present in `message`.
*
* @param sectionTitle The title of the section
* @param message The message to place in the section
* @param options Extra option
* @param editOptions Data to be directly passed into the POST request.
*/
async addNewSection(
sectionTitle: string,
message: string,
options: TalkPageEditOptions = {},
editOptions: Record<string, any> = {}
): Promise<void> {
await window.deputy.wiki.postWithEditToken(
Object.assign( {
// Overridable options.
redirect: true
}, editOptions, {
// Non-overridable options
action: 'edit',
title: this.talkPage.getPrefixedText(),
summary: decorateEditSummary( options.summary ),
section: 'new',
sectiontitle: sectionTitle,
text: message
} )
);
}

}

0 comments on commit a7cc696

Please sign in to comment.