-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathListener.php
196 lines (176 loc) · 5.87 KB
/
Listener.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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Activity;
use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Events\ACallEndedEvent;
use OCA\Talk\Events\ARoomEvent;
use OCA\Talk\Events\AttendeesAddedEvent;
use OCA\Talk\Events\CallEndedEvent;
use OCA\Talk\Events\CallEndedForEveryoneEvent;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\RecordingService;
use OCA\Talk\Service\RoomService;
use OCP\Activity\Exceptions\InvalidValueException;
use OCP\Activity\IManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IUser;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
/**
* @template-implements IEventListener<Event>
*/
class Listener implements IEventListener {
public function __construct(
protected IManager $activityManager,
protected IUserSession $userSession,
protected ChatManager $chatManager,
protected ParticipantService $participantService,
protected RoomService $roomService,
protected RecordingService $recordingService,
protected LoggerInterface $logger,
protected ITimeFactory $timeFactory,
) {
}
public function handle(Event $event): void {
if ($event instanceof ARoomEvent && $event->getRoom()->isFederatedConversation()) {
return;
}
match (get_class($event)) {
CallEndedEvent::class,
CallEndedForEveryoneEvent::class => $this->generateCallActivity($event),
AttendeesAddedEvent::class => $this->generateInvitationActivity($event->getRoom(), $event->getAttendees()),
};
}
/**
* Call activity: "You attended a call with {user1} and {user2}"
*/
protected function generateCallActivity(ACallEndedEvent $event): void {
$room = $event->getRoom();
$actor = $event->getActor();
$activeSince = $event->getOldValue();
$duration = $this->timeFactory->getTime() - $activeSince->getTimestamp();
$userIds = $this->participantService->getParticipantUserIds($room, $activeSince);
$cloudIds = $this->participantService->getParticipantActorIdsByActorType($room, [Attendee::ACTOR_FEDERATED_USERS], $activeSince);
$numGuests = $this->participantService->getActorsCountByType($room, Attendee::ACTOR_GUESTS, $activeSince->getTimestamp());
$numGuests += $this->participantService->getActorsCountByType($room, Attendee::ACTOR_EMAILS, $activeSince->getTimestamp());
$message = 'call_ended';
if ($event instanceof CallEndedForEveryoneEvent) {
$message = 'call_ended_everyone';
} elseif (($room->getType() === Room::TYPE_ONE_TO_ONE || $room->getType() === Room::TYPE_ONE_TO_ONE_FORMER) && \count($userIds) === 1) {
$message = 'call_missed';
}
if ($actor instanceof Participant) {
$actorId = $actor->getAttendee()->getActorId();
$actorType = $actor->getAttendee()->getActorType();
} else {
$actorType = Attendee::ACTOR_GUESTS;
$actorId = Attendee::ACTOR_ID_SYSTEM;
}
$this->chatManager->addSystemMessage($room, $actorType, $actorId, json_encode([
'message' => $message,
'parameters' => [
'users' => $userIds,
'cloudIds' => $cloudIds,
'guests' => $numGuests,
'duration' => $duration,
],
]), $this->timeFactory->getDateTime(), false);
if (empty($userIds)) {
return;
}
$activity = $this->activityManager->generateEvent();
try {
$activity->setApp('spreed')
->setType('spreed')
->setAuthor('')
->setObject('room', $room->getId())
->setTimestamp($this->timeFactory->getTime())
->setSubject('call', [
'room' => $room->getId(),
'users' => $userIds,
'cloudIds' => $cloudIds,
'guests' => $numGuests,
'duration' => $duration,
]);
} catch (InvalidValueException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return;
}
foreach ($userIds as $userId) {
try {
$activity->setAffectedUser($userId);
$this->activityManager->publish($activity);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
}
/**
* Invitation activity: "{actor} invited you to {call}"
*
* @param Room $room
* @param Attendee[] $attendees
*/
protected function generateInvitationActivity(Room $room, array $attendees): void {
$actor = $this->userSession->getUser();
if (!$actor instanceof IUser) {
return;
}
$actorId = $actor->getUID();
$event = $this->activityManager->generateEvent();
try {
$event->setApp('spreed')
->setType('spreed')
->setAuthor($actorId)
->setObject('room', $room->getId())
->setTimestamp($this->timeFactory->getTime())
->setSubject('invitation', [
'user' => $actor->getUID(),
'room' => $room->getId(),
]);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return;
}
// We know the new participant is in the room,
// so skip loading them just to make sure they can read it.
// Must be overwritten later on for one-to-one chats.
$roomName = $room->getDisplayName($actorId);
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 {
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
// Overwrite the room name with the other participant
$roomName = $room->getDisplayName($attendee->getActorId());
}
$event
->setObject('room', $room->getId(), $roomName)
->setSubject('invitation', [
'user' => $actor->getUID(),
'room' => $room->getId(),
'name' => $roomName,
])
->setAffectedUser($attendee->getActorId());
$this->activityManager->publish($event);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
}
}