1<?php
2
3declare(strict_types=1);
4/**
5 * @copyright 2021 Joas Schilling <coding@schilljs.com>
6 *
7 * @author Joas Schilling <coding@schilljs.com>
8 *
9 * @license GNU AGPL version 3 or any later version
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as
13 * published by the Free Software Foundation, either version 3 of the
14 * License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25namespace OC\Core\Controller;
26
27use OC\Contacts\ContactsMenu\Manager;
28use OCP\AppFramework\Http;
29use OCP\AppFramework\Http\DataResponse;
30use OCP\Contacts\ContactsMenu\IEntry;
31use OCP\IRequest;
32use OCP\IUserSession;
33use OCP\Share\IShare;
34
35class HoverCardController extends \OCP\AppFramework\OCSController {
36
37	/** @var Manager */
38	private $manager;
39
40	/** @var IUserSession */
41	private $userSession;
42
43	/**
44	 * @param IRequest $request
45	 * @param IUserSession $userSession
46	 * @param Manager $manager
47	 */
48	public function __construct(IRequest $request, IUserSession $userSession, Manager $manager) {
49		parent::__construct('core', $request);
50		$this->userSession = $userSession;
51		$this->manager = $manager;
52	}
53
54	/**
55	 * @NoAdminRequired
56	 *
57	 * @param string $userId
58	 * @return DataResponse
59	 */
60	public function getUser(string $userId): DataResponse {
61		$contact = $this->manager->findOne($this->userSession->getUser(), IShare::TYPE_USER, $userId);
62
63		if (!$contact) {
64			return new DataResponse([], Http::STATUS_NOT_FOUND);
65		}
66
67		$data = $this->entryToArray($contact);
68
69		$actions = $data['actions'];
70		if ($data['topAction']) {
71			array_unshift($actions, $data['topAction']);
72		}
73
74		return new DataResponse([
75			'userId' => $userId,
76			'displayName' => $contact->getFullName(),
77			'actions' => $actions,
78		]);
79	}
80
81	protected function entryToArray(IEntry $entry): array {
82		return json_decode(json_encode($entry), true);
83	}
84}
85