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 Oct 26, 2023
1 parent 5f4e5cb commit 824d280
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
8 changes: 7 additions & 1 deletion lib/Contracts/IMailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,14 @@ public function updateTag(int $id, string $displayName, string $color, string $u
*
* @throws ClientException
*/
public function deleteTag(int $id, string $userId, Account $account): Tag;
public function deleteTag(int $id, string $userId, array $accounts): Tag;

/**
* Delete message Tags and untagged messages on Imap
*
* @throws ClientException
*/
public function deleteTagForAccount(int $id, string $userId, Tag $tag, Account $account): void;
/**
* @param Account $srcAccount
* @param Mailbox $srcMailbox
Expand Down
5 changes: 3 additions & 2 deletions lib/Controller/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ public function update(int $id, string $displayName, string $color): JSONRespons
#[TrapError]
public function delete(int $id, int $accountId): JSONResponse {
try {
$account = $this->accountService->find($this->currentUserId, $accountId);
$accounts = $this->accountService->findByUserId($this->currentUserId);
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->mailManager->deleteTag($id, $this->currentUserId, $account);
$this->mailManager->deleteTag($id, $this->currentUserId, $accounts);

return new JSONResponse($id);
}

Expand Down
54 changes: 38 additions & 16 deletions lib/Service/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,18 @@ public function flagMessage(Account $account, string $mailbox, int $uid, string
)
);
}
public function tagMessageWithClient(Horde_Imap_Client_Socket $client, Account $account, Mailbox $mailbox, Message $message, Tag $tag, bool $value):void {
public function tagMessageWithClient(Horde_Imap_Client_Socket $client, Account $account, Mailbox $mailbox, array $messages, Tag $tag, bool $value):void {

if ($this->isPermflagsEnabled($client, $account, $mailbox->getName()) === true) {
$messageIds = array_map(static function (Message $message) {
return $message->getUid();
}, $messages);
try {
if ($value) {
// imap keywords and flags work the same way
$this->imapMessageMapper->addFlag($client, $mailbox, [$message->getUid()], $tag->getImapLabel());
$this->imapMessageMapper->addFlag($client, $mailbox, $messageIds, $tag->getImapLabel());
} else {
$this->imapMessageMapper->removeFlag($client, $mailbox, [$message->getUid()], $tag->getImapLabel());
$this->imapMessageMapper->removeFlag($client, $mailbox, $messageIds, $tag->getImapLabel());
}
} catch (Horde_Imap_Client_Exception $e) {
throw new ServiceException(
Expand All @@ -517,9 +520,13 @@ public function tagMessageWithClient(Horde_Imap_Client_Socket $client, Account $
);
}
if ($value) {
$this->tagMapper->tagMessage($tag, $message->getMessageId(), $account->getUserId());
foreach ($messages as $message) {
$this->tagMapper->tagMessage($tag, $message->getMessageId(), $account->getUserId());
}
} else {
$this->tagMapper->untagMessage($tag, $message->getMessageId());
foreach ($messages as $message) {
$this->tagMapper->untagMessage($tag, $message->getMessageId());
}
}
}
}
Expand Down Expand Up @@ -847,7 +854,21 @@ public function updateTag(int $id, string $displayName, string $color, string $u

return $this->tagMapper->update($tag);
}
public function deleteTag(int $id, string $userId, Account $account): Tag {

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

foreach ($accounts as $account) {
$this->deleteTagForAccount($id, $userId, $tag, $account);
}
return $this->tagMapper->delete($tag);
}

public function deleteTagForAccount(int $id, string $userId, Tag $tag, Account $account) :void {
try {
$messageTags = $this->messageTagsMapper->getMessagesByTag($id);
$messages = array_merge(... array_map(function ($messageTag) use ($account) {
Expand All @@ -857,25 +878,26 @@ public function deleteTag(int $id, string $userId, Account $account): Tag {
throw new ClientException('Messages not found', 0, $e);
}

try {
$tag = $this->tagMapper->getTagForUser($id, $userId);
} catch (DoesNotExistException $e) {
throw new ClientException('Tag not found', 0, $e);
}
$client = $this->imapClientFactory->getClient($account);

foreach ($messageTags as $messageTag) {
$this->messageTagsMapper->delete($messageTag);
}

$groupedMessages = [];
foreach ($messages as $message) {
$mailbox = $this->getMailbox($userId, $message->getMailboxId());
$this->tagMessageWithClient($client, $account, $mailbox, $message, $tag, false);
$mailboxId = $message->getMailboxId();
if (array_key_exists($mailboxId, $groupedMessages)) {
$groupedMessages[$mailboxId][] = $message;
} else {
$groupedMessages[$mailboxId] = [$message];
}
}

foreach ($groupedMessages as $mailboxId => $messages) {
$mailbox = $this->getMailbox($userId, $mailboxId);
$this->tagMessageWithClient($client, $account, $mailbox, $messages, $tag, false);
}
$client->logout();
return $this->tagMapper->delete($tag);

}
public function moveThread(Account $srcAccount, Mailbox $srcMailbox, Account $dstAccount, Mailbox $dstMailbox, string $threadRootId): array {
$mailAccount = $srcAccount->getMailAccount();
Expand Down

0 comments on commit 824d280

Please sign in to comment.