ExiprationNotification.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Sharing\Command;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\IDBConnection;
  10. use OCP\Notification\IManager as NotificationManager;
  11. use OCP\Share\IManager as ShareManager;
  12. use OCP\Share\IShare;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class ExiprationNotification extends Command {
  17. public function __construct(
  18. private ITimeFactory $time,
  19. private NotificationManager $notificationManager,
  20. private IDBConnection $connection,
  21. private ShareManager $shareManager,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure() {
  26. $this
  27. ->setName('sharing:expiration-notification')
  28. ->setDescription('Notify share initiators when a share will expire the next day.');
  29. }
  30. public function execute(InputInterface $input, OutputInterface $output): int {
  31. //Current time
  32. $minTime = $this->time->getDateTime();
  33. $minTime->add(new \DateInterval('P1D'));
  34. $minTime->setTime(0, 0, 0);
  35. $maxTime = clone $minTime;
  36. $maxTime->setTime(23, 59, 59);
  37. $shares = $this->shareManager->getAllShares();
  38. $now = $this->time->getDateTime();
  39. /** @var IShare $share */
  40. foreach ($shares as $share) {
  41. if ($share->getExpirationDate() === null
  42. || $share->getExpirationDate()->getTimestamp() < $minTime->getTimestamp()
  43. || $share->getExpirationDate()->getTimestamp() > $maxTime->getTimestamp()) {
  44. continue;
  45. }
  46. $notification = $this->notificationManager->createNotification();
  47. $notification->setApp('files_sharing')
  48. ->setDateTime($now)
  49. ->setObject('share', $share->getFullId())
  50. ->setSubject('expiresTomorrow');
  51. // Only send to initiator for now
  52. $notification->setUser($share->getSharedBy());
  53. $this->notificationManager->notify($notification);
  54. }
  55. return 0;
  56. }
  57. }