summaryrefslogtreecommitdiff
blob: c00ce7401eee7737243e206a3f5ab436885fc4b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
( function () {
	/**
	 * Notification API handler
	 *
	 * @class
	 * @extends mw.echo.api.APIHandler
	 *
	 * @constructor
	 * @param {Object} [config] Configuration object
	 */
	mw.echo.api.LocalAPIHandler = function MwEchoApiLocalAPIHandler( config ) {
		// Parent constructor
		mw.echo.api.LocalAPIHandler.super.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 ) {
		type = Array.isArray( type ) ? type : [ type ];

		// This is a GET request, not a POST request, for multi-DC support (see T222851)
		return this.api.get( {
			action: 'echomarkseen',
			type: type.length === 1 ? type[ 0 ] : 'all',
			timestampFormat: 'ISO_8601'
		} )
			.then( function ( data ) {
				return data.query.echomarkseen.timestamp;
			} );
	};

	/**
	 * @inheritdoc
	 */
	mw.echo.api.LocalAPIHandler.prototype.markAllRead = function ( source, type ) {
		var data;
		type = Array.isArray( type ) ? type : [ type ];
		data = {
			action: 'echomarkread',
			sections: type.join( '|' )
		};
		if ( !this.isSourceLocal( source ) ) {
			data.wikis = source;
		}

		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 ( source, itemIdArray, isRead ) {
		var data = {
			action: 'echomarkread'
		};

		if ( !this.isSourceLocal( source ) ) {
			data.wikis = source;
		}

		if ( isRead ) {
			data.list = itemIdArray.join( '|' );
		} else {
			data.unreadlist = itemIdArray.join( '|' );
		}

		return this.api.postWithToken( 'csrf', data );
	};

	/**
	 * Fetch the number of unread notifications.
	 *
	 * @param {string} type Notification type, 'alert', 'message' or 'all'
	 * @param {boolean} [ignoreCrossWiki] Ignore cross-wiki notifications when fetching the count.
	 *  If set to false (by default) it counts notifications across all wikis.
	 * @return {jQuery.Promise} Promise which resolves with the unread count
	 */
	mw.echo.api.LocalAPIHandler.prototype.fetchUnreadCount = function ( type, ignoreCrossWiki ) {
		var normalizedType = this.normalizedType[ type ],
			apiData = {
				action: 'query',
				meta: 'notifications',
				notsections: normalizedType,
				notgroupbysection: 1,
				notmessageunreadfirst: 1,
				notlimit: this.limit,
				notprop: 'count',
				uselang: this.userLang
			};

		if ( !ignoreCrossWiki ) {
			apiData.notcrosswikisummary = 1;
		}

		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
		} );
	};
}() );