-
Notifications
You must be signed in to change notification settings - Fork 438
/
Notifier.php
655 lines (574 loc) · 20.4 KB
/
Notifier.php
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Chat;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Files\Util;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\Session;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Webinary;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\IComment;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
/**
* Helper class for notifications related to user mentions in chat messages.
*
* This class uses the NotificationManager to create and remove the
* notifications as needed; OCA\Talk\Notification\Notifier is the one that
* prepares the notifications for display.
*/
class Notifier {
public function __construct(
private INotificationManager $notificationManager,
private IUserManager $userManager,
private IGroupManager $groupManager,
private ParticipantService $participantService,
private IConfig $config,
private ITimeFactory $timeFactory,
private Util $util,
) {
}
/**
* Notifies the user mentioned in the comment.
*
* The comment must be a chat message comment. That is, its "objectId" must
* be the room ID.
*
* Not every user mentioned in the message is notified, but only those that
* are able to participate in the room.
*
* @param array[] $alreadyNotifiedUsers
* @psalm-param array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}> $alreadyNotifiedUsers
* @return string[] Users that were mentioned
* @psalm-return array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}>
*/
public function notifyMentionedUsers(Room $chat, IComment $comment, array $alreadyNotifiedUsers, bool $silent): array {
$usersToNotify = $this->getUsersToNotify($chat, $comment, $alreadyNotifiedUsers);
if (!$usersToNotify) {
return $alreadyNotifiedUsers;
}
$shouldFlush = false;
if (!$silent) {
$notification = $this->createNotification($chat, $comment, 'mention');
$parameters = $notification->getSubjectParameters();
$shouldFlush = $this->notificationManager->defer();
}
foreach ($usersToNotify as $mentionedUser) {
if ($this->shouldMentionedUserBeNotified($mentionedUser['id'], $comment, $chat, $mentionedUser['attendee'] ?? null)) {
if (!$silent) {
$notification->setUser($mentionedUser['id']);
if (isset($mentionedUser['reason'])) {
$notification->setSubject('mention_' . $mentionedUser['reason'], array_merge($parameters, [
'sourceId' => $mentionedUser['sourceId'] ?? null,
]));
} else {
$notification->setSubject('mention', $parameters);
}
$this->notificationManager->notify($notification);
}
$alreadyNotifiedUsers[] = $mentionedUser;
}
}
if ($shouldFlush) {
$this->notificationManager->flush();
}
return $alreadyNotifiedUsers;
}
/**
* @param Room $chat
* @param IComment $comment
* @param array $alreadyNotifiedUsers
* @psalm-param array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}> $alreadyNotifiedUsers
* @return array
* @psalm-return array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}>
*/
public function getUsersToNotify(Room $chat, IComment $comment, array $alreadyNotifiedUsers): array {
$usersToNotify = $this->getMentionedUsers($comment);
$usersToNotify = $this->getMentionedGroupMembers($chat, $comment, $usersToNotify);
$usersToNotify = $this->addMentionAllToList($chat, $usersToNotify);
$usersToNotify = $this->removeAlreadyNotifiedUsers($usersToNotify, $alreadyNotifiedUsers);
return $usersToNotify;
}
/**
* @param array $usersToNotify
* @psalm-param array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}> $usersToNotify
* @param array $alreadyNotifiedUsers
* @psalm-param array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}> $alreadyNotifiedUsers
* @return array
* @psalm-return array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}>
*/
private function removeAlreadyNotifiedUsers(array $usersToNotify, array $alreadyNotifiedUsers): array {
return array_filter($usersToNotify, static function (array $userToNotify) use ($alreadyNotifiedUsers): bool {
foreach ($alreadyNotifiedUsers as $alreadyNotified) {
if ($alreadyNotified['id'] === $userToNotify['id'] && $alreadyNotified['type'] === $userToNotify['type']) {
return false;
}
}
return true;
});
}
/**
* @param Room $chat
* @param array $list
* @psalm-param array<int, array{id: string, type: string, reason: string, sourceId?: string}> $list
* @return array
* @psalm-return array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}>
*/
private function addMentionAllToList(Room $chat, array $list): array {
$usersToNotify = array_filter($list, static function (array $entry): bool {
return $entry['type'] !== Attendee::ACTOR_USERS || $entry['id'] !== 'all';
});
if (count($list) === count($usersToNotify)) {
return $usersToNotify;
}
$attendees = $this->participantService->getActorsByType($chat, Attendee::ACTOR_USERS);
foreach ($attendees as $attendee) {
$alreadyAddedToNotify = array_filter($list, static function ($user) use ($attendee): bool {
return $user['id'] === $attendee->getActorId();
});
if (!empty($alreadyAddedToNotify)) {
continue;
}
$usersToNotify[] = [
'id' => $attendee->getActorId(),
'type' => $attendee->getActorType(),
'attendee' => $attendee,
'reason' => 'all',
];
}
return $usersToNotify;
}
/**
* Notifies the author that wrote the comment which was replied to
*
* The comment must be a chat message comment. That is, its "objectId" must
* be the room ID.
*
* The author of the message is notified only if he is still able to participate in the room
*
* @param Room $chat
* @param IComment $comment
* @param IComment $replyTo
* @param bool $silent
* @return array[] Actor that was replied to
* @psalm-return array<int, array{id: string, type: string, reason: string}>
*/
public function notifyReplyToAuthor(Room $chat, IComment $comment, IComment $replyTo, bool $silent): array {
if ($replyTo->getActorType() !== Attendee::ACTOR_USERS && $replyTo->getActorType() !== Attendee::ACTOR_FEDERATED_USERS) {
// No reply notification when the replyTo-author was not a user or federated user
return [];
}
if ($replyTo->getActorType() === Attendee::ACTOR_FEDERATED_USERS) {
return [
[
'id' => $replyTo->getActorId(),
'type' => $replyTo->getActorType(),
'reason' => 'reply',
],
];
}
if (!$this->shouldMentionedUserBeNotified($replyTo->getActorId(), $comment, $chat)) {
return [];
}
if (!$silent) {
$notification = $this->createNotification($chat, $comment, 'reply');
$notification->setUser($replyTo->getActorId());
$this->notificationManager->notify($notification);
}
return [
[
'id' => $replyTo->getActorId(),
'type' => $replyTo->getActorType(),
'reason' => 'reply',
],
];
}
/**
* Notifies the user mentioned in the comment.
*
* The comment must be a chat message comment. That is, its "objectId" must
* be the room ID.
*
* Not every user mentioned in the message is notified, but only those that
* are able to participate in the room.
*
* @param Room $chat
* @param IComment $comment
* @param array[] $alreadyNotifiedUsers
* @param bool $silent
* @psalm-param array<int, array{id: string, type: string, reason: string, sourceId?: string, attendee?: Attendee}> $alreadyNotifiedUsers
*/
public function notifyOtherParticipant(Room $chat, IComment $comment, array $alreadyNotifiedUsers, bool $silent): void {
if ($silent) {
return;
}
$participants = $this->participantService->getParticipantsByNotificationLevel($chat, Participant::NOTIFY_ALWAYS);
$notification = $this->createNotification($chat, $comment, 'chat');
foreach ($participants as $participant) {
if (!$this->shouldParticipantBeNotified($participant, $comment, $alreadyNotifiedUsers)) {
continue;
}
$notification->setUser($participant->getAttendee()->getActorId());
$this->notificationManager->notify($notification);
}
// Also notify default participants in one-to-one chats or when the admin default is "always"
if ($this->getDefaultGroupNotification() === Participant::NOTIFY_ALWAYS || $chat->getType() === Room::TYPE_ONE_TO_ONE) {
$participants = $this->participantService->getParticipantsByNotificationLevel($chat, Participant::NOTIFY_DEFAULT);
foreach ($participants as $participant) {
if (!$this->shouldParticipantBeNotified($participant, $comment, $alreadyNotifiedUsers)) {
continue;
}
$notification->setUser($participant->getAttendee()->getActorId());
$this->notificationManager->notify($notification);
}
}
}
public function notifyReacted(Room $chat, IComment $comment, IComment $reaction): void {
if ($comment->getActorType() !== Attendee::ACTOR_USERS) {
return;
}
if ($comment->getActorType() === $reaction->getActorType() && $comment->getActorId() === $reaction->getActorId()) {
return;
}
try {
$participant = $this->participantService->getParticipant($chat, $comment->getActorId(), false);
} catch (ParticipantNotFoundException $e) {
return;
}
$notificationLevel = $participant->getAttendee()->getNotificationLevel();
if ($notificationLevel === Participant::NOTIFY_DEFAULT) {
if ($chat->getType() === Room::TYPE_ONE_TO_ONE) {
$notificationLevel = Participant::NOTIFY_ALWAYS;
} else {
$notificationLevel = $this->getDefaultGroupNotification();
}
}
if ($notificationLevel === Participant::NOTIFY_ALWAYS) {
$notification = $this->createNotification($chat, $comment, 'reaction', [
'reaction' => $reaction->getMessage(),
], $reaction);
$notification->setUser($comment->getActorId());
$this->notificationManager->notify($notification);
}
}
/**
* Removes all the pending notifications for the room with the given ID.
*/
public function removePendingNotificationsForRoom(Room $chat, bool $chatOnly = false): void {
$notification = $this->notificationManager->createNotification();
$shouldFlush = $this->notificationManager->defer();
// @todo this should be in the Notifications\Hooks
$notification->setApp('spreed');
$objectTypes = [
'chat',
'reminder',
];
if (!$chatOnly) {
$objectTypes = [
'call',
'chat',
'room',
'recording',
'recording_information',
'remote_talk_share',
];
}
foreach ($objectTypes as $type) {
$notification->setObject($type, $chat->getToken());
$this->notificationManager->markProcessed($notification);
}
if ($shouldFlush) {
$this->notificationManager->flush();
}
}
/**
* Removes all the pending mention notifications for the room
*
* @param Room $chat
* @param ?string $userId
*/
public function markMentionNotificationsRead(Room $chat, ?string $userId): void {
if ($userId === null || $userId === '') {
return;
}
$shouldFlush = $this->notificationManager->defer();
$notification = $this->notificationManager->createNotification();
$notification
->setApp('spreed')
->setObject('chat', $chat->getToken())
->setUser($userId);
$this->notificationManager->markProcessed($notification);
if ($shouldFlush) {
$this->notificationManager->flush();
}
}
/**
* Remove all mention notifications of users that got their mention removed
*
* @param list<string> $userIds
*/
public function removeMentionNotificationAfterEdit(Room $chat, IComment $comment, array $userIds): void {
$shouldFlush = $this->notificationManager->defer();
$notification = $this->notificationManager->createNotification();
$notification
->setApp('spreed')
->setObject('chat', $chat->getToken())
// FIXME message_parameters are not handled by notification app, so this removes all notifications :(
->setMessage('comment', [
'commentId' => $comment->getId(),
]);
foreach (['mention_all', 'mention_direct'] as $subject) {
$notification->setSubject($subject);
foreach ($userIds as $userId) {
$notification->setUser($userId);
$this->notificationManager->markProcessed($notification);
}
}
if ($shouldFlush) {
$this->notificationManager->flush();
}
}
/**
* Returns the IDs of the users mentioned in the given comment.
*
* @param IComment $comment
* @return string[] the mentioned user IDs
*/
public function getMentionedUserIds(IComment $comment): array {
$mentionedUsers = $this->getMentionedUsers($comment);
return array_map(static function ($mentionedUser) {
return $mentionedUser['id'];
}, $mentionedUsers);
}
/**
* Returns the cloud IDs of the federated users mentioned in the given comment.
*
* @param IComment $comment
* @return string[] the mentioned cloud IDs
*/
public function getMentionedCloudIds(IComment $comment): array {
$mentionedFederatedUsers = $this->getMentionedFederatedUsers($comment);
return array_map(static function ($mentionedUser) {
return $mentionedUser['id'];
}, $mentionedFederatedUsers);
}
/**
* @param IComment $comment
* @return array[]
* @psalm-return array<int, array{type: string, id: string, reason: string}>
*/
private function getMentionedUsers(IComment $comment): array {
$mentions = $comment->getMentions();
if (empty($mentions)) {
return [];
}
$mentionedUsers = [];
foreach ($mentions as $mention) {
if ($mention['type'] !== 'user') {
continue;
}
$mentionedUsers[] = [
'id' => $mention['id'],
'type' => Attendee::ACTOR_USERS,
'reason' => 'direct',
];
}
return $mentionedUsers;
}
/**
* @param IComment $comment
* @return array[]
* @psalm-return array<int, array{type: string, id: string, reason: string}>
*/
private function getMentionedFederatedUsers(IComment $comment): array {
$mentions = $comment->getMentions();
if (empty($mentions)) {
return [];
}
$mentionedUsers = [];
foreach ($mentions as $mention) {
if ($mention['type'] !== 'federated_user') {
continue;
}
$mentionedUsers[] = [
'id' => $mention['id'],
'type' => Attendee::ACTOR_FEDERATED_USERS,
'reason' => 'direct',
];
}
return $mentionedUsers;
}
/**
* @param Room $chat
* @param IComment $comment
* @param array $list
* @psalm-param array<int, array{id: string, type: string, reason: string}> $list
* @return array[]
* @psalm-return array<int, array{type: string, id: string, reason: string, sourceId?: string}>
*/
private function getMentionedGroupMembers(Room $chat, IComment $comment, array $list): array {
$mentions = $comment->getMentions();
if (empty($mentions)) {
return [];
}
$alreadyMentionedUserIds = array_filter(
array_map(static fn (array $entry) => $entry['type'] === Attendee::ACTOR_USERS ? $entry['id'] : null, $list),
static fn ($userId) => $userId !== null
);
$alreadyMentionedUserIds = array_flip($alreadyMentionedUserIds);
foreach ($mentions as $mention) {
if ($mention['type'] !== 'group') {
continue;
}
$group = $this->groupManager->get($mention['id']);
if (!$group instanceof IGroup) {
continue;
}
try {
$this->participantService->getParticipantByActor($chat, Attendee::ACTOR_GROUPS, $group->getGID());
} catch (ParticipantNotFoundException $e) {
continue;
}
$members = $group->getUsers();
foreach ($members as $member) {
if (isset($alreadyMentionedUserIds[$member->getUID()])) {
continue;
}
$list[] = [
'id' => $member->getUID(),
'type' => Attendee::ACTOR_USERS,
'reason' => 'group',
'sourceId' => $group->getGID(),
];
$alreadyMentionedUserIds[$member->getUID()] = true;
}
}
return $list;
}
/**
* Creates a notification for the given chat message comment and mentioned
* user ID.
*/
private function createNotification(Room $chat, IComment $comment, string $subject, array $subjectData = [], ?IComment $reaction = null): INotification {
$subjectData['userType'] = $reaction ? $reaction->getActorType() : $comment->getActorType();
$subjectData['userId'] = $reaction ? $reaction->getActorId() : $comment->getActorId();
$notification = $this->notificationManager->createNotification();
$notification
->setApp('spreed')
->setObject('chat', $chat->getToken())
->setSubject($subject, $subjectData)
->setMessage($comment->getVerb(), [
'commentId' => $comment->getId(),
])
->setDateTime($reaction ? $reaction->getCreationDateTime() : $comment->getCreationDateTime());
return $notification;
}
protected function getDefaultGroupNotification(): int {
return (int) $this->config->getAppValue('spreed', 'default_group_notification', (string) Participant::NOTIFY_MENTION);
}
/**
* Determines whether a user should be notified about the mention:
*
* 1. The user did not mention themself
* 2. The user must exist
* 3. The user must be a participant of the room
* 4. The user must not be active in the room
*
* @param string $userId
* @param IComment $comment
* @param Room $room
* @param Attendee|null $attendee
* @return bool
*/
protected function shouldMentionedUserBeNotified(string $userId, IComment $comment, Room $room, ?Attendee $attendee = null): bool {
if ($comment->getActorType() === Attendee::ACTOR_USERS && $userId === $comment->getActorId()) {
// Do not notify the user if they mentioned themselves
return false;
}
try {
if (!$attendee instanceof Attendee) {
if (!$this->userManager->userExists($userId)) {
return false;
}
$participant = $this->participantService->getParticipant($room, $userId, false);
$attendee = $participant->getAttendee();
} else {
$participant = new Participant($room, $attendee, null);
}
if ($room->getLobbyState() !== Webinary::LOBBY_NONE &&
!($participant->getPermissions() & Attendee::PERMISSIONS_LOBBY_IGNORE)) {
return false;
}
$notificationLevel = $attendee->getNotificationLevel();
if ($notificationLevel === Participant::NOTIFY_DEFAULT) {
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
$notificationLevel = Participant::NOTIFY_ALWAYS;
} else {
$notificationLevel = $this->getDefaultGroupNotification();
}
}
return $notificationLevel !== Participant::NOTIFY_NEVER;
} catch (ParticipantNotFoundException $e) {
if ($room->getObjectType() === 'file' && $this->util->canUserAccessFile($room->getObjectId(), $userId)) {
// Users are added on mentions in file-rooms,
// so they can see the room in their room list and
// the notification can be parsed and links to an existing room,
// where they are a participant of.
$userDisplayName = $this->userManager->getDisplayName($userId);
$this->participantService->addUsers($room, [[
'actorType' => Attendee::ACTOR_USERS,
'actorId' => $userId,
'displayName' => $userDisplayName ?? $userId,
]]);
return true;
}
return false;
}
}
/**
* Determines whether a participant should be notified about the message:
*
* 1. The participant is not a guest
* 2. The participant is not the writing user
* 3. The participant was not mentioned already
* 4. The participant must not be active in the room
*
* @param Participant $participant
* @param IComment $comment
* @param array $alreadyNotifiedUsers
* @psalm-param array<int, array{type: string, id: string, reason: string, sourceId?: string, attendee?: Attendee}> $alreadyNotifiedUsers
* @return bool
*/
protected function shouldParticipantBeNotified(Participant $participant, IComment $comment, array $alreadyNotifiedUsers): bool {
if ($participant->getAttendee()->getActorType() !== Attendee::ACTOR_USERS) {
return false;
}
$userId = $participant->getAttendee()->getActorId();
if ($comment->getActorType() === Attendee::ACTOR_USERS && $userId === $comment->getActorId()) {
// Do not notify the author
return false;
}
$actorType = $participant->getAttendee()->getActorType();
foreach ($alreadyNotifiedUsers as $user) {
if ($user['id'] === $userId && $user['type'] === $actorType) {
return false;
}
}
if ($participant->getSession() instanceof Session) {
// User is online
return $participant->getSession()->getLastPing() < $this->timeFactory->getTime() - Session::SESSION_TIMEOUT;
}
return true;
}
}