2015-08-13 00:54:16 +00:00
|
|
|
( function ( mw, $ ) {
|
|
|
|
/**
|
|
|
|
* Notification view model
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @mixins OO.EventEmitter
|
|
|
|
*
|
|
|
|
* @constructor
|
2015-11-11 23:22:36 +00:00
|
|
|
* @param {mw.echo.dm.NetworkHandler} networkHandler Network handler
|
2015-08-13 00:54:16 +00:00
|
|
|
* @param {Object} [config] Configuration object
|
2015-10-16 23:18:25 +00:00
|
|
|
* @cfg {string} [id] Model id, used to refer to the model specifically.
|
|
|
|
* Falls back to the model's unique source
|
|
|
|
* @cfg {string} [title=''] An optional title for the model. This is mostly used
|
|
|
|
* for nested bundled models inside group items.
|
2015-10-27 00:05:43 +00:00
|
|
|
* @cfg {string|string[]} [type='alert'] Notification type 'alert', 'message'
|
|
|
|
* or an array [ 'alert', 'message' ]
|
2015-11-11 23:22:36 +00:00
|
|
|
* @cfg {string} [source='local'] Model source, 'local' or some symbolic name identifying
|
|
|
|
* the source of the notification items for the network handler.
|
2015-08-13 00:54:16 +00:00
|
|
|
* @cfg {number} [limit=25] Notification limit
|
|
|
|
* @cfg {string} [userLang] User language
|
2015-11-25 04:07:54 +00:00
|
|
|
* @cfg {boolean} [foreign] The model's source is foreign
|
2015-10-16 23:18:25 +00:00
|
|
|
* @cfg {boolean} [removeReadNotifications=false] Remove read notifications completely. This
|
|
|
|
* means the model will only contain unread notifications. This is useful for
|
|
|
|
* cross-wiki bundled notifications.
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2015-11-11 23:22:36 +00:00
|
|
|
mw.echo.dm.NotificationsModel = function MwEchoDmNotificationsModel( networkHandler, config ) {
|
2015-08-13 00:54:16 +00:00
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Mixin constructor
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Mixin constructor
|
2015-10-28 20:47:54 +00:00
|
|
|
mw.echo.dm.SortedList.call( this );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
|
|
|
this.type = config.type || 'alert';
|
2015-11-11 23:22:36 +00:00
|
|
|
this.source = config.source || 'local';
|
2015-10-16 23:18:25 +00:00
|
|
|
this.id = config.id || this.source;
|
|
|
|
this.title = config.title || '';
|
|
|
|
|
|
|
|
this.markingAllAsRead = false;
|
|
|
|
this.removeReadNotifications = !!config.removeReadNotifications;
|
2015-11-25 04:07:54 +00:00
|
|
|
this.foreign = !!config.foreign;
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
this.networkHandler = networkHandler;
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-10-17 00:02:39 +00:00
|
|
|
this.seenTime = mw.config.get( 'wgEchoSeenTime' ) || {};
|
2015-08-13 00:54:16 +00:00
|
|
|
|
|
|
|
// Store references to unseen and unread notifications
|
|
|
|
this.unseenNotifications = new mw.echo.dm.NotificationList();
|
|
|
|
this.unreadNotifications = new mw.echo.dm.NotificationList();
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.aggregate( {
|
|
|
|
seen: 'itemSeen',
|
2015-10-16 23:18:25 +00:00
|
|
|
read: 'itemRead',
|
|
|
|
empty: 'itemGroupEmpty'
|
2015-08-13 00:54:16 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.connect( this, {
|
|
|
|
itemSeen: 'onItemSeen',
|
2015-10-16 23:18:25 +00:00
|
|
|
itemRead: 'onItemRead',
|
|
|
|
itemGroupEmpty: 'onItemGroupEmpty'
|
2015-08-13 00:54:16 +00:00
|
|
|
} );
|
2015-10-28 20:47:54 +00:00
|
|
|
|
|
|
|
this.setSortingCallback( function ( a, b ) {
|
|
|
|
var diff;
|
|
|
|
|
|
|
|
if ( !a.isRead() && b.isRead() ) {
|
|
|
|
return -1; // Unread items are always above read items
|
|
|
|
} else if ( a.isRead() && !b.isRead() ) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
// Reverse sorting
|
2015-10-16 23:18:25 +00:00
|
|
|
diff = Number( b.getTimestamp() ) - Number( a.getTimestamp() );
|
2015-10-28 20:47:54 +00:00
|
|
|
if ( diff !== 0 ) {
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback on IDs
|
|
|
|
return b.getId() - a.getId();
|
|
|
|
}
|
|
|
|
} );
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
OO.initClass( mw.echo.dm.NotificationsModel );
|
|
|
|
OO.mixinClass( mw.echo.dm.NotificationsModel, OO.EventEmitter );
|
2015-10-28 20:47:54 +00:00
|
|
|
OO.mixinClass( mw.echo.dm.NotificationsModel, mw.echo.dm.SortedList );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event updateSeenTime
|
|
|
|
*
|
|
|
|
* Seen time has been updated
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event unseenChange
|
2015-11-03 22:51:20 +00:00
|
|
|
* @param {mw.echo.dm.NotificationItem} items An array of the unseen items
|
2015-08-13 00:54:16 +00:00
|
|
|
*
|
|
|
|
* Items' seen status has changed
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event unreadChange
|
2015-11-03 22:51:20 +00:00
|
|
|
* @param {mw.echo.dm.NotificationItem} items An array of the unread items
|
2015-08-13 00:54:16 +00:00
|
|
|
*
|
|
|
|
* Items' read status has changed
|
|
|
|
*/
|
|
|
|
|
2015-09-17 20:47:23 +00:00
|
|
|
/**
|
|
|
|
* @event allRead
|
|
|
|
*
|
|
|
|
* All items are marked as read
|
|
|
|
*/
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
/**
|
|
|
|
* @event empty
|
|
|
|
*
|
|
|
|
* The model is empty
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event done
|
|
|
|
* @param {boolean} success The operation is successful
|
|
|
|
* @param {Object} result The result of the operation. For success, the
|
|
|
|
* result includes the ids of the items. For failures, the result
|
|
|
|
* includes the error code from the failed API request
|
|
|
|
* @param {string[]} [result.ids] An array of notification IDs that were
|
|
|
|
* fetched from the API. This only appears on success.
|
|
|
|
* @param {string} [result.errCode] The error code from the API.
|
|
|
|
* This only appears on failure.
|
|
|
|
* @param {Object} [result.errObj] The error object from the API.
|
|
|
|
* This only appears on failure.
|
|
|
|
*
|
|
|
|
* The process of fetching notifications from the API has finished
|
|
|
|
*/
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to item seen state change
|
|
|
|
*
|
|
|
|
* @param {mw.echo.dm.NotificationItem} item Notification item
|
|
|
|
* @param {boolean} isSeen Notification is seen
|
|
|
|
* @fires unseenChange
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.onItemSeen = function ( item, isSeen ) {
|
|
|
|
var id = item && item.getId(),
|
|
|
|
unseenItem = id && this.unseenNotifications.getItemById( id );
|
|
|
|
|
|
|
|
if ( unseenItem ) {
|
|
|
|
if ( isSeen ) {
|
|
|
|
this.unseenNotifications.removeItems( [ unseenItem ] );
|
|
|
|
} else {
|
|
|
|
this.unseenNotifications.addItems( [ unseenItem ] );
|
|
|
|
}
|
|
|
|
this.emit( 'unseenChange', this.unseenNotifications.getItems() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to item read state change
|
|
|
|
*
|
|
|
|
* @param {mw.echo.dm.NotificationItem} item Notification item
|
|
|
|
* @param {boolean} isRead Notification is read
|
|
|
|
* @fires unreadChange
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.onItemRead = function ( item, isRead ) {
|
|
|
|
var id = item && item.getId(),
|
|
|
|
unreadItem = id && this.unreadNotifications.getItemById( id );
|
|
|
|
|
2015-10-28 20:47:54 +00:00
|
|
|
// Update unread status and emit events
|
2015-08-13 00:54:16 +00:00
|
|
|
if ( unreadItem ) {
|
|
|
|
if ( isRead ) {
|
2015-10-16 23:18:25 +00:00
|
|
|
if ( !this.markingAllAsRead ) {
|
|
|
|
this.markItemReadInApi( id );
|
|
|
|
}
|
|
|
|
if ( this.removeReadNotifications ) {
|
|
|
|
// Remove this notification from the model
|
|
|
|
this.removeItems( [ unreadItem ] );
|
|
|
|
}
|
2015-12-21 23:13:29 +00:00
|
|
|
// Remove the item from the counter after all other operations
|
|
|
|
// finished, since some of the operations check if there are any
|
|
|
|
// unread notifications to begin with.
|
|
|
|
this.unreadNotifications.removeItems( [ unreadItem ] );
|
2015-08-13 00:54:16 +00:00
|
|
|
} else {
|
2015-09-03 21:49:50 +00:00
|
|
|
this.unreadNotifications.addItems( [ unreadItem ] );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
this.emit( 'unreadChange', this.unreadNotifications.getItems() );
|
|
|
|
}
|
2015-09-17 20:47:23 +00:00
|
|
|
|
|
|
|
if ( this.unreadNotifications.isEmpty() ) {
|
|
|
|
this.emit( 'allRead' );
|
|
|
|
}
|
2015-10-01 17:56:40 +00:00
|
|
|
|
|
|
|
if ( !this.countUnreadTalkPageNotifications() ) {
|
|
|
|
this.emit( 'allTalkRead' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
/**
|
|
|
|
* Respond to grouped item being empty
|
|
|
|
*
|
|
|
|
* @param {mw.echo.dm.NotificationItem} item Group item
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.onItemGroupEmpty = function ( item ) {
|
|
|
|
// TODO: When we have other types of bundles, we should check how to handle
|
|
|
|
// empty bundles (and bundles with only 1 item left)
|
|
|
|
// In this case, the notification is a "cross wiki" notification, which
|
|
|
|
// goes away when it is empty
|
|
|
|
this.removeItems( [ item ] );
|
|
|
|
};
|
|
|
|
|
2015-10-01 20:35:35 +00:00
|
|
|
/**
|
|
|
|
* Count the unread messages that originate from the user talk page.
|
|
|
|
*
|
|
|
|
* @return {number} Number of unread talk page messages
|
|
|
|
*/
|
2015-10-01 17:56:40 +00:00
|
|
|
mw.echo.dm.NotificationsModel.prototype.countUnreadTalkPageNotifications = function () {
|
|
|
|
var i, len,
|
|
|
|
talk = 0,
|
|
|
|
items = this.unreadNotifications.getItems();
|
|
|
|
|
|
|
|
for ( i = 0, len = items.length; i < len; i++ ) {
|
|
|
|
if ( items[i].getCategory() === 'edit-user-talk' ) {
|
|
|
|
talk++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return talk;
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of the notifications that this model deals with.
|
2015-10-27 00:05:43 +00:00
|
|
|
* Notifications types can be 'alert', 'message' or an array of both.
|
2015-08-13 00:54:16 +00:00
|
|
|
*
|
2015-10-27 00:05:43 +00:00
|
|
|
* @return {string|string[]} Notifications type
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getType = function () {
|
|
|
|
return this.type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the counter of how many notifications are unseen
|
|
|
|
*
|
|
|
|
* @return {number} Number of unseen notifications
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getUnseenCount = function () {
|
|
|
|
return this.unseenNotifications.getItemCount();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the counter of how many notifications are unread
|
|
|
|
*
|
|
|
|
* @return {number} Number of unread notifications
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getUnreadCount = function () {
|
|
|
|
return this.unreadNotifications.getItemCount();
|
|
|
|
};
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
/**
|
|
|
|
* Get the counter of how many regular, non bundled notifications are unread
|
|
|
|
*
|
|
|
|
* @return {number} Number of non bundled unread notifications
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getNonbundledUnreadCount = function () {
|
|
|
|
var i,
|
|
|
|
nonBundleItems = 0,
|
|
|
|
items = this.getItems();
|
|
|
|
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
|
|
|
if (
|
|
|
|
!( items[ i ] instanceof mw.echo.dm.NotificationGroupItem ) &&
|
|
|
|
!items[ i ].isRead()
|
|
|
|
) {
|
|
|
|
nonBundleItems++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nonBundleItems;
|
|
|
|
};
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
/**
|
|
|
|
* Set the system seen time - the last time we've marked notification as seen
|
|
|
|
*
|
2015-09-03 21:49:50 +00:00
|
|
|
* @private
|
2015-08-13 00:54:16 +00:00
|
|
|
* @param {string} Mediawiki seen timestamp in Mediawiki timestamp format
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.setSeenTime = function ( time ) {
|
2015-10-27 00:05:43 +00:00
|
|
|
var i,
|
|
|
|
type = $.isArray( this.type ) ? this.type : [ this.type ];
|
|
|
|
|
|
|
|
for ( i = 0; i < type.length; i++ ) {
|
|
|
|
// Update all types
|
|
|
|
this.seenTime[ type[ i ] ] = time;
|
|
|
|
}
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the system seen time
|
|
|
|
*
|
2015-10-27 00:05:43 +00:00
|
|
|
* @param {string} [type] Notification type
|
2015-08-13 00:54:16 +00:00
|
|
|
* @return {string} Mediawiki seen timestamp in Mediawiki timestamp format
|
|
|
|
*/
|
2015-10-27 00:05:43 +00:00
|
|
|
mw.echo.dm.NotificationsModel.prototype.getSeenTime = function ( type ) {
|
|
|
|
type = type || ( $.isArray( this.type ) ? this.type[ 0 ] : this.type );
|
|
|
|
|
|
|
|
return this.seenTime[ type ];
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the seen timestamp
|
|
|
|
*
|
2015-10-27 00:05:43 +00:00
|
|
|
* @param {string|string[]} [type] Notification type
|
2015-08-13 00:54:16 +00:00
|
|
|
* @return {jQuery.Promise} A promise that resolves with the seen timestamp
|
|
|
|
* @fires updateSeenTime
|
|
|
|
*/
|
2015-10-27 00:05:43 +00:00
|
|
|
mw.echo.dm.NotificationsModel.prototype.updateSeenTime = function ( type ) {
|
2015-12-21 18:49:11 +00:00
|
|
|
var i, len, promise,
|
2015-09-03 21:49:50 +00:00
|
|
|
items = this.unseenNotifications.getItems();
|
|
|
|
|
2015-10-27 00:05:43 +00:00
|
|
|
type = type || this.type;
|
|
|
|
|
2015-09-03 21:49:50 +00:00
|
|
|
// Update the notifications seen status
|
|
|
|
for ( i = 0, len = items.length; i < len; i++ ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
items[ i ].toggleSeen( true );
|
2015-09-03 21:49:50 +00:00
|
|
|
}
|
|
|
|
this.emit( 'updateSeenTime' );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-12-21 18:49:11 +00:00
|
|
|
// Only update seenTime in the API locally
|
2015-11-25 04:07:54 +00:00
|
|
|
if ( !this.isForeign() ) {
|
2015-12-21 18:49:11 +00:00
|
|
|
promise = this.getApi().updateSeenTime( type );
|
|
|
|
} else {
|
|
|
|
promise = $.Deferred().resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise
|
2015-10-17 00:02:39 +00:00
|
|
|
.then( this.setSeenTime.bind( this ) );
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all notifications as read
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that resolves when all notifications
|
|
|
|
* were marked as read.
|
2015-10-16 23:18:25 +00:00
|
|
|
* @fires empty
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.markAllRead = function () {
|
2015-10-16 23:18:25 +00:00
|
|
|
var i, len,
|
|
|
|
items = this.unreadNotifications.getItems(),
|
|
|
|
length = items.length;
|
|
|
|
|
|
|
|
// In some cases our model is empty out of technicalities -- that is,
|
|
|
|
// we didn't fetch its items yet. In that case, when markAllRead is
|
|
|
|
// called, we should emit the empty event (that would have been
|
|
|
|
// emitted if there were items that were then marked as read and removed)
|
|
|
|
// and return a resolved promise
|
|
|
|
if ( length === 0 ) {
|
|
|
|
if ( this.removeReadNotifications ) {
|
|
|
|
this.emit( 'empty' );
|
|
|
|
}
|
2015-08-13 00:54:16 +00:00
|
|
|
return $.Deferred().resolve( 0 ).promise();
|
|
|
|
}
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
this.markingAllAsRead = true;
|
|
|
|
for ( i = 0, len = items.length; i < len; i++ ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
if ( !items[ i ].isForeign() ) {
|
2015-10-16 23:18:25 +00:00
|
|
|
items[ i ].toggleRead( true );
|
|
|
|
items[ i ].toggleSeen( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.unreadNotifications.clearItems();
|
|
|
|
this.markingAllAsRead = false;
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
return this.getApi().markAllRead();
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
2015-09-18 23:05:35 +00:00
|
|
|
/**
|
|
|
|
* Update the read status of a notification item in the API
|
|
|
|
*
|
|
|
|
* @param {string} itemId Item id
|
|
|
|
* @return {jQuery.Promise} A promise that resolves when the notifications
|
|
|
|
* were marked as read.
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.markItemReadInApi = function ( itemId ) {
|
|
|
|
if ( !this.unreadNotifications.getItemCount() ) {
|
|
|
|
return $.Deferred().resolve( 0 ).promise();
|
|
|
|
}
|
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
return this.getApi().markItemRead( itemId );
|
2015-09-18 23:05:35 +00:00
|
|
|
};
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
/**
|
|
|
|
* Fetch notifications from the API and update the notifications list.
|
|
|
|
*
|
2015-09-15 06:13:51 +00:00
|
|
|
* @param {jQuery.Promise} An existing promise querying the API for notifications.
|
|
|
|
* This allows us to send an API request external to the DM and have the model
|
|
|
|
* handle the operation as if it asked for the request itself, updating all that
|
|
|
|
* needs to be updated and emitting all proper events.
|
2015-08-13 00:54:16 +00:00
|
|
|
* @return {jQuery.Promise} A promise that resolves with an array of notification
|
|
|
|
* id's.
|
|
|
|
*/
|
2015-09-15 06:13:51 +00:00
|
|
|
mw.echo.dm.NotificationsModel.prototype.fetchNotifications = function ( apiPromise ) {
|
2015-10-17 00:02:39 +00:00
|
|
|
var model = this;
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-09-16 20:42:17 +00:00
|
|
|
// Rebuild the notifications promise either when it is null or when
|
|
|
|
// it exists in a failed state
|
2015-10-16 23:18:25 +00:00
|
|
|
return ( apiPromise || this.getApi().fetchNotifications() )
|
|
|
|
.then(
|
|
|
|
// Success
|
|
|
|
function ( result ) {
|
|
|
|
var notifData, id, t, tlen, s,
|
|
|
|
notificationModel, types, content,
|
|
|
|
newNotifData = {},
|
|
|
|
sources = {},
|
|
|
|
optionItems = [],
|
|
|
|
idArray = [],
|
|
|
|
data = OO.getProp( result.query, 'notifications', model.type ) || { index: [] },
|
|
|
|
sourceDefinitions = OO.getProp( result.query, 'notifications', 'sources' ) || {};
|
|
|
|
|
|
|
|
types = $.isArray( model.type ) ? model.type : [ model.type ];
|
|
|
|
|
|
|
|
for ( t = 0, tlen = types.length; t < tlen; t++ ) {
|
|
|
|
data = OO.getProp( result.query, 'notifications', types[ t ] ) || { list: [] };
|
|
|
|
for ( id in data.list ) {
|
|
|
|
notifData = data.list[ id ];
|
|
|
|
content = notifData['*'] || {};
|
|
|
|
|
|
|
|
if ( model.getItemById( id ) ) {
|
|
|
|
// Skip if we already have the item
|
|
|
|
// TODO: Instead of skipping, we should consider repopulating
|
|
|
|
// the item, in case there are any changes that would result
|
|
|
|
// in repositioning/resorting in the future.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect common data
|
|
|
|
newNotifData = {
|
2015-10-27 00:05:43 +00:00
|
|
|
read: !!notifData.read,
|
|
|
|
seen: !!notifData.read || notifData.timestamp.mw <= model.getSeenTime(),
|
2015-12-22 16:07:30 +00:00
|
|
|
timestamp: notifData.timestamp.utcmw,
|
2015-10-27 00:05:43 +00:00
|
|
|
category: notifData.category,
|
2015-10-16 23:18:25 +00:00
|
|
|
content: {
|
|
|
|
header: content.header,
|
|
|
|
body: content.body
|
|
|
|
},
|
|
|
|
iconURL: content.iconUrl,
|
|
|
|
iconType: content.icon,
|
2015-10-27 00:05:43 +00:00
|
|
|
type: model.getType(),
|
2015-11-25 04:07:54 +00:00
|
|
|
foreign: model.isForeign(),
|
2015-12-23 18:33:59 +00:00
|
|
|
source: model.getSource(),
|
2015-10-16 23:18:25 +00:00
|
|
|
primaryUrl: OO.getProp( content.links, 'primary', 'url' ),
|
|
|
|
secondaryUrls: OO.getProp( content.links, 'secondary' ) || []
|
|
|
|
};
|
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
if ( notifData.type === 'foreign' ) {
|
2015-10-16 23:18:25 +00:00
|
|
|
// Define sources
|
|
|
|
sources = {};
|
|
|
|
for ( s = 0; s < notifData.sources.length; s++ ) {
|
|
|
|
sources[ notifData.sources[ s ] ] = sourceDefinitions[ notifData.sources[ s ] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create model
|
|
|
|
notificationModel = new mw.echo.dm.NotificationGroupItem(
|
|
|
|
model.networkHandler,
|
|
|
|
sources,
|
|
|
|
id,
|
|
|
|
$.extend( true, {}, newNotifData, {
|
|
|
|
// This should probably be separated by bundled
|
|
|
|
// type. Some types don't have read messages, but
|
|
|
|
// some do
|
|
|
|
removeReadNotifications: true,
|
2015-11-25 04:07:54 +00:00
|
|
|
// Override the foreign flag to 'true' for cross-wiki
|
2015-10-16 23:18:25 +00:00
|
|
|
// notifications.
|
2015-11-25 04:07:54 +00:00
|
|
|
// For bundles that are not foreign (like regular
|
2015-10-16 23:18:25 +00:00
|
|
|
// bundles of notifications) this flag should be false
|
2015-11-25 04:07:54 +00:00
|
|
|
foreign: true,
|
2015-10-16 23:18:25 +00:00
|
|
|
count: notifData.count
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
notificationModel = new mw.echo.dm.NotificationItem(
|
|
|
|
id,
|
|
|
|
newNotifData
|
|
|
|
);
|
2015-10-27 00:05:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
idArray.push( notifData.id );
|
|
|
|
optionItems.push( notificationModel );
|
|
|
|
}
|
2015-10-27 00:05:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
// Add to the model
|
|
|
|
model.addItems( optionItems, 0 );
|
2015-10-17 00:02:39 +00:00
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
model.emit( 'done', true, { ids: idArray } );
|
|
|
|
return idArray;
|
|
|
|
},
|
|
|
|
// Failure
|
2015-12-21 21:47:09 +00:00
|
|
|
function ( errCode, errObj ) {
|
|
|
|
model.emit( 'done', false, { errCode: errCode, errInfo: OO.getProp( errObj, 'error', 'info' ) } );
|
2015-10-16 23:18:25 +00:00
|
|
|
}
|
|
|
|
);
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the unread and unseen tracking lists when we add items
|
|
|
|
*
|
|
|
|
* @param {mw.echo.dm.NotificationItem[]} items Items to add
|
|
|
|
*/
|
2015-10-28 20:47:54 +00:00
|
|
|
mw.echo.dm.NotificationsModel.prototype.addItems = function ( items ) {
|
2015-08-13 00:54:16 +00:00
|
|
|
var i, len;
|
|
|
|
|
|
|
|
for ( i = 0, len = items.length; i < len; i++ ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
if ( !items[ i ].isRead() ) {
|
|
|
|
this.unreadNotifications.addItems( [ items[ i ] ] );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
if ( !items[ i ].isSeen() ) {
|
|
|
|
this.unseenNotifications.addItems( [ items[ i ] ] );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Parent
|
2015-10-28 20:47:54 +00:00
|
|
|
mw.echo.dm.SortedList.prototype.addItems.call( this, items );
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the unread and unseen tracking lists when we remove items
|
|
|
|
*
|
|
|
|
* @param {mw.echo.dm.NotificationItem[]} items Items to remove
|
|
|
|
* @param {number} index Index to add items at
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.removeItems = function ( items ) {
|
|
|
|
var i, len;
|
|
|
|
|
|
|
|
for ( i = 0, len = items.length; i < len; i++ ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
this.unreadNotifications.removeItems( [ items[ i ] ] );
|
|
|
|
this.unseenNotifications.removeItems( [ items[ i ] ] );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parent
|
2015-10-28 20:47:54 +00:00
|
|
|
mw.echo.dm.SortedList.prototype.removeItems.call( this, items );
|
2015-10-16 23:18:25 +00:00
|
|
|
|
|
|
|
if ( this.isEmpty() ) {
|
|
|
|
this.emit( 'empty' );
|
|
|
|
}
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the unread and unseen tracking lists when we clear items
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.clearItems = function () {
|
|
|
|
this.unreadNotifications.clearItems();
|
|
|
|
this.unseenNotifications.clearItems();
|
|
|
|
|
|
|
|
// Parent
|
2015-10-28 20:47:54 +00:00
|
|
|
mw.echo.dm.SortedList.prototype.clearItems.call( this );
|
2015-10-16 23:18:25 +00:00
|
|
|
this.emit( 'empty' );
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query the API for unread count of the notifications in this model
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} jQuery promise that's resolved when the unread count is fetched
|
|
|
|
* and the badge label is updated.
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.fetchUnreadCountFromApi = function () {
|
2015-11-11 23:22:36 +00:00
|
|
|
return this.getApi().fetchUnreadCount();
|
2015-10-17 00:02:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the model is fetching notifications from the API
|
|
|
|
*
|
|
|
|
* @return {boolean} The model is in the process of fetching from the API
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.isFetchingNotifications = function () {
|
2015-11-11 23:22:36 +00:00
|
|
|
return this.getApi().isFetchingNotifications();
|
2015-10-17 00:02:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the model has an api error state flagged
|
|
|
|
*
|
|
|
|
* @return {boolean} The model is in api error state
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.isFetchingErrorState = function () {
|
2015-11-11 23:22:36 +00:00
|
|
|
return this.getApi().isFetchingErrorState();
|
2015-10-17 00:02:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the fetch notifications promise
|
|
|
|
* @return {jQuery.Promise} Promise that is resolved when notifications were
|
|
|
|
* fetched from the API.
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getFetchNotificationPromise = function () {
|
2015-11-11 23:22:36 +00:00
|
|
|
return this.getApi().getFetchNotificationPromise();
|
2015-08-13 00:54:16 +00:00
|
|
|
};
|
2015-11-11 23:22:36 +00:00
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
/**
|
|
|
|
* Get the timestamp of the latest unread item
|
|
|
|
*
|
|
|
|
* @return {mw.echo.dm.APIHandler} API handler
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getTimestamp = function () {
|
|
|
|
var items = this.getItems();
|
|
|
|
|
|
|
|
// This is a sorted list, so the top (first) item is also the 'latest'
|
|
|
|
// item for this purpose.
|
|
|
|
return items[ 0 ] && items[ 0 ].getTimestamp();
|
|
|
|
};
|
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
/**
|
|
|
|
* Get the API handler associated with this model's source
|
|
|
|
*
|
|
|
|
* @return {mw.echo.dm.APIHandler} API handler
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getApi = function () {
|
|
|
|
return this.networkHandler.getApiHandler( this.source );
|
|
|
|
};
|
|
|
|
|
2015-10-16 23:18:25 +00:00
|
|
|
/**
|
|
|
|
* Get the network handler
|
|
|
|
*
|
|
|
|
* @return {mw.echo.dm.NetworkHandler} Network handler
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getNetworkHandler = function () {
|
|
|
|
return this.networkHandler;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the source this model is associated with
|
|
|
|
*
|
|
|
|
* @return {string} Symbolic name for the APIHandler source
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getSource = function () {
|
|
|
|
return this.source;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the title of this model
|
|
|
|
*
|
|
|
|
* @return {string} Title
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getTitle = function () {
|
|
|
|
return this.title;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id of this model
|
|
|
|
*
|
|
|
|
* @return {string} id
|
|
|
|
*/
|
|
|
|
mw.echo.dm.NotificationsModel.prototype.getId = function () {
|
|
|
|
return this.id;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-11-25 04:07:54 +00:00
|
|
|
* This model is foreign
|
2015-10-16 23:18:25 +00:00
|
|
|
*
|
2015-11-25 04:07:54 +00:00
|
|
|
* @return {boolean} Model is foreign
|
2015-10-16 23:18:25 +00:00
|
|
|
*/
|
2015-11-25 04:07:54 +00:00
|
|
|
mw.echo.dm.NotificationsModel.prototype.isForeign = function () {
|
|
|
|
return this.foreign;
|
2015-10-16 23:18:25 +00:00
|
|
|
};
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
} )( mediaWiki, jQuery );
|