-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathAbstractUserNotificationEvent.class.php
More file actions
321 lines (274 loc) · 7.14 KB
/
AbstractUserNotificationEvent.class.php
File metadata and controls
321 lines (274 loc) · 7.14 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace wcf\system\user\notification\event;
use wcf\data\DatabaseObjectDecorator;
use wcf\data\IMessage;
use wcf\data\language\Language;
use wcf\data\user\notification\event\UserNotificationEvent;
use wcf\data\user\notification\UserNotification;
use wcf\data\user\UserProfile;
use wcf\system\user\notification\object\IUserNotificationObject;
use wcf\system\WCF;
use wcf\util\DateUtil;
use wcf\util\StringUtil;
/**
* Provides a default implementation for user notification events.
*
* @author Joshua Ruesweg, Marcel Werk, Oliver Kliebisch
* @copyright 2001-2019 WoltLab GmbH, Oliver Kliebisch
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @mixin UserNotificationEvent
* @extends DatabaseObjectDecorator<UserNotificationEvent>
*/
abstract class AbstractUserNotificationEvent extends DatabaseObjectDecorator implements
IUserNotificationEvent,
IMessage
{
/**
* @inheritDoc
*/
protected static $baseClass = UserNotificationEvent::class;
/**
* author object
* @var UserProfile
*/
protected $author;
/**
* list of authors for stacked notifications
* @var UserProfile[]
*/
protected $authors = [];
/**
* notification stacking support
* @var bool
*/
protected $stackable = false;
/**
* user notification
* @var UserNotification
*/
protected $notification;
/**
* user notification object
* @var IUserNotificationObject
*/
protected $userNotificationObject;
/**
* additional data for this event
* @var mixed[]
*/
protected $additionalData = [];
/**
* language object
* @var Language
*/
protected $language;
/**
* list of point of times for each period's end
* @var string[]
*/
protected static $periods = [];
#[\Override]
public function setAuthors(array $authors)
{
$this->authors = $authors;
// Ensure that the original author is the first in the list.
\uasort($this->authors, function ($a, $b) {
if ($a->userID == $b->userID) {
return 0;
}
if ($a->userID == $this->getAuthorID()) {
return -1;
}
if ($b->userID == $this->getAuthorID()) {
return 1;
}
return 0;
});
}
#[\Override]
public function setObject(
UserNotification $notification,
IUserNotificationObject $object,
UserProfile $author,
array $additionalData = []
) {
$this->notification = $notification;
$this->userNotificationObject = $object;
$this->author = $author;
$this->additionalData = $additionalData;
}
#[\Override]
public function getAuthorID()
{
return $this->author->userID;
}
#[\Override]
public function getAuthor()
{
return $this->author;
}
#[\Override]
public function getAuthors()
{
return $this->authors;
}
#[\Override]
public function isVisible()
{
return $this->getDecoratedObject()->validateOptions() && $this->getDecoratedObject()->validatePermissions();
}
#[\Override]
public function getEmailTitle()
{
return $this->getTitle();
}
#[\Override]
public function getEmailMessage(string $notificationType = 'instant')
{
return $this->getMessage();
}
#[\Override]
public function getEventHash()
{
return \sha1($this->eventID . '-' . $this->userNotificationObject->getObjectID());
}
#[\Override]
public function setLanguage(Language $language)
{
$this->language = $language;
}
/**
* Returns the language of this event.
*
* @return Language
*/
public function getLanguage()
{
if ($this->language !== null) {
return $this->language;
}
return WCF::getLanguage();
}
#[\Override]
public function isStackable()
{
return $this->stackable;
}
/**
* Returns the readable period matching this notification.
*
* @return string
*/
public function getPeriod()
{
if (empty(self::$periods)) {
$date = DateUtil::getDateTimeByTimestamp(TIME_NOW);
$date->setTimezone(WCF::getUser()->getTimeZone());
$date->setTime(0, 0, 0);
self::$periods[$date->getTimestamp()] = WCF::getLanguage()->get('wcf.date.period.today');
// 1 day back
$date->modify('-1 day');
self::$periods[$date->getTimestamp()] = WCF::getLanguage()->get('wcf.date.period.yesterday');
$formatter = \IntlDateFormatter::create(
WCF::getLanguage()->getLocale(),
timezone: WCF::getUser()->getTimeZone(),
pattern: 'EEEE'
);
// 2-6 days back
for ($i = 0; $i < 6; $i++) {
$date->modify('-1 day');
self::$periods[$date->getTimestamp()] = $formatter->format($date);
}
}
foreach (self::$periods as $time => $period) {
if ($this->notification->time >= $time) {
return $period;
}
}
return WCF::getLanguage()->get('wcf.date.period.older');
}
#[\Override]
public function supportsEmailNotification()
{
return true;
}
#[\Override]
public function checkAccess()
{
return true;
}
#[\Override]
public function deleteNoAccessNotification()
{
return true;
}
#[\Override]
public function isConfirmed()
{
return $this->notification->confirmTime > 0;
}
#[\Override]
public function getNotification()
{
return $this->notification;
}
#[\Override]
public function getUserNotificationObject()
{
return $this->userNotificationObject;
}
/**
* @return int
* @deprecated 6.3 No longer in use.
*/
public function getComments()
{
return 0;
}
/**
* @return string[]
* @deprecated 6.3 No longer in use.
*/
public function getCategories()
{
return [
$this->notification->objectType,
];
}
#[\Override]
public function getExcerpt(int $maxLength = 255)
{
return StringUtil::truncateHTML($this->getFormattedMessage(), $maxLength);
}
#[\Override]
public function getFormattedMessage()
{
return $this->getMessage();
}
#[\Override]
public function __toString(): string
{
return $this->getFormattedMessage();
}
#[\Override]
public function getTime()
{
return $this->getNotification()->time;
}
#[\Override]
public function getUserID()
{
return $this->getAuthorID();
}
#[\Override]
public function getUsername()
{
return $this->getAuthor()->username;
}
#[\Override]
public function getEventName(): string
{
return WCF::getLanguage()->getDynamicVariable("wcf.user.notification.{$this->objectType}.{$this->eventName}");
}
}