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
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Talk\Notification;
use OCA\Talk\AppInfo\Application;
use OCA\Talk\Controller\ChatController;
use OCA\Talk\Events\AParticipantModifiedEvent;
use OCA\Talk\Events\AttendeesAddedEvent;
use OCA\Talk\Events\BeforeParticipantModifiedEvent;
use OCA\Talk\Events\CallNotificationSendEvent;
use OCA\Talk\Events\ParticipantModifiedEvent;
use OCA\Talk\Events\UserJoinedRoomEvent;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Notification\IManager;
use Psr\Log\LoggerInterface;
/**
* @template-implements IEventListener<Event>
*/
class Listener implements IEventListener {
protected bool $shouldSendCallNotification = false;
public function __construct(
protected IDBConnection $connection,
protected IManager $notificationManager,
protected ParticipantService $participantsService,
protected IEventDispatcher $dispatcher,
protected IUserSession $userSession,
protected ITimeFactory $timeFactory,
protected LoggerInterface $logger,
) {
}
public function handle(Event $event): void {
match (get_class($event)) {
CallNotificationSendEvent::class => $this->sendCallNotification($event->getRoom(), $event->getActor()->getAttendee(), $event->getTarget()->getAttendee()),
AttendeesAddedEvent::class => $this->generateInvitation($event->getRoom(), $event->getAttendees()),
UserJoinedRoomEvent::class => $this->handleUserJoinedRoomEvent($event),
BeforeParticipantModifiedEvent::class => $this->checkCallNotifications($event),
ParticipantModifiedEvent::class => $this->afterParticipantJoinedCall($event),
};
}
/**
* Room invitation: "{actor} invited you to {call}"
*
* @param Room $room
* @param Attendee[] $attendees
*/
protected function generateInvitation(Room $room, array $attendees): void {
if ($room->getObjectType() === Room::OBJECT_TYPE_FILE) {
return;
}
$actor = $this->userSession->getUser();
if (!$actor instanceof IUser) {
return;
}
$actorId = $actor->getUID();
$notification = $this->notificationManager->createNotification();
$shouldFlush = $this->notificationManager->defer();
$dateTime = $this->timeFactory->getDateTime();
try {
$notification->setApp(Application::APP_ID)
->setDateTime($dateTime)
->setObject('room', $room->getToken())
->setSubject('invitation', [
'actorId' => $actor->getUID(),
]);
} catch (\InvalidArgumentException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
if ($shouldFlush) {
$this->notificationManager->flush();
}
return;
}
foreach ($attendees as $attendee) {
if ($attendee->getActorType() !== Attendee::ACTOR_USERS) {
// No user => no activity
continue;
}
if ($actorId === $attendee->getActorId()) {
// No activity for self-joining and the creator
continue;
}
try {
$notification->setUser($attendee->getActorId());
$this->notificationManager->notify($notification);
} catch (\InvalidArgumentException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
if ($shouldFlush) {
$this->notificationManager->flush();
}
}
protected function handleUserJoinedRoomEvent(UserJoinedRoomEvent $event): void {
$this->markInvitationRead($event->getRoom(), $event->getUser());
$this->markReactionNotificationsRead($event->getRoom(), $event->getUser());
}
/**
* Room invitation: "{actor} invited you to {call}"
*/
protected function markInvitationRead(Room $room, IUser $user): void {
$notification = $this->notificationManager->createNotification();
try {
$notification->setApp(Application::APP_ID)
->setUser($user->getUID())
->setObject('room', $room->getToken())
->setSubject('invitation');
$this->notificationManager->markProcessed($notification);
} catch (\InvalidArgumentException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return;
}
}
/**
* Reaction: "{user} reacted with {reaction} in {call}"
*
* We should not mark reactions read based on the read-status of the comment
* they apply to, but the point in time when the reaction as done.
* However, these messages are not visible and don't update the read marker,
* so we purge them on joining the conversation.
* This already happened before on the initial loading of a chat with
* {@see ChatController::getMessageContext()}, but not on follow-up visits
* (when the room history was not empty in the browser storage) this does
* not trigger, so it was possible to end a session with all messages read,
* but still having notifications about reactions.
* For normal chat messages this happens in {@see Notifier::parseChatMessage()}
*/
protected function markReactionNotificationsRead(Room $room, IUser $user): void {
$notification = $this->notificationManager->createNotification();
try {
$notification->setApp(Application::APP_ID)
->setUser($user->getUID())
->setObject('chat', $room->getToken())
->setSubject('reaction');
$this->notificationManager->markProcessed($notification);
} catch (\InvalidArgumentException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return
|