Debian Codegraph @

extensions/Echo/modules/model/mw.echo.dm.ModelManager.js

raw symbols SCIP
1 ( function () {
2 /**
3 * A container that manages all models that are involved in creating
4 * the notification list. There are currently two types of models:
5 * - mw.echo.dm.SortedList - This currently includes the local model
6 * or any model that has individual messages.
7 * - mw.echo.dm.CrossWikiNotificationItem - This is a model for the
8 * cross wiki notification, which acts as an item but itself contains
9 * a list.
10 *
11 * All notification models that are managed by the manager must implement the
12 * following methods:
13 * - getName - This should retrieve the model's name for the manager to fetch
14 * - isGroup - This should be true for xwiki model and local bundles
15 * - hasUnseen - This should iterate in the model's items and check whether
16 * there are any unseen notifications within them.
17 * - getCount - Get a total count of available notifications currently in the model
18 *
19 * @class
20 * @mixes OO.EventEmitter
21 *
22 * @constructor
23 * @param {mw.echo.dm.UnreadNotificationCounter} counter Unread counter
24 * @param {Object} [config] Configuration object
25 * @param {string|string[]} [config.type="message"] The type of the notifications in
26 * the models that this manager handles.
27 * @param {number} [config.itemsPerPage=25] Number of items per page
28 * @param {string} [config.readState] Notifications read state. Pass through to mw.echo.dm.FiltersModel
29 */
30 mw.echo.dm.ModelManager = function MwEchoDmModelManager( counter, config ) {
31 config = config || {};
32
33 // Mixin constructor
34 OO.EventEmitter.call( this );
35
36 this.counter = counter;
37 // Keep types in an array
38 this.types = config.type || 'message';
39 this.types = Array.isArray( this.types ) ?
40 config.type : [ this.types ];
41
42 this.seenTimeModel = new mw.echo.dm.SeenTimeModel( { types: this.types } );
43
44 this.notificationModels = {};
45 this.paginationModel = new mw.echo.dm.PaginationModel( {
46 itemsPerPage: config.itemsPerPage || 25
47 } );
48 this.filtersModel = new mw.echo.dm.FiltersModel( {
49 selectedSource: 'local',
50 readState: config.readState
51 } );
52
53 // Events
54 this.seenTimeModel.connect( this, { update: 'onSeenTimeUpdate' } );
55
56 this.localCounter = config.localCounter || new mw.echo.dm.UnreadNotificationCounter();
57 this.localCounter.connect( this, { countChange: [ 'emit', 'localCountChange' ] } );
58 };
59
60 OO.initClass( mw.echo.dm.ModelManager );
61 OO.mixinClass( mw.echo.dm.ModelManager, OO.EventEmitter );
62
63 /* Events */
64
65 /**
66 * The model has been rebuilt or has been updated
67 *
68 * @event mw.echo.dm.ModelManager#update
69 * @param {Object[]} Current available notifications
70 */
71
72 /**
73 * A model has been permanently removed
74 *
75 * @event mw.echo.dm.ModelManager#discard
76 * @param {string} modelId Discard model id
77 */
78
79 /**
80 * All notifications in that source are seen
81 *
82 * @event mw.echo.dm.ModelManager#seen
83 * @param {string} source Source where seenTime was updated
84 * @param {number} timestamp The new seen timestamp, as a full UTC ISO 8601 timestamp
85 */
86
87 /**
88 * There are no more local talk page notifications
89 *
90 * @event mw.echo.dm.ModelManager#allTalkRead
91 */
92
93 /**
94 * A specific item inside a notifications model has been updated
95 *
96 * @event mw.echo.dm.ModelManager#modelItemUpdate
97 * @param {string} modelId Model ID
98 * @param {mw.echo.dm.NotificationItem} item Updated item
99 */
100
101 /**
102 * There was a change in the count of local unread notifications
103 *
104 * @event mw.echo.dm.ModelManager#localCountChange
105 */
106
107 /* Methods */
108
109 /**
110 * Respond to seen time change for a given source
111 *
112 * @param {string} timestamp Seen time, as a full UTC ISO 8601 timestamp
113 * @fires mw.echo.dm.ModelManager#seen
114 */
115 mw.echo.dm.ModelManager.prototype.onSeenTimeUpdate = function ( timestamp ) {
116 const models = this.getAllNotificationModels();
117
118 for ( const modelId in models ) {
119 models[ modelId ].updateSeenState( timestamp );
120 }
121
122 this.emit( 'seen', timestamp );
123 };
124
125 /**
126 * Respond to a notification model discarding items.
127 *
128 * @param {string} modelId Model name
129 */
130 mw.echo.dm.ModelManager.prototype.onModelDiscardItems = function ( modelId ) {
131 const model = this.getNotificationModel( modelId );
132
133 // If the model is empty, remove it
134 if ( model.isEmpty() ) {
135 this.removeNotificationModel( modelId );
136 }
137 };
138
139 /**
140 * Get the notifications
141 *
142 * @return {Object} Object of notification models and their symbolic names
143 */
144 mw.echo.dm.ModelManager.prototype.getAllNotificationModels = function () {
145 return this.notificationModels;
146 };
147
148 /**
149 * Set the models in the manager.
150 *
151 * @param {Object} modelDefinitions An object defining the models
152 * The format for the definition object is:
153 * {
154 * 'modelId': {mw.echo.dm.SortedList},
155 * ...
156 * }
157 * @fires mw.echo.dm.ModelManager#update
158 */
159 mw.echo.dm.ModelManager.prototype.setNotificationModels = function ( modelDefinitions ) {
160 this.resetNotificationModels();
161
162 for ( const modelId in modelDefinitions ) {
163 this.notificationModels[ modelId ] = modelDefinitions[ modelId ];
164 this.notificationModels[ modelId ].connect( this, {
165 discard: [ 'onModelDiscardItems', modelId ],
166 itemUpdate: [ 'onModelItemUpdate', modelId ]
167 } );
168 }
169
170 // Update pagination count
171 this.updateCurrentPageItemCount();
172
173 this.emit( 'update', this.getAllNotificationModels() );
174 };
175
176 /**
177 * Respond to model update event
178 *
179 * @param {string} modelName Model name
180 * @param {mw.echo.dm.NotificationItem} item Notification item
181 * @fires mw.echo.dm.ModelManager#modelItemUpdate
182 */
183 mw.echo.dm.ModelManager.prototype.onModelItemUpdate = function ( modelName, item ) {
184 this.checkLocalUnreadTalk();
185
186 this.emit( 'modelItemUpdate', modelName, item );
187 };
188
189 /**
190 * Update the current page item count based on available items
191 */
192 mw.echo.dm.ModelManager.prototype.updateCurrentPageItemCount = function () {
193 this.getPaginationModel().setCurrentPageItemCount( this.getAllNotificationCount() );
194 };
195
196 /**
197 * Go over all the notification models and return the total number of
198 * available notifications.
199 *
200 * @return {number} A count of all notifications
201 */
202 mw.echo.dm.ModelManager.prototype.getAllNotificationCount = function () {
203 const models = this.getAllNotificationModels();
204
205 let count = 0;
206 for ( const model in models ) {
207 count += models[ model ].getCount();
208 }
209
210 return count;
211 };
212
213 /**
214 * Get a notification model.
215 *
216 * @param {string} modelName Unique model name
217 * @return {mw.echo.dm.SortedList} Notifications model
218 */
219 mw.echo.dm.ModelManager.prototype.getNotificationModel = function ( modelName ) {
220 return this.notificationModels[ modelName ];
221 };
222
223 /**
224 * Get the pagination model
225 *
226 * @return {mw.echo.dm.PaginationModel} Pagination model
227 */
228 mw.echo.dm.ModelManager.prototype.getPaginationModel = function () {
229 return this.paginationModel;
230 };
231
232 /**
233 * Get the filters model
234 *
235 * @return {mw.echo.dm.FiltersModel} Filters model
236 */
237 mw.echo.dm.ModelManager.prototype.getFiltersModel = function () {
238 return this.filtersModel;
239 };
240
241 /**
242 * Remove a model from the manager
243 *
244 * @param {string} modelName Symbolic name of the model
245 * @fires mw.echo.dm.ModelManager#discard
246 */
247 mw.echo.dm.ModelManager.prototype.removeNotificationModel = function ( modelName ) {
248 delete this.notificationModels[ modelName ];
249
250 this.emit( 'discard', modelName );
251 };
252
253 /**
254 * Reset all models
255 *
256 * @private
257 */
258 mw.echo.dm.ModelManager.prototype.resetNotificationModels = function () {
259 for ( const model in this.notificationModels ) {
260 if ( Object.prototype.hasOwnProperty.call( this.notificationModels, model ) ) {
261 this.notificationModels[ model ].disconnect( this );
262 delete this.notificationModels[ model ];
263 }
264 }
265 };
266
267 /**
268 * Get the unread notification counter
269 *
270 * @return {mw.echo.dm.UnreadNotificationCounter} Unread notification counter
271 */
272 mw.echo.dm.ModelManager.prototype.getUnreadCounter = function () {
273 return this.counter;
274 };
275
276 /**
277 * Check if the local model has any unread notifications.
278 *
279 * @return {boolean} Local model has unread notifications.
280 */
281 mw.echo.dm.ModelManager.prototype.hasLocalUnread = function () {
282 return this.getLocalNotifications().some( ( item ) => !item.isRead() );
283 };
284
285 /**
286 * Get local unread notifications
287 *
288 * @return {mw.echo.dm.NotificationItem[]} Local unread notifications
289 */
290 mw.echo.dm.ModelManager.prototype.getLocalUnread = function () {
291 return this.getLocalNotifications().filter( ( item ) => !item.isRead() );
292 };
293 /**
294 * Check whether there are talk notifications, and emit an event
295 * in case there aren't any left.
296 *
297 * @fires mw.echo.dm.ModelManager#allTalkRead
298 */
299 mw.echo.dm.ModelManager.prototype.checkLocalUnreadTalk = function () {
300 if ( !this.hasLocalUnreadTalk() ) {
301 this.emit( 'allTalkRead' );
302 }
303 };
304
305 /**
306 * Check if the local model has any unread talk page notifications.
307 *
308 * @return {boolean} Local model has unread talk page notifications.
309 */
310 mw.echo.dm.ModelManager.prototype.hasLocalUnreadTalk = function () {
311 return this.getLocalNotifications().some(
312 ( item ) => !item.isRead() && item.getCategory() === 'edit-user-talk'
313 );
314 };
315
316 /**
317 * Check if a model has any unseen notifications.
318 *
319 * @param {string} modelId Model ID
320 * @return {boolean} The given model has unseen notifications.
321 */
322 mw.echo.dm.ModelManager.prototype.hasUnseenInModel = function ( modelId ) {
323 const model = this.getNotificationModel( modelId || 'local' );
324
325 return model && model.hasUnseen();
326 };
327
328 /**
329 * Check if a model has any unseen notifications.
330 *
331 * @param {string} [source='local'] Model source
332 * @return {boolean} The given models has unseen notifications.
333 */
334 mw.echo.dm.ModelManager.prototype.hasUnseenInSource = function ( source ) {
335 source = source || 'local';
336 const modelNames = this.getModelsBySource( source );
337
338 for ( let i = 0; i < modelNames.length; i++ ) {
339 if ( this.getNotificationModel( modelNames[ i ] ).hasUnseen() ) {
340 return true;
341 }
342 }
343
344 return false;
345 };
346
347 /**
348 * Check if there are unseen notifications in any of the models
349 *
350 * @return {boolean} Local model has unseen notifications.
351 */
352 mw.echo.dm.ModelManager.prototype.hasUnseenInAnyModel = function () {
353 const models = this.getAllNotificationModels();
354
355 for ( const model in models ) {
356 if ( models[ model ].hasUnseen() ) {
357 return true;
358 }
359 }
360
361 return false;
362 };
363
364 /**
365 * Get the system seen time
366 *
367 * @param {string} [source='local'] Seen time source
368 * @return {string} Seen time, as a full UTC ISO 8601 timestamp
369 */
370 mw.echo.dm.ModelManager.prototype.getSeenTime = function ( source ) {
371 return this.getSeenTimeModel().getSeenTime( source );
372 };
373
374 /**
375 * Get the array of model types
376 *
377 * @return {string[]} Model types
378 */
379 mw.echo.dm.ModelManager.prototype.getTypes = function () {
380 return this.types;
381 };
382
383 /**
384 * Get the model types string; 'message', 'alert', or 'all'
385 *
386 * @return {string} Model types
387 */
388 mw.echo.dm.ModelManager.prototype.getTypeString = function () {
389 return (
390 this.types.length === 1 ?
391 this.types[ 0 ] :
392 'all'
393 );
394 };
395
396 /**
397 * Get the local counter
398 *
399 * @return {mw.echo.dm.UnreadNotificationCounter} Local counter
400 */
401 mw.echo.dm.ModelManager.prototype.getLocalCounter = function () {
402 return this.localCounter;
403 };
404
405 /**
406 * Get all local notifications
407 *
408 * @return {mw.echo.dm.NotificationItem[]} all local notifications
409 */
410 mw.echo.dm.ModelManager.prototype.getLocalNotifications = function () {
411 return this.getNotificationsBySource( 'local' );
412 };
413
414 /**
415 * Get all notifications that come from a given source
416 *
417 * @param {string} [source='local'] Source name
418 * @return {mw.echo.dm.NotificationItem[]} All notifications from that source
419 */
420 mw.echo.dm.ModelManager.prototype.getNotificationsBySource = function ( source ) {
421 source = source || 'local';
422
423 const notifications = [];
424 Object.keys( this.getAllNotificationModels() ).forEach( ( modelName ) => {
425 const model = this.getNotificationModel( modelName );
426 if ( model.getSource() === source ) {
427 notifications.push( ...model.getItems() );
428 }
429 } );
430 return notifications;
431 };
432
433 /**
434 * Get all models that have a specific source
435 *
436 * @param {string} [source='local'] Given source
437 * @return {string[]} All model IDs that use this source
438 */
439 mw.echo.dm.ModelManager.prototype.getModelsBySource = function ( source ) {
440 const modelIds = [];
441
442 source = source || 'local';
443
444 Object.keys( this.getAllNotificationModels() ).forEach( ( modelName ) => {
445 const model = this.getNotificationModel( modelName );
446 if ( model.getSource() === source ) {
447 modelIds.push( modelName );
448 }
449 } );
450 return modelIds;
451 };
452
453 /**
454 * Get the SeenTime model
455 *
456 * @return {mw.echo.dm.SeenTimeModel} SeenTime model
457 */
458 mw.echo.dm.ModelManager.prototype.getSeenTimeModel = function () {
459 return this.seenTimeModel;
460
461 };
462
463 }() );