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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
<?php
class EchoNotification extends EchoAbstractEntity implements Bundleable {
/**
* @var User
*/
protected $user;
/**
* @var EchoEvent
*/
protected $event;
/**
* The target page object for the notification if there is one. Null means
* the information has not been loaded.
*
* @var EchoTargetPage[]|null
*/
protected $targetPages;
/**
* @var string
*/
protected $timestamp;
/**
* @var string
*/
protected $readTimestamp;
/**
* Determine whether this is a bundle base. Default is 1,
* which means it's a bundle base
* @var int
*/
protected $bundleBase = 1;
/**
* The hash used to determine if a set of event could be bundled
* @var string
*/
protected $bundleHash = '';
/**
* The hash used to bundle events to display
* @var string
*/
protected $bundleDisplayHash = '';
/**
* @var EchoNotification[]
*/
protected $bundledNotifications;
/**
* Do not use this constructor.
*/
protected function __construct() {
}
/**
* Creates an EchoNotification object based on event and user
* @param array $info The following keys are required:
* - 'event' The EchoEvent being notified about.
* - 'user' The User being notified.
* @throws MWException
* @return EchoNotification
*/
public static function create( array $info ) {
$obj = new EchoNotification();
static $validFields = [ 'event', 'user' ];
foreach ( $validFields as $field ) {
if ( isset( $info[$field] ) ) {
$obj->$field = $info[$field];
} else {
throw new MWException( "Field $field is required" );
}
}
if ( !$obj->user instanceof User ) {
throw new InvalidArgumentException( 'Invalid user parameter, expected: User object' );
}
if ( !$obj->event instanceof EchoEvent ) {
throw new InvalidArgumentException( 'Invalid event parameter, expected: EchoEvent object' );
}
// Notification timestamp should be the same as event timestamp
$obj->timestamp = $obj->event->getTimestamp();
// Safe fallback
if ( !$obj->timestamp ) {
$obj->timestamp = wfTimestampNow();
}
// @Todo - Database insert logic should not be inside the model
$obj->insert();
return $obj;
}
/**
* Adds this new notification object to the backend storage.
*/
protected function insert() {
global $wgEchoNotifications;
$notifMapper = new EchoNotificationMapper();
// Get the bundle key for this event if web bundling is enabled
$bundleKey = '';
if ( !empty( $wgEchoNotifications[$this->event->getType()]['bundle']['web'] ) ) {
Hooks::run( 'EchoGetBundleRules', [ $this->event, &$bundleKey ] );
}
if ( $bundleKey ) {
$hash = md5( $bundleKey );
$this->bundleHash = $hash;
$lastNotif = $notifMapper->fetchNewestByUserBundleHash( $this->user, $hash );
// Use a new display hash if:
// 1. there was no last bundle notification
// 2. last bundle notification with the same hash was read
if ( $lastNotif && !$lastNotif->getReadTimestamp() ) {
$this->bundleDisplayHash = $lastNotif->getBundleDisplayHash();
} else {
$this->bundleDisplayHash = md5( $bundleKey . '-display-hash-' . wfTimestampNow() );
}
}
$notifUser = MWEchoNotifUser::newFromUser( $this->user );
$section = $this->event->getSection();
// Add listener to refresh notification count upon insert
$notifMapper->attachListener( 'insert', 'refresh-notif-count',
function () use ( $notifUser, $section ) {
$notifUser->resetNotificationCount();
}
);
$notifMapper->insert( $this );
if ( $this->event->getCategory() === 'edit-user-talk' ) {
$this->user->setNewtalk( true );
}
Hooks::run( 'EchoCreateNotificationComplete', [ $this ] );
}
/**
* Load a notification record from std class
* @param stdClass $row
* @param EchoTargetPage[]|null $targetPages An array of EchoTargetPage instances, or null if not loaded.
* @return EchoNotification|false False if failed to load/unserialize
*/
public static function newFromRow( $row, $targetPages = null ) {
$notification = new EchoNotification();
if ( property_exists( $row, 'event_type' ) ) {
$notification->event = EchoEvent::newFromRow( $row );
} else {
$notification->event = EchoEvent::newFromID( $row->notification_event );
}
if ( $notification->event === false ) {
return false;
}
$notification->targetPages = $targetPages;
$notification->user = User::newFromId( $row->notification_user );
// Notification timestamp should never be empty
$notification->timestamp = wfTimestamp( TS_MW, $row->notification_timestamp );
// Only convert to MW format if it is not empty, otherwise
// wfTimestamp would use current timestamp for empty cases
if ( $row->notification_read_timestamp ) {
$notification->readTimestamp = wfTimestamp( TS_MW, $row->notification_read_timestamp );
}
$notification->bundleBase = $row->notification_bundle_base;
$notification->bundleHash = $row->notification_bundle_hash;
$notification->bundleDisplayHash = $row->notification_bundle_display_hash;
return $notification;
}
/**
* Convert object property to database row array
* @return array
*/
public function toDbArray() {
return [
'notification_event' => $this->event->getId(),
'notification_user' => $this->user->getId(),
'notification_timestamp' => $this->timestamp,
'notification_read_timestamp' => $this->readTimestamp,
'notification_bundle_base' => $this->bundleBase,
'notification_bundle_hash' => $this->bundleHash,
'notification_bundle_display_hash' => $this->bundleDisplayHash
];
}
/**
* Getter method
* @return EchoEvent The event for this notification
*/
public function getEvent() {
return $this->event;
}
/**
* Getter method
* @return User The recipient of this notification
*/
public function getUser() {
return $this->user;
}
/**
* Getter method
* @return string Notification creation timestamp
*/
public function getTimestamp() {
return $this->timestamp;
}
/**
* Getter method
* @return string|null Notification read timestamp
*/
public function getReadTimestamp() {
return $this->readTimestamp;
}
public function isRead() {
return $this->getReadTimestamp() !== null;
}
/**
* Getter method
* @return int Notification bundle base
*/
public function getBundleBase() {
return $this->bundleBase;
}
/**
* Getter method
* @return string|null Notification bundle hash
*/
public function getBundleHash() {
return $this->bundleHash;
}
/**
* Getter method
* @return string|null Notification bundle display hash
*/
public function getBundleDisplayHash() {
return $this->bundleDisplayHash;
}
/**
* Getter method. Returns an array of EchoTargetPages, or null if they have
* not been loaded.
*
* @return EchoTargetPage[]|null
*/
public function getTargetPages() {
return $this->targetPages;
}
public function setBundledNotifications( $notifications ) {
$this->bundledNotifications = $notifications;
}
public function getBundledNotifications() {
return $this->bundledNotifications;
}
/**
* @inheritDoc
*/
public function canBeBundled() {
return !$this->isRead();
}
/**
* @inheritDoc
*/
public function getBundlingKey() {
return $this->getBundleHash();
}
/**
* @inheritDoc
*/
public function setBundledElements( $bundleables ) {
$this->setBundledNotifications( $bundleables );
}
/**
* @inheritDoc
*/
public function getSortingKey() {
return ( $this->isRead() ? '0' : '1' ) . '_' . $this->getTimestamp();
}
}
|