Skip to content

Commit

Permalink
fix(outbox): add status for messages
Browse files Browse the repository at this point in the history
and try resending failed messages at POF

Signed-off-by: Anna Larch <[email protected]>
  • Loading branch information
miaulalala committed Feb 26, 2024
1 parent a865c99 commit c35fe55
Show file tree
Hide file tree
Showing 16 changed files with 620 additions and 345 deletions.
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCA\Mail\Events\MessageSentEvent;
use OCA\Mail\Events\NewMessagesSynchronized;
use OCA\Mail\Events\OutboxMessageCreatedEvent;
use OCA\Mail\Events\OutboxMessageStatusChangeEvent;
use OCA\Mail\Events\SynchronizationEvent;
use OCA\Mail\HordeTranslationHandler;
use OCA\Mail\Http\Middleware\ErrorMiddleware;
Expand All @@ -67,6 +68,7 @@
use OCA\Mail\Listener\NewMessageClassificationListener;
use OCA\Mail\Listener\OauthTokenRefreshListener;
use OCA\Mail\Listener\OptionalIndicesListener;
use OCA\Mail\Listener\OutboxStatusChangeListener;
use OCA\Mail\Listener\OutOfOfficeListener;
use OCA\Mail\Listener\SaveSentMessageListener;
use OCA\Mail\Listener\SpamReportListener;
Expand Down Expand Up @@ -136,6 +138,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(DraftSavedEvent::class, DeleteDraftListener::class);
$context->registerEventListener(DraftMessageCreatedEvent::class, DeleteDraftListener::class);
$context->registerEventListener(OutboxMessageCreatedEvent::class, DeleteDraftListener::class);
$context->registerEventListener(OutboxMessageStatusChangeEvent::class, OutboxStatusChangeListener::class);
$context->registerEventListener(MailboxesSynchronizedEvent::class, MailboxesSynchronizedSpecialMailboxesUpdater::class);
$context->registerEventListener(MessageFlaggedEvent::class, MessageCacheUpdaterListener::class);
$context->registerEventListener(MessageFlaggedEvent::class, SpamReportListener::class);
Expand Down
35 changes: 35 additions & 0 deletions lib/Db/LocalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,29 @@
* @method setSmimeCertificateId(?int $smimeCertificateId)
* @method bool|null getSmimeEncrypt()
* @method setSmimeEncrypt (bool $smimeEncryt)
* @method int getStatus();
* @method setStatus(int $status);
* @method bool isMdnRequested()
* @method setMdnRequested(bool $mdnRequested)
* @method string|null getRaw()
* @method setRaw(string|null $raw)
*/
class LocalMessage extends Entity implements JsonSerializable {
public const TYPE_OUTGOING = 0;
public const TYPE_DRAFT = 1;

public const STATUS_UNPROCESSED = 0;
public const STATUS_NO_SENT_MAILBOX = 1;
public const STATUS_SMIME_SIGN_NO_CERT_ID = 2;
public const STATUS_SMIME_SIGN_CERT = 3;
public const STATUS_SMIME_SIGN_FAIL = 4;
public const STATUS_SMIME_ENCRYPT_NO_CERT_ID = 5;
public const STATUS_SMIME_ENCRYPT_CERT = 6;
public const STATUS_SMIME_ENCRYT_FAIL = 7;
public const STATUS_TOO_MANY_RECIPIENTS = 8;
public const STATUS_RATELIMIT = 9;
public const STATUS_IMAP_SEND_FAIL = 10;
public const STATUS_IMAP_SENT_MAILBOX_FAIL = 11;
/**
* @var int
* @psalm-var self::TYPE_*
Expand Down Expand Up @@ -116,6 +134,18 @@ class LocalMessage extends Entity implements JsonSerializable {
/** @var bool|null */
protected $smimeEncrypt;

/**
* @var int
* @psalm-var self::STATUS_*
*/
protected $status;

/** @var bool */
protected $mdnRequested;

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

public function __construct() {
$this->addType('type', 'integer');
$this->addType('accountId', 'integer');
Expand All @@ -127,6 +157,8 @@ public function __construct() {
$this->addType('smimeSign', 'boolean');
$this->addType('smimeCertificateId', 'integer');
$this->addType('smimeEncrypt', 'boolean');
$this->addType('status', 'integer');
$this->addType('mdnRequested', 'boolean');
}

#[ReturnTypeWillChange]
Expand Down Expand Up @@ -168,6 +200,9 @@ public function jsonSerialize() {
'smimeCertificateId' => $this->getSmimeCertificateId(),
'smimeSign' => $this->getSmimeSign() === true,
'smimeEncrypt' => $this->getSmimeEncrypt() === true,
'status' => $this->getStatus(),
'mdnRequested' => $this->getMdnRequested() === true,

Check failure on line 204 in lib/Db/LocalMessage.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedMagicMethod

lib/Db/LocalMessage.php:204:29: UndefinedMagicMethod: Magic method OCA\Mail\Db\LocalMessage::getmdnrequested does not exist (see https://psalm.dev/219)

Check failure on line 204 in lib/Db/LocalMessage.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable27

UndefinedMagicMethod

lib/Db/LocalMessage.php:204:29: UndefinedMagicMethod: Magic method OCA\Mail\Db\LocalMessage::getmdnrequested does not exist (see https://psalm.dev/219)

Check failure on line 204 in lib/Db/LocalMessage.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable26

UndefinedMagicMethod

lib/Db/LocalMessage.php:204:29: UndefinedMagicMethod: Magic method OCA\Mail\Db\LocalMessage::getmdnrequested does not exist (see https://psalm.dev/219)

Check failure on line 204 in lib/Db/LocalMessage.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

UndefinedMagicMethod

lib/Db/LocalMessage.php:204:29: UndefinedMagicMethod: Magic method OCA\Mail\Db\LocalMessage::getmdnrequested does not exist (see https://psalm.dev/219)
'raw' => $this->getRaw(),
];
}

Expand Down
27 changes: 11 additions & 16 deletions lib/Events/BeforeMessageSentEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use Horde_Mime_Mail;
use OCA\Mail\Account;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Db\Message;
use OCA\Mail\Model\IMessage;
use OCA\Mail\Model\NewMessageData;
Expand All @@ -39,9 +40,6 @@ class BeforeMessageSentEvent extends Event {
/** @var Account */
private $account;

/** @var NewMessageData */
private $newMessageData;

/** @var Message|null */
private $draft;

Expand All @@ -55,16 +53,13 @@ class BeforeMessageSentEvent extends Event {
private $repliedToMessageId;

public function __construct(Account $account,
NewMessageData $newMessageData,
?string $repliedToMessageId,
?Message $draft,
IMessage $message,
Horde_Mime_Mail $mail) {
Horde_Mime_Mail $mail,
private LocalMessage $localMessage) {
parent::__construct();
$this->account = $account;
$this->newMessageData = $newMessageData;
$this->repliedToMessageId = $repliedToMessageId;
$this->draft = $draft;
$this->message = $message;
$this->mail = $mail;
}
Expand All @@ -73,23 +68,23 @@ public function getAccount(): Account {
return $this->account;
}

public function getNewMessageData(): NewMessageData {
return $this->newMessageData;
}

public function getRepliedToMessageId(): ?string {
return $this->repliedToMessageId;
}

public function getDraft(): ?Message {
return $this->draft;
}

public function getMessage(): IMessage {
return $this->message;
}

public function getMail(): Horde_Mime_Mail {
return $this->mail;
}

public function getLocalMessage(): LocalMessage {
return $this->localMessage;
}

public function setLocalMessage(LocalMessage $localMessage): void {
$this->localMessage = $localMessage;

Check failure on line 88 in lib/Events/BeforeMessageSentEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InaccessibleProperty

lib/Events/BeforeMessageSentEvent.php:88:3: InaccessibleProperty: OCA\Mail\Events\BeforeMessageSentEvent::$localMessage is marked readonly (see https://psalm.dev/054)

Check failure on line 88 in lib/Events/BeforeMessageSentEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable27

InaccessibleProperty

lib/Events/BeforeMessageSentEvent.php:88:3: InaccessibleProperty: OCA\Mail\Events\BeforeMessageSentEvent::$localMessage is marked readonly (see https://psalm.dev/054)

Check failure on line 88 in lib/Events/BeforeMessageSentEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable26

InaccessibleProperty

lib/Events/BeforeMessageSentEvent.php:88:3: InaccessibleProperty: OCA\Mail\Events\BeforeMessageSentEvent::$localMessage is marked readonly (see https://psalm.dev/054)

Check failure on line 88 in lib/Events/BeforeMessageSentEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InaccessibleProperty

lib/Events/BeforeMessageSentEvent.php:88:3: InaccessibleProperty: OCA\Mail\Events\BeforeMessageSentEvent::$localMessage is marked readonly (see https://psalm.dev/054)
}
}
9 changes: 0 additions & 9 deletions lib/Events/DraftSavedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,20 @@ class DraftSavedEvent extends Event {
/** @var Account */
private $account;

/** @var NewMessageData */
private $newMessageData;

/** @var Message|null */
private $draft;

public function __construct(Account $account,
NewMessageData $newMessageData,
?Message $draft) {
parent::__construct();
$this->account = $account;
$this->newMessageData = $newMessageData;
$this->draft = $draft;
}

public function getAccount(): Account {
return $this->account;
}

public function getNewMessageData(): NewMessageData {
return $this->newMessageData;
}

public function getDraft(): ?Message {
return $this->draft;
}
Expand Down
24 changes: 11 additions & 13 deletions lib/Events/MessageSentEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use Horde_Mime_Mail;
use OCA\Mail\Account;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Db\Message;
use OCA\Mail\Model\IMessage;
use OCA\Mail\Model\NewMessageData;
Expand Down Expand Up @@ -55,16 +56,13 @@ class MessageSentEvent extends Event {
private $mail;

public function __construct(Account $account,
NewMessageData $newMessageData,
?string $repliedToMessageId,
?Message $draft,
IMessage $message,
Horde_Mime_Mail $mail) {
Horde_Mime_Mail $mail,
private LocalMessage $localMessage) {
parent::__construct();
$this->account = $account;
$this->newMessageData = $newMessageData;
$this->repliedToMessageId = $repliedToMessageId;
$this->draft = $draft;
$this->message = $message;
$this->mail = $mail;
}
Expand All @@ -73,23 +71,23 @@ public function getAccount(): Account {
return $this->account;
}

public function getNewMessageData(): NewMessageData {
return $this->newMessageData;
}

public function getRepliedToMessageId(): ?string {
return $this->repliedToMessageId;
}

public function getDraft(): ?Message {
return $this->draft;
}

public function getMessage(): IMessage {
return $this->message;
}

public function getMail(): Horde_Mime_Mail {
return $this->mail;
}

public function getLocalMessage(): LocalMessage {
return $this->localMessage;
}

public function setLocalMessage(LocalMessage $localMessage): void {
$this->localMessage = $localMessage;

Check failure on line 91 in lib/Events/MessageSentEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InaccessibleProperty

lib/Events/MessageSentEvent.php:91:3: InaccessibleProperty: OCA\Mail\Events\MessageSentEvent::$localMessage is marked readonly (see https://psalm.dev/054)

Check failure on line 91 in lib/Events/MessageSentEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable27

InaccessibleProperty

lib/Events/MessageSentEvent.php:91:3: InaccessibleProperty: OCA\Mail\Events\MessageSentEvent::$localMessage is marked readonly (see https://psalm.dev/054)

Check failure on line 91 in lib/Events/MessageSentEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable26

InaccessibleProperty

lib/Events/MessageSentEvent.php:91:3: InaccessibleProperty: OCA\Mail\Events\MessageSentEvent::$localMessage is marked readonly (see https://psalm.dev/054)

Check failure on line 91 in lib/Events/MessageSentEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InaccessibleProperty

lib/Events/MessageSentEvent.php:91:3: InaccessibleProperty: OCA\Mail\Events\MessageSentEvent::$localMessage is marked readonly (see https://psalm.dev/054)
}
}
42 changes: 42 additions & 0 deletions lib/Events/OutboxMessageStatusChangeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* @copyright 2024 Anna Larch <[email protected]>
*
* @author Anna Larch <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Events;

use OCA\Mail\Db\LocalMessage;
use OCP\EventDispatcher\Event;

/**
* @psalm-immutable
*/
class OutboxMessageStatusChangeEvent extends Event {

public function __construct(private LocalMessage $message) {
}

public function getMessage(): LocalMessage {
return $this->message;
}

public function setMessage(LocalMessage $message): void {
$this->message = $message;

Check failure on line 40 in lib/Events/OutboxMessageStatusChangeEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InaccessibleProperty

lib/Events/OutboxMessageStatusChangeEvent.php:40:3: InaccessibleProperty: OCA\Mail\Events\OutboxMessageStatusChangeEvent::$message is marked readonly (see https://psalm.dev/054)

Check failure on line 40 in lib/Events/OutboxMessageStatusChangeEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable27

InaccessibleProperty

lib/Events/OutboxMessageStatusChangeEvent.php:40:3: InaccessibleProperty: OCA\Mail\Events\OutboxMessageStatusChangeEvent::$message is marked readonly (see https://psalm.dev/054)

Check failure on line 40 in lib/Events/OutboxMessageStatusChangeEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable26

InaccessibleProperty

lib/Events/OutboxMessageStatusChangeEvent.php:40:3: InaccessibleProperty: OCA\Mail\Events\OutboxMessageStatusChangeEvent::$message is marked readonly (see https://psalm.dev/054)

Check failure on line 40 in lib/Events/OutboxMessageStatusChangeEvent.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InaccessibleProperty

lib/Events/OutboxMessageStatusChangeEvent.php:40:3: InaccessibleProperty: OCA\Mail\Events\OutboxMessageStatusChangeEvent::$message is marked readonly (see https://psalm.dev/054)
}
}
2 changes: 1 addition & 1 deletion lib/Exception/ManyRecipientsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@

class ManyRecipientsException extends ClientException {
public function __construct() {
parent::__construct("Many recipients in TO and/or CC");
parent::__construct("Many recipients in TO and/or CC and/or BCC");
}
}
2 changes: 1 addition & 1 deletion lib/Listener/AntiAbuseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function handle(Event $event): void {

$this->service->onBeforeMessageSent(
$user,
$event->getNewMessageData(),
$event->getLocalMessage(),
);
}
}
47 changes: 47 additions & 0 deletions lib/Listener/OutboxStatusChangeListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/**
* @copyright 2024 Anna Larch <[email protected]>
*
* @author Anna Larch <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Listener;

use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Db\LocalMessageMapper;
use OCA\Mail\Events\OutboxMessageStatusChangeEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

/**
* @template-implements IEventListener<Event|OutboxMessageStatusChangeEvent>
*/
class OutboxStatusChangeListener implements IEventListener {

public function __construct(private LocalMessageMapper $messageMapper) {
}

public function handle(Event $event): void {
if (!($event instanceof OutboxMessageStatusChangeEvent)) {
return;
}

$message = $event->getMessage();
$this->messageMapper->update($message);

}
}
Loading

0 comments on commit c35fe55

Please sign in to comment.