Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2019-04-19 11:13:35 +0300
committerChristoph Wurst <christoph@winzerhof-wurst.at>2019-04-19 11:13:35 +0300
commit79198f8375f26425b2b9395a7014c3ffc6881583 (patch)
tree899699ba6b6a2473405ec665c267c0ed26ddc8c1 /lib
parent36ed424ed6aae5d67829aacc04edf48d54e0d501 (diff)
Make it possible to create subfolders and show folder stats
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/Contracts/IMailManager.php10
-rw-r--r--lib/Controller/FoldersController.php23
-rw-r--r--lib/IMAP/FolderMapper.php21
-rw-r--r--lib/IMAP/FolderStats.php50
-rw-r--r--lib/Service/MailManager.php13
5 files changed, 115 insertions, 2 deletions
diff --git a/lib/Contracts/IMailManager.php b/lib/Contracts/IMailManager.php
index 84341cd0b..c94e7cb81 100644
--- a/lib/Contracts/IMailManager.php
+++ b/lib/Contracts/IMailManager.php
@@ -25,6 +25,7 @@ namespace OCA\Mail\Contracts;
use OCA\Mail\Account;
use OCA\Mail\Folder;
+use OCA\Mail\IMAP\FolderStats;
use OCA\Mail\IMAP\Sync\Request as SyncRequest;
use OCA\Mail\IMAP\Sync\Response as SyncResponse;
@@ -45,6 +46,14 @@ interface IMailManager {
public function createFolder(Account $account, string $name): Folder;
/**
+ * @param Account $account
+ * @param string $folderId
+ *
+ * @return FolderStats
+ */
+ public function getFolderStats(Account $account, string $folderId): FolderStats;
+
+ /**
* @param Account
* @param SyncRequest $syncRequest
* @return SyncResponse
@@ -60,4 +69,5 @@ interface IMailManager {
*/
public function moveMessage(Account $sourceAccount, string $sourceFolderId, int $messageId,
Account $destinationAccount, string $destFolderId);
+
}
diff --git a/lib/Controller/FoldersController.php b/lib/Controller/FoldersController.php
index cd83975cc..baaf9e48c 100644
--- a/lib/Controller/FoldersController.php
+++ b/lib/Controller/FoldersController.php
@@ -25,6 +25,8 @@ declare(strict_types=1);
namespace OCA\Mail\Controller;
+use function base64_decode;
+use function is_array;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Exception\NotImplemented;
use OCA\Mail\Http\JSONResponse;
@@ -105,6 +107,27 @@ class FoldersController extends Controller {
/**
* @NoAdminRequired
* @TrapError
+ *
+ * @param int $accountId
+ * @param string $folderId
+ *
+ * @return JSONResponse
+ */
+ public function stats(int $accountId, string $folderId): JSONResponse {
+ $account = $this->accountService->find($this->currentUserId, $accountId);
+
+ if (empty($accountId) || empty($folderId)) {
+ return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
+ }
+
+ $stats = $this->mailManager->getFolderStats($account, base64_decode($folderId));
+
+ return new JSONResponse($stats);
+ }
+
+ /**
+ * @NoAdminRequired
+ * @TrapError
*/
public function show() {
throw new NotImplemented();
diff --git a/lib/IMAP/FolderMapper.php b/lib/IMAP/FolderMapper.php
index cc784c0e8..b5821b412 100644
--- a/lib/IMAP/FolderMapper.php
+++ b/lib/IMAP/FolderMapper.php
@@ -23,15 +23,16 @@ declare(strict_types=1);
namespace OCA\Mail\IMAP;
+use function array_filter;
+use function array_map;
+use function reset;
use Horde_Imap_Client;
use Horde_Imap_Client_Exception;
-use Horde_Imap_Client_Mailbox;
use Horde_Imap_Client_Socket;
use OCA\Mail\Account;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Folder;
use OCA\Mail\SearchFolder;
-use function reset;
class FolderMapper {
@@ -128,6 +129,22 @@ class FolderMapper {
}
/**
+ * @param Horde_Imap_Client_Socket $client
+ * @param string $mailbox
+ *
+ * @throws Horde_Imap_Client_Exception
+ */
+ public function getFoldersStatusAsObject(Horde_Imap_Client_Socket $client,
+ string $mailbox) {
+ $status = $client->status($mailbox);
+
+ return new FolderStats(
+ $status['messages'],
+ $status['unseen']
+ );
+ }
+
+ /**
* @param Folder[] $folders
*/
public function detectFolderSpecialUse(array $folders) {
diff --git a/lib/IMAP/FolderStats.php b/lib/IMAP/FolderStats.php
new file mode 100644
index 000000000..9b41b91d8
--- /dev/null
+++ b/lib/IMAP/FolderStats.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @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\IMAP;
+
+use JsonSerializable;
+
+class FolderStats implements JsonSerializable {
+
+ /** @var int */
+ private $total;
+
+ /** @var int */
+ private $unread;
+
+ public function __construct(int $total, int $unread) {
+
+ $this->total = $total;
+ $this->unread = $unread;
+ }
+
+ public function jsonSerialize() {
+ return [
+ 'total' => $this->total,
+ 'unread' => $this->unread,
+ ];
+ }
+}
diff --git a/lib/Service/MailManager.php b/lib/Service/MailManager.php
index 87ff4ce44..a2346e738 100644
--- a/lib/Service/MailManager.php
+++ b/lib/Service/MailManager.php
@@ -28,6 +28,7 @@ use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Folder;
use OCA\Mail\IMAP\FolderMapper;
+use OCA\Mail\IMAP\FolderStats;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\IMAP\MessageMapper;
use OCA\Mail\IMAP\Sync\Request;
@@ -88,6 +89,18 @@ class MailManager implements IMailManager {
/**
* @param Account $account
+ * @param string $folderId
+ *
+ * @return FolderStats
+ */
+ public function getFolderStats(Account $account, string $folderId): FolderStats {
+ $client = $this->imapClientFactory->getClient($account);
+
+ return $this->folderMapper->getFoldersStatusAsObject($client, $folderId);
+ }
+
+ /**
+ * @param Account $account
* @param Request $syncRequest
* @return Response
*/