Debian Codegraph @

extensions/Echo/modules/api/mw.echo.api.EchoApi.js

raw symbols SCIP
1 ( function () {
2 /**
3 * A class defining Echo API instructions and network operations
4 *
5 * @class
6 *
7 * @constructor
8 * @param {Object} config Configuration options
9 * @param {number} [config.limit=25] Number of notifications to fetch
10 */
11 mw.echo.api.EchoApi = function MwEchoApiEchoApi( config ) {
12 config = config || {};
13
14 this.network = new mw.echo.api.NetworkHandler( config );
15
16 this.fetchingPromise = null;
17 this.limit = config.limit || 25;
18 this.fetchingPrioritizer = new mw.echo.api.PromisePrioritizer();
19 };
20
21 OO.initClass( mw.echo.api.EchoApi );
22
23 /**
24 * Register a set of foreign sources.
25 *
26 * @param {Object} sources Object mapping source names to config objects
27 * @param {boolean} [unreadOnly=false] Fetch only unread notifications
28 * @param {number} [limit] Specific limit of notifications. Defaults to
29 * the default limit stated in the class.
30 */
31 mw.echo.api.EchoApi.prototype.registerForeignSources = function ( sources, unreadOnly, limit ) {
32 limit = limit || this.limit;
33
34 for ( const s in sources ) {
35 this.network.setApiHandler( s, new mw.echo.api.ForeignAPIHandler( sources[ s ].url, {
36 unreadOnly: !!unreadOnly,
37 limit: limit
38 } ) );
39 }
40 };
41
42 /**
43 * Register a set of local sources.
44 *
45 * @param {string[]} sources An array of source names
46 */
47 mw.echo.api.EchoApi.prototype.registerLocalSources = function ( sources ) {
48 const localHandler = this.network.getApiHandler( 'local' );
49
50 for ( let i = 0; i < sources.length; i++ ) {
51 this.network.setApiHandler( sources[ i ], localHandler );
52 }
53 };
54
55 /**
56 * Fetch all pages with unread notifications in them per wiki
57 *
58 * @param {string[]} [sources=all] Requested sources
59 * @return {jQuery.Promise} Promise that is resolved with an object
60 * of pages with the number of unread notifications per wiki
61 */
62 mw.echo.api.EchoApi.prototype.fetchUnreadNotificationPages = function ( sources ) {
63 return this.network.getApiHandler( 'local' ).fetchUnreadNotificationPages( sources )
64 .then( ( data ) => OO.getProp( data, 'query', 'unreadnotificationpages' ) );
65 };
66
67 /**
68 * Fetch notifications from a given source with given filters
69 *
70 * @param {string} type Notification type to fetch: 'alert', 'message', or 'all'
71 * @param {string} [source] The source from which to fetch the notifications.
72 * If not given, the local notifications will be fetched.
73 * @param {Object} [filters] Filter values
74 * @return {jQuery.Promise} Promise that is resolved with all notifications for the
75 * requested types.
76 */
77 mw.echo.api.EchoApi.prototype.fetchFilteredNotifications = function ( type, source, filters ) {
78 source = source || 'local';
79
80 if ( source === 'local' ) {
81 return this.fetchNotifications( type, source, true, filters );
82 } else {
83 return this.fetchNotificationsFromRemoteSource( type, source, true, filters );
84 }
85 };
86
87 /**
88 * Convert the filter object to the relevant API parameters.
89 *
90 * @param {Object} [filterObject] The filter object
91 * @param {string} [filterObject.continue] A continue variable
92 * defining the offset to fetch notifications
93 * @param {string} [filterObject.readState] Notification read
94 * state, 'all', 'read' or 'unread'
95 * @param {boolean} [filterObject.unreadFirst] Fetch unread notifications
96 * first in the sorting order.
97 * @param {boolean} [filterObject.bundle] Bundle local notifications
98 * @param {string|string[]} [filterObject.titles] Requested titles. To request notifications with no title,
99 * use null (standalone or as an array element).
100 * @return {Object} API parameter definitions to override
101 */
102 mw.echo.api.EchoApi.prototype.convertFiltersToAPIParams = function ( filterObject ) {
103 const overrideParams = {};
104
105 filterObject = filterObject || {};
106
107 if ( filterObject.continue ) {
108 overrideParams.notcontinue = filterObject.continue;
109 }
110
111 if ( filterObject.unreadFirst ) {
112 overrideParams.notunreadfirst = 1;
113 }
114
115 if ( filterObject.bundle ) {
116 overrideParams.notbundle = 1;
117 }
118
119 if ( filterObject.readState && filterObject.readState !== 'all' ) {
120 overrideParams.notfilter = filterObject.readState === 'read' ?
121 'read' :
122 '!read';
123 }
124
125 if ( filterObject.titles ) {
126 const titles = Array.isArray( filterObject.titles ) ? filterObject.titles : [ filterObject.titles ];
127 if ( titles.indexOf( null ) !== -1 ) {
128 // Map null to '[]'
129 titles.splice( titles.indexOf( null ), 1, '[]' );
130 }
131 overrideParams.nottitles = titles;
132 }
133
134 return overrideParams;
135 };
136
137 /**
138 * Fetch remote notifications from a given source. This skips the local fetching that is
139 * usually done and calls the remote wiki directly.
140 *
141 * @param {string} type Notification type to fetch: 'alert', 'message', or 'all'
142 * @param {string|string[]} [source] The source from which to fetch the notifications.
143 * If not given, the local notifications will be fetched.
144 * @param {boolean} [isForced] Force a refresh on the fetch notifications promise
145 * @param {Object} [filters] Filter values
146 * @return {jQuery.Promise} Promise that is resolved with all notifications for the
147 * requested types.
148 */
149 mw.echo.api.EchoApi.prototype.fetchNotificationsFromRemoteSource = function ( type, source, isForced, filters ) {
150 const handler = this.network.getApiHandler( source );
151
152 if ( !handler ) {
153 return $.Deferred().reject().promise();
154 }
155
156 return this.fetchingPrioritizer.prioritize( handler.fetchNotifications(
157 type,
158 // For the remote source, we are fetching 'local' notifications
159 'local',
160 !!isForced,
161 this.convertFiltersToAPIParams( filters )
162 ) )
163 .then( ( result ) => OO.getProp( result.query, 'notifications' ) );
164 };
165
166 /**
167 * Fetch notifications from the server based on type
168 *
169 * @param {string} type Notification type to fetch: 'alert', 'message', or 'all'
170 * @param {string|string[]} [sources] The source from which to fetch the notifications.
171 * If not given, the local notifications will be fetched.
172 * @param {boolean} [isForced] Force a refresh on the fetch notifications promise
173 * @param {Object} [filters] Filter values
174 * @return {jQuery.Promise} Promise that is resolved with all notifications for the
175 * requested types.
176 */
177 mw.echo.api.EchoApi.prototype.fetchNotifications = function ( type, sources, isForced, filters ) {
178 sources = Array.isArray( sources ) ?
179 sources :
180 sources ?
181 [ sources ] :
182 'local';
183
184 return this.fetchingPrioritizer.prioritize( this.network.getApiHandler( 'local' ).fetchNotifications(
185 type,
186 sources,
187 isForced,
188 this.convertFiltersToAPIParams( filters )
189 ) )
190 .then( ( result ) => OO.getProp( result.query, 'notifications' ) );
191 };
192
193 /**
194 * Fetch notifications from several sources
195 *
196 * @param {string[]} sourceArray An array of sources to fetch from the group
197 * @param {string} type Notification type
198 * @param {boolean} bundle Bundle local notifications
199 * @return {jQuery.Promise} A promise that resolves with an object that maps wiki
200 * names to an array of their items' API data objects.
201 */
202 mw.echo.api.EchoApi.prototype.fetchNotificationGroups = function ( sourceArray, type, bundle ) {
203 const overrideParams = { notcrosswikisummary: false, notbundle: bundle };
204 return this.network.getApiHandler( 'local' ).fetchNotifications( type, sourceArray, true, overrideParams )
205 .then( ( result ) => {
206 const items = OO.getProp( result, 'query', 'notifications', 'list' ),
207 groups = {};
208
209 // Split the items to groups
210 for ( let i = 0; i < items.length; i++ ) {
211 groups[ items[ i ].wiki ] = groups[ items[ i ].wiki ] || [];
212 groups[ items[ i ].wiki ].push( items[ i ] );
213 }
214
215 return groups;
216 } );
217 };
218
219 /**
220 * Mark items as read in the API.
221 *
222 * @param {string[]} itemIds An array of item IDs to mark as read
223 * @param {string} source The source that these items belong to
224 * @param {boolean} [isRead] The read state of the item; true for marking the
225 * item as read, false for marking the item as unread
226 * @return {jQuery.Promise} A promise that is resolved when the operation
227 * is complete, with the number of unread notifications still remaining
228 * for that type in the given source
229 */
230 mw.echo.api.EchoApi.prototype.markItemsRead = function ( itemIds, source, isRead ) {
231 // markasread is proxied via the local API
232 return this.network.getApiHandler( 'local' ).markItemsRead( source, itemIds, isRead );
233 };
234
235 /**
236 * Mark all notifications for a given type as read in the given source.
237 *
238 * @param {string} source Symbolic name of notifications source
239 * @param {string} type Notifications type
240 * @return {jQuery.Promise} A promise that is resolved when the operation
241 * is complete, with the number of unread notifications still remaining
242 * for that type in the given source
243 */
244 mw.echo.api.EchoApi.prototype.markAllRead = function ( source, type ) {
245 // markasread is proxied via the local API
246 return this.network.getApiHandler( 'local' ).markAllRead( source, type );
247 };
248
249 /**
250 * Fetch the number of unread notifications for the given type in the given
251 * source.
252 *
253 * @param {string} source Notifications source
254 * @param {string} type Notification type
255 * @param {boolean} [localOnly] Fetches only the count of local notifications,
256 * and ignores cross-wiki notifications.
257 * @return {jQuery.Promise} A promise that is resolved with the number of
258 * unread notifications for the given type and source.
259 */
260 mw.echo.api.EchoApi.prototype.fetchUnreadCount = function ( source, type, localOnly ) {
261 return this.network.getApiHandler( source ).fetchUnreadCount( type, localOnly );
262 };
263
264 /**
265 * Update the seenTime property for the given type.
266 * We only need to update this in a single source for the seenTime
267 * to be updated globally - but we will let the consumer of
268 * this method override the choice of which source to update.
269 *
270 * @param {string} [type='alert,message'] Notification type
271 * @param {string} [source='local'] Notification source
272 * @return {jQuery.Promise} A promise that is resolved when the operation is complete.
273 */
274 mw.echo.api.EchoApi.prototype.updateSeenTime = function ( type, source ) {
275 source = source || 'local';
276 type = type || [ 'alert', 'message' ];
277
278 return this.network.getApiHandler( source ).updateSeenTime( type );
279 };
280
281 /**
282 * Send a general query to the API. This is mostly for dynamic actions
283 * where other extensions may set up API actions that are unique and
284 * unanticipated.
285 *
286 * @param {Object} params API parameters
287 * @param {string} [source='local'] Requested source to query
288 * @return {jQuery.Promise} Promise that is resolved when the action
289 * is complete
290 */
291 mw.echo.api.EchoApi.prototype.queryAPI = function ( params, source ) {
292 source = source || 'local';
293 return this.network.getApiHandler( source )
294 .queryAPI( params );
295 };
296
297 /**
298 * Check whether the API promise for fetch notification is in an error
299 * state for the given source and notification type.
300 *
301 * @param {string} source Notification source.
302 * @param {string} type Notification type
303 * @return {boolean} The API response for fetching notification has
304 * resolved in an error state, or is rejected.
305 */
306 mw.echo.api.EchoApi.prototype.isFetchingErrorState = function ( source, type ) {
307 return this.network.getApiHandler( source ).isFetchingErrorState( type, [ source ] );
308 };
309
310 /**
311 * Get the fetch notifications promise active for the current source and type.
312 *
313 * @param {string} source Notification source.
314 * @param {string} type Notification type
315 * @return {jQuery.Promise} Promise that is resolved when notifications are
316 * fetched from the API.
317 */
318 mw.echo.api.EchoApi.prototype.getFetchNotificationPromise = function ( source, type ) {
319 return this.network.getApiHandler( source ).getFetchNotificationPromise( type );
320 };
321
322 /**
323 * Get the set limit for fetching notifications per request
324 *
325 * @return {number} Limit of notifications per request
326 */
327 mw.echo.api.EchoApi.prototype.getLimit = function () {
328 return this.limit;
329 };
330 }() );