diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-09-29 22:09:53 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-09-29 22:09:53 +0300 |
commit | 5cc1751c53a2f0f7afc7283c3ce9e2ac3f773bd8 (patch) | |
tree | 8e563e625660c4cadd2b44a0e79fd7dcc9ab0768 /lib/Command | |
parent | 839788a9892febc38728843dfc89c3c94e2d4403 (diff) |
Add a CLI command to build threads for an account's exported data
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/Command')
-rw-r--r-- | lib/Command/Thread.php | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/lib/Command/Thread.php b/lib/Command/Thread.php new file mode 100644 index 000000000..3fba7ed3d --- /dev/null +++ b/lib/Command/Thread.php @@ -0,0 +1,92 @@ +<?php + +declare(strict_types=1); + +/** + * @author Christoph Wurst <christoph@winzerhof-wurst.at> + * + * Mail + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Mail\Command; + +use OCA\Mail\IMAP\Threading\DatabaseMessage; +use OCA\Mail\IMAP\Threading\ThreadBuilder; +use Psr\Log\LoggerInterface; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use function array_map; +use function file_exists; +use function file_get_contents; +use function json_decode; + +class Thread extends Command { + public const ARGUMENT_INPUT_FILE = 'thread-file'; + + /** @var ThreadBuilder */ + private $builder; + + /** @var LoggerInterface */ + private $logger; + + public function __construct(ThreadBuilder $builder, + LoggerInterface $logger) { + parent::__construct(); + $this->builder = $builder; + $this->logger = $logger; + } + + /** + * @return void + */ + protected function configure() { + $this->setName('mail:thread'); + $this->setDescription('Build threads from the exported data of an account'); + $this->addArgument(self::ARGUMENT_INPUT_FILE, InputArgument::REQUIRED); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $inputFile = $input->getArgument(self::ARGUMENT_INPUT_FILE); + + if (!file_exists($inputFile)) { + $output->writeln("<error>File $inputFile does not exist</error>"); + return 1; + } + + $json = file_get_contents($inputFile); + if ($json === false) { + $output->writeln("<error>Could not read thread data</error>"); + return 2; + } + $parsed = json_decode($json, true); + $threadData = array_map(function ($serialized) { + return new DatabaseMessage( + $serialized['databaseId'], + $serialized['subject'], + $serialized['id'], + $serialized['references'], + $serialized['threadRootId'] + ); + }, $parsed); + + $threads = $this->builder->build($threadData, $this->logger); + $output->writeln(count($threads) . " threads built from " . count($threadData) . " messages"); + + return 0; + } +} |