mediawiki-extensions-Echo/modules/api/mw.echo.api.LocalAPIHandler.js
Moriel Schottlender 820d2b0fa7 Add a cross-wiki sidebar to the Special:Notifications page
Add a sidebar with cross-wiki sources and pages of unread notifications.
The filter allows the user to fetch notifications from a foreign source
and specific pages if those exist.

Bug: T129366
Change-Id: I57d827a47f80274d75364c2099a9624049a26834
2016-06-21 14:49:42 -07:00

120 lines
2.9 KiB
JavaScript

( function ( mw, $ ) {
/**
* Notification API handler
*
* @class
* @extends mw.echo.api.APIHandler
*
* @constructor
* @param {Object} [config] Configuration object
*/
mw.echo.api.LocalAPIHandler = function MwEchoApiLocalAPIHandler( config ) {
config = config || {};
// Parent constructor
mw.echo.api.LocalAPIHandler.parent.call( this,
new mw.Api( { ajax: { cache: false } } ),
config
);
};
/* Setup */
OO.inheritClass( mw.echo.api.LocalAPIHandler, mw.echo.api.APIHandler );
/**
* @inheritdoc
*/
mw.echo.api.LocalAPIHandler.prototype.fetchNotifications = function ( type, source, isForced, overrideParams ) {
if ( overrideParams ) {
return this.createNewFetchNotificationPromise( type, source, overrideParams );
} else if ( isForced || this.isFetchingErrorState( type, source ) ) {
// Force new promise
return this.createNewFetchNotificationPromise( type, source, overrideParams );
}
return this.getFetchNotificationPromise( type, source, overrideParams );
};
/**
* @inheritdoc
*/
mw.echo.api.LocalAPIHandler.prototype.updateSeenTime = function ( type ) {
return this.api.postWithToken( 'csrf', {
action: 'echomarkseen',
type: this.normalizedType[ type ]
} )
.then( function ( data ) {
return data.query.echomarkseen.timestamp;
} );
};
/**
* @inheritdoc
*/
mw.echo.api.LocalAPIHandler.prototype.markAllRead = function ( type ) {
var data = {
action: 'echomarkread',
sections: this.normalizedType[ type ]
};
return this.api.postWithToken( 'csrf', data )
.then( function ( result ) {
return OO.getProp( result.query, 'echomarkread', type, 'rawcount' ) || 0;
} );
};
/**
* @inheritdoc
*/
mw.echo.api.LocalAPIHandler.prototype.markItemsRead = function ( itemIdArray, isRead ) {
var data = {
action: 'echomarkread'
};
if ( isRead ) {
data.list = itemIdArray.join( '|' );
} else {
data.unreadlist = itemIdArray.join( '|' );
}
return this.api.postWithToken( 'csrf', data );
};
/**
* @inheritdoc
*/
mw.echo.api.LocalAPIHandler.prototype.fetchUnreadCount = function ( type ) {
var normalizedType = this.normalizedType[ type ],
apiData = {
action: 'query',
meta: 'notifications',
notsections: normalizedType,
notgroupbysection: 1,
notmessageunreadfirst: 1,
notlimit: this.limit,
notprop: 'count',
notcrosswikisummary: 1,
uselang: this.userLang
};
return this.api.get( apiData )
.then( function ( result ) {
if ( type === 'message' || type === 'alert' ) {
return OO.getProp( result.query, 'notifications', normalizedType, 'rawcount' ) || 0;
} else {
return OO.getProp( result.query, 'notifications', 'rawcount' ) || 0;
}
} );
};
/**
* @inheritdoc
*/
mw.echo.api.LocalAPIHandler.prototype.getTypeParams = function ( type ) {
return $.extend( {}, this.typeParams[ type ], {
notcrosswikisummary: 1
} );
};
} )( mediaWiki, jQuery );