| 1 | /** |
| 2 | * @typedef {Object} mw.Api.WatchedPage |
| 3 | * @property {string} title Full page name |
| 4 | * @property {boolean} watched Whether the page is now watched (true) or unwatched (false) |
| 5 | */ |
| 6 | |
| 7 | ( function () { |
| 8 | |
| 9 | /** |
| 10 | * @private |
| 11 | * |
| 12 | * @param {string|mw.Title|string[]|mw.Title[]} pages Full page name or instance of mw.Title, or an |
| 13 | * array thereof. If an array is passed, the return value passed to the promise will also be an |
| 14 | * array of appropriate objects. |
| 15 | * @param {Object} [addParams] |
| 16 | * @return {jQuery.Promise<mw.Api.WatchedPage|mw.Api.WatchedPage[]>} |
| 17 | */ |
| 18 | function doWatchInternal( pages, addParams ) { |
| 19 | // XXX: Parameter addParams is undocumented because we inherit this |
| 20 | // documentation in the public method... |
| 21 | const apiPromise = this.postWithToken( 'watch', |
| 22 | Object.assign( |
| 23 | { |
| 24 | formatversion: 2, |
| 25 | action: 'watch', |
| 26 | titles: Array.isArray( pages ) ? pages : String( pages ) |
| 27 | }, |
| 28 | addParams |
| 29 | ) |
| 30 | ); |
| 31 | |
| 32 | return apiPromise |
| 33 | .then( |
| 34 | // If a single page was given (not an array) respond with a single item as well. |
| 35 | ( data ) => Array.isArray( pages ) ? data.watch : data.watch[ 0 ] |
| 36 | ) |
| 37 | .promise( { abort: apiPromise.abort } ); |
| 38 | } |
| 39 | |
| 40 | Object.assign( mw.Api.prototype, /** @lends mw.Api.prototype */ { |
| 41 | /** |
| 42 | * Convenience method for `action=watch`. |
| 43 | * |
| 44 | * @method |
| 45 | * @since 1.35 - expiry parameter can be passed when |
| 46 | * Watchlist Expiry is enabled |
| 47 | * @param {string|mw.Title|string[]|mw.Title[]} pages Full page name or instance of mw.Title, or an |
| 48 | * array thereof. If an array is passed, the return value passed to the promise will also be an |
| 49 | * array of appropriate objects. |
| 50 | * @param {string} [expiry] When the page should expire from the watchlist. If omitted, the |
| 51 | * page will not expire. |
| 52 | * @return {jQuery.Promise<mw.Api.WatchedPage|mw.Api.WatchedPage[]>} A promise that resolves |
| 53 | * with an object (or array of objects) describing each page that was passed in and its |
| 54 | * current watched/unwatched status. |
| 55 | */ |
| 56 | watch: function ( pages, expiry ) { |
| 57 | return doWatchInternal.call( this, pages, { expiry: expiry } ); |
| 58 | }, |
| 59 | |
| 60 | /** |
| 61 | * Convenience method for `action=watch&unwatch=1`. |
| 62 | * |
| 63 | * @method |
| 64 | * @param {string|mw.Title|string[]|mw.Title[]} pages Full page name or instance of mw.Title, or an |
| 65 | * array thereof. If an array is passed, the return value passed to the promise will also be an |
| 66 | * array of appropriate objects. |
| 67 | * @return {jQuery.Promise<mw.Api.WatchedPage|mw.Api.WatchedPage[]>} A promise that resolves |
| 68 | * with an object (or array of objects) describing each page that was passed in and its |
| 69 | * current watched/unwatched status. |
| 70 | */ |
| 71 | unwatch: function ( pages ) { |
| 72 | return doWatchInternal.call( this, pages, { unwatch: 1 } ); |
| 73 | } |
| 74 | } ); |
| 75 | |
| 76 | }() ); |