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
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Federation;
use OCA\Talk\AppInfo\Application;
use OCA\Talk\Exceptions\CannotReachRemoteException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Exceptions\UnauthorizedException;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\Invitation;
use OCA\Talk\Model\InvitationMapper;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\Federation\Exceptions\ProviderCouldNotAddShareException;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OCP\IUser;
use OCP\Notification\IManager;
use SensitiveParameter;
/**
* Class FederationManager
*
* @package OCA\Talk\Federation
*
* FederationManager handles incoming federated rooms
*/
class FederationManager {
public const OCM_RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND';
public const TALK_ROOM_RESOURCE = 'talk-room';
public const TALK_PROTOCOL_NAME = 'nctalk';
public const NOTIFICATION_SHARE_ACCEPTED = 'SHARE_ACCEPTED';
public const NOTIFICATION_SHARE_DECLINED = 'SHARE_DECLINED';
public const NOTIFICATION_SHARE_UNSHARED = 'SHARE_UNSHARED';
public const NOTIFICATION_ROOM_MODIFIED = 'ROOM_MODIFIED';
public const NOTIFICATION_MESSAGE_POSTED = 'MESSAGE_POSTED';
public const TOKEN_LENGTH = 64;
public function __construct(
private Manager $manager,
private ParticipantService $participantService,
private InvitationMapper $invitationMapper,
private BackendNotifier $backendNotifier,
private IManager $notificationManager,
private ICloudIdManager $cloudIdManager,
private RestrictionValidator $restrictionValidator,
) {
}
/**
* Check if $sharedBy is allowed to invite $shareWith
*
* @throws \InvalidArgumentException
*/
public function isAllowedToInvite(
IUser $user,
ICloudId $cloudIdToInvite,
): void {
$this->restrictionValidator->isAllowedToInvite($user, $cloudIdToInvite);
}
public function addRemoteRoom(
IUser $user,
int $remoteAttendeeId,
int $roomType,
string $roomName,
string $remoteToken,
string $remoteServerUrl,
#[SensitiveParameter]
string $sharedSecret,
string $inviterCloudId,
string $inviterDisplayName,
string $localCloudId,
): Invitation {
$couldHaveInviteWithOtherCasing = false;
try {
$room = $this->manager->getRoomByToken($remoteToken, null, $remoteServerUrl);
$couldHaveInviteWithOtherCasing = true;
} catch (RoomNotFoundException) {
$room = $this->manager->createRemoteRoom($roomType, $roomName, $remoteToken, $remoteServerUrl);
}
if ($couldHaveInviteWithOtherCasing) {
try {
$this->invitationMapper->getInvitationForUserByLocalRoom($room, $user->getUID(), true);
throw new ProviderCouldNotAddShareException('User already invited', '', Http::STATUS_BAD_REQUEST);
} catch (DoesNotExistException) {
// Not invited with any casing already, so all good.
}
}
$invitation = new Invitation();
$invitation->setUserId($user->getUID());
$invitation->setState(Invitation::STATE_PENDING);
$invitation->setLocalRoomId($room->getId());
$invitation->setLocalCloudId($localCloudId);
$invitation->setAccessToken($sharedSecret);
$invitation->setRemoteServerUrl($remoteServerUrl);
$invitation->setRemoteToken($remoteToken);
$invitation->setRemoteAttendeeId($remoteAttendeeId);
$invitation->setInviterCloudId($inviterCloudId);
$invitation->setInviterDisplayName($inviterDisplayName);
$this->invitationMapper->insert($invitation);
return $invitation;
}
protected function markNotificationProcessed(string $userId, int $shareId): void {
$notification = $this->notificationManager->createNotification();
$notification->setApp(Application::APP_ID)
->setUser($userId)
->setObject('remote_talk_share', (string) $shareId);
$this->notificationManager->markProcessed($notification);
}
/**
* @throws \InvalidArgumentException
* @throws CannotReachRemoteException
*/
public function acceptRemoteRoomShare(IUser $user, int $shareId): Participant {
try {
$invitation = $this->invitationMapper->getInvitationById($shareId);
} catch (DoesNotExistException $e) {
throw new \InvalidArgumentException('invitation');
}
if ($invitation->getUserId() !== $user->getUID()) {
throw new UnauthorizedException('user');
}
if ($invitation->getState() === Invitation::STATE_ACCEPTED) {
throw new \InvalidArgumentException('state');
}
$cloudId = $this->cloudIdManager->getCloudId($user->getUID(), null);
// Add user to the room
$room = $this->manager->getRoomById($invitation->getLocalRoomId());
if (
!$this->backendNotifier->sendShareAccepted($invitation->getRemoteServerUrl(), $invitation->getRemoteAttendeeId(), $invitation->getAccessToken(), $user->getDisplayName(), $cloudId->getId())
) {
throw new CannotReachRemoteException();
}
$participant = [
[
'actorType' => Attendee::ACTOR_USERS,
'actorId' => $user->getUID(),
'displayName' => $user->getDisplayName(),
'accessToken' => $invitation->getAccessToken(),
'remoteId' => $invitation->getRemoteAttendeeId(),
'invitedCloudId' => $invitation->getLocalCloudId(),
'lastReadMessage' => $room->getLastMessageId(),
]
];
$attendees = $this->participantService->addUsers($room, $participant, $user);
/** @var Attendee $attendee */
$attendee = array_pop($attendees);
$invitation->setState(Invitation::STATE_ACCEPTED);
$invitation->setLocalCloudId($cloudId->getId());
$this->invitationMapper->update($invitation);
$this->markNotificationProcessed($user->getUID(), $shareId);
return new Participant($room, $attendee, null);
}
/**
* @throws DoesNotExistException
*/
public function getRemoteShareById(int $shareId): Invitation {
return $this->invitationMapper->getInvitationById($shareId);
}
/**
* @throws \InvalidArgumentException
* @throws UnauthorizedException
*/
public function rejectRemoteRoomShare(IUser $user, int $shareId): void {
try {
$invitation = $this->invitationMapper->getInvitationById($shareId);
} catch (DoesNotExistException $e) {
throw new \InvalidArgumentException('invitation');
}
if ($invitation->getUserId() !== $user->getUID()) {
throw new UnauthorizedException('user');
}
if ($invitation->getState() !== Invitation::STATE_PENDING) {
|