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