You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB

  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\AppFramework\OCSController;
  15. use OCP\IRequest;
  16. use OCP\IUserSession;
  17. use OCP\Share\IShare;
  18. /**
  19. * @psalm-import-type CoreContactsAction from ResponseDefinitions
  20. */
  21. class HoverCardController extends OCSController {
  22. public function __construct(
  23. IRequest $request,
  24. private IUserSession $userSession,
  25. private Manager $manager,
  26. ) {
  27. parent::__construct('core', $request);
  28. }
  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: list<CoreContactsAction>}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>
  34. *
  35. * 200: Account details returned
  36. * 404: Account not found
  37. */
  38. #[NoAdminRequired]
  39. #[ApiRoute(verb: 'GET', url: '/v1/{userId}', root: '/hovercard')]
  40. public function getUser(string $userId): DataResponse {
  41. $contact = $this->manager->findOne($this->userSession->getUser(), IShare::TYPE_USER, $userId);
  42. if (!$contact) {
  43. return new DataResponse([], Http::STATUS_NOT_FOUND);
  44. }
  45. $data = $contact->jsonSerialize();
  46. $actions = $data['actions'];
  47. if ($data['topAction']) {
  48. array_unshift($actions, $data['topAction']);
  49. }
  50. /** @var list<CoreContactsAction> $actions */
  51. return new DataResponse([
  52. 'userId' => $userId,
  53. 'displayName' => $contact->getFullName(),
  54. 'actions' => $actions,
  55. ]);
  56. }
  57. }