-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2125 from nextcloud/feature/threading
Build message threads after each sync and store thread root ID for later reference
- Loading branch information
Showing
6 changed files
with
325 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace OCA\Mail\Events; | ||
|
||
use OCA\Mail\Account; | ||
use OCP\EventDispatcher\Event; | ||
|
||
class SynchronizationEvent extends Event { | ||
|
||
/** @var Account */ | ||
private $account; | ||
|
||
public function __construct(Account $account) { | ||
parent::__construct(); | ||
|
||
$this->account = $account; | ||
} | ||
|
||
public function getAccount(): Account { | ||
return $this->account; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace OCA\Mail\IMAP\Threading; | ||
|
||
use function json_decode; | ||
|
||
class DatabaseMessage extends Message { | ||
|
||
/** @var int */ | ||
private $databaseId; | ||
|
||
/** @var string|null */ | ||
private $threadRootId; | ||
|
||
/** @var bool */ | ||
private $dirty = false; | ||
|
||
public function __construct(int $databaseId, | ||
string $subject, | ||
string $id, | ||
array $references, | ||
?string $threadRootId) { | ||
parent::__construct($subject, $id, $references); | ||
|
||
$this->databaseId = $databaseId; | ||
$this->threadRootId = $threadRootId; | ||
} | ||
|
||
public static function fromRowData(int $id, | ||
string $subject, | ||
?string $messageId, | ||
?string $references, | ||
?string $inReplyTo, | ||
?string $threadRootId): self { | ||
$referencesForThreading = $references !== null ? json_decode($references, true) : []; | ||
if (!empty($inReplyTo)) { | ||
$referencesForThreading[] = $inReplyTo; | ||
} | ||
|
||
return new self( | ||
$id, | ||
$subject, | ||
$messageId, | ||
$referencesForThreading, | ||
$threadRootId | ||
); | ||
} | ||
|
||
public function getDatabaseId(): int { | ||
return $this->databaseId; | ||
} | ||
|
||
public function getThreadRootId(): ?string { | ||
return $this->threadRootId; | ||
} | ||
|
||
public function setThreadRootId(?string $threadRootId): void { | ||
// Only update the thread ID if it has a value, is different and we haven't set one before | ||
if ($threadRootId !== null && $this->threadRootId !== $threadRootId && !$this->dirty) { | ||
$this->dirty = true; | ||
$this->threadRootId = $threadRootId; | ||
} | ||
} | ||
|
||
public function isDirty(): bool { | ||
return $this->dirty; | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
lib/Listener/AccountSynchronizedThreadUpdaterListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace OCA\Mail\Listener; | ||
|
||
use Generator; | ||
use OCA\Mail\Db\MessageMapper; | ||
use OCA\Mail\Events\SynchronizationEvent; | ||
use OCA\Mail\IMAP\Threading\Container; | ||
use OCA\Mail\IMAP\Threading\DatabaseMessage; | ||
use OCA\Mail\IMAP\Threading\ThreadBuilder; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\ILogger; | ||
use function array_chunk; | ||
use function iterator_to_array; | ||
|
||
class AccountSynchronizedThreadUpdaterListener implements IEventListener { | ||
|
||
/** @var MessageMapper */ | ||
private $mapper; | ||
|
||
/** @var ThreadBuilder */ | ||
private $builder; | ||
|
||
/** @var ILogger */ | ||
private $logger; | ||
|
||
public function __construct(MessageMapper $mapper, | ||
ThreadBuilder $builder, | ||
ILogger $logger) { | ||
$this->mapper = $mapper; | ||
$this->builder = $builder; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof SynchronizationEvent)) { | ||
// Unrelated | ||
return; | ||
} | ||
|
||
$accountId = $event->getAccount()->getId(); | ||
$messages = $this->mapper->findThreadingData($event->getAccount()); | ||
$this->logger->debug("Account $accountId has " . count($messages) . " messages for threading"); | ||
$threads = $this->builder->build($messages); | ||
$this->logger->debug("Account $accountId has " . count($threads) . " threads"); | ||
$flattened = iterator_to_array($this->flattenThreads($threads), false); | ||
$this->logger->debug("Account $accountId has " . count($flattened) . " messages with a new thread ID"); | ||
foreach (array_chunk($flattened, 500) as $chunk) { | ||
$this->mapper->writeThreadIds($chunk); | ||
} | ||
} | ||
|
||
/** | ||
* @param Container[] $threads | ||
* | ||
* @return DatabaseMessage[]|Generator | ||
*/ | ||
private function flattenThreads(array $threads, | ||
?string $threadId = null): Generator { | ||
foreach ($threads as $thread) { | ||
if (($message = $thread->getMessage()) !== null) { | ||
/** @var DatabaseMessage $message */ | ||
if ($threadId === null) { | ||
// No parent -> let's use own ID | ||
$message->setThreadRootId($message->getId()); | ||
} else { | ||
$message->setThreadRootId($threadId); | ||
} | ||
if ($message->isDirty()) { | ||
yield $message; | ||
} | ||
} | ||
|
||
yield from $this->flattenThreads( | ||
$thread->getChildren(), | ||
$message === null ? null : $message->getId() | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.