2016-01-15 22:11:33 +00:00
|
|
|
( function ( mw ) {
|
|
|
|
/**
|
|
|
|
* A class defining Echo API instructions and network operations
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
mw.echo.api.EchoApi = function MwEchoApiEchoApi() {
|
|
|
|
this.network = new mw.echo.api.NetworkHandler();
|
|
|
|
|
|
|
|
this.fetchingPromise = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
OO.initClass( mw.echo.api.EchoApi );
|
|
|
|
|
2016-02-27 05:43:46 +00:00
|
|
|
/**
|
|
|
|
* Register a set of sources.
|
|
|
|
*
|
|
|
|
* @param {Object} sources Object mapping source names to config objects
|
|
|
|
*/
|
|
|
|
mw.echo.api.EchoApi.prototype.registerForeignSources = function ( sources ) {
|
|
|
|
var s;
|
|
|
|
for ( s in sources ) {
|
2016-05-13 18:54:38 +00:00
|
|
|
this.network.setApiHandler( s, new mw.echo.api.ForeignAPIHandler( sources[ s ].url ) );
|
2016-02-27 05:43:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-15 22:11:33 +00:00
|
|
|
/**
|
|
|
|
* Fetch notifications from the server based on type
|
2016-03-07 13:29:15 +00:00
|
|
|
*
|
2016-03-25 18:52:59 +00:00
|
|
|
* @param {string} type Notification type to fetch: 'alert', 'message', or 'all'
|
2016-05-04 00:50:27 +00:00
|
|
|
* @param {string|string[]} [sources] The source from which to fetch the notifications.
|
|
|
|
* If not given, the local notifications will be fetched.
|
2016-01-15 22:11:33 +00:00
|
|
|
* @param {boolean} [isForced] Force a refresh on the fetch notifications promise
|
2016-03-25 18:47:00 +00:00
|
|
|
* @return {jQuery.Promise} Promise that is resolved with all notifications for the
|
2016-01-15 22:11:33 +00:00
|
|
|
* requested types.
|
|
|
|
*/
|
2016-05-04 00:50:27 +00:00
|
|
|
mw.echo.api.EchoApi.prototype.fetchNotifications = function ( type, sources, isForced ) {
|
|
|
|
sources = Array.isArray( sources ) ?
|
|
|
|
sources :
|
|
|
|
sources ?
|
|
|
|
[ sources ] :
|
|
|
|
null;
|
2016-01-15 22:11:33 +00:00
|
|
|
|
2016-05-04 00:50:27 +00:00
|
|
|
return this.network.getApiHandler( 'local' ).fetchNotifications( type, sources, isForced )
|
2016-01-15 22:11:33 +00:00
|
|
|
.then( function ( result ) {
|
2016-02-27 05:43:46 +00:00
|
|
|
return OO.getProp( result.query, 'notifications' );
|
2016-01-15 22:11:33 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch notifications from several sources
|
|
|
|
*
|
|
|
|
* @param {string[]} sourceArray An array of sources to fetch from the group
|
|
|
|
* @param {string} type Notification type
|
2016-04-10 13:31:02 +00:00
|
|
|
* @return {jQuery.Promise} A promise that resolves with an object that maps wiki
|
|
|
|
* names to an array of their items' API data objects.
|
2016-01-15 22:11:33 +00:00
|
|
|
*/
|
|
|
|
mw.echo.api.EchoApi.prototype.fetchNotificationGroups = function ( sourceArray, type ) {
|
2016-05-04 00:50:27 +00:00
|
|
|
return this.network.getApiHandler( 'local' ).fetchNotifications( type, sourceArray )
|
|
|
|
.then( function ( result ) {
|
2016-04-10 13:31:02 +00:00
|
|
|
var i,
|
|
|
|
items = OO.getProp( result, 'query', 'notifications', 'list' ),
|
|
|
|
groups = {};
|
|
|
|
|
|
|
|
// Split the items to groups
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
|
|
|
groups[ items[ i ].wiki ] = groups[ items[ i ].wiki ] || [];
|
|
|
|
groups[ items[ i ].wiki ].push( items[ i ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups;
|
2016-05-04 00:50:27 +00:00
|
|
|
} );
|
2016-01-15 22:11:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark items as read in the API.
|
|
|
|
*
|
|
|
|
* @param {string[]} itemIds An array of item IDs to mark as read
|
|
|
|
* @param {string} source The source that these items belong to
|
2016-03-04 22:44:22 +00:00
|
|
|
* @param {boolean} [isRead] The read state of the item; true for marking the
|
|
|
|
* item as read, false for marking the item as unread
|
2016-01-15 22:11:33 +00:00
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the operation
|
|
|
|
* is complete, with the number of unread notifications still remaining
|
|
|
|
* for that type in the given source
|
|
|
|
*/
|
2016-03-04 22:44:22 +00:00
|
|
|
mw.echo.api.EchoApi.prototype.markItemsRead = function ( itemIds, source, isRead ) {
|
|
|
|
return this.network.getApiHandler( source ).markItemsRead( itemIds, isRead );
|
2016-01-15 22:11:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all notifications for a given type as read in the given source.
|
|
|
|
*
|
2016-03-25 18:47:00 +00:00
|
|
|
* @param {string} source Symbolic name of notifications source
|
|
|
|
* @param {string} type Notifications type
|
2016-01-15 22:11:33 +00:00
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the operation
|
|
|
|
* is complete, with the number of unread notifications still remaining
|
|
|
|
* for that type in the given source
|
|
|
|
*/
|
|
|
|
mw.echo.api.EchoApi.prototype.markAllRead = function ( source, type ) {
|
|
|
|
// FIXME: This specific method sends an operation
|
|
|
|
// to the API that marks all notifications of the given type as read regardless
|
|
|
|
// of whether they were actually seen by the user.
|
|
|
|
// We should consider removing the use of this method and, instead,
|
|
|
|
// using strictly the 'markItemsRead' by giving the API only the
|
|
|
|
// notifications that are available to the user.
|
|
|
|
return this.network.getApiHandler( source ).markAllRead( type );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the number of unread notifications for the given type in the given
|
|
|
|
* source.
|
|
|
|
*
|
|
|
|
* @param {string} source Notifications source
|
|
|
|
* @param {string} type Notification type
|
|
|
|
* @return {jQuery.Promise} A promise that is resolved with the number of
|
|
|
|
* unread notifications for the given type and source.
|
|
|
|
*/
|
|
|
|
mw.echo.api.EchoApi.prototype.fetchUnreadCount = function ( source, type ) {
|
|
|
|
return this.network.getApiHandler( source ).fetchUnreadCount( type );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the seenTime property for the given type and source.
|
|
|
|
*
|
|
|
|
* @param {string} source Notification source
|
|
|
|
* @param {string} type Notification type
|
|
|
|
* @return {jQuery.Promise} A promise that is resolved when the operation is complete.
|
|
|
|
*/
|
|
|
|
mw.echo.api.EchoApi.prototype.updateSeenTime = function ( source, type ) {
|
|
|
|
return this.network.getApiHandler( source ).updateSeenTime( type );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the API promise for fetch notification is in an error
|
|
|
|
* state for the given source and notification type.
|
|
|
|
*
|
|
|
|
* @param {string} source Notification source.
|
|
|
|
* @param {string} type Notification type
|
2016-03-25 18:47:00 +00:00
|
|
|
* @return {boolean} The API response for fetching notification has
|
2016-01-15 22:11:33 +00:00
|
|
|
* resolved in an error state, or is rejected.
|
|
|
|
*/
|
|
|
|
mw.echo.api.EchoApi.prototype.isFetchingErrorState = function ( source, type ) {
|
2016-05-04 00:50:27 +00:00
|
|
|
return this.network.getApiHandler( source ).isFetchingErrorState( type, [ source ] );
|
2016-01-15 22:11:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the fetch notifications promise active for the current source and type.
|
|
|
|
*
|
|
|
|
* @param {string} source Notification source.
|
|
|
|
* @param {string} type Notification type
|
|
|
|
* @return {jQuery.Promise} Promise that is resolved when notifications are
|
|
|
|
* fetched from the API.
|
|
|
|
*/
|
|
|
|
mw.echo.api.EchoApi.prototype.getFetchNotificationPromise = function ( source, type ) {
|
|
|
|
return this.network.getApiHandler( source ).getFetchNotificationPromise( type );
|
|
|
|
};
|
|
|
|
|
|
|
|
} )( mediaWiki );
|