summaryrefslogtreecommitdiff
blob: f2b9441bfcdf7fd8e50558145dc1310e11c7620d (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
<?php

/**
 * Manages what wikis a user has unread notifications on
 */
class EchoUnreadWikis {
	/**
	 * @var string
	 */
	const DEFAULT_TS = '00000000000000';

	/**
	 * @var int
	 */
	private $id;

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

	/**
	 * @param int $id Central user id
	 */
	public function __construct( $id ) {
		$this->id = $id;
		$this->dbFactory = MWEchoDbFactory::newFromDefault();
	}

	/**
	 * Use the user id provided by the CentralIdLookup
	 *
	 * @param User $user
	 * @return EchoUnreadWikis|false
	 */
	public static function newFromUser( User $user ) {
		$lookup = CentralIdLookup::factory();
		$id = $lookup->centralIdFromLocalUser( $user, CentralIdLookup::AUDIENCE_RAW );
		if ( !$id ) {
			return false;
		}

		return new self( $id );
	}

	/**
	 * @param int $index DB_* constant
	 * @return bool|\Wikimedia\Rdbms\IDatabase
	 */
	private function getDB( $index ) {
		return $this->dbFactory->getSharedDb( $index );
	}

	/**
	 * @return array[]
	 */
	public function getUnreadCounts() {
		$dbr = $this->getDB( DB_REPLICA );
		if ( $dbr === false ) {
			return [];
		}

		$rows = $dbr->select(
			'echo_unread_wikis',
			[
				'euw_wiki',
				'euw_alerts', 'euw_alerts_ts',
				'euw_messages', 'euw_messages_ts',
			],
			[ 'euw_user' => $this->id ],
			__METHOD__
		);

		$wikis = [];
		foreach ( $rows as $row ) {
			if ( !$row->euw_alerts && !$row->euw_messages ) {
				// This shouldn't happen, but lets be safe...
				continue;
			}
			$wikis[$row->euw_wiki] = [
				EchoAttributeManager::ALERT => [
					'count' => $row->euw_alerts,
					'ts' => $row->euw_alerts_ts,
				],
				EchoAttributeManager::MESSAGE => [
					'count' => $row->euw_messages,
					'ts' => $row->euw_messages_ts,
				],
			];
		}

		return $wikis;
	}

	/**
	 * @param string $wiki Wiki code
	 * @param int $alertCount Number of alerts
	 * @param MWTimestamp|bool $alertTime Timestamp of most recent unread alert, or
	 *   false meaning no timestamp because there are no unread alerts.
	 * @param int $msgCount Number of messages
	 * @param MWTimestamp|bool $msgTime Timestamp of most recent message, or
	 *   false meaning no timestamp because there are no unread messages.
	 */
	public function updateCount( $wiki, $alertCount, $alertTime, $msgCount, $msgTime ) {
		$dbw = $this->getDB( DB_MASTER );
		if ( $dbw === false || $dbw->isReadOnly() ) {
			return;
		}

		$conditions = [
			'euw_user' => $this->id,
			'euw_wiki' => $wiki,
		];

		if ( $alertCount || $msgCount ) {
			$values = [
				'euw_alerts' => $alertCount,
				'euw_alerts_ts' => $alertTime
					? $alertTime->getTimestamp( TS_MW )
					: static::DEFAULT_TS,
				'euw_messages' => $msgCount,
				'euw_messages_ts' => $msgTime
					? $msgTime->getTimestamp( TS_MW )
					: static::DEFAULT_TS,
			];

			// when there is unread alert(s) and/or message(s), upsert the row
			$dbw->upsert(
				'echo_unread_wikis',
				$conditions + $values,
				[ 'euw_user', 'euw_wiki' ],
				$values,
				__METHOD__
			);
		} else {
			// No unread notifications, delete the row
			$dbw->delete(
				'echo_unread_wikis',
				$conditions,
				__METHOD__
			);
		}
	}
}