Skip to content

Commit

Permalink
fixup! Fix Lost update between last draft and sending a message
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <[email protected]>
  • Loading branch information
hamza221 committed Aug 15, 2023
1 parent ca4606f commit 5ac4d7c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/Controller/DraftsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function update(int $id,
?string $inReplyToMessageId = null,
?int $smimeCertificateId = null,
?int $sendAt = null): JsonResponse {
$message = $this->service->getMessage($id, $this->userId, LocalMessage::TYPE_DRAFT);
$message = $this->service->getMessage($id, $this->userId);
$account = $this->accountService->find($this->userId, $accountId);

($sendAt !== null)
Expand Down Expand Up @@ -208,7 +208,7 @@ public function update(int $id,
*/
#[TrapError]
public function destroy(int $id): JsonResponse {
$message = $this->service->getMessage($id, $this->userId, LocalMessage::TYPE_DRAFT);
$message = $this->service->getMessage($id, $this->userId);
$this->accountService->find($this->userId, $message->getAccountId());

$this->service->deleteMessage($this->userId, $message);
Expand All @@ -223,7 +223,7 @@ public function destroy(int $id): JsonResponse {
*/
#[TrapError]
public function move(int $id): JsonResponse {
$message = $this->service->getMessage($id, $this->userId, LocalMessage::TYPE_DRAFT);
$message = $this->service->getMessage($id, $this->userId);
$account = $this->accountService->find($this->userId, $message->getAccountId());

$this->service->sendMessage($message, $account);
Expand Down
5 changes: 2 additions & 3 deletions lib/Controller/OutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use OCA\Mail\Http\JsonResponse;
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\DraftsService;
use OCA\Mail\Service\OutboxService;
use OCA\Mail\Service\SmimeService;
use OCP\AppFramework\Controller;
Expand Down Expand Up @@ -156,8 +155,8 @@ public function create(
* @return JsonResponse
*/
#[TrapError]
public function createFromDraft(DraftsService $draftsService, int $id, int $sendAt = null): JsonResponse {
$draftMessage = $draftsService->getMessage($id, $this->userId, LocalMessage::TYPE_OUTGOING);
public function createFromDraft(int $id, int $sendAt = null): JsonResponse {
$draftMessage = $this->service->getMessage($id, $this->userId);
// Locate the account to check authorization
$this->accountService->find($this->userId, $draftMessage->getAccountId());

Expand Down
5 changes: 2 additions & 3 deletions lib/Service/DraftsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,11 @@ public function sendMessage(LocalMessage $message, Account $account): void {
*
* @param int $id
* @param string $userId
* @param int $type
* @return LocalMessage
* @throws DoesNotExistException
*/
public function getMessage(int $id, string $userId, int $type): LocalMessage {
return $this->mapper->findById($id, $userId, $type);
public function getMessage(int $id, string $userId): LocalMessage {
return $this->mapper->findById($id, $userId, LocalMessage::TYPE_DRAFT);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/Service/DraftServiceIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function testSaveAndGetMessage(): void {
$this->assertNotEmpty($message->getRecipients());
$this->assertEmpty($message->getAttachments());

$retrieved = $this->service->getMessage($message->getId(), $this->user->getUID(), LocalMessage::TYPE_DRAFT);
$retrieved = $this->service->getMessage($message->getId(), $this->user->getUID());
$this->assertNotEmpty($message->getRecipients());
$this->assertEmpty($message->getAttachments());

Expand All @@ -180,7 +180,7 @@ public function testSaveAndDeleteMessage(): void {
$this->service->deleteMessage($this->user->getUID(), $saved);

$this->expectException(DoesNotExistException::class);
$this->service->getMessage($message->getId(), $this->user->getUID(), LocalMessage::TYPE_DRAFT);
$this->service->getMessage($message->getId(), $this->user->getUID());
}

public function testSaveAndUpdateMessage(): void {
Expand Down Expand Up @@ -267,6 +267,6 @@ public function testSaveAndSendMessage(): void {
$this->service->sendMessage($saved, new Account($this->account));

$this->expectException(DoesNotExistException::class);
$this->service->getMessage($message->getId(), $this->user->getUID(), LocalMessage::TYPE_DRAFT);
$this->service->getMessage($message->getId(), $this->user->getUID());
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Service/DraftsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function testGetMessage(): void {
->with(1, $this->userId)
->willReturn($message);

$this->draftsService->getMessage(1, $this->userId, LocalMessage::TYPE_DRAFT);
$this->draftsService->getMessage(1, $this->userId);
}

public function testNoMessage(): void {
Expand All @@ -137,7 +137,7 @@ public function testNoMessage(): void {
->willThrowException(new DoesNotExistException('Could not fetch any messages'));

$this->expectException(DoesNotExistException::class);
$this->draftsService->getMessage(1, $this->userId, LocalMessage::TYPE_DRAFT);
$this->draftsService->getMessage(1, $this->userId);
}

public function testDeleteMessage(): void {
Expand Down

0 comments on commit 5ac4d7c

Please sign in to comment.