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.

HoverCardController.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Kate Döen <kate.doeen@nextcloud.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. */
  25. namespace OC\Core\Controller;
  26. use OC\Contacts\ContactsMenu\Manager;
  27. use OCA\Core\ResponseDefinitions;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\Attribute\ApiRoute;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\IRequest;
  32. use OCP\IUserSession;
  33. use OCP\Share\IShare;
  34. /**
  35. * @psalm-import-type CoreContactsAction from ResponseDefinitions
  36. */
  37. class HoverCardController extends \OCP\AppFramework\OCSController {
  38. public function __construct(
  39. IRequest $request,
  40. private IUserSession $userSession,
  41. private Manager $manager,
  42. ) {
  43. parent::__construct('core', $request);
  44. }
  45. /**
  46. * @NoAdminRequired
  47. *
  48. * Get the account details for a hovercard
  49. *
  50. * @param string $userId ID of the user
  51. * @return DataResponse<Http::STATUS_OK, array{userId: string, displayName: string, actions: CoreContactsAction[]}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  52. *
  53. * 200: Account details returned
  54. * 404: Account not found
  55. */
  56. #[ApiRoute(verb: 'GET', url: '/v1/{userId}', root: '/hovercard')]
  57. public function getUser(string $userId): DataResponse {
  58. $contact = $this->manager->findOne($this->userSession->getUser(), IShare::TYPE_USER, $userId);
  59. if (!$contact) {
  60. return new DataResponse([], Http::STATUS_NOT_FOUND);
  61. }
  62. $data = $contact->jsonSerialize();
  63. $actions = $data['actions'];
  64. if ($data['topAction']) {
  65. array_unshift($actions, $data['topAction']);
  66. }
  67. /** @var CoreContactsAction[] $actions */
  68. return new DataResponse([
  69. 'userId' => $userId,
  70. 'displayName' => $contact->getFullName(),
  71. 'actions' => $actions,
  72. ]);
  73. }
  74. }