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

[stable3.3] fix: Fix sending local draft #8685

Merged
merged 2 commits into from
Aug 8, 2023
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
5 changes: 5 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@
'url' => '/api/outbox/{id}',
'verb' => 'POST'
],
[
'name' => 'outbox#createFromDraft',
'url' => '/api/outbox/from-draft/{id}',
'verb' => 'POST'
],
[
'name' => 'googleIntegration#configure',
'url' => '/api/integration/google',
Expand Down
20 changes: 20 additions & 0 deletions lib/Controller/OutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 @@ -149,6 +150,25 @@ public function create(
return JsonResponse::success($message, Http::STATUS_CREATED);
}

/**
* @NoAdminRequired
*
* @return JsonResponse
*/
#[TrapError]
public function createFromDraft(DraftsService $draftsService, int $id, int $sendAt = null): JsonResponse {
$draftMessage = $draftsService->getMessage($id, $this->userId);
// Locate the account to check authorization
$this->accountService->find($this->userId, $draftMessage->getAccountId());

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

return JsonResponse::success(
$outboxMessage,
Http::STATUS_CREATED,
);
}

/**
* @NoAdminRequired
*
Expand Down
6 changes: 4 additions & 2 deletions lib/Db/LocalMessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,18 @@ public function getAllForUser(string $userId, int $type = LocalMessage::TYPE_OUT
}

/**
* @param LocalMessage::TYPE_* $type
* @throws DoesNotExistException
*/
public function findById(int $id, string $userId): LocalMessage {
public function findById(int $id, string $userId, int $type): LocalMessage {
$qb = $this->db->getQueryBuilder();
$qb->select('m.*')
->from('mail_accounts', 'a')
->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.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)
$qb->expr()->eq('m.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
$qb->expr()->eq('type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
);
$entity = $this->findEntity($qb);
$entity->setAttachments($this->attachmentMapper->findByLocalMessageId($userId, $id));
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/DraftsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function sendMessage(LocalMessage $message, Account $account): void {
* @throws DoesNotExistException
*/
public function getMessage(int $id, string $userId): LocalMessage {
return $this->mapper->findById($id, $userId);
return $this->mapper->findById($id, $userId, LocalMessage::TYPE_DRAFT);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion lib/Service/OutboxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getMessages(string $userId): array {
* @throws DoesNotExistException
*/
public function getMessage(int $id, string $userId): LocalMessage {
return $this->mapper->findById($id, $userId);
return $this->mapper->findById($id, $userId, LocalMessage::TYPE_OUTGOING);
}

/**
Expand Down Expand Up @@ -274,4 +274,11 @@ public function flush(): void {
}
}
}

public function convertDraft(LocalMessage $draftMessage, int $sendAt): LocalMessage {
$outboxMessage = clone $draftMessage;
$outboxMessage->setType(LocalMessage::TYPE_OUTGOING);
$outboxMessage->setSendAt($sendAt);
return $this->mapper->update($outboxMessage);
}
}
15 changes: 12 additions & 3 deletions src/components/NewMessageModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default {
let idToReturn
const dataForServer = this.getDataForServer(data, true)
if (!id) {
const { id } = await saveDraft(data.account, dataForServer)
const { id } = await saveDraft(dataForServer)
dataForServer.id = id
await this.$store.dispatch('patchComposerData', { id, draftId: dataForServer.draftId })
this.canSaveDraft = true
Expand Down Expand Up @@ -307,17 +307,26 @@ export default {
}

if (!this.composerData.id) {
const { id } = await saveDraft(data.account, dataForServer)
// This is a new message
const { id } = await saveDraft(dataForServer)
dataForServer.id = id
await this.$store.dispatch('outbox/enqueueMessage', {
message: dataForServer,
})
} else {
} else if (this.composerData.type === 0) {
// This is an outbox message
dataForServer.id = this.composerData.id
await this.$store.dispatch('outbox/updateMessage', {
message: dataForServer,
id: this.composerData.id,
})
} else {
// This is a draft
dataForServer.id = this.composerData.id
await this.$store.dispatch('outbox/enqueueFromDraft', {
draftMessage: dataForServer,
id: this.composerData.id,
})
}

if (!data.sendAt || data.sendAt < Math.floor((now + UNDO_DELAY) / 1000)) {
Expand Down
6 changes: 2 additions & 4 deletions src/service/DraftService.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ import { generateUrl } from '@nextcloud/router'

import { convertAxiosError } from '../errors/convert'

export async function saveDraft(accountId, data) {
const url = generateUrl('/apps/mail/api/drafts', {
accountId,
})
export async function saveDraft(data) {
const url = generateUrl('/apps/mail/api/drafts')

try {
return (await axios.post(url, data)).data.data
Expand Down
10 changes: 10 additions & 0 deletions src/service/OutboxService.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ export async function enqueueMessage(message) {
const { data } = await axios.post(url, message)
return data.data
}

export async function enqueueMessageFromDraft(id, message) {
const url = generateUrl('/apps/mail/api/outbox/from-draft/{id}', {
id,
})

const { data } = await axios.post(url, message)
return data.data
}

export async function updateMessage(message, id) {
const url = generateUrl('/apps/mail/api/outbox/{id}', {
id,
Expand Down
13 changes: 13 additions & 0 deletions src/store/outbox/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ export default {
return message
},

async enqueueFromDraft({ commit }, { id, draftMessage }) {
const message = await OutboxService.enqueueMessageFromDraft(id, draftMessage)

commit('addMessage', { message })

// Future drafts/sends after an error should go through outbox logic
commit('convertComposerMessageToOutbox', { message }, {
root: true,
})

return message
},

async stopMessage({ commit }, { message }) {
commit('stopMessage', { message })
const updatedMessage = await OutboxService.updateMessage({
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Db/LocalMessageMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testFindAllForUser(): void {
* @depends testFindAllForUser
*/
public function testFindById(): void {
$row = $this->mapper->findById($this->entity->getId(), $this->account->getUserId());
$row = $this->mapper->findById($this->entity->getId(), $this->account->getUserId(), LocalMessage::TYPE_OUTGOING);

$this->assertEquals(LocalMessage::TYPE_OUTGOING, $row->getType());
$this->assertEquals(2, $row->getAliasId());
Expand All @@ -122,7 +122,7 @@ public function testFindById(): void {

public function testFindByIdNotFound(): void {
$this->expectException(DoesNotExistException::class);
$this->mapper->findById(1337, $this->account->getUserId());
$this->mapper->findById(1337, $this->account->getUserId(), LocalMessage::TYPE_DRAFT);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Db/RecipientMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function testUpdateRecipients(): void {
$results = $this->mapper->findByLocalMessageId($message->getId());
$this->assertCount(1, $results);

$message = $this->localMessageMapper->findById($message->getId(), $this->getTestAccountUserId());
$message = $this->localMessageMapper->findById($message->getId(), $this->getTestAccountUserId(), LocalMessage::TYPE_OUTGOING);

$pierre = new Recipient();
$pierre->setLabel('Pierre');
Expand Down
Loading