2016-04-10 13:31:02 +00:00
|
|
|
( function ( mw, $ ) {
|
|
|
|
/**
|
|
|
|
* Controller for Echo notifications
|
|
|
|
*
|
|
|
|
* @param {mw.echo.api.EchoApi} echoApi Echo API
|
|
|
|
* @param {mw.echo.dm.ModelManager} manager Model manager
|
|
|
|
* @param {Object} [config] Configuration
|
|
|
|
*/
|
|
|
|
mw.echo.Controller = function MwEchoController( echoApi, manager, config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
this.api = echoApi;
|
|
|
|
this.manager = manager;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
OO.initClass( mw.echo.Controller );
|
|
|
|
|
2016-05-20 04:22:41 +00:00
|
|
|
/**
|
2016-05-31 22:32:16 +00:00
|
|
|
* Update a filter value.
|
|
|
|
* The method accepts a filter name and as many arguments
|
|
|
|
* as needed.
|
2016-05-20 04:22:41 +00:00
|
|
|
*
|
|
|
|
* @param {string} filter Filter name
|
|
|
|
*/
|
2016-05-31 22:32:16 +00:00
|
|
|
mw.echo.Controller.prototype.setFilter = function ( filter ) {
|
|
|
|
var filtersModel = this.manager.getFiltersModel(),
|
|
|
|
values = Array.prototype.slice.call( arguments );
|
|
|
|
|
|
|
|
values.shift();
|
|
|
|
|
2016-05-20 04:22:41 +00:00
|
|
|
if ( filter === 'readState' ) {
|
2016-05-31 22:32:16 +00:00
|
|
|
filtersModel.setReadState( values[ 0 ] );
|
|
|
|
} else if ( filter === 'sourcePage' ) {
|
|
|
|
filtersModel.setCurrentSourcePage( values[ 0 ], values[ 1 ] );
|
2016-05-20 04:22:41 +00:00
|
|
|
}
|
2016-05-31 22:32:16 +00:00
|
|
|
|
|
|
|
// Reset pagination
|
|
|
|
this.manager.getPaginationModel().reset();
|
2016-05-20 04:22:41 +00:00
|
|
|
};
|
|
|
|
|
2016-03-16 22:47:20 +00:00
|
|
|
/**
|
|
|
|
* Fetch the next page by date
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that resolves with an object where the keys are
|
|
|
|
* days and the items are item IDs.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.fetchNextPageByDate = function () {
|
|
|
|
this.manager.getPaginationModel().forwards();
|
|
|
|
return this.fetchLocalNotificationsByDate();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the previous page by date
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that resolves with an object where the keys are
|
|
|
|
* days and the items are item IDs.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.fetchPrevPageByDate = function () {
|
|
|
|
this.manager.getPaginationModel().backwards();
|
|
|
|
return this.fetchLocalNotificationsByDate();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the first page by date
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that resolves with an object where the keys are
|
|
|
|
* days and the items are item IDs.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.fetchFirstPageByDate = function () {
|
|
|
|
this.manager.getPaginationModel().setCurrPageIndex( 0 );
|
|
|
|
return this.fetchLocalNotificationsByDate();
|
|
|
|
};
|
|
|
|
|
2016-05-31 22:32:16 +00:00
|
|
|
/**
|
|
|
|
* Fetch unread pages in all wikis and create foreign API sources
|
|
|
|
* as needed.
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that resolves when the page filter
|
|
|
|
* model is updated with the unread notification count per page per wiki
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.fetchUnreadPagesByWiki = function () {
|
|
|
|
var controller = this,
|
|
|
|
filterModel = this.manager.getFiltersModel(),
|
|
|
|
sourcePageModel = filterModel.getSourcePagesModel();
|
|
|
|
|
|
|
|
return this.api.fetchUnreadNotificationPages()
|
|
|
|
.then( function ( data ) {
|
|
|
|
var source,
|
2016-07-07 19:20:39 +00:00
|
|
|
result = {},
|
2016-05-31 22:32:16 +00:00
|
|
|
foreignSources = {};
|
|
|
|
|
|
|
|
for ( source in data ) {
|
|
|
|
if ( source !== mw.config.get( 'wgDBname' ) ) {
|
|
|
|
// Collect sources for API
|
|
|
|
foreignSources[ source ] = data[ source ].source;
|
|
|
|
}
|
2016-07-07 19:20:39 +00:00
|
|
|
result[ source === mw.config.get( 'wgDBname' ) ? 'local' : source ] = data[ source ];
|
2016-05-31 22:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register the foreign sources in the API
|
|
|
|
controller.api.registerForeignSources( foreignSources, false );
|
|
|
|
|
|
|
|
// Register pages
|
2016-07-07 19:20:39 +00:00
|
|
|
sourcePageModel.setAllSources( result );
|
2016-05-31 22:32:16 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2016-03-16 22:47:20 +00:00
|
|
|
/**
|
|
|
|
* Fetch notifications from the local API and sort them by date.
|
|
|
|
* This method ignores cross-wiki notifications and bundles.
|
|
|
|
*
|
|
|
|
* @param {number} [page] Page number. If not given, it defaults to the current
|
|
|
|
* page.
|
|
|
|
* @return {jQuery.Promise} A promise that resolves with an object where the keys are
|
|
|
|
* days and the items are item IDs.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.fetchLocalNotificationsByDate = function ( page ) {
|
|
|
|
var controller = this,
|
|
|
|
pagination = this.manager.getPaginationModel(),
|
2016-05-20 04:22:41 +00:00
|
|
|
filters = this.manager.getFiltersModel(),
|
2016-05-31 22:32:16 +00:00
|
|
|
currentSource = filters.getSourcePagesModel().getCurrentSource(),
|
2016-03-16 22:47:20 +00:00
|
|
|
continueValue = pagination.getPageContinue( page || pagination.getCurrPageIndex() );
|
|
|
|
|
2016-05-31 00:12:31 +00:00
|
|
|
pagination.setItemsPerPage( this.api.getLimit() );
|
2016-05-31 22:32:16 +00:00
|
|
|
|
|
|
|
return this.api.fetchFilteredNotifications(
|
2016-03-16 22:47:20 +00:00
|
|
|
this.manager.getTypeString(),
|
2016-05-31 22:32:16 +00:00
|
|
|
currentSource,
|
|
|
|
{
|
|
|
|
continue: continueValue,
|
|
|
|
readState: filters.getReadState(),
|
2016-06-23 13:51:05 +00:00
|
|
|
titles: filters.getSourcePagesModel().getCurrentPage()
|
2016-05-31 22:32:16 +00:00
|
|
|
}
|
2016-03-16 22:47:20 +00:00
|
|
|
)
|
|
|
|
.then( function ( data ) {
|
2016-07-14 00:03:57 +00:00
|
|
|
var i, notifData, newNotifData, date, itemModel, symbolicName,
|
2016-03-16 22:47:20 +00:00
|
|
|
dateItemIds = {},
|
|
|
|
dateItems = {},
|
|
|
|
models = {};
|
|
|
|
|
|
|
|
data = data || { list: [] };
|
|
|
|
|
|
|
|
// Go over the data
|
|
|
|
for ( i = 0; i < data.list.length; i++ ) {
|
|
|
|
notifData = data.list[ i ];
|
|
|
|
|
|
|
|
// Collect common data
|
|
|
|
newNotifData = controller.createNotificationData( notifData );
|
|
|
|
if ( notifData.type !== 'foreign' ) {
|
|
|
|
date = newNotifData.timestamp.substring( 0, 8 );
|
2016-06-08 23:53:20 +00:00
|
|
|
newNotifData.modelName = 'local_' + date;
|
|
|
|
newNotifData.source = currentSource;
|
2016-03-16 22:47:20 +00:00
|
|
|
|
|
|
|
// Single notifications
|
|
|
|
itemModel = new mw.echo.dm.NotificationItem(
|
|
|
|
notifData.id,
|
|
|
|
newNotifData
|
|
|
|
);
|
|
|
|
|
|
|
|
dateItems[ date ] = dateItems[ date ] || [];
|
|
|
|
dateItems[ date ].push( itemModel );
|
|
|
|
|
|
|
|
dateItemIds[ date ] = dateItemIds[ date ] || [];
|
|
|
|
dateItemIds[ date ].push( notifData.id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the models
|
|
|
|
for ( date in dateItems ) {
|
|
|
|
symbolicName = 'local_' + date;
|
|
|
|
|
|
|
|
// Set up model
|
|
|
|
models[ symbolicName ] = new mw.echo.dm.NotificationsList( {
|
|
|
|
type: controller.manager.getTypes(),
|
2016-06-08 23:53:20 +00:00
|
|
|
name: symbolicName,
|
|
|
|
source: currentSource,
|
2016-03-16 22:47:20 +00:00
|
|
|
title: date,
|
|
|
|
timestamp: date
|
|
|
|
} );
|
|
|
|
|
|
|
|
models[ symbolicName ].setItems( dateItems[ date ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register local sources
|
|
|
|
controller.api.registerLocalSources( Object.keys( models ) );
|
|
|
|
|
|
|
|
// Update the manager
|
|
|
|
controller.manager.setNotificationModels( models );
|
|
|
|
|
|
|
|
// Update the pagination
|
2016-05-31 00:12:31 +00:00
|
|
|
pagination.setNextPageContinue( data.continue );
|
2016-03-16 22:47:20 +00:00
|
|
|
|
|
|
|
return dateItemIds;
|
|
|
|
} );
|
|
|
|
};
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Fetch notifications from the local API and update the notifications list.
|
|
|
|
*
|
|
|
|
* @param {boolean} [isForced] Force a renewed fetching promise. If set to false, the
|
|
|
|
* model will request the stored/cached fetching promise from the API. A 'true' value
|
|
|
|
* will force the API to re-request that information from the server and update the
|
|
|
|
* notifications.
|
|
|
|
* @return {jQuery.Promise} A promise that resolves with an array of notification IDs
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.fetchLocalNotifications = function ( isForced ) {
|
|
|
|
var controller = this,
|
|
|
|
// Create a new local list model
|
|
|
|
localListModel = new mw.echo.dm.NotificationsList( {
|
|
|
|
type: this.manager.getTypes()
|
|
|
|
} ),
|
|
|
|
localItems = [],
|
|
|
|
idArray = [];
|
|
|
|
|
|
|
|
this.manager.counter.update();
|
|
|
|
|
|
|
|
// Fetch the notifications from the database
|
|
|
|
// Initially, we're going to have to split the operation
|
|
|
|
// between local notifications and x-wiki notifications
|
|
|
|
// until the backend gives us the x-wiki notifications as
|
|
|
|
// part of the original response.
|
2016-07-07 23:29:40 +00:00
|
|
|
return this.api.fetchNotifications( this.manager.getTypeString(), 'local', !!isForced, { unreadFirst: true } /* filters */ )
|
2016-04-10 13:31:02 +00:00
|
|
|
.then(
|
|
|
|
// Success
|
|
|
|
function ( data ) {
|
|
|
|
var i, notifData, content, newNotifData,
|
|
|
|
foreignListModel, source, itemModel,
|
2016-06-07 20:08:16 +00:00
|
|
|
allModels = { local: localListModel },
|
|
|
|
createBundledNotification = function ( modelName, rawBundledNotifData ) {
|
|
|
|
var bundleNotifData = controller.createNotificationData( rawBundledNotifData );
|
|
|
|
bundleNotifData.bundled = true;
|
|
|
|
bundleNotifData.modelName = modelName;
|
|
|
|
return new mw.echo.dm.NotificationItem(
|
|
|
|
rawBundledNotifData.id,
|
|
|
|
bundleNotifData
|
|
|
|
);
|
|
|
|
};
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
data = data || { list: [] };
|
|
|
|
|
|
|
|
// Go over the data
|
|
|
|
for ( i = 0; i < data.list.length; i++ ) {
|
|
|
|
notifData = data.list[ i ];
|
|
|
|
content = notifData[ '*' ] || {};
|
|
|
|
|
|
|
|
// Collect common data
|
|
|
|
newNotifData = controller.createNotificationData( notifData );
|
|
|
|
if ( notifData.type === 'foreign' ) {
|
|
|
|
// x-wiki notification multi-group
|
|
|
|
// We need to request a new list model
|
2016-06-08 23:53:20 +00:00
|
|
|
newNotifData.name = 'xwiki';
|
2016-04-10 13:31:02 +00:00
|
|
|
allModels.xwiki = foreignListModel = new mw.echo.dm.CrossWikiNotificationItem( notifData.id, newNotifData );
|
|
|
|
foreignListModel.setForeign( true );
|
|
|
|
|
|
|
|
// Register foreign sources
|
2016-05-31 22:32:16 +00:00
|
|
|
controller.api.registerForeignSources( notifData.sources, true );
|
2016-04-10 13:31:02 +00:00
|
|
|
// Add the lists according to the sources
|
|
|
|
for ( source in notifData.sources ) {
|
|
|
|
foreignListModel.getList().addGroup(
|
|
|
|
source,
|
|
|
|
notifData.sources[ source ]
|
|
|
|
);
|
|
|
|
}
|
2016-06-07 20:08:16 +00:00
|
|
|
} else if ( newNotifData.bundledNotifications ) {
|
|
|
|
// local bundle
|
|
|
|
newNotifData.modelName = 'bundle_' + notifData.id;
|
|
|
|
itemModel = new mw.echo.dm.BundleNotificationItem(
|
|
|
|
notifData.id,
|
|
|
|
newNotifData.bundledNotifications.map( createBundledNotification.bind( null, newNotifData.modelName ) ),
|
|
|
|
newNotifData
|
|
|
|
);
|
|
|
|
allModels[ newNotifData.modelName ] = itemModel;
|
2016-04-10 13:31:02 +00:00
|
|
|
} else {
|
|
|
|
// Local single notifications
|
|
|
|
itemModel = new mw.echo.dm.NotificationItem(
|
|
|
|
notifData.id,
|
|
|
|
newNotifData
|
|
|
|
);
|
|
|
|
|
|
|
|
idArray.push( notifData.id );
|
|
|
|
localItems.push( itemModel );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh local items
|
|
|
|
localListModel.addItems( localItems );
|
|
|
|
|
|
|
|
// Update the controller
|
2016-03-16 22:47:20 +00:00
|
|
|
controller.manager.setNotificationModels( allModels );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
return idArray;
|
|
|
|
},
|
|
|
|
// Failure
|
|
|
|
function ( errCode, errObj ) {
|
2016-03-16 22:47:20 +00:00
|
|
|
if ( !controller.manager.getNotificationModel( 'local' ) ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
// Update the controller
|
2016-03-16 22:47:20 +00:00
|
|
|
controller.manager.setNotificationModels( { local: localListModel } );
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
errCode: errCode,
|
|
|
|
errInfo: OO.getProp( errObj, 'error', 'info' )
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create notification data config object for notification items from the
|
|
|
|
* given API data.
|
|
|
|
*
|
|
|
|
* @param {Object} apiData API data
|
|
|
|
* @return {Object} Notification config data object
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.createNotificationData = function ( apiData ) {
|
|
|
|
var content = apiData[ '*' ] || {};
|
|
|
|
|
|
|
|
return {
|
2016-07-07 00:15:47 +00:00
|
|
|
type: apiData.section,
|
2016-04-10 13:31:02 +00:00
|
|
|
foreign: false,
|
|
|
|
source: 'local',
|
|
|
|
count: apiData.count,
|
|
|
|
read: !!apiData.read,
|
|
|
|
seen: !!apiData.read || apiData.timestamp.mw <= this.manager.getSeenTime(),
|
|
|
|
timestamp: apiData.timestamp.utcmw,
|
|
|
|
category: apiData.category,
|
|
|
|
content: {
|
|
|
|
header: content.header,
|
2016-06-07 20:08:16 +00:00
|
|
|
compactHeader: content.compactHeader,
|
2016-04-10 13:31:02 +00:00
|
|
|
body: content.body
|
|
|
|
},
|
|
|
|
iconURL: content.iconUrl,
|
|
|
|
iconType: content.icon,
|
|
|
|
primaryUrl: OO.getProp( content.links, 'primary', 'url' ),
|
2016-06-02 14:00:48 +00:00
|
|
|
secondaryUrls: OO.getProp( content.links, 'secondary' ) || [],
|
2016-06-07 20:08:16 +00:00
|
|
|
bundledIds: apiData.bundledIds,
|
|
|
|
bundledNotifications: apiData.bundledNotifications
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all items within a given list model as read.
|
|
|
|
*
|
|
|
|
* NOTE: This method is strictly for list models, and will not work for
|
|
|
|
* group list models. To mark items as read in the xwiki model, whether
|
|
|
|
* it is pre-populated or not, please see #markEntireCrossWikiItemAsRead
|
|
|
|
*
|
|
|
|
* @param {string} [modelName] Symbolic name for the model
|
2016-06-07 20:08:16 +00:00
|
|
|
* @param {boolean} [isRead=true]
|
2016-04-10 13:31:02 +00:00
|
|
|
* @return {jQuery.Promise} Promise that is resolved when all items
|
|
|
|
* were marked as read.
|
|
|
|
*/
|
2016-06-07 20:08:16 +00:00
|
|
|
mw.echo.Controller.prototype.markEntireListModelRead = function ( modelName, isRead ) {
|
2016-06-02 14:00:48 +00:00
|
|
|
var i, items, item,
|
2016-04-10 13:31:02 +00:00
|
|
|
itemIds = [],
|
2016-03-16 22:47:20 +00:00
|
|
|
model = this.manager.getNotificationModel( modelName || 'local' );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
if ( !model ) {
|
|
|
|
// Model doesn't exist
|
|
|
|
return $.Deferred().reject();
|
|
|
|
}
|
|
|
|
|
2016-06-07 20:08:16 +00:00
|
|
|
// Default to true
|
|
|
|
isRead = isRead === undefined ? true : isRead;
|
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
items = model.getItems();
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
2016-06-02 14:00:48 +00:00
|
|
|
item = items[ i ];
|
2016-06-07 20:08:16 +00:00
|
|
|
if ( item.isRead() !== isRead ) {
|
2016-06-21 12:27:39 +00:00
|
|
|
itemIds.push( item.getId() );
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 20:08:16 +00:00
|
|
|
return this.markItemsRead( itemIds, model.getName(), isRead );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all local notifications as read
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} Promise that is resolved when all
|
|
|
|
* local notifications have been marked as read.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.markLocalNotificationsRead = function () {
|
|
|
|
var itemIds = [];
|
|
|
|
|
|
|
|
this.manager.getLocalNotifications().forEach( function ( notification ) {
|
|
|
|
if ( !notification.isRead() ) {
|
|
|
|
itemIds = itemIds.concat( notification.getAllIds() );
|
|
|
|
notification.toggleRead( true );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.manager.getUnreadCounter().estimateChange( -itemIds.length );
|
|
|
|
return this.api.markItemsRead( itemIds, 'local', true ).then( this.refreshUnreadCount.bind( this ) );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch notifications from the cross-wiki sources.
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} Promise that is resolved when all items
|
|
|
|
* from the cross-wiki sources are populated into the cross-wiki
|
|
|
|
* model.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.fetchCrossWikiNotifications = function () {
|
|
|
|
var controller = this,
|
2016-03-16 22:47:20 +00:00
|
|
|
xwikiModel = this.manager.getNotificationModel( 'xwiki' );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
if ( !xwikiModel ) {
|
|
|
|
// There is no xwiki notifications model, so we can't
|
|
|
|
// fetch into it
|
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.api.fetchNotificationGroups( xwikiModel.getSourceNames(), this.manager.getTypeString() )
|
|
|
|
.then(
|
|
|
|
function ( groupList ) {
|
|
|
|
var i, notifData, listModel, group, groupItems,
|
|
|
|
items = [];
|
|
|
|
|
|
|
|
for ( group in groupList ) {
|
|
|
|
listModel = xwikiModel.getItemBySource( group );
|
|
|
|
groupItems = groupList[ group ];
|
|
|
|
|
|
|
|
items = [];
|
|
|
|
for ( i = 0; i < groupItems.length; i++ ) {
|
|
|
|
notifData = controller.createNotificationData( groupItems[ i ] );
|
|
|
|
items.push(
|
|
|
|
new mw.echo.dm.NotificationItem( groupItems[ i ].id, $.extend( notifData, {
|
2016-05-31 22:32:16 +00:00
|
|
|
modelName: 'xwiki',
|
2016-04-10 13:31:02 +00:00
|
|
|
source: group,
|
2016-05-19 20:55:21 +00:00
|
|
|
bundled: true,
|
2016-04-10 13:31:02 +00:00
|
|
|
foreign: true
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Add items
|
|
|
|
listModel.setItems( items );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function ( errCode, errObj ) {
|
|
|
|
return {
|
|
|
|
errCode: errCode,
|
|
|
|
errInfo: errCode === 'http' ?
|
|
|
|
mw.msg( 'echo-api-failure-cross-wiki' ) :
|
|
|
|
OO.getProp( errObj, 'error', 'info' )
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark local items as read in the API.
|
|
|
|
*
|
|
|
|
* @param {string[]|string} itemIds An array of item IDs, or a single item ID, to mark as read
|
2016-06-08 23:53:20 +00:00
|
|
|
* @param {string} modelName The name of the model that these items belong to
|
2016-04-10 13:31:02 +00:00
|
|
|
* @param {boolean} [isRead=true] The read state of the item; true for marking the
|
|
|
|
* item as read, false for marking the item as unread
|
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the operation
|
|
|
|
* is complete, with the number of unread notifications still remaining
|
|
|
|
* for the set type of this controller, in the given source.
|
|
|
|
*/
|
2016-06-08 23:53:20 +00:00
|
|
|
mw.echo.Controller.prototype.markItemsRead = function ( itemIds, modelName, isRead ) {
|
2016-07-14 00:03:57 +00:00
|
|
|
var items,
|
|
|
|
model = this.manager.getNotificationModel( modelName ),
|
|
|
|
readState = this.manager.getFiltersModel().getReadState(),
|
2016-06-07 20:08:16 +00:00
|
|
|
allIds = [];
|
2016-06-08 23:53:20 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
itemIds = Array.isArray( itemIds ) ? itemIds : [ itemIds ];
|
|
|
|
|
|
|
|
// Default to true
|
|
|
|
isRead = isRead === undefined ? true : isRead;
|
|
|
|
|
2016-07-14 00:03:57 +00:00
|
|
|
items = model.findByIds( itemIds );
|
|
|
|
|
|
|
|
// If we are only looking at specific read state,
|
|
|
|
// then we need to make sure the items are removed
|
|
|
|
// from the visible list, because they no longer
|
|
|
|
// correspond with the chosen state filter
|
|
|
|
if ( readState === 'read' && !isRead ) {
|
|
|
|
model.discardItems( items );
|
|
|
|
} else if ( readState === 'unread' && isRead ) {
|
|
|
|
model.discardItems( items );
|
|
|
|
// TODO: We should also find a way to update the pagination
|
|
|
|
// here properly. Do we pull more items from the next page
|
|
|
|
// when items are cleared? Do we set some threshhold for
|
|
|
|
// removed items where if it is reached, we update the list
|
|
|
|
// to reflect the new pagination? etc.
|
|
|
|
}
|
|
|
|
|
|
|
|
items.forEach( function ( notification ) {
|
2016-06-02 14:00:48 +00:00
|
|
|
allIds = allIds.concat( notification.getAllIds() );
|
2016-07-14 00:03:57 +00:00
|
|
|
if ( readState === 'all' ) {
|
|
|
|
notification.toggleRead( isRead );
|
|
|
|
}
|
2016-04-10 13:31:02 +00:00
|
|
|
} );
|
|
|
|
|
2016-07-14 00:03:57 +00:00
|
|
|
// Update pagination count
|
|
|
|
this.manager.updateCurrentPageItemCount();
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
this.manager.getUnreadCounter().estimateChange( isRead ? -allIds.length : allIds.length );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2016-06-08 23:53:20 +00:00
|
|
|
return this.api.markItemsRead( allIds, model.getSource(), isRead ).then( this.refreshUnreadCount.bind( this ) );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark cross-wiki items as read in the API.
|
|
|
|
*
|
|
|
|
* @param {string[]|string} itemIds An array of item IDs, or a single item ID, to mark as read
|
2016-07-04 14:00:59 +00:00
|
|
|
* @param {string} source The name for the source list that these items belong to
|
2016-04-10 13:31:02 +00:00
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the operation
|
|
|
|
* is complete, with the number of unread notifications still remaining
|
|
|
|
* for the set type of this controller, in the given source.
|
|
|
|
*/
|
2016-07-04 14:00:59 +00:00
|
|
|
mw.echo.Controller.prototype.markCrossWikiItemsRead = function ( itemIds, source ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
var sourceModel,
|
2016-07-04 14:00:59 +00:00
|
|
|
notifs,
|
2016-03-16 22:47:20 +00:00
|
|
|
xwikiModel = this.manager.getNotificationModel( 'xwiki' );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
if ( !xwikiModel ) {
|
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
|
|
|
this.manager.getUnreadCounter().estimateChange( -itemIds.length );
|
|
|
|
|
|
|
|
itemIds = Array.isArray( itemIds ) ? itemIds : [ itemIds ];
|
|
|
|
|
2016-07-14 00:03:57 +00:00
|
|
|
sourceModel = xwikiModel.getList().getGroupByName( source );
|
2016-04-10 13:31:02 +00:00
|
|
|
notifs = sourceModel.findByIds( itemIds );
|
2016-05-19 20:55:21 +00:00
|
|
|
sourceModel.discardItems( notifs );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2016-07-04 14:00:59 +00:00
|
|
|
return this.api.markItemsRead( itemIds, source, true )
|
2016-04-10 13:31:02 +00:00
|
|
|
.then( this.refreshUnreadCount.bind( this ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all cross-wiki notifications from all sources as read
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} Promise that is resolved when all notifications
|
|
|
|
* are marked as read
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.markEntireCrossWikiItemAsRead = function () {
|
|
|
|
var controller = this,
|
2016-03-16 22:47:20 +00:00
|
|
|
xwikiModel = this.manager.getNotificationModel( 'xwiki' );
|
2016-04-10 13:31:02 +00:00
|
|
|
|
|
|
|
if ( !xwikiModel ) {
|
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.api.fetchNotificationGroups( xwikiModel.getSourceNames(), this.manager.getTypeString() )
|
|
|
|
.then( function ( groupList ) {
|
|
|
|
var i, listModel, group, groupItems,
|
|
|
|
promises = [],
|
|
|
|
idArray = [],
|
|
|
|
itemCounter = 0;
|
|
|
|
|
|
|
|
for ( group in groupList ) {
|
|
|
|
listModel = xwikiModel.getItemBySource( group );
|
|
|
|
groupItems = groupList[ group ];
|
|
|
|
|
|
|
|
idArray = [];
|
|
|
|
for ( i = 0; i < groupItems.length; i++ ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
idArray = idArray.concat( groupItems[ i ].id ).concat( groupItems[ i ].bundledIds || [] );
|
2016-04-10 13:31:02 +00:00
|
|
|
}
|
|
|
|
itemCounter += idArray.length;
|
|
|
|
|
|
|
|
// Mark items as read in the API
|
|
|
|
promises.push(
|
2016-05-31 22:32:16 +00:00
|
|
|
controller.markCrossWikiItemsRead( idArray, listModel.getName() )
|
2016-04-10 13:31:02 +00:00
|
|
|
);
|
|
|
|
}
|
2016-06-02 14:00:48 +00:00
|
|
|
|
2016-04-10 13:31:02 +00:00
|
|
|
// Synchronously remove this model from the widget
|
|
|
|
controller.removeCrossWikiItem();
|
|
|
|
|
|
|
|
return mw.echo.api.NetworkHandler.static.waitForAllPromises( promises );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the entire cross-wiki model.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.removeCrossWikiItem = function () {
|
2016-03-16 22:47:20 +00:00
|
|
|
this.manager.removeNotificationModel( 'xwiki' );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh the unread notifications counter
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the counter
|
|
|
|
* is updated with the actual unread count from the server.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.refreshUnreadCount = function () {
|
|
|
|
return this.manager.getUnreadCounter().update();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-07-08 22:56:01 +00:00
|
|
|
* Update seenTime for the given source
|
2016-04-10 13:31:02 +00:00
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the
|
|
|
|
* seenTime was updated for all given types.
|
|
|
|
*/
|
2016-07-08 22:56:01 +00:00
|
|
|
mw.echo.Controller.prototype.updateSeenTime = function ( source ) {
|
|
|
|
var controller = this;
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2016-07-08 22:56:01 +00:00
|
|
|
return this.api.updateSeenTime( this.getTypes(), source )
|
|
|
|
.then( function ( time ) {
|
|
|
|
controller.manager.getSeenTimeModel().setSeenTimeForSource( source, time );
|
|
|
|
} );
|
|
|
|
};
|
2016-04-10 13:31:02 +00:00
|
|
|
|
2016-07-08 22:56:01 +00:00
|
|
|
/**
|
|
|
|
* Update local seen time
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the
|
|
|
|
* seenTime was updated for all given types.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.updateLocalSeenTime = function () {
|
|
|
|
return this.updateSeenTime( 'local' );
|
2016-04-10 13:31:02 +00:00
|
|
|
};
|
|
|
|
|
2016-07-08 22:56:01 +00:00
|
|
|
/**
|
|
|
|
* Update seenTime for the currently selected source
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the
|
|
|
|
* seenTime was updated for all given types.
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.updateSeenTimeForCurrentSource = function () {
|
|
|
|
var currSource = this.manager.getFiltersModel().getSourcePagesModel().getCurrentSource();
|
|
|
|
|
|
|
|
return this.updateSeenTime( currSource );
|
|
|
|
};
|
2016-04-10 13:31:02 +00:00
|
|
|
/**
|
|
|
|
* Get the types associated with the controller and model
|
|
|
|
*
|
|
|
|
* @return {string[]} Notification types
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.getTypes = function () {
|
|
|
|
return this.manager.getTypes();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a string representation of the notification type.
|
|
|
|
* It could be 'alert', 'message' or, if both are set, 'all'
|
|
|
|
*
|
|
|
|
* @return {string} String representation of notifications type
|
|
|
|
*/
|
|
|
|
mw.echo.Controller.prototype.getTypeString = function () {
|
|
|
|
return this.manager.getTypeString();
|
|
|
|
};
|
|
|
|
} )( mediaWiki, jQuery );
|