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
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 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\Events\AddParticipantsEvent;
use OCA\Talk\Events\JoinRoomUserEvent;
use OCA\Talk\Events\RoomEvent;
use OCA\Talk\Room;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Notification\IManager;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserSession;
class Listener {
/** @var IManager */
protected $notificationManager;
/** @var IUserSession */
protected $userSession;
/** @var ITimeFactory */
protected $timeFactory;
/** @var ILogger */
protected $logger;
public function __construct(IManager $notificationManager,
IUserSession $userSession,
ITimeFactory $timeFactory,
ILogger $logger) {
$this->notificationManager = $notificationManager;
$this->userSession = $userSession;
$this->timeFactory = $timeFactory;
$this->logger = $logger;
}
public static function register(IEventDispatcher $dispatcher): void {
$listener = static function(AddParticipantsEvent $event) {
$room = $event->getRoom();
if ($room->getObjectType() === 'file') {
return;
}
/** @var self $listener */
$listener = \OC::$server->query(self::class);
$listener->generateInvitation($room, $event->getParticipants());
};
$dispatcher->addListener(Room::EVENT_AFTER_USERS_ADD, $listener);
$listener = static function(JoinRoomUserEvent $event) {
/** @var self $listener */
$listener = \OC::$server->query(self::class);
$listener->markInvitationRead($event->getRoom());
};
$dispatcher->addListener(Room::EVENT_AFTER_ROOM_CONNECT, $listener);
$listener = static function(RoomEvent $event) {
/** @var self $listener */
$listener = \OC::$server->query(self::class);
$listener->generateCallNotifications($event->getRoom());
};
$dispatcher->addListener(Room::EVENT_BEFORE_SESSION_JOIN_CALL, $listener);
$listener = static function(RoomEvent $event) {
/** @var self $listener */
$listener = \OC::$server->query(self::class);
$listener->markCallNotificationsRead($event->getRoom());
};
$dispatcher->addListener(Room::EVENT_AFTER_SESSION_JOIN_CALL, $listener);
}
/**
* Room invitation: "{actor} invited you to {call}"
*
* @param Room $room
* @param array[] $participants
*/
public function generateInvitation(Room $room, array $participants): void {
$actor = $this->userSession->getUser();
if (!$actor instanceof IUser) {
return;
}
$actorId = $actor->getUID();
$notification = $this->notificationManager->createNotification();
$dateTime = $this->timeFactory->getDateTime();
try {
$notification->setApp('spreed')
->setDateTime($dateTime)
->setObject('room', $room->getToken())
->setSubject('invitation', [
'actorId' => $actor->getUID(),
]);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e, ['app' => 'spreed']);
return;
}
foreach ($participants as $participant) {
if ($actorId === $participant['userId']) {
// No activity for self-joining and the creator
continue;
}
try {
$notification->setUser($participant['userId']);
$this->notificationManager->notify($notification);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e, ['app' => 'spreed']);
}
}
}
/**
* Room invitation: "{actor} invited you to {call}"
*
* @param Room $room
*/
public function markInvitationRead(Room $room): void {
$currentUser = $this->userSession->getUser();
if (!$currentUser instanceof IUser) {
return;
}
$notification = $this->notificationManager->createNotification();
try {
$notification->setApp('spreed')
->setUser($currentUser->getUID())
->setObject('room', $room->getToken())
->setSubject('invitation');
$this->notificationManager->markProcessed($notification);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e, ['app' => 'spreed']);
return;
}
}
/**
* Call notification: "{user} wants to talk with you"
*
* @param Room $room
*/
public function generateCallNotifications(Room $room): void {
if ($room->getActiveSince() instanceof \DateTime) {
// Call already active => No new notifications
return;
}
if ($room->getObjectType() === 'file') {
return;
}
$actor = $this->userSession->getUser();
$actorId = $actor instanceof IUser ? $actor->getUID() :'';
$notification = $this->notificationManager->createNotification();
$dateTime = $this->timeFactory->getDateTime();
try {
// Remove all old notifications for this room
$notification->setApp('spreed')
->setObject('room', $room->getToken());
$this->notificationManager->markProcessed($notification);
$notification->setObject('call', $room->getToken());
$this->notificationManager->markProcessed($notification);
$notification->setSubject('call', [
'callee' => $actorId,
])
->setDateTime($dateTime);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e, ['app' => 'spreed']);
return;
}
$userIds = $room->getNotInCallUserIds();
foreach ($userIds as $userId) {
if ($actorId === $userId) {
continue;
}
try {
$notification->setUser($userId);
$this->notificationManager->notify($notification);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e, ['app' => 'spreed']);
}
}
}
/**
* Call notification: "{user} wants to talk with you"
*
* @param Room $room
*/
public function markCallNotificationsRead(Room $room): void {
$currentUser = $this->userSession->getUser();
if (!$currentUser instanceof IUser) {
return;
}
$notification = $this->notificationManager->createNotification();
try {
$notification->setApp('spreed')
->setUser($currentUser->getUID())
->setObject('call', $room->getToken())
->setSubject('call');
$this->notificationManager->markProcessed($notification);
} catch (\InvalidArgumentException $e) {
$this->logger->logException($e, ['app' => 'spreed']);
return;
}
}
}
|