| 1 | ( function () { |
| 2 | /** |
| 3 | * Abstract notification API handler |
| 4 | * |
| 5 | * @abstract |
| 6 | * @class |
| 7 | * |
| 8 | * @constructor |
| 9 | * @param {mw.Api} api |
| 10 | * @param {Object} [config] Configuration object |
| 11 | * @param {number} [config.limit=25] The limit on how many notifications to fetch |
| 12 | * @param {string} [config.userLang=mw.config.get( 'wgUserLanguage' )] User language. Defaults |
| 13 | * to the default user language configuration settings. |
| 14 | */ |
| 15 | mw.echo.api.APIHandler = function MwEchoApiAPIHandler( api, config ) { |
| 16 | config = config || {}; |
| 17 | |
| 18 | this.fetchNotificationsPromise = {}; |
| 19 | this.apiErrorState = {}; |
| 20 | |
| 21 | this.limit = config.limit || 25; |
| 22 | this.userLang = config.userLang || mw.config.get( 'wgUserLanguage' ); |
| 23 | |
| 24 | this.api = api; |
| 25 | |
| 26 | // Map the logical type to the type |
| 27 | // that the API recognizes |
| 28 | this.normalizedType = { |
| 29 | message: 'message', |
| 30 | alert: 'alert', |
| 31 | all: 'message|alert' |
| 32 | }; |
| 33 | |
| 34 | // Parameters that are sent through |
| 35 | // to the 'fetch notification' promise |
| 36 | // per type |
| 37 | this.typeParams = { |
| 38 | message: {}, |
| 39 | alert: {}, |
| 40 | all: {} |
| 41 | }; |
| 42 | }; |
| 43 | |
| 44 | /* Setup */ |
| 45 | |
| 46 | OO.initClass( mw.echo.api.APIHandler ); |
| 47 | |
| 48 | /** |
| 49 | * Fetch notifications from the API. |
| 50 | * |
| 51 | * @param {string} type Notification type |
| 52 | * @param {Object} [overrideParams] An object defining parameters to override in the API |
| 53 | * fetching call. |
| 54 | * @return {jQuery.Promise} A promise that resolves with an object containing the |
| 55 | * notification items |
| 56 | */ |
| 57 | mw.echo.api.APIHandler.prototype.fetchNotifications = null; |
| 58 | |
| 59 | /** |
| 60 | * Send a general query to the API. This is mostly for dynamic actions |
| 61 | * where other extensions may set up API actions that are unique and |
| 62 | * unanticipated. |
| 63 | * |
| 64 | * @param {Object} data Data object about the operation. |
| 65 | * @param {string} [data.tokenType=csrf] Token type, 'csrf', 'watch', etc |
| 66 | * @param {Object} [data.params] Parameters to pass to the API call |
| 67 | * @return {jQuery.Promise} Promise that is resolved when the action |
| 68 | * is complete |
| 69 | */ |
| 70 | mw.echo.api.APIHandler.prototype.queryAPI = function ( data ) { |
| 71 | return this.api.postWithToken( data.tokenType || 'csrf', data.params ); |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * Fetch all pages with unread notifications in them per wiki |
| 76 | * |
| 77 | * @param {string|string[]} [sources=*] Requested sources. If not given |
| 78 | * or if a '*' is given, all available sources will be queried |
| 79 | * @return {jQuery.Promise} Promise that is resolved with an object |
| 80 | * of pages with the number of unread notifications per wiki |
| 81 | */ |
| 82 | mw.echo.api.APIHandler.prototype.fetchUnreadNotificationPages = function ( sources ) { |
| 83 | sources = sources || '*'; |
| 84 | const params = { |
| 85 | action: 'query', |
| 86 | meta: 'unreadnotificationpages', |
| 87 | uselang: this.userLang, |
| 88 | unpgrouppages: true, |
| 89 | unpwikis: sources |
| 90 | }; |
| 91 | |
| 92 | return this.api.get( params ); |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * Check if the given source is local |
| 97 | * |
| 98 | * @param {string|string[]} sources Source names |
| 99 | * @return {boolean} Source is local |
| 100 | */ |
| 101 | mw.echo.api.APIHandler.prototype.isSourceLocal = function ( sources ) { |
| 102 | return Array.isArray( sources ) ? |
| 103 | ( |
| 104 | sources.indexOf( 'local' ) !== -1 || |
| 105 | sources.indexOf( mw.config.get( 'wgWikiID' ) ) !== -1 |
| 106 | ) : |
| 107 | ( |
| 108 | sources === 'local' || |
| 109 | sources === mw.config.get( 'wgWikiID' ) |
| 110 | ); |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * Create a new fetchNotifications promise that queries the API and overrides |
| 115 | * the cached promise. |
| 116 | * |
| 117 | * @param {string} type Notification type |
| 118 | * @param {string[]} [sources] An array of sources to query |
| 119 | * @param {Object} [overrideParams] An object defining parameters to override in the API |
| 120 | * fetching call. |
| 121 | * @return {jQuery.Promise} Promise that is resolved when notifications are |
| 122 | * fetched from the API. |
| 123 | */ |
| 124 | mw.echo.api.APIHandler.prototype.createNewFetchNotificationPromise = function ( type, sources, overrideParams ) { |
| 125 | const params = Object.assign( { |
| 126 | action: 'query', |
| 127 | formatversion: 2, |
| 128 | meta: 'notifications', |
| 129 | notsections: this.normalizedType[ type ], |
| 130 | notformat: 'model', |
| 131 | notlimit: this.limit, |
| 132 | notprop: 'list|count|seenTime', |
| 133 | uselang: this.userLang |
| 134 | }, this.getTypeParams( type ) ); |
| 135 | |
| 136 | let fetchingSource = 'local'; |
| 137 | if ( !this.isSourceLocal( sources ) ) { |
| 138 | params.notwikis = sources; |
| 139 | params.notfilter = '!read'; |
| 140 | fetchingSource = 'foreign'; |
| 141 | } |
| 142 | |
| 143 | // Initialize the nested value if it doesn't yet exist |
| 144 | this.fetchNotificationsPromise[ type ] = this.fetchNotificationsPromise[ type ] || {}; |
| 145 | this.apiErrorState[ type ] = this.apiErrorState[ type ] || {}; |
| 146 | |
| 147 | // Reset cached values |
| 148 | this.fetchNotificationsPromise[ type ][ fetchingSource ] = null; |
| 149 | this.apiErrorState[ type ][ fetchingSource ] = false; |
| 150 | |
| 151 | // Create the fetch promise |
| 152 | const fetchNotifPromise = this.api.get( $.extend( true, params, overrideParams ) ); |
| 153 | |
| 154 | // Only cache promises that don't have override params in them |
| 155 | if ( !overrideParams ) { |
| 156 | this.fetchNotificationsPromise[ type ][ fetchingSource ] = fetchNotifPromise; |
| 157 | } |
| 158 | |
| 159 | return fetchNotifPromise |
| 160 | .fail( () => { |
| 161 | // Mark API error state |
| 162 | this.apiErrorState[ type ][ fetchingSource ] = true; |
| 163 | } ); |
| 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * Update the seen timestamp |
| 168 | * |
| 169 | * @param {string|string[]} [types] Notification type 'message', 'alert' or [ 'message', 'alert' ]. |
| 170 | * @return {jQuery.Promise} A promise that resolves with the seen timestamp, as a full UTC |
| 171 | * ISO 8601 timestamp. |
| 172 | */ |
| 173 | mw.echo.api.APIHandler.prototype.updateSeenTime = null; |
| 174 | |
| 175 | /** |
| 176 | * Mark all notifications as read |
| 177 | * |
| 178 | * @param {string} source Wiki name |
| 179 | * @param {string|string[]} type Notification type 'message', 'alert' or 'all'. |
| 180 | * @return {jQuery.Promise} A promise that resolves when all notifications |
| 181 | * are marked as read. |
| 182 | */ |
| 183 | mw.echo.api.APIHandler.prototype.markAllRead = null; |
| 184 | |
| 185 | /** |
| 186 | * Mark multiple notification items as read using specific IDs |
| 187 | * |
| 188 | * @abstract |
| 189 | * @param {string} source Wiki name |
| 190 | * @param {string[]} itemIdArray An array of notification item IDs |
| 191 | * @param {boolean} [isRead] Item's new read state; true for marking the item |
| 192 | * as read, false for marking the item as unread |
| 193 | * @return {jQuery.Promise} A promise that resolves when all given notifications |
| 194 | * are marked as read. |
| 195 | */ |
| 196 | mw.echo.api.APIHandler.prototype.markItemsRead = null; |
| 197 | |
| 198 | /** |
| 199 | * Update the read status of a notification item in the API |
| 200 | * |
| 201 | * @param {string} itemId Item id |
| 202 | * @param {boolean} [isRead] Item's new read state; true for marking the item |
| 203 | * as read, false for marking the item as unread |
| 204 | * @return {jQuery.Promise} A promise that resolves when the notifications |
| 205 | * are marked as read. |
| 206 | */ |
| 207 | mw.echo.api.APIHandler.prototype.markItemRead = function ( itemId, isRead ) { |
| 208 | return this.markItemsRead( [ itemId ], isRead ); |
| 209 | }; |
| 210 | |
| 211 | /** |
| 212 | * Query the API for unread count of the notifications in this model |
| 213 | * |
| 214 | * @param {string} type Notification type 'message', 'alert' or 'all'. |
| 215 | * @return {jQuery.Promise} jQuery promise that's resolved when the unread count is fetched |
| 216 | * and the badge label is updated. |
| 217 | */ |
| 218 | mw.echo.api.APIHandler.prototype.fetchUnreadCount = null; |
| 219 | |
| 220 | /** |
| 221 | * Check whether the model has an API error state flagged |
| 222 | * |
| 223 | * @param {string} type Notification type, 'alert', 'message' or 'all' |
| 224 | * @param {string|string[]} sources Source names |
| 225 | * @return {boolean} The model is in API error state |
| 226 | */ |
| 227 | mw.echo.api.APIHandler.prototype.isFetchingErrorState = function ( type, sources ) { |
| 228 | let fetchingSource = 'local'; |
| 229 | |
| 230 | if ( !this.isSourceLocal( sources ) ) { |
| 231 | fetchingSource = 'foreign'; |
| 232 | } |
| 233 | return !!( this.apiErrorState[ type ] && this.apiErrorState[ type ][ fetchingSource ] ); |
| 234 | }; |
| 235 | |
| 236 | /** |
| 237 | * Return the fetch notifications promise |
| 238 | * |
| 239 | * @param {string} type Notification type, 'alert', 'message' or 'all' |
| 240 | * @param {string|string[]} [sources] A name of a source or an array of sources to query |
| 241 | * @param {Object} [overrideParams] An object defining parameters to override in the API |
| 242 | * fetching call. |
| 243 | * @return {jQuery.Promise} Promise that is resolved when notifications are |
| 244 | * fetched from the API. |
| 245 | */ |
| 246 | mw.echo.api.APIHandler.prototype.getFetchNotificationPromise = function ( type, sources, overrideParams ) { |
| 247 | let fetchingSource = 'local'; |
| 248 | |
| 249 | if ( !this.isSourceLocal( sources ) ) { |
| 250 | fetchingSource = 'foreign'; |
| 251 | } |
| 252 | if ( overrideParams || !this.fetchNotificationsPromise[ type ] || !this.fetchNotificationsPromise[ type ][ fetchingSource ] ) { |
| 253 | this.createNewFetchNotificationPromise( type, sources, overrideParams ); |
| 254 | } |
| 255 | return this.fetchNotificationsPromise[ type ][ fetchingSource ]; |
| 256 | }; |
| 257 | |
| 258 | /** |
| 259 | * Get the extra parameters for fetching notifications for a given |
| 260 | * notification type. |
| 261 | * |
| 262 | * @param {string} type Notification type |
| 263 | * @return {Object} Extra API parameters for fetch notifications |
| 264 | */ |
| 265 | mw.echo.api.APIHandler.prototype.getTypeParams = function ( type ) { |
| 266 | return this.typeParams[ type ]; |
| 267 | }; |
| 268 | }() ); |