Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store message (HTML) text preview in db cache #6962

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- **🙈 We’re not reinventing the wheel!** Based on the great [Horde](https://horde.org) libraries.
- **📬 Want to host your own mail server?** We do not have to reimplement this as you could set up [Mail-in-a-Box](https://mailinabox.email)!
]]></description>
<version>1.14.0-alpha.4</version>
<version>1.14.0-alpha.5</version>
<licence>agpl</licence>
<author>Greta Doçi</author>
<author homepage="https://github.com/nextcloud/groupware">Nextcloud Groupware Team</author>
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"christophwurst/kitinerary-sys": "^0.1.0",
"ezyang/htmlpurifier": "4.14.0",
"gravatarphp/gravatar": "^2.0",
"html2text/html2text": "^4.3",
"nextcloud/horde-managesieve": "^1.0",
"nextcloud/horde-smtp": "^1.0",
"psr/log": "^1",
Expand Down
43 changes: 42 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,13 @@ public function updatePreviewDataBulk(Message ...$messages): array {
return $messages;
}

public function resetPreviewDataFlag(): void {
$qb = $this->db->getQueryBuilder();
$update = $qb->update($this->getTableName())
->set('structure_analyzed', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL));
$update->executeStatement();
}

public function deleteAll(Mailbox $mailbox): void {
$messageIdQuery = $this->db->getQueryBuilder();
$messageIdQuery->select('id')
Expand Down
58 changes: 39 additions & 19 deletions lib/IMAP/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Horde_Imap_Client_Socket;
use Horde_Mime_Mail;
use Horde_Mime_Part;
use Html2Text\Html2Text;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Model\IMAPMessage;
Expand Down Expand Up @@ -682,36 +683,55 @@ public function getBodyStructureData(Horde_Imap_Client_Socket $client,
}
}

$textBodyId = $structure->findBody('text');
// $htmlBodyId = $structure->findBody('html');
// $htmlBody = $data->getBodyPart($htmlBodyId);

$partsQuery = new Horde_Imap_Client_Fetch_Query();
if ($textBodyId === null) {
$textBodyId = $structure->findBody() ?? $structure->findBody('text');
$htmlBodyId = $structure->findBody('html');
if ($textBodyId === null && $htmlBodyId === null) {
return new MessageStructureData($hasAttachments, $text);
}
$partsQuery->bodyPart($textBodyId, [
'decode' => true,
'peek' => true,
]);
$partsQuery->mimeHeader($textBodyId, [
'peek' => true
]);
if ($htmlBodyId !== null) {
$partsQuery = new Horde_Imap_Client_Fetch_Query();
$partsQuery->bodyPart($htmlBodyId, [
'decode' => true,
'peek' => true,
]);
$partsQuery->mimeHeader($htmlBodyId, [
'peek' => true
]);
}
if ($textBodyId !== null) {
$partsQuery = new Horde_Imap_Client_Fetch_Query();
$partsQuery->bodyPart($textBodyId, [
'decode' => true,
'peek' => true,
]);
$partsQuery->mimeHeader($textBodyId, [
'peek' => true
]);
}
$parts = $client->fetch($mailbox, $partsQuery, [
'ids' => new Horde_Imap_Client_Ids([$fetchData->getUid()]),
]);
/** @var Horde_Imap_Client_Data_Fetch $part */
$part = $parts[$fetchData->getUid()];
$body = $part->getBodyPart($textBodyId);

if (!empty($body)) {
$htmlBody = $part->getBodyPart($htmlBodyId);
if (!empty($htmlBody)) {
$mimeHeaders = $fetchData->getMimeHeader($htmlBodyId, Horde_Imap_Client_Data_Fetch::HEADER_PARSE);
if ($enc = $mimeHeaders->getValue('content-transfer-encoding')) {
$structure->setTransferEncoding($enc);
}
$structure->setContents($htmlBody);
$html = new Html2Text($structure->getContents());
return new MessageStructureData($hasAttachments, trim($html->getText()));
}
$textBody = $part->getBodyPart($textBodyId);
if (!empty($textBody)) {
$mimeHeaders = $fetchData->getMimeHeader($textBodyId, Horde_Imap_Client_Data_Fetch::HEADER_PARSE);
if ($enc = $mimeHeaders->getValue('content-transfer-encoding')) {
$structure->setTransferEncoding($enc);
$structure->setContents($textBody);
return new MessageStructureData($hasAttachments, $structure->getContents());
}
$structure->setContents($body);
/** @var string $text */
$text = $structure->getContents();
return new MessageStructureData($hasAttachments, $textBody);
}

return new MessageStructureData($hasAttachments, $text);
Expand Down
56 changes: 56 additions & 0 deletions lib/Migration/Version1140Date20220808203258.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* @author 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\Migration;

use Closure;
use OCA\Mail\Db\MessageMapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Psr\Log\LoggerInterface;
use function method_exists;

class Version1140Date20220808203258 extends SimpleMigrationStep {
private LoggerInterface $logger;
private MessageMapper $messageMapper;

public function __construct(MessageMapper $messageMapper,
LoggerInterface $logger) {
$this->logger = $logger;
$this->messageMapper = $messageMapper;
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
if (!method_exists($this->messageMapper, 'resetPreviewDataFlag')) {
$this->logger->warning('Service method missing due to in process upgrade');
return;
}
$this->messageMapper->resetPreviewDataFlag();
}
}