1
0

PropFindPlugin.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\FilesReminders\Dav;
  8. use DateTimeInterface;
  9. use OCA\DAV\Connector\Sabre\Node;
  10. use OCA\FilesReminders\Service\ReminderService;
  11. use OCP\AppFramework\Db\DoesNotExistException;
  12. use OCP\IUser;
  13. use OCP\IUserSession;
  14. use Sabre\DAV\INode;
  15. use Sabre\DAV\PropFind;
  16. use Sabre\DAV\Server;
  17. use Sabre\DAV\ServerPlugin;
  18. class PropFindPlugin extends ServerPlugin {
  19. public const REMINDER_DUE_DATE_PROPERTY = '{http://nextcloud.org/ns}reminder-due-date';
  20. public function __construct(
  21. private ReminderService $reminderService,
  22. private IUserSession $userSession,
  23. ) {
  24. }
  25. public function initialize(Server $server): void {
  26. $server->on('propFind', [$this, 'propFind']);
  27. }
  28. public function propFind(PropFind $propFind, INode $node) {
  29. if (!in_array(static::REMINDER_DUE_DATE_PROPERTY, $propFind->getRequestedProperties())) {
  30. return;
  31. }
  32. if (!($node instanceof Node)) {
  33. return;
  34. }
  35. $propFind->handle(
  36. static::REMINDER_DUE_DATE_PROPERTY,
  37. function () use ($node) {
  38. $user = $this->userSession->getUser();
  39. if (!($user instanceof IUser)) {
  40. return '';
  41. }
  42. $fileId = $node->getId();
  43. try {
  44. $reminder = $this->reminderService->getDueForUser($user, $fileId);
  45. } catch (DoesNotExistException $e) {
  46. return '';
  47. }
  48. return $reminder->getDueDate()->format(DateTimeInterface::ATOM); // ISO 8601
  49. },
  50. );
  51. }
  52. }