diff --git a/appinfo/info.xml b/appinfo/info.xml index dc131af..3429e64 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -19,6 +19,7 @@ OCA\MonthlyStatusEmail\Command\SendMail + OCA\MonthlyStatusEmail\Command\SendAllMail OCA\MonthlyStatusEmail\Settings\PersonalSettings diff --git a/composer.json b/composer.json index 34e78bd..a0a6097 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "cs:check": "php-cs-fixer fix --dry-run --diff", "lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l", "psalm": "psalm.phar --threads=1", - "psalm:update-baseline": "psalm.phar --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml", + "psalm:update-baseline": "psalm.phar --threads=1 --update-baseline", "psalm:clear": "psalm.phar --clear-cache && psalm --clear-global-cache", "psalm:fix": "psalm.phar --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType" }, diff --git a/lib/Command/SendAllMail.php b/lib/Command/SendAllMail.php new file mode 100644 index 0000000..f4c94a8 --- /dev/null +++ b/lib/Command/SendAllMail.php @@ -0,0 +1,80 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\MonthlyStatusEmail\Command; + +use OCP\IUserManager; +use OCP\IUser; +use OC\Core\Command\Base; +use OCA\MonthlyStatusEmail\Service\MailSender; +use OCA\MonthlyStatusEmail\Service\NotificationTrackerService; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class SendAllMail extends Base { + /** @var IUserManager $userManager */ + private $userManager; + /** @var NotificationTrackerService */ + private $service; + /** @var MailSender */ + private $mailSender; + + public function __construct( + NotificationTrackerService $service, + IUserManager $userManager, + MailSender $mailSender + ) { + parent::__construct(); + $this->userManager = $userManager; + $this->service = $service; + $this->mailSender = $mailSender; + } + + protected function configure() { + $this + ->setName('monthly_status_email:send-all') + ->setDescription('Send the notification mail to all users'); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $this->userManager->callForSeenUsers(function (IUser $user) use ($output) { + $trackedNotification = $this->service->find($user->getUID()); + + $to = $user->getEMailAddress(); + if ($to === null) { + // We don't have any email address, not sure what to do here. + $output->writeln('User doesn\'t have an email address'); + return; + } + $ret = $this->mailSender->sendMonthlyMailTo($trackedNotification); + if ($ret) { + $output->writeln('Email sent to ' . ($user->getDisplayName() ?? $user->getUID())); + } else { + $output->writeln('Failure sending email to ' . $user->getDisplayName()); + } + }); + return 0; + } +} diff --git a/lib/Jobs/InitDatabaseJob.php b/lib/Jobs/InitDatabaseJob.php index db159be..ee14b41 100644 --- a/lib/Jobs/InitDatabaseJob.php +++ b/lib/Jobs/InitDatabaseJob.php @@ -26,8 +26,9 @@ namespace OCA\MonthlyStatusEmail\Jobs; -use OC\BackgroundJob\QueuedJob; use OCA\MonthlyStatusEmail\Service\NotificationTrackerService; +use OCP\BackgroundJob\QueuedJob; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IUser; use OCP\IUserManager; @@ -43,8 +44,10 @@ class InitDatabaseJob extends QueuedJob { public function __construct( NotificationTrackerService $service, - IUserManager $userManager + IUserManager $userManager, + ITimeFactory $timeFactory ) { + parent::__construct($timeFactory); $this->service = $service; $this->userManager = $userManager; } diff --git a/lib/Service/MailSender.php b/lib/Service/MailSender.php index 3267d0f..1eb6a95 100644 --- a/lib/Service/MailSender.php +++ b/lib/Service/MailSender.php @@ -25,8 +25,7 @@ namespace OCA\MonthlyStatusEmail\Service; -use Icewind\SMB\IServer; -use OC\Files\FileInfo; +use OCP\Files\FileInfo; use OCA\MonthlyStatusEmail\Db\NotificationTracker; use OCP\DB\Exception; use OCP\IConfig; diff --git a/lib/Service/StorageInfoProvider.php b/lib/Service/StorageInfoProvider.php index 0a9bddf..f6cedcf 100644 --- a/lib/Service/StorageInfoProvider.php +++ b/lib/Service/StorageInfoProvider.php @@ -4,13 +4,64 @@ namespace OCA\MonthlyStatusEmail\Service; use OCP\IUser; +use OC\Files\View; class StorageInfoProvider { + /** + * Calculate the disc space for the given path + * + * @throws \OCP\Files\NotFoundException + */ public function getStorageInfo(IUser $user): array { - // make sure FS is setup before querying storage related stuff... \OC_Util::setupFS($user->getUID()); - // Handle storage specific events - return \OC_Helper::getStorageInfo('/'); + // return storage info without adding mount points + $includeExtStorage = \OC::$server->getConfig()->getSystemValueBool('quota_include_external_storage', false); + $view = new View("/" . $user->getUID() . "/files"); + $fullPath = $view->getAbsolutePath(''); + + $rootInfo = $view->getFileInfo('', 'ext'); + if (!$rootInfo instanceof \OCP\Files\FileInfo) { + throw new \OCP\Files\NotFoundException(); + } + $used = $rootInfo->getSize(true); + if ($used < 0) { + $used = 0; + } + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; + $mount = $rootInfo->getMountPoint(); + $storage = $mount->getStorage(); + $sourceStorage = $storage; + $internalPath = $rootInfo->getInternalPath(); + if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { + /** @var \OC\Files\Storage\Wrapper\Quota $storage */ + $quota = $sourceStorage->getQuota(); + } + + $free = $sourceStorage->free_space($internalPath); + if ($free >= 0) { + $total = $free + $used; + } else { + $total = $free; //either unknown or unlimited + } + if ($total > 0) { + if ($quota > 0 && $total > $quota) { + $total = $quota; + } + // prevent division by zero or error codes (negative values) + $relative = round(($used / $total) * 10000) / 100; + } else { + $relative = 0; + } + + $info = [ + 'free' => $free, + 'used' => $used, + 'quota' => (int)$quota, + 'total' => $total, + 'relative' => $relative, + ]; + + return $info; } } diff --git a/psalm.xml b/psalm.xml index 4da5f19..45e594c 100644 --- a/psalm.xml +++ b/psalm.xml @@ -7,6 +7,9 @@ xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" errorBaseline="tests/psalm-baseline.xml" > + + + diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index e70273e..af22b63 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,35 +1,10 @@ - - - Base - - - - - QueuedJob - - - - - InitDatabaseJob - - - - - FileInfo - - null - - - View - - NotificationTracker @@ -37,9 +12,4 @@ NotificationTracker[] - - - \OC_Util - - diff --git a/tests/stub.phpstub b/tests/stub.phpstub new file mode 100644 index 0000000..9340c5e --- /dev/null +++ b/tests/stub.phpstub @@ -0,0 +1,113 @@ +