-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathFilesIntegrationController.php
More file actions
228 lines (209 loc) · 8.22 KB
/
Copy pathFilesIntegrationController.php
File metadata and controls
228 lines (209 loc) · 8.22 KB
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Controller;
use OCA\Talk\Config;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Files\Util;
use OCA\Talk\Manager;
use OCA\Talk\Room;
use OCA\Talk\Service\RoomService;
use OCA\Talk\TalkSession;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\Attribute\UseSession;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Files\FileInfo;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as IShareManager;
class FilesIntegrationController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private readonly Manager $manager,
private readonly RoomService $roomService,
private readonly IShareManager $shareManager,
private readonly ISession $session,
private readonly IUserSession $userSession,
private readonly TalkSession $talkSession,
private readonly Util $util,
private readonly IConfig $config,
private readonly IAppConfig $appConfig,
private readonly IL10N $l,
) {
parent::__construct($appName, $request);
}
/**
* Get the token of the room associated to the given file id
*
* This is the counterpart of self::getRoomByShareToken() for file ids
* instead of share tokens, although both return the same room token if the
* given file id and share token refer to the same file.
*
* If there is no room associated to the given file id a new room is
* created; the new room is a public room associated with a "file" object
* with the given file id. Unlike normal rooms in which the owner is the
* user that created the room these are special rooms without owner
* (although self joined users with direct access to the file become
* persistent participants automatically when they join until they
* explicitly leave or no longer have access to the file).
*
* In any case, to create or even get the token of the room, the file must
* be shared and the user must be the owner of a public share of the file
* (like a link share, for example) or have direct access to that file; an
* error is returned otherwise. A user has direct access to a file if they
* have access to it (or to an ancestor) through a user, group, circle or
* room share (but not through a link share, for example), or if they are the
* owner of such a file.
*
* @param string $fileId ID of the file
* @return DataResponse<Http::STATUS_OK, array{token: string}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, null, array{}>
* @throws OCSNotFoundException Share not found
*
* 200: Room token returned
* 400: Rooms not allowed for shares
*/
#[NoAdminRequired]
#[ApiRoute(verb: 'GET', url: '/api/{apiVersion}/file/{fileId}', requirements: [
'apiVersion' => '(v1)',
'fileId' => '.+',
])]
public function getRoomByFileId(string $fileId): DataResponse {
if (!$this->appConfig->getAppValueBool(Config::CONVERSATIONS_FILES)) {
return new DataResponse(null, Http::STATUS_BAD_REQUEST);
}
$currentUser = $this->userSession->getUser();
if (!$currentUser instanceof IUser) {
throw new OCSException($this->l->t('File is not shared, or shared but not with the user'), Http::STATUS_UNAUTHORIZED);
}
$node = $this->util->getAnyNodeOfFileAccessibleByUser($fileId, $currentUser->getUID());
if ($node === null) {
throw new OCSNotFoundException($this->l->t('File is not shared, or shared but not with the user'));
}
$users = $this->util->getUsersWithAccessFile($fileId, $currentUser->getUID());
if (count($users) <= 1 && !$this->util->canGuestsAccessFile($fileId)) {
throw new OCSNotFoundException($this->l->t('File is not shared, or shared but not with the user'));
}
try {
$room = $this->manager->getRoomByObject('file', $fileId);
} catch (RoomNotFoundException) {
$name = $node->getName();
$name = $this->roomService->prepareConversationName($name);
$room = $this->roomService->createConversation(
Room::TYPE_PUBLIC,
$name,
null,
Room::OBJECT_TYPE_FILE,
$fileId,
);
}
return new DataResponse([
'token' => $room->getToken()
]);
}
/**
* Returns the token of the room associated to the file of the given
* share token
*
* This is the counterpart of self::getRoomByFileId() for share tokens
* instead of file ids, although both return the same room token if the
* given file id and share token refer to the same file.
*
* If there is no room associated to the file id of the given share token a
* new room is created; the new room is a public room associated with a
* "file" object with the file id of the given share token. Unlike normal
* rooms in which the owner is the user that created the room these are
* special rooms without owner (although self joined users with direct
* access to the file become persistent participants automatically when they
* join until they explicitly leave or no longer have access to the file).
*
* In any case, to create or even get the token of the room, the file must
* be publicly shared (like a link share, for example); an error is returned
* otherwise.
*
* Besides the token of the room this also returns the current user ID and
* display name, if any; this is needed by the Talk sidebar to know the
* actual current user, as the public share page uses the incognito mode and
* thus logged-in users as seen as guests.
*
* @param string $shareToken Token of the file share
* @return DataResponse<Http::STATUS_OK, array{token: string, userId: string, userDisplayName: string}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Room token and user info returned
* 400: Rooms not allowed for shares
* 404: Share not found
*/
#[PublicPage]
#[UseSession]
#[BruteForceProtection(action: 'shareinfo')]
#[ApiRoute(verb: 'GET', url: '/api/{apiVersion}/publicshare/{shareToken}', requirements: [
'apiVersion' => '(v1)',
'shareToken' => '.+',
])]
public function getRoomByShareToken(string $shareToken): DataResponse {
if (!$this->appConfig->getAppValueBool(Config::CONVERSATIONS_FILES)
|| !$this->appConfig->getAppValueBool(Config::CONVERSATIONS_FILES_PUBLIC_SHARES)) {
return new DataResponse(null, Http::STATUS_BAD_REQUEST);
}
try {
$share = $this->shareManager->getShareByToken($shareToken);
if ($share->getPassword() !== null) {
$shareId = $this->session->get('public_link_authenticated');
if ($share->getId() !== $shareId) {
throw new ShareNotFound();
}
}
} catch (ShareNotFound) {
$response = new DataResponse(null, Http::STATUS_NOT_FOUND);
$response->throttle(['token' => $shareToken, 'action' => 'shareinfo']);
return $response;
}
try {
if ($share->getNodeType() !== FileInfo::TYPE_FILE) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}
$fileId = (string)$share->getNodeId();
try {
$room = $this->manager->getRoomByObject('file', $fileId);
} catch (RoomNotFoundException) {
$name = $share->getNode()->getName();
$name = $this->roomService->prepareConversationName($name);
$room = $this->roomService->createConversation(
Room::TYPE_PUBLIC,
$name,
null,
Room::OBJECT_TYPE_FILE,
$fileId,
);
}
} catch (NotFoundException) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}
$this->talkSession->setFileShareTokenForRoom($room->getToken(), $shareToken);
$currentUser = $this->userSession->getUser();
$currentUserId = $currentUser instanceof IUser ? $currentUser->getUID() : '';
$currentUserDisplayName = $currentUser instanceof IUser ? $currentUser->getDisplayName() : '';
return new DataResponse([
'token' => $room->getToken(),
'userId' => $currentUserId,
'userDisplayName' => $currentUserDisplayName,
]);
}
}