ExiprationNotification.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OCA\Files_Sharing\Command;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\IDBConnection;
  28. use OCP\Notification\IManager as NotificationManager;
  29. use OCP\Share\IManager as ShareManager;
  30. use OCP\Share\IShare;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class ExiprationNotification extends Command {
  35. /** @var NotificationManager */
  36. private $notificationManager;
  37. /** @var IDBConnection */
  38. private $connection;
  39. /** @var ITimeFactory */
  40. private $time;
  41. /** @var ShareManager */
  42. private $shareManager;
  43. public function __construct(ITimeFactory $time,
  44. NotificationManager $notificationManager,
  45. IDBConnection $connection,
  46. ShareManager $shareManager) {
  47. parent::__construct();
  48. $this->notificationManager = $notificationManager;
  49. $this->connection = $connection;
  50. $this->time = $time;
  51. $this->shareManager = $shareManager;
  52. }
  53. protected function configure() {
  54. $this
  55. ->setName('sharing:expiration-notification')
  56. ->setDescription('Notify share initiators when a share will expire the next day.');
  57. }
  58. public function execute(InputInterface $input, OutputInterface $output): int {
  59. //Current time
  60. $minTime = $this->time->getDateTime();
  61. $minTime->add(new \DateInterval('P1D'));
  62. $minTime->setTime(0,0,0);
  63. $maxTime = clone $minTime;
  64. $maxTime->setTime(23, 59, 59);
  65. $shares = $this->shareManager->getAllShares();
  66. $now = $this->time->getDateTime();
  67. /** @var IShare $share */
  68. foreach ($shares as $share) {
  69. if ($share->getExpirationDate() === null
  70. || $share->getExpirationDate()->getTimestamp() < $minTime->getTimestamp()
  71. || $share->getExpirationDate()->getTimestamp() > $maxTime->getTimestamp()) {
  72. continue;
  73. }
  74. $notification = $this->notificationManager->createNotification();
  75. $notification->setApp('files_sharing')
  76. ->setDateTime($now)
  77. ->setObject('share', $share->getFullId())
  78. ->setSubject('expiresTomorrow');
  79. // Only send to initiator for now
  80. $notification->setUser($share->getSharedBy());
  81. $this->notificationManager->notify($notification);
  82. }
  83. return 0;
  84. }
  85. }