Skip to content

Commit

Permalink
fixup! feat: delete tags
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <[email protected]>
  • Loading branch information
hamza221 committed Sep 18, 2023
1 parent b3eccf8 commit 5381d7d
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 14 deletions.
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
],
[
'name' => 'tags#delete',
'url' => '/api/tags/{id}',
'url' => '/api/tags/{id}/tags/{accountId}',
'verb' => 'DELETE'
],
[
Expand Down
2 changes: 1 addition & 1 deletion lib/Contracts/IMailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public function updateTag(int $id, string $displayName, string $color, string $u
* @return int

Check failure on line 326 in lib/Contracts/IMailManager.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

MismatchingDocblockReturnType

lib/Contracts/IMailManager.php:326:13: MismatchingDocblockReturnType: Docblock has incorrect return type 'int', should be 'OCA\Mail\Db\Tag' (see https://psalm.dev/142)

Check failure on line 326 in lib/Contracts/IMailManager.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

MismatchingDocblockReturnType

lib/Contracts/IMailManager.php:326:13: MismatchingDocblockReturnType: Docblock has incorrect return type 'int', should be 'OCA\Mail\Db\Tag' (see https://psalm.dev/142)

Check failure on line 326 in lib/Contracts/IMailManager.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

MismatchingDocblockReturnType

lib/Contracts/IMailManager.php:326:13: MismatchingDocblockReturnType: Docblock has incorrect return type 'int', should be 'OCA\Mail\Db\Tag' (see https://psalm.dev/142)
* @throws ClientException if the given tag does not exist
*/
public function deleteTag(int $id, string $userId): Tag;
public function deleteTag(int $id, string $userId, array $messages, Account $account): Tag;

/**
* @param Account $srcAccount
Expand Down
20 changes: 16 additions & 4 deletions lib/Controller/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,28 @@
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\AccountService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

class TagsController extends Controller {
private string $currentUserId;
private IMailManager $mailManager;

private AccountService $accountService;


public function __construct(IRequest $request,
string $UserId,
IMailManager $mailManager
IMailManager $mailManager, AccountService $accountService,
) {
parent::__construct(Application::APP_ID, $request);
$this->currentUserId = $UserId;
$this->mailManager = $mailManager;
$this->accountService = $accountService;
}

/**
Expand Down Expand Up @@ -89,9 +96,14 @@ public function update(int $id, string $displayName, string $color): JSONRespons
* @throws ClientException
*/
#[TrapError]
public function delete(int $id): JSONResponse {

$this->mailManager->deleteTag($id, $this->currentUserId);
public function delete(int $id, int $accountId): JSONResponse {
try {
$account = $this->accountService->find($this->currentUserId, $accountId);
$messages = $this->mailManager->getMessagesByTag($this->currentUserId, $id, $account);

Check failure on line 102 in lib/Controller/TagsController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

UndefinedInterfaceMethod

lib/Controller/TagsController.php:102:36: UndefinedInterfaceMethod: Method OCA\Mail\Contracts\IMailManager::getMessagesByTag does not exist (see https://psalm.dev/181)

Check failure on line 102 in lib/Controller/TagsController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

UndefinedInterfaceMethod

lib/Controller/TagsController.php:102:36: UndefinedInterfaceMethod: Method OCA\Mail\Contracts\IMailManager::getMessagesByTag does not exist (see https://psalm.dev/181)

Check failure on line 102 in lib/Controller/TagsController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

UndefinedInterfaceMethod

lib/Controller/TagsController.php:102:36: UndefinedInterfaceMethod: Method OCA\Mail\Contracts\IMailManager::getMessagesByTag does not exist (see https://psalm.dev/181)
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->mailManager->deleteTag($id, $this->currentUserId, $messages, $account);
return new JSONResponse($id);

Check failure on line 107 in lib/Controller/TagsController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidArgument

lib/Controller/TagsController.php:107:27: InvalidArgument: Argument 1 of OCP\AppFramework\Http\JSONResponse::__construct expects array<array-key, mixed>|object, but int provided (see https://psalm.dev/004)

Check failure on line 107 in lib/Controller/TagsController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

InvalidArgument

lib/Controller/TagsController.php:107:27: InvalidArgument: Argument 1 of OCP\AppFramework\Http\JSONResponse::__construct expects array<array-key, mixed>|object, but int provided (see https://psalm.dev/004)

Check failure on line 107 in lib/Controller/TagsController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

InvalidArgument

lib/Controller/TagsController.php:107:27: InvalidArgument: Argument 1 of OCP\AppFramework\Http\JSONResponse::__construct expects array<array-key, mixed>|object, but int provided (see https://psalm.dev/004)
}

Expand Down
51 changes: 51 additions & 0 deletions lib/Db/MessageTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Hamza Mahjoubi <[email protected]>
*
* @author 2023 Hamza Mahjoubi <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Db;

use JsonSerializable;
use OCP\AppFramework\Db\Entity;
use ReturnTypeWillChange;

/**
* @method string getImapMessageId()
* @method void setImapMessageId(string $imapMessageId)
* @method int getTagId()
* @method void setTagId(int $tagId)
*/
class MessageTags extends Entity implements JsonSerializable {
protected $imapMessageId;
protected $tagId;


#[ReturnTypeWillChange]
public function jsonSerialize() {
return [
'id' => $this->getId(),
'imapMessageId' => $this->getImapMessageId(),
'tagId' => $this->getTagId(),
];
}
}
57 changes: 57 additions & 0 deletions lib/Db/MessageTagsMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/**
* @copyright 2021 Anna Larch <[email protected]>
*
* @author 2021 Anna Larch <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Db;

use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IL10N;

/**
* @template-extends QBMapper<MessageTags>
*/
class MessageTagsMapper extends QBMapper {
/** @var IL10N */
private $l10n;

public function __construct(IDBConnection $db,
IL10N $l10n) {
parent::__construct($db, 'mail_message_tags');
$this->l10n = $l10n;
}

public function getMessagesByTag(int $id): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('tag_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)),

);
return $this->findEntities($qb);
}

}
25 changes: 23 additions & 2 deletions lib/Service/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Db\Message;
use OCA\Mail\Db\MessageMapper as DbMessageMapper;
use OCA\Mail\Db\MessageTagsMapper;
use OCA\Mail\Db\Tag;
use OCA\Mail\Db\TagMapper;
use OCA\Mail\Db\ThreadMapper;
Expand Down Expand Up @@ -99,6 +100,9 @@ class MailManager implements IMailManager {
/** @var TagMapper */
private $tagMapper;

/** @var MessageTagsMapper */
private $messageTagsMapper;

/** @var ThreadMapper */
private $threadMapper;

Expand All @@ -111,6 +115,7 @@ public function __construct(IMAPClientFactory $imapClientFactory,
IEventDispatcher $eventDispatcher,
LoggerInterface $logger,
TagMapper $tagMapper,
MessageTagsMapper $messageTagsMapper,
ThreadMapper $threadMapper) {
$this->imapClientFactory = $imapClientFactory;
$this->mailboxMapper = $mailboxMapper;
Expand All @@ -121,6 +126,7 @@ public function __construct(IMAPClientFactory $imapClientFactory,
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
$this->tagMapper = $tagMapper;
$this->messageTagsMapper = $messageTagsMapper;
$this->threadMapper = $threadMapper;
}

Expand Down Expand Up @@ -247,6 +253,18 @@ public function getMessage(string $uid, int $id): Message {
return $this->dbMessageMapper->findByUserId($uid, $id);
}

public function getMessagesByTag(string $uid, int $tagId, Account $account): array {
try {
$messageTags = $this->messageTagsMapper->getMessagesByTag($tagId);
$messages = array_map(function ($messageTag) use ($uid, $account) {
return $this->getByMessageId($account, $messageTag->getImapMessageId());
}, $messageTags);
} catch (DoesNotExistException $e) {
throw new ClientException('Messages not found', 0, $e);
}
return array_merge(...$messages);

Check failure on line 265 in lib/Service/MailManager.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

NamedArgumentNotAllowed

lib/Service/MailManager.php:265:25: NamedArgumentNotAllowed: Method array_merge called with named unpacked array array<array-key, array<array-key, OCA\Mail\Db\Message>> (array with string keys) (see https://psalm.dev/268)

Check failure on line 265 in lib/Service/MailManager.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

NamedArgumentNotAllowed

lib/Service/MailManager.php:265:25: NamedArgumentNotAllowed: Method array_merge called with named unpacked array array<array-key, array<array-key, OCA\Mail\Db\Message>> (array with string keys) (see https://psalm.dev/268)

Check failure on line 265 in lib/Service/MailManager.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

NamedArgumentNotAllowed

lib/Service/MailManager.php:265:25: NamedArgumentNotAllowed: Method array_merge called with named unpacked array array<array-key, array<array-key, OCA\Mail\Db\Message>> (array with string keys) (see https://psalm.dev/268)
}

/**
* @param Horde_Imap_Client_Socket $client
* @param Account $account
Expand Down Expand Up @@ -818,13 +836,16 @@ public function updateTag(int $id, string $displayName, string $color, string $u

return $this->tagMapper->update($tag);
}
public function deleteTag(int $id, string $userId): Tag {
public function deleteTag(int $id, string $userId, array $messages, Account $account): Tag {
try {
$tag = $this->tagMapper->getTagForUser($id, $userId);
} catch (DoesNotExistException $e) {
throw new ClientException('Tag not found', 0, $e);
}

foreach ($messages as $message) {
$mailbox = $this->getMailbox($userId, $message->getMailboxId());
$this->tagMessage($account, $mailbox->getName(), $message, $tag, false);
}
return $this->tagMapper->delete($tag);

}
Expand Down
5 changes: 5 additions & 0 deletions src/components/DeleteTagModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export default {
type: Object,
required: true,
},
accountId: {
type: Number,
required: true,
},
},
data() {
return {
Expand All @@ -62,6 +66,7 @@ export default {
try {
await this.$store.dispatch('deleteTag', {
tag: this.tag,
accountId: this.accountId,
})
showSuccess(t('mail', 'Tag: {name} deleted', { name: this.tag.displayName }))
this.$emit('close')
Expand Down
5 changes: 4 additions & 1 deletion src/components/TagModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@
</ActionText>
</div>
</div>
<DeleteTagModal v-if="deleteTagModal" :tag="tagToDelete" @close="closeDeleteModal" />
<DeleteTagModal v-if="deleteTagModal"
:tag="tagToDelete"
:account-id="envelopes[0].accountId"
@close="closeDeleteModal" />
</Modal>
</template>

Expand Down
6 changes: 3 additions & 3 deletions src/service/MessageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ export async function updateEnvelopeTag(id, displayName, color) {
await axios.put(url, { displayName, color })
}

export async function deleteTag(id) {
const url = generateUrl('/apps/mail/api/tags/{id}', {
id,
export async function deleteTag(id, accountId) {
const url = generateUrl('/apps/mail/api/tags/{id}/tags/{accountId}', {
id, accountId,
})

await axios.delete(url)
Expand Down
4 changes: 2 additions & 2 deletions src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1270,9 +1270,9 @@ export default {
logger.debug('tag updated', { tag, displayName, color })
})
},
async deleteTag({ commit }, { tag }) {
async deleteTag({ commit }, { tag, accountId }) {
return handleHttpAuthErrors(commit, async () => {
await deleteTag(tag.id)
await deleteTag(tag.id, accountId)
commit('deleteTag', { id: tag.id })
logger.debug('tag updated', { tag })
})
Expand Down

0 comments on commit 5381d7d

Please sign in to comment.