2018-11-12 13:56:38 +00:00
|
|
|
( function () {
|
2016-03-15 21:13:19 +00:00
|
|
|
/**
|
|
|
|
* Echo notification UnreadNotificationCounter model
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @mixins OO.EventEmitter
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} api An instance of EchoAPI.
|
|
|
|
* @param {string} type The notification type 'message', 'alert', or 'all'.
|
|
|
|
* @param {number} max Maximum number supported. Above this number there is no precision, we only know it is 'more than max'.
|
2016-06-29 23:36:03 +00:00
|
|
|
* @param {Object} config Configuration object
|
|
|
|
* @cfg {boolean} [localOnly=false] The update only takes into account
|
|
|
|
* local notifications and ignores the number of cross-wiki notifications.
|
|
|
|
* @cfg {string} [source='local'] The source for this counter. Specifically important if the counter
|
|
|
|
* is set to be counting only local notifications
|
2016-03-15 21:13:19 +00:00
|
|
|
*/
|
2016-06-29 23:36:03 +00:00
|
|
|
mw.echo.dm.UnreadNotificationCounter = function mwEchoDmUnreadNotificationCounter( api, type, max, config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
2016-03-15 21:13:19 +00:00
|
|
|
// Mixin constructor
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
|
|
|
this.api = api;
|
|
|
|
this.type = type;
|
|
|
|
this.max = max;
|
2016-06-29 23:36:03 +00:00
|
|
|
this.prioritizer = new mw.echo.api.PromisePrioritizer();
|
2016-03-15 21:13:19 +00:00
|
|
|
|
|
|
|
this.count = 0;
|
2016-06-29 23:36:03 +00:00
|
|
|
this.localOnly = config.localOnly === undefined ? false : !!config.localOnly;
|
|
|
|
this.source = config.source || 'local';
|
2016-03-15 21:13:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.mixinClass( mw.echo.dm.UnreadNotificationCounter, OO.EventEmitter );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event countChange
|
|
|
|
* @param {number} count Notification count
|
|
|
|
*
|
|
|
|
* The number of unread notification represented by this counter has changed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2016-09-12 21:06:49 +00:00
|
|
|
/**
|
|
|
|
* Normalizes for a capped count in case the requested count
|
|
|
|
* is higher than the cap.
|
|
|
|
*
|
|
|
|
* This is the client-side version of
|
|
|
|
* NotificationController::getCappedNotificationCount.
|
|
|
|
*
|
|
|
|
* @param {number} count Count before cap is applied
|
|
|
|
* @return {number} Count with cap applied
|
|
|
|
*/
|
|
|
|
mw.echo.dm.UnreadNotificationCounter.prototype.getCappedNotificationCount = function ( count ) {
|
2016-09-23 23:11:28 +00:00
|
|
|
if ( count < 0 ) {
|
|
|
|
return 0;
|
|
|
|
} else if ( count <= this.max ) {
|
2016-09-12 21:06:49 +00:00
|
|
|
return count;
|
|
|
|
} else {
|
|
|
|
return this.max + 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-15 21:13:19 +00:00
|
|
|
/**
|
|
|
|
* Get the current count
|
|
|
|
*
|
|
|
|
* @return {number} current count
|
|
|
|
*/
|
|
|
|
mw.echo.dm.UnreadNotificationCounter.prototype.getCount = function () {
|
|
|
|
return this.count;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current count
|
|
|
|
*
|
|
|
|
* @param {number} count
|
|
|
|
* @param {boolean} isEstimation Whether this number is estimated or accurate
|
|
|
|
*/
|
|
|
|
mw.echo.dm.UnreadNotificationCounter.prototype.setCount = function ( count, isEstimation ) {
|
|
|
|
if ( isEstimation ) {
|
|
|
|
if ( this.count > this.max ) {
|
|
|
|
// this prevents toggling between 90-ish and 99+
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( count < 0 ) {
|
|
|
|
// wrong estimation?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 23:11:28 +00:00
|
|
|
// Normalize
|
|
|
|
count = this.getCappedNotificationCount( count );
|
|
|
|
|
2016-03-15 21:13:19 +00:00
|
|
|
if ( count !== this.count ) {
|
|
|
|
this.count = count;
|
|
|
|
this.emit( 'countChange', this.count );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Report an estimated change to this counter
|
|
|
|
*
|
|
|
|
* @param {number} delta
|
|
|
|
*/
|
|
|
|
mw.echo.dm.UnreadNotificationCounter.prototype.estimateChange = function ( delta ) {
|
|
|
|
this.setCount( this.count + delta, true );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request that this counter update itself from the API
|
2016-06-29 23:36:03 +00:00
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} Promise that is resolved when the actual unread
|
|
|
|
* count is fetched, with the actual unread notification count.
|
2016-03-15 21:13:19 +00:00
|
|
|
*/
|
|
|
|
mw.echo.dm.UnreadNotificationCounter.prototype.update = function () {
|
|
|
|
var model = this;
|
2016-06-29 23:36:03 +00:00
|
|
|
|
|
|
|
if ( !this.api ) {
|
|
|
|
return $.Deferred().reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.prioritizer.prioritize( this.api.fetchUnreadCount(
|
|
|
|
this.source,
|
|
|
|
this.type,
|
|
|
|
this.localOnly
|
|
|
|
) ).then( function ( actualCount ) {
|
2016-03-15 21:13:19 +00:00
|
|
|
model.setCount( actualCount, false );
|
2016-06-29 23:36:03 +00:00
|
|
|
|
|
|
|
return actualCount;
|
2016-03-15 21:13:19 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2016-06-29 23:36:03 +00:00
|
|
|
/**
|
|
|
|
* Set the source for this counter
|
|
|
|
*
|
|
|
|
* @param {string} source Source name
|
|
|
|
*/
|
|
|
|
mw.echo.dm.UnreadNotificationCounter.prototype.setSource = function ( source ) {
|
|
|
|
this.source = source;
|
|
|
|
};
|
|
|
|
|
2018-11-12 13:56:38 +00:00
|
|
|
}() );
|