-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathUserApiController.php
More file actions
72 lines (60 loc) · 2.07 KB
/
UserApiController.php
File metadata and controls
72 lines (60 loc) · 2.07 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Text\Controller;
use OCA\Text\Middleware\Attribute\RequireDocumentSession;
use OCA\Text\Service\SessionService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\Share\IShare;
class UserApiController extends ApiController implements ISessionAwareController {
use TSessionAwareController;
public function __construct(
string $appName,
IRequest $request,
private SessionService $sessionService,
private ISearch $collaboratorSearch,
private IUserManager $userManager,
) {
parent::__construct($appName, $request);
}
#[PublicPage]
#[NoAdminRequired]
#[RequireDocumentSession]
public function index(string $filter = '', int $limit = 5): DataResponse {
$limit = min($limit, 50);
$sessions = $this->sessionService->getAllSessions($this->getSession()->getDocumentId());
$users = [];
// Add joined users to the autocomplete list
foreach ($sessions as $session) {
$sessionUserId = $session['userId'];
if ($sessionUserId === null || isset($users[$sessionUserId])) {
continue;
}
$displayName = $this->userManager->getDisplayName($sessionUserId) ?? '';
if (stripos($displayName, $filter) !== false || stripos($sessionUserId, $filter) !== false) {
$users[$sessionUserId] = $displayName;
}
}
if (!$this->getSession()->isGuest()) {
// Add other users to the autocomplete list
[$result] = $this->collaboratorSearch->search($filter, [IShare::TYPE_USER], false, $limit, 0);
$userSearch = array_merge($result['users'], $result['exact']['users']);
foreach ($userSearch as ['label' => $label, 'value' => $value]) {
if (isset($value['shareWith'])) {
$id = $value['shareWith'];
$users[$id] = $label;
}
}
}
return new DataResponse($users);
}
}