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 Apr 4, 2024
1 parent 7559ef6 commit 3e84000
Show file tree
Hide file tree
Showing 52 changed files with 2,802 additions and 2,120 deletions.
7 changes: 0 additions & 7 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use OCA\Mail\Dashboard\UnreadMailWidget;
use OCA\Mail\Dashboard\UnreadMailWidgetV2;
use OCA\Mail\Events\BeforeImapClientCreated;
use OCA\Mail\Events\BeforeMessageSentEvent;
use OCA\Mail\Events\DraftMessageCreatedEvent;
use OCA\Mail\Events\DraftSavedEvent;
use OCA\Mail\Events\MailboxesSynchronizedEvent;
Expand All @@ -55,9 +54,7 @@
use OCA\Mail\Http\Middleware\ProvisioningMiddleware;
use OCA\Mail\Listener\AccountSynchronizedThreadUpdaterListener;
use OCA\Mail\Listener\AddressCollectionListener;
use OCA\Mail\Listener\AntiAbuseListener;
use OCA\Mail\Listener\DeleteDraftListener;
use OCA\Mail\Listener\FlagRepliedMessageListener;
use OCA\Mail\Listener\HamReportListener;
use OCA\Mail\Listener\InteractionListener;
use OCA\Mail\Listener\MailboxesSynchronizedSpecialMailboxesUpdater;
Expand All @@ -68,7 +65,6 @@
use OCA\Mail\Listener\OauthTokenRefreshListener;
use OCA\Mail\Listener\OptionalIndicesListener;
use OCA\Mail\Listener\OutOfOfficeListener;
use OCA\Mail\Listener\SaveSentMessageListener;
use OCA\Mail\Listener\SpamReportListener;
use OCA\Mail\Listener\UserDeletedListener;
use OCA\Mail\Notification\Notifier;
Expand Down Expand Up @@ -132,7 +128,6 @@ public function register(IRegistrationContext $context): void {

$context->registerEventListener(AddMissingIndicesEvent::class, OptionalIndicesListener::class);
$context->registerEventListener(BeforeImapClientCreated::class, OauthTokenRefreshListener::class);
$context->registerEventListener(BeforeMessageSentEvent::class, AntiAbuseListener::class);
$context->registerEventListener(DraftSavedEvent::class, DeleteDraftListener::class);
$context->registerEventListener(DraftMessageCreatedEvent::class, DeleteDraftListener::class);
$context->registerEventListener(OutboxMessageCreatedEvent::class, DeleteDraftListener::class);
Expand All @@ -143,9 +138,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(MessageFlaggedEvent::class, MoveJunkListener::class);
$context->registerEventListener(MessageDeletedEvent::class, MessageCacheUpdaterListener::class);
$context->registerEventListener(MessageSentEvent::class, AddressCollectionListener::class);
$context->registerEventListener(MessageSentEvent::class, FlagRepliedMessageListener::class);
$context->registerEventListener(MessageSentEvent::class, InteractionListener::class);
$context->registerEventListener(MessageSentEvent::class, SaveSentMessageListener::class);
$context->registerEventListener(NewMessagesSynchronized::class, NewMessageClassificationListener::class);
$context->registerEventListener(NewMessagesSynchronized::class, MessageKnownSinceListener::class);
$context->registerEventListener(SynchronizationEvent::class, AccountSynchronizedThreadUpdaterListener::class);
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJob/QuotaJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function run($argument): void {
}

$quota = $this->mailManager->getQuota($account);
if($quota === null) {
if ($quota === null) {
$this->logger->debug('Could not get quota information for account <' . $account->getEmail() . '>', ['app' => 'mail']);
return;
}
Expand Down
22 changes: 3 additions & 19 deletions lib/Contracts/IMailTransmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace OCA\Mail\Contracts;

use OCA\Mail\Account;
use OCA\Mail\Db\Alias;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\Message;
Expand All @@ -37,27 +36,12 @@ interface IMailTransmission {
/**
* Send a new message or reply to an existing one
*
* @param NewMessageData $messageData
* @param string|null $repliedToMessageId
* @param Alias|null $alias
* @param Message|null $draft
*
* @throws SentMailboxNotSetException
* @throws ServiceException
*/
public function sendMessage(NewMessageData $messageData,
?string $repliedToMessageId = null,
?Alias $alias = null,
?Message $draft = null): void;

/**
* @param Account $account
* @param LocalMessage $message
* @throws ClientException
* @param LocalMessage $localMessage
* @throws SentMailboxNotSetException
* @throws ServiceException
* @return void
*/
public function sendLocalMessage(Account $account, LocalMessage $message): void;
public function sendMessage(Account $account, LocalMessage $localMessage): void;

/**
* @param Account $account
Expand Down
1 change: 0 additions & 1 deletion lib/Controller/DraftsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ public function update(int $id,
$message = $this->service->getMessage($id, $this->userId);
$account = $this->accountService->find($this->userId, $accountId);


$message->setType(LocalMessage::TYPE_DRAFT);
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
Expand Down
14 changes: 10 additions & 4 deletions lib/Controller/OutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function createFromDraft(DraftsService $draftsService, int $id, ?int $sen

$outboxMessage = $this->service->convertDraft($draftMessage, $sendAt);

return JsonResponse::success(
return JsonResponse::success(
$outboxMessage,
Http::STATUS_CREATED,
);
Expand Down Expand Up @@ -209,6 +209,9 @@ public function update(
?int $sendAt = null
): JsonResponse {
$message = $this->service->getMessage($id, $this->userId);
if ($message->getStatus() === LocalMessage::STATUS_PROCESSED) {
return JsonResponse::error('Cannot modify already sent message', Http::STATUS_FORBIDDEN, [$message]);
}
$account = $this->accountService->find($this->userId, $accountId);

$message->setAccountId($accountId);
Expand All @@ -217,7 +220,6 @@ public function update(
$message->setBody($body);
$message->setEditorBody($editorBody);
$message->setHtml($isHtml);
$message->setFailed($failed);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
$message->setSmimeSign($smimeSign);
Expand All @@ -244,8 +246,12 @@ public function send(int $id): JsonResponse {
$message = $this->service->getMessage($id, $this->userId);
$account = $this->accountService->find($this->userId, $message->getAccountId());

$this->service->sendMessage($message, $account);
return JsonResponse::success(
$message = $this->service->sendMessage($message, $account);

if($message->getStatus() !== LocalMessage::STATUS_PROCESSED) {
return JsonResponse::error('Could not send message', Http::STATUS_INTERNAL_SERVER_ERROR, [$message]);
}
return JsonResponse::success(
'Message sent', Http::STATUS_ACCEPTED
);
}
Expand Down
31 changes: 30 additions & 1 deletion lib/Db/LocalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,30 @@
* @method setSmimeCertificateId(?int $smimeCertificateId)
* @method bool|null getSmimeEncrypt()
* @method setSmimeEncrypt (bool $smimeEncryt)
* @method int|null getStatus();
* @method setStatus(?int $status);
* @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_RAW = 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_SMPT_SEND_FAIL = 10;
public const STATUS_IMAP_SENT_MAILBOX_FAIL = 11;
public const STATUS_PROCESSED = 12;
/**
* @var int
* @var int<1,12>
* @psalm-var self::TYPE_*
*/
protected $type;
Expand Down Expand Up @@ -116,6 +133,15 @@ class LocalMessage extends Entity implements JsonSerializable {
/** @var bool|null */
protected $smimeEncrypt;

/**
* @var int|null
* @psalm-var int-mask-of<self::STATUS_*>
*/
protected $status;

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

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

#[ReturnTypeWillChange]
Expand Down Expand Up @@ -168,6 +195,8 @@ public function jsonSerialize() {
'smimeCertificateId' => $this->getSmimeCertificateId(),
'smimeSign' => $this->getSmimeSign() === true,
'smimeEncrypt' => $this->getSmimeEncrypt() === true,
'status' => $this->getStatus(),
'raw' => $this->getRaw(),
];
}

Expand Down
7 changes: 2 additions & 5 deletions lib/Db/LocalMessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public function getAllForUser(string $userId, int $type = LocalMessage::TYPE_OUT
->join('a', $this->getTableName(), 'm', $qb->expr()->eq('m.account_id', 'a.id'))
->where(
$qb->expr()->eq('a.user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR),
$qb->expr()->eq('m.type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)
$qb->expr()->eq('m.type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
$qb->expr()->neq('m.status', $qb->createNamedParameter(LocalMessage::STATUS_PROCESSED, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)
);
$rows = $qb->executeQuery();

Expand Down Expand Up @@ -134,10 +135,6 @@ public function findDue(int $time, int $type = LocalMessage::TYPE_OUTGOING): arr
$qb->expr()->isNotNull('send_at'),
$qb->expr()->eq('type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
$qb->expr()->lte('send_at', $qb->createNamedParameter($time, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
$qb->expr()->orX(
$qb->expr()->isNull('failed'),
$qb->expr()->eq('failed', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL),
)
)
->orderBy('send_at', 'asc');
$messages = $this->findEntities($select);
Expand Down
95 changes: 0 additions & 95 deletions lib/Events/BeforeMessageSentEvent.php

This file was deleted.

8 changes: 4 additions & 4 deletions lib/Events/DraftSavedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class DraftSavedEvent extends Event {
/** @var Account */
private $account;

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

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

public function __construct(Account $account,
NewMessageData $newMessageData,
?Message $draft) {
?NewMessageData $newMessageData = null,
?Message $draft = null) {
parent::__construct();
$this->account = $account;
$this->newMessageData = $newMessageData;
Expand All @@ -53,7 +53,7 @@ public function getAccount(): Account {
return $this->account;
}

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

Expand Down
Loading

0 comments on commit 3e84000

Please sign in to comment.