HoverCardController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\DataResponse;
  30. use OCP\IRequest;
  31. use OCP\IUserSession;
  32. use OCP\Share\IShare;
  33. /**
  34. * @psalm-import-type CoreContactsAction from ResponseDefinitions
  35. */
  36. class HoverCardController extends \OCP\AppFramework\OCSController {
  37. public function __construct(
  38. IRequest $request,
  39. private IUserSession $userSession,
  40. private Manager $manager,
  41. ) {
  42. parent::__construct('core', $request);
  43. }
  44. /**
  45. * @NoAdminRequired
  46. *
  47. * Get the user details for a hovercard
  48. *
  49. * @param string $userId ID of the user
  50. * @return DataResponse<Http::STATUS_OK, array{userId: string, displayName: string, actions: CoreContactsAction[]}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  51. *
  52. * 200: User details returned
  53. * 404: User not found
  54. */
  55. public function getUser(string $userId): DataResponse {
  56. $contact = $this->manager->findOne($this->userSession->getUser(), IShare::TYPE_USER, $userId);
  57. if (!$contact) {
  58. return new DataResponse([], Http::STATUS_NOT_FOUND);
  59. }
  60. $data = $contact->jsonSerialize();
  61. $actions = $data['actions'];
  62. if ($data['topAction']) {
  63. array_unshift($actions, $data['topAction']);
  64. }
  65. /** @var CoreContactsAction[] $actions */
  66. return new DataResponse([
  67. 'userId' => $userId,
  68. 'displayName' => $contact->getFullName(),
  69. 'actions' => $actions,
  70. ]);
  71. }
  72. }