Debian Codegraph @

extensions/Echo/modules/api/mw.echo.api.LocalAPIHandler.js

raw symbols SCIP
1 ( function () {
2 /**
3 * Notification API handler
4 *
5 * @class
6 * @extends mw.echo.api.APIHandler
7 *
8 * @constructor
9 * @param {Object} [config] Configuration object
10 */
11 mw.echo.api.LocalAPIHandler = function MwEchoApiLocalAPIHandler( config ) {
12 // Parent constructor
13 mw.echo.api.LocalAPIHandler.super.call( this,
14 new mw.Api( { ajax: { cache: false } } ),
15 config
16 );
17 };
18
19 /* Setup */
20
21 OO.inheritClass( mw.echo.api.LocalAPIHandler, mw.echo.api.APIHandler );
22
23 /**
24 * @inheritdoc
25 */
26 mw.echo.api.LocalAPIHandler.prototype.fetchNotifications = function ( type, source, isForced, overrideParams ) {
27 if ( overrideParams ) {
28 return this.createNewFetchNotificationPromise( type, source, overrideParams );
29 } else if ( isForced || this.isFetchingErrorState( type, source ) ) {
30 // Force new promise
31 return this.createNewFetchNotificationPromise( type, source, overrideParams );
32 }
33
34 return this.getFetchNotificationPromise( type, source, overrideParams );
35 };
36
37 /**
38 * @inheritdoc
39 */
40 mw.echo.api.LocalAPIHandler.prototype.updateSeenTime = function ( type ) {
41 type = Array.isArray( type ) ? type : [ type ];
42
43 // This is a GET request, not a POST request, for multi-DC support (see T222851)
44 return this.api.get( {
45 action: 'echomarkseen',
46 type: type.length === 1 ? type[ 0 ] : 'all',
47 timestampFormat: 'ISO_8601'
48 } )
49 .then( ( data ) => data.query.echomarkseen.timestamp );
50 };
51
52 /**
53 * @inheritdoc
54 */
55 mw.echo.api.LocalAPIHandler.prototype.markAllRead = function ( source, type ) {
56 const data = {
57 action: 'echomarkread'
58 };
59 type = Array.isArray( type ) ? type : [ type ];
60 if ( type.indexOf( 'all' ) !== -1 ) {
61 // As specified in the documentation of the parent function, the type parameter can be
62 // 'all'. We especially handle that case here to match the PHP API. Note: Other values
63 // of the array will be ignored.
64 data.all = true;
65 } else {
66 data.sections = type;
67 }
68 if ( !this.isSourceLocal( source ) ) {
69 data.wikis = source;
70 }
71
72 return this.api.postWithToken( 'csrf', data )
73 .then( ( result ) => OO.getProp( result.query, 'echomarkread', type, 'rawcount' ) || 0 );
74 };
75
76 /**
77 * @inheritdoc
78 */
79 mw.echo.api.LocalAPIHandler.prototype.markItemsRead = function ( source, itemIdArray, isRead ) {
80 const data = {
81 action: 'echomarkread'
82 };
83
84 if ( !this.isSourceLocal( source ) ) {
85 data.wikis = source;
86 }
87
88 if ( isRead ) {
89 data.list = itemIdArray;
90 } else {
91 data.unreadlist = itemIdArray;
92 }
93
94 return this.api.postWithToken( 'csrf', data );
95 };
96
97 /**
98 * Fetch the number of unread notifications.
99 *
100 * @param {string} type Notification type, 'alert', 'message' or 'all'
101 * @param {boolean} [ignoreCrossWiki] Ignore cross-wiki notifications when fetching the count.
102 * If set to false (by default) it counts notifications across all wikis.
103 * @return {jQuery.Promise} Promise which resolves with the unread count
104 */
105 mw.echo.api.LocalAPIHandler.prototype.fetchUnreadCount = function ( type, ignoreCrossWiki ) {
106 const normalizedType = this.normalizedType[ type ],
107 apiData = {
108 action: 'query',
109 meta: 'notifications',
110 notsections: normalizedType,
111 notgroupbysection: 1,
112 notmessageunreadfirst: 1,
113 notlimit: this.limit,
114 notprop: 'count',
115 uselang: this.userLang
116 };
117
118 if ( !ignoreCrossWiki ) {
119 apiData.notcrosswikisummary = 1;
120 }
121
122 return this.api.get( apiData )
123 .then( ( result ) => {
124 if ( type === 'message' || type === 'alert' ) {
125 return OO.getProp( result.query, 'notifications', normalizedType, 'rawcount' ) || 0;
126 } else {
127 return OO.getProp( result.query, 'notifications', 'rawcount' ) || 0;
128 }
129 } );
130 };
131
132 /**
133 * @inheritdoc
134 */
135 mw.echo.api.LocalAPIHandler.prototype.getTypeParams = function ( type ) {
136 return Object.assign( {}, this.typeParams[ type ], {
137 notcrosswikisummary: 1
138 } );
139 };
140 }() );