Skip to content

Commit

Permalink
Only show each thread once per message list
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed May 26, 2021
1 parent 548f90f commit f97fc51
Show file tree
Hide file tree
Showing 17 changed files with 728 additions and 120 deletions.
10 changes: 10 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@
'name' => 'sieve#updateActiveScript',
'url' => '/api/sieve/active/{id}',
'verb' => 'PUT'
],
[
'name' => 'thread#delete',
'url' => '/api/thread/{id}',
'verb' => 'DELETE'
],
[
'name' => 'thread#move',
'url' => '/api/thread/{id}',
'verb' => 'POST'
]
],
'resources' => [
Expand Down
4 changes: 2 additions & 2 deletions lib/Contracts/IMailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ public function getImapMessage(Account $account,

/**
* @param Account $account
* @param int $messageId database message ID
* @param string $threadRootId thread root id
*
* @return Message[]
*/
public function getThread(Account $account, int $messageId): array;
public function getThread(Account $account, string $threadRootId): array;

/**
* @param Account $sourceAccount
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public function getThread(int $id): JSONResponse {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

return new JSONResponse($this->mailManager->getThread($account, $id));
return new JSONResponse($this->mailManager->getThread($account, $message->getThreadRootId()));
}

/**
Expand Down
162 changes: 162 additions & 0 deletions lib/Controller/ThreadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

declare(strict_types=1);

/**
* @author Daniel Kesselberg <mail@danielkesselberg.de>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Mail\Controller;

use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\ThreadMapper;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
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;
use Psr\Log\LoggerInterface;

class ThreadController extends Controller {

/** @var string */
private $currentUserId;

/** @var LoggerInterface */
private $logger;

/** @var AccountService */
private $accountService;

/** @var IMailManager */
private $mailManager;

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

public function __construct(string $appName,
IRequest $request,
string $UserId,
LoggerInterface $logger,
AccountService $accountService,
IMailManager $mailManager,
ThreadMapper $threadMapper) {
parent::__construct($appName, $request);

$this->currentUserId = $UserId;
$this->logger = $logger;
$this->accountService = $accountService;
$this->mailManager = $mailManager;
$this->threadMapper = $threadMapper;
}

/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
*
* @return JSONResponse
* @throws ClientException
* @throws ServiceException
*/
public function delete(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

$mailAccount = $account->getMailAccount();
$messageInTrash = $mailbox->getId() === $mailAccount->getTrashMailboxId();

$messages = $this->threadMapper->findMessageUidsAndMailboxNamesByAccountAndThreadRoot(
$mailAccount,
$message->getThreadRootId(),
$messageInTrash
);

foreach ($messages as $message) {
$this->logger->debug('deleting message', [
'messageId' => $message['messageUid'],
]);

$this->mailManager->deleteMessage(
$account,
$message['mailboxName'],
$message['messageUid']
);
}

return new JSONResponse();
}

/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
* @param int $destMailboxId
*
* @return JSONResponse
* @throws ClientException
* @throws ServiceException
*/
public function move(int $id, int $destMailboxId): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$srcMailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$srcAccount = $this->accountService->find($this->currentUserId, $srcMailbox->getAccountId());
$dstMailbox = $this->mailManager->getMailbox($this->currentUserId, $destMailboxId);
$dstAccount = $this->accountService->find($this->currentUserId, $dstMailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

$mailAccount = $srcAccount->getMailAccount();
$messageInTrash = $srcMailbox->getId() === $mailAccount->getTrashMailboxId();

$messages = $this->threadMapper->findMessageUidsAndMailboxNamesByAccountAndThreadRoot(
$mailAccount,
$message->getThreadRootId(),
$messageInTrash
);

foreach ($messages as $message) {
$this->logger->debug('move message', [
'messageId' => $message['messageUid'],
'destMailboxId' => $destMailboxId
]);

$this->mailManager->moveMessage(
$srcAccount,
$message['mailboxName'],
$message['messageUid'],
$dstAccount,
$dstMailbox->getName()
);
}

return new JSONResponse();
}
}
Loading

0 comments on commit f97fc51

Please sign in to comment.