PropFindPlugin.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2024 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\FilesReminders\Dav;
  25. use DateTimeInterface;
  26. use OCA\DAV\Connector\Sabre\Node;
  27. use OCA\FilesReminders\Service\ReminderService;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\IUser;
  30. use OCP\IUserSession;
  31. use Sabre\DAV\INode;
  32. use Sabre\DAV\PropFind;
  33. use Sabre\DAV\Server;
  34. use Sabre\DAV\ServerPlugin;
  35. class PropFindPlugin extends ServerPlugin {
  36. public const REMINDER_DUE_DATE_PROPERTY = '{http://nextcloud.org/ns}reminder-due-date';
  37. public function __construct(
  38. private ReminderService $reminderService,
  39. private IUserSession $userSession,
  40. ) {
  41. }
  42. public function initialize(Server $server): void {
  43. $server->on('propFind', [$this, 'propFind']);
  44. }
  45. public function propFind(PropFind $propFind, INode $node) {
  46. if (!in_array(static::REMINDER_DUE_DATE_PROPERTY, $propFind->getRequestedProperties())) {
  47. return;
  48. }
  49. if (!($node instanceof Node)) {
  50. return;
  51. }
  52. $propFind->handle(
  53. static::REMINDER_DUE_DATE_PROPERTY,
  54. function () use ($node) {
  55. $user = $this->userSession->getUser();
  56. if (!($user instanceof IUser)) {
  57. return '';
  58. }
  59. $fileId = $node->getId();
  60. try {
  61. $reminder = $this->reminderService->getDueForUser($user, $fileId);
  62. } catch (DoesNotExistException $e) {
  63. return '';
  64. }
  65. return $reminder->getDueDate()->format(DateTimeInterface::ATOM); // ISO 8601
  66. },
  67. );
  68. }
  69. }