Skip to content

Commit

Permalink
fix: add ability to send alternet text (html and plain)
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Dec 11, 2024
1 parent 791e126 commit 816d78f
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 140 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The rating depends on the installed text processing backend. See [the rating ove
Learn more about the Nextcloud Ethical AI Rating [in our blog](https://nextcloud.com/blog/nextcloud-ethical-ai-rating/).
]]></description>
<version>4.2.0-alpha.0</version>
<version>4.2.2-alpha.0</version>
<licence>agpl</licence>
<author homepage="https://github.com/ChristophWurst">Christoph Wurst</author>
<author homepage="https://github.com/GretaD">GretaD</author>
Expand Down
22 changes: 18 additions & 4 deletions lib/Controller/DraftsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,16 @@ public function create(
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
$message->setSubject($subject);
$message->setBody($body);
if ($isHtml) {
$message->setBodyPlain(null);
$message->setBodyHtml($body);
$message->setHtml(true);

Check warning on line 107 in lib/Controller/DraftsController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DraftsController.php#L104-L107

Added lines #L104 - L107 were not covered by tests
} else {
$message->setBodyPlain($body);
$message->setBodyHtml(null);
$message->setHtml(false);

Check warning on line 111 in lib/Controller/DraftsController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DraftsController.php#L109-L111

Added lines #L109 - L111 were not covered by tests
}
$message->setEditorBody($editorBody);
$message->setHtml($isHtml);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setUpdatedAt($this->timeFactory->getTime());
$message->setSendAt($sendAt);
Expand Down Expand Up @@ -169,9 +176,16 @@ public function update(int $id,
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
$message->setSubject($subject);
$message->setBody($body);
if ($isHtml) {
$message->setBodyPlain(null);
$message->setBodyHtml($body);
$message->setHtml(true);

Check warning on line 182 in lib/Controller/DraftsController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DraftsController.php#L179-L182

Added lines #L179 - L182 were not covered by tests
} else {
$message->setBodyPlain($body);
$message->setBodyHtml(null);
$message->setHtml(false);

Check warning on line 186 in lib/Controller/DraftsController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/DraftsController.php#L184-L186

Added lines #L184 - L186 were not covered by tests
}
$message->setEditorBody($editorBody);
$message->setHtml($isHtml);
$message->setFailed($failed);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
Expand Down
19 changes: 14 additions & 5 deletions lib/Controller/OutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,16 @@ public function create(
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
$message->setSubject($subject);
$message->setBody($body);
if ($isHtml) {
$message->setBodyPlain(null);
$message->setBodyHtml($body);
$message->setHtml(true);

Check warning on line 124 in lib/Controller/OutboxController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/OutboxController.php#L121-L124

Added lines #L121 - L124 were not covered by tests
} else {
$message->setBodyPlain($body);
$message->setBodyHtml(null);
$message->setHtml(false);

Check warning on line 128 in lib/Controller/OutboxController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/OutboxController.php#L126-L128

Added lines #L126 - L128 were not covered by tests
}
$message->setEditorBody($editorBody);
$message->setHtml($isHtml);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
$message->setPgpMime($isPgpMime);
Expand Down Expand Up @@ -181,7 +188,8 @@ public function update(
int $id,
int $accountId,
string $subject,
string $body,
?string $bodyPlain,
?string $bodyHtml,
?string $editorBody,
bool $isHtml,
bool $smimeSign,
Expand All @@ -207,9 +215,10 @@ public function update(
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
$message->setSubject($subject);
$message->setBody($body);
$message->setEditorBody($editorBody);
$message->setBodyPlain($bodyPlain);
$message->setBodyHtml($bodyHtml);

Check warning on line 219 in lib/Controller/OutboxController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/OutboxController.php#L218-L219

Added lines #L218 - L219 were not covered by tests
$message->setHtml($isHtml);
$message->setEditorBody($editorBody);

Check warning on line 221 in lib/Controller/OutboxController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/OutboxController.php#L221

Added line #L221 was not covered by tests
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
$message->setPgpMime($isPgpMime);
Expand Down
16 changes: 11 additions & 5 deletions lib/Db/LocalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
* @method void setSendAt(?int $sendAt)
* @method string getSubject()
* @method void setSubject(string $subject)
* @method string getBody()
* @method void setBody(?string $body)
* @method string getBodyPlain()
* @method void setBodyPlain(?string $bodyPlain)
* @method string getBodyHtml()
* @method void setBodyHtml(?string $bodyHtml)
* @method string|null getEditorBody()
* @method void setEditorBody(string $body)
* @method bool isHtml()
Expand Down Expand Up @@ -88,8 +90,11 @@ class LocalMessage extends Entity implements JsonSerializable {
/** @var string */
protected $subject;

/** @var string */
protected $body;
/** @var string|null */
protected $bodyPlain;

/** @var string|null */
protected $bodyHtml;

/** @var string|null */
protected $editorBody;
Expand Down Expand Up @@ -163,7 +168,8 @@ public function jsonSerialize() {
'sendAt' => $this->getSendAt(),
'updatedAt' => $this->getUpdatedAt(),
'subject' => $this->getSubject(),
'body' => $this->getBody(),
'bodyPlain' => $this->getBodyPlain(),
'bodyHtml' => $this->getBodyHtml(),

Check warning on line 172 in lib/Db/LocalMessage.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/LocalMessage.php#L171-L172

Added lines #L171 - L172 were not covered by tests
'editorBody' => $this->getEditorBody(),
'isHtml' => ($this->isHtml() === true),
'isPgpMime' => ($this->isPgpMime() === true),
Expand Down
45 changes: 45 additions & 0 deletions lib/Migration/Version4200Date20241210000000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version4200Date20241210000000 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
$schema = $schemaClosure();

Check warning on line 27 in lib/Migration/Version4200Date20241210000000.php

View check run for this annotation

Codecov / codecov/patch

lib/Migration/Version4200Date20241210000000.php#L26-L27

Added lines #L26 - L27 were not covered by tests

$outboxTable = $schema->getTable('mail_local_messages');
if ($outboxTable->hasColumn('body')) {
$outboxTable->dropColumn('body');

Check warning on line 31 in lib/Migration/Version4200Date20241210000000.php

View check run for this annotation

Codecov / codecov/patch

lib/Migration/Version4200Date20241210000000.php#L29-L31

Added lines #L29 - L31 were not covered by tests
}
if (!$outboxTable->hasColumn('body_plain')) {
$outboxTable->addColumn('body_plain', Types::TEXT, [
'notnull' => false,
]);

Check warning on line 36 in lib/Migration/Version4200Date20241210000000.php

View check run for this annotation

Codecov / codecov/patch

lib/Migration/Version4200Date20241210000000.php#L33-L36

Added lines #L33 - L36 were not covered by tests
}
if (!$outboxTable->hasColumn('body_html')) {
$outboxTable->addColumn('body_html', Types::TEXT, [
'notnull' => false,
]);

Check warning on line 41 in lib/Migration/Version4200Date20241210000000.php

View check run for this annotation

Codecov / codecov/patch

lib/Migration/Version4200Date20241210000000.php#L38-L41

Added lines #L38 - L41 were not covered by tests
}
return $schema;

Check warning on line 43 in lib/Migration/Version4200Date20241210000000.php

View check run for this annotation

Codecov / codecov/patch

lib/Migration/Version4200Date20241210000000.php#L43

Added line #L43 was not covered by tests
}
}
11 changes: 7 additions & 4 deletions lib/Provider/Command/MessageSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar
$localMessage->setType($localMessage::TYPE_OUTGOING);
$localMessage->setAccountId($account->getId());
$localMessage->setSubject((string)$message->getSubject());
$localMessage->setBody((string)$message->getBody());
// disabled due to issues caused by opening these messages in gui
//$localMessage->setEditorBody($message->getBody());
$localMessage->setHtml(true);
$localMessage->setBodyPlain($message->getBodyPlain());
$localMessage->setBodyHtml($message->getBodyHtml());
if (!empty($message->getBodyHtml())) {
$localMessage->setHtml(true);

Check warning on line 81 in lib/Provider/Command/MessageSend.php

View check run for this annotation

Codecov / codecov/patch

lib/Provider/Command/MessageSend.php#L81

Added line #L81 was not covered by tests
} else {
$localMessage->setHtml(false);
}
$localMessage->setSendAt($this->time->getTime());
// convert mail provider addresses to recipient addresses
$to = $this->convertAddressArray($message->getTo());
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/MailTransmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public function sendMessage(Account $account, LocalMessage $localMessage): void
new DataUriParser()
);
$mimePart = $mimeMessage->build(
$localMessage->isHtml(),
$localMessage->getBody(),
$localMessage->getBodyPlain(),
$localMessage->getBodyHtml(),

Check warning on line 126 in lib/Service/MailTransmission.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/MailTransmission.php#L125-L126

Added lines #L125 - L126 were not covered by tests
$attachmentParts,
$localMessage->isPgpMime() === true
);
Expand Down
Loading

0 comments on commit 816d78f

Please sign in to comment.