HoverCardController.php 1.8 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 OC\Core\ResponseDefinitions;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  13. use OCP\AppFramework\Http\DataResponse;
  14. use OCP\IRequest;
  15. use OCP\IUserSession;
  16. use OCP\Share\IShare;
  17. /**
  18. * @psalm-import-type CoreContactsAction from ResponseDefinitions
  19. */
  20. class HoverCardController extends \OCP\AppFramework\OCSController {
  21. public function __construct(
  22. IRequest $request,
  23. private IUserSession $userSession,
  24. private Manager $manager,
  25. ) {
  26. parent::__construct('core', $request);
  27. }
  28. /**
  29. * Get the account details for a hovercard
  30. *
  31. * @param string $userId ID of the user
  32. * @return DataResponse<Http::STATUS_OK, array{userId: string, displayName: string, actions: CoreContactsAction[]}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  33. *
  34. * 200: Account details returned
  35. * 404: Account not found
  36. */
  37. #[NoAdminRequired]
  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. }