1
0

ExiprationNotification.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /** @var NotificationManager */
  18. private $notificationManager;
  19. /** @var IDBConnection */
  20. private $connection;
  21. /** @var ITimeFactory */
  22. private $time;
  23. /** @var ShareManager */
  24. private $shareManager;
  25. public function __construct(ITimeFactory $time,
  26. NotificationManager $notificationManager,
  27. IDBConnection $connection,
  28. ShareManager $shareManager) {
  29. parent::__construct();
  30. $this->notificationManager = $notificationManager;
  31. $this->connection = $connection;
  32. $this->time = $time;
  33. $this->shareManager = $shareManager;
  34. }
  35. protected function configure() {
  36. $this
  37. ->setName('sharing:expiration-notification')
  38. ->setDescription('Notify share initiators when a share will expire the next day.');
  39. }
  40. public function execute(InputInterface $input, OutputInterface $output): int {
  41. //Current time
  42. $minTime = $this->time->getDateTime();
  43. $minTime->add(new \DateInterval('P1D'));
  44. $minTime->setTime(0, 0, 0);
  45. $maxTime = clone $minTime;
  46. $maxTime->setTime(23, 59, 59);
  47. $shares = $this->shareManager->getAllShares();
  48. $now = $this->time->getDateTime();
  49. /** @var IShare $share */
  50. foreach ($shares as $share) {
  51. if ($share->getExpirationDate() === null
  52. || $share->getExpirationDate()->getTimestamp() < $minTime->getTimestamp()
  53. || $share->getExpirationDate()->getTimestamp() > $maxTime->getTimestamp()) {
  54. continue;
  55. }
  56. $notification = $this->notificationManager->createNotification();
  57. $notification->setApp('files_sharing')
  58. ->setDateTime($now)
  59. ->setObject('share', $share->getFullId())
  60. ->setSubject('expiresTomorrow');
  61. // Only send to initiator for now
  62. $notification->setUser($share->getSharedBy());
  63. $this->notificationManager->notify($notification);
  64. }
  65. return 0;
  66. }
  67. }