Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UserApiController.php « Controller « lib - github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ef476995976f6c0d4547d000caeaa01e2ba8448 (plain)
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
<?php

namespace OCA\Text\Controller;

use OCA\Text\Service\SessionService;
use \OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\DataResponse;
use OCP\Collaboration\Collaborators\ISearch;
use \OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IShare;

class UserApiController extends ApiController {
	private ISearch $collaboratorSearch;
	private IUserSession $userSession;
	private IUserManager $userManager;
	private SessionService $sessionService;

	public function __construct($appName, IRequest $request, SessionService $sessionService, ISearch $ISearch, IUserManager $userManager, IUserSession $userSession) {
		parent::__construct($appName, $request);

		$this->sessionService = $sessionService;
		$this->collaboratorSearch = $ISearch;
		$this->userSession = $userSession;
		$this->userManager = $userManager;
	}

	/**
	 * @NoAdminRequired
	 * @PublicPage
	 */
	public function index(int $documentId, int $sessionId, string $sessionToken, string $filter, int $limit = 5): DataResponse {
		if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
			return new DataResponse([], 403);
		}

		$sessions = $this->sessionService->getAllSessions($documentId);

		$users = [];

		// Add joined users to the autocomplete list
		foreach ($sessions as $session) {
			$sessionUserId = $session['userId'];
			if ($sessionUserId !== null && !isset($users[$sessionUserId])) {
				$displayName = $this->userManager->getDisplayName($sessionUserId);
				if (stripos($displayName, $filter) !== false) {
					$users[$sessionUserId] = $displayName;
				}
			}
		}

		$currentSession = $this->sessionService->getSession($documentId, $sessionId, $sessionToken);
		if ($currentSession->getUserId() !== null) {
			// 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);
	}
}