summaryrefslogtreecommitdiff
blob: d8087715a0e66f9151355d063599be5447231874 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php

/**
 * Database gateway which handles direct database interaction with the
 * echo_notification & echo_event for a user, that wouldn't require
 * loading data into models
 */
class EchoUserNotificationGateway {

	/**
	 * @var MWEchoDbFactory
	 */
	protected $dbFactory;

	/**
	 * @var User
	 */
	protected $user;

	/**
	 * The tables for this gateway.
	 *
	 * @var string
	 */
	protected static $eventTable = 'echo_event';

	/**
	 * The tables for this gateway.
	 *
	 * @var string
	 */
	protected static $notificationTable = 'echo_notification';
	/**
	 * @var Config
	 */
	private $config;

	/**
	 * @param User $user
	 * @param MWEchoDbFactory $dbFactory
	 * @param Config $config
	 */
	public function __construct( User $user, MWEchoDbFactory $dbFactory, Config $config ) {
		$this->user = $user;
		$this->dbFactory = $dbFactory;
		$this->config = $config;
	}

	public function getDB( $dbSource ) {
		return $this->dbFactory->getEchoDb( $dbSource );
	}

	/**
	 * Mark notifications as read
	 * @param array $eventIDs
	 * @return bool Returns true when data has been updated in DB, false on
	 *   failure, or when there was nothing to update
	 */
	public function markRead( array $eventIDs ) {
		if ( !$eventIDs ) {
			return false;
		}

		$dbw = $this->getDB( DB_MASTER );
		if ( $dbw->isReadOnly() ) {
			return false;
		}

		$success = true;
		foreach (
			array_chunk( $eventIDs, $this->config->get( 'UpdateRowsPerQuery' ) ) as $batch
		) {
			$success = $dbw->update(
				self::$notificationTable,
				[ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ],
				[
					'notification_user' => $this->user->getId(),
					'notification_event' => $batch,
					'notification_read_timestamp' => null,
				],
				__METHOD__
			) && $success;
		}

		return $success;
	}

	/**
	 * Mark notifications as unread
	 * @param array $eventIDs
	 * @return bool Returns true when data has been updated in DB, false on
	 *   failure, or when there was nothing to update
	 */
	public function markUnRead( array $eventIDs ) {
		if ( !$eventIDs ) {
			return false;
		}

		$dbw = $this->getDB( DB_MASTER );
		if ( $dbw->isReadOnly() ) {
			return false;
		}

		$success = true;
		foreach (
			array_chunk( $eventIDs, $this->config->get( 'UpdateRowsPerQuery' ) ) as $batch
		) {
			$success = $dbw->update(
				self::$notificationTable,
				[ 'notification_read_timestamp' => null ],
				[
					'notification_user' => $this->user->getId(),
					'notification_event' => $batch,
					'notification_read_timestamp IS NOT NULL'
				],
				__METHOD__
			) && $success;
		}
		return $success;
	}

	/**
	 * Mark all notification as read, use MWEchoNotifUser::markAllRead() instead
	 * @deprecated may need this when running in a job or revive this when we
	 * have updateJoin()
	 */
	public function markAllRead() {
		$dbw = $this->getDB( DB_MASTER );
		if ( $dbw->isReadOnly() ) {
			return false;
		}

		$dbw->update(
			self::$notificationTable,
			[ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ],
			[
				'notification_user' => $this->user->getId(),
				'notification_read_timestamp' => null,
			],
			__METHOD__
		);

		return true;
	}

	/**
	 * Get notification count for the types specified
	 * @param int $dbSource use master or replica storage to pull count
	 * @param array $eventTypesToLoad event types to retrieve
	 * @param int $cap Max count
	 * @return int
	 */
	public function getCappedNotificationCount(
		$dbSource,
		array $eventTypesToLoad = [],
		$cap = MWEchoNotifUser::MAX_BADGE_COUNT
	) {
		// double check
		if ( !in_array( $dbSource, [ DB_REPLICA, DB_MASTER ] ) ) {
			$dbSource = DB_REPLICA;
		}

		if ( !$eventTypesToLoad ) {
			return 0;
		}

		$db = $this->getDB( $dbSource );
		return $db->selectRowCount(
			[
				self::$notificationTable,
				self::$eventTable
			],
			'1',
			[
				'notification_user' => $this->user->getId(),
				'notification_read_timestamp' => null,
				'event_deleted' => 0,
				'event_type' => $eventTypesToLoad,
			],
			__METHOD__,
			[ 'LIMIT' => $cap ],
			[
				'echo_event' => [ 'LEFT JOIN', 'notification_event=event_id' ],
			]
		);
	}

	/**
	 * IMPORTANT: should only call this function if the number of unread notification
	 * is reasonable, for example, unread notification count is less than the max
	 * display defined in MWEchoNotifUser::MAX_BADGE_COUNT
	 * @param string $type
	 * @return int[]
	 */
	public function getUnreadNotifications( $type ) {
		$dbr = $this->getDB( DB_REPLICA );
		$res = $dbr->select(
			[
				self::$notificationTable,
				self::$eventTable
			],
			[ 'notification_event' ],
			[
				'notification_user' => $this->user->getId(),
				'notification_read_timestamp' => null,
				'event_deleted' => 0,
				'event_type' => $type,
				'notification_event = event_id'
			],
			__METHOD__
		);

		$eventIds = [];
		if ( $res ) {
			foreach ( $res as $row ) {
				$eventIds[$row->notification_event] = $row->notification_event;
			}
		}

		return $eventIds;
	}

}