1
0

HoverCardController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Contacts\ContactsMenu\Manager;
  9. use OCA\Core\ResponseDefinitions;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\IRequest;
  14. use OCP\IUserSession;
  15. use OCP\Share\IShare;
  16. /**
  17. * @psalm-import-type CoreContactsAction from ResponseDefinitions
  18. */
  19. class HoverCardController extends \OCP\AppFramework\OCSController {
  20. public function __construct(
  21. IRequest $request,
  22. private IUserSession $userSession,
  23. private Manager $manager,
  24. ) {
  25. parent::__construct('core', $request);
  26. }
  27. /**
  28. * @NoAdminRequired
  29. *
  30. * Get the account details for a hovercard
  31. *
  32. * @param string $userId ID of the user
  33. * @return DataResponse<Http::STATUS_OK, array{userId: string, displayName: string, actions: CoreContactsAction[]}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  34. *
  35. * 200: Account details returned
  36. * 404: Account not found
  37. */
  38. #[ApiRoute(verb: 'GET', url: '/v1/{userId}', root: '/hovercard')]
  39. public function getUser(string $userId): DataResponse {
  40. $contact = $this->manager->findOne($this->userSession->getUser(), IShare::TYPE_USER, $userId);
  41. if (!$contact) {
  42. return new DataResponse([], Http::STATUS_NOT_FOUND);
  43. }
  44. $data = $contact->jsonSerialize();
  45. $actions = $data['actions'];
  46. if ($data['topAction']) {
  47. array_unshift($actions, $data['topAction']);
  48. }
  49. /** @var CoreContactsAction[] $actions */
  50. return new DataResponse([
  51. 'userId' => $userId,
  52. 'displayName' => $contact->getFullName(),
  53. 'actions' => $actions,
  54. ]);
  55. }
  56. }