Merge pull request #3158 from nextcloud/feature/add_quota_imap_mail

Display account quota
This commit is contained in:
Christoph Wurst 2020-06-05 09:30:09 +02:00 коммит произвёл GitHub
Родитель 23e4b121f1 8742cb289d
Коммит 7c703be522
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 263 добавлений и 13 удалений

Просмотреть файл

@ -54,6 +54,11 @@ return [
'url' => '/api/accounts/{accountId}/signature',
'verb' => 'PUT'
],
[
'name' => 'accounts#getQuota',
'url' => '/api/accounts/{accountId}/quota',
'verb' => 'GET'
],
[
'name' => 'folders#sync',
'url' => '/api/accounts/{accountId}/folders/{folderId}/sync',

Просмотреть файл

@ -29,6 +29,7 @@ use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Folder;
use OCA\Mail\IMAP\FolderStats;
use OCA\Mail\Model\IMAPMessage;
use OCA\Mail\Service\Quota;
interface IMailManager {
@ -126,4 +127,11 @@ interface IMailManager {
* @throws ServiceException
*/
public function flagMessage(Account $account, string $mailbox, int $uid, string $flag, bool $value): void;
/**
* @param Account $account
*
* @return Quota|null
*/
public function getQuota(Account $account): ?Quota;
}

Просмотреть файл

@ -30,9 +30,11 @@ declare(strict_types=1);
namespace OCA\Mail\Controller;
use Exception;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailTransmission;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Http\JsonResponse as MailJsonResponse;
use OCA\Mail\Model\NewMessageData;
use OCA\Mail\Model\RepliedMessageData;
use OCA\Mail\Service\AccountService;
@ -72,7 +74,20 @@ class AccountsController extends Controller {
/** @var SetupService */
private $setup;
public function __construct(string $appName, IRequest $request, AccountService $accountService, GroupsIntegration $groupsIntegration, $UserId, ILogger $logger, IL10N $l10n, AliasesService $aliasesService, IMailTransmission $mailTransmission, SetupService $setup
/** @var IMailManager */
private $mailManager;
public function __construct(string $appName,
IRequest $request,
AccountService $accountService,
GroupsIntegration $groupsIntegration,
$UserId,
ILogger $logger,
IL10N $l10n,
AliasesService $aliasesService,
IMailTransmission $mailTransmission,
SetupService $setup,
IMailManager $mailManager
) {
parent::__construct($appName, $request);
$this->accountService = $accountService;
@ -83,6 +98,7 @@ class AccountsController extends Controller {
$this->aliasesService = $aliasesService;
$this->mailTransmission = $mailTransmission;
$this->setup = $setup;
$this->mailManager = $mailManager;
}
/**
@ -393,4 +409,22 @@ class AccountsController extends Controller {
throw $ex;
}
}
/**
* @NoAdminRequired
*
* @param int $accountId
*
* @return JSONResponse
* @throws ClientException
*/
public function getQuota(int $accountId): JSONResponse {
$account = $this->accountService->find($this->currentUserId, $accountId);
$quota = $this->mailManager->getQuota($account);
if ($quota === null) {
return MailJsonResponse::fail([], Http::STATUS_NOT_IMPLEMENTED);
}
return MailJsonResponse::success($quota);
}
}

Просмотреть файл

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace OCA\Mail\Http;
use JsonSerializable;
use OCA\Mail\Exception\ClientException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse as Base;
@ -46,7 +47,13 @@ class JsonResponse extends Base {
$this->addHeader('x-mail-response', 'true');
}
public static function success(array $data = [],
/**
* @param array|JsonSerializable|bool|string $data
* @param int $status
*
* @return static
*/
public static function success($data = null,
int $status = Http::STATUS_OK): self {
return new self(
[
@ -57,7 +64,13 @@ class JsonResponse extends Base {
);
}
public static function fail(array $data = [],
/**
* @param array|JsonSerializable|bool|string $data
* @param int $status
*
* @return static
*/
public static function fail($data = null,
int $status = Http::STATUS_BAD_REQUEST): self {
return new self(
[

Просмотреть файл

@ -31,6 +31,7 @@ use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\mail\lib\Service\Quota;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\BackgroundJob\IJobList;
use function array_map;
@ -156,4 +157,7 @@ class AccountService {
$mailAccount->setSignature($signature);
$this->mapper->save($mailAccount);
}
public function getQuota(): Quota {
}
}

Просмотреть файл

@ -25,6 +25,7 @@ namespace OCA\Mail\Service;
use Horde_Imap_Client;
use Horde_Imap_Client_Exception;
use Horde_Imap_Client_Exception_NoSupportExtension;
use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\Mailbox;
@ -43,6 +44,8 @@ use OCA\Mail\IMAP\MessageMapper;
use OCA\Mail\Model\IMAPMessage;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\EventDispatcher\IEventDispatcher;
use function array_map;
use function array_values;
class MailManager implements IMailManager {
@ -315,4 +318,49 @@ class MailManager implements IMailManager {
)
);
}
/**
* @param Account $account
*
* @return Quota|null
* @see https://tools.ietf.org/html/rfc2087
*/
public function getQuota(Account $account): ?Quota {
$client = $this->imapClientFactory->getClient($account);
/**
* Get all the quotas roots of the user's mailboxes
*/
try {
$quotas = array_map(static function (Folder $mb) use ($client) {
return $client->getQuotaRoot($mb->getMailbox());
}, $this->folderMapper->getFolders($account, $client));
} catch (Horde_Imap_Client_Exception_NoSupportExtension $ex) {
return null;
}
/**
* Extract the 'storage' quota
*
* Falls back to 0/0 if this quota has no storage information
*
* @see https://tools.ietf.org/html/rfc2087#section-3
*/
$storageQuotas = array_map(function (array $root) {
return $root['storage'] ?? [
'usage' => 0,
'limit' => 0,
];
}, array_merge(...array_values($quotas)));
/**
* Deduplicate identical quota roots
*/
$storage = array_merge(...array_values($storageQuotas));
return new Quota(
1024 * (int)($storage['usage'] ?? 0),
1024 * (int)($storage['limit'] ?? 0)
);
}
}

58
lib/Service/Quota.php Normal file
Просмотреть файл

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
/**
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2020 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\Service;
use JsonSerializable;
class Quota implements JsonSerializable {
/** @var int */
private $usage;
/** @var int */
private $limit;
public function __construct(int $usage,
int $limit) {
$this->usage = $usage;
$this->limit = $limit;
}
public function getUsage(): int {
return $this->usage;
}
public function getLimit(): int {
return $this->limit;
}
public function jsonSerialize() {
return [
'usage' => $this->getUsage(),
'limit' => $this->getLimit(),
];
}
}

Просмотреть файл

@ -29,12 +29,16 @@
:title="account.emailAddress"
:to="firstFolderRoute"
:exact="true"
@update:menuOpen="onMenuToggle"
>
<!-- Color dot -->
<AppNavigationIconBullet v-if="bulletColor" slot="icon" :color="bulletColor" />
<!-- Actions -->
<template #actions>
<ActionText v-if="!account.isUnified" icon="icon-info" :title="t('mail', 'Quota')">
{{ quotaText }}
</ActionText>
<ActionRouter :to="settingsRoute" icon="icon-settings">
{{ t('mail', 'Edit account') }}
</ActionRouter>
@ -68,10 +72,13 @@ import ActionRouter from '@nextcloud/vue/dist/Components/ActionRouter'
import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
import ActionCheckbox from '@nextcloud/vue/dist/Components/ActionCheckbox'
import ActionInput from '@nextcloud/vue/dist/Components/ActionInput'
import ActionText from '@nextcloud/vue/dist/Components/ActionText'
import {formatFileSize} from '@nextcloud/files'
import {generateUrl} from '@nextcloud/router'
import {calculateAccountColor} from '../util/AccountColor'
import logger from '../logger'
import {fetchQuota} from '../service/AccountService'
export default {
name: 'NavigationAccount',
@ -82,6 +89,7 @@ export default {
ActionButton,
ActionCheckbox,
ActionInput,
ActionText,
},
props: {
account: {
@ -108,6 +116,7 @@ export default {
delete: false,
},
savingShowOnlySubscribed: false,
quota: undefined,
}
},
computed: {
@ -140,6 +149,19 @@ export default {
iconError() {
return this.account.error ? 'icon-error' : undefined
},
quotaText() {
if (this.quota === undefined) {
return t('mail', 'Loading …')
}
if (this.quota === false) {
return t('mail', 'Not supported by the server')
}
return t('mail', '{usage} of {limit} used', {
usage: formatFileSize(this.quota.usage),
limit: formatFileSize(this.quota.limit),
})
},
},
methods: {
createFolder(e) {
@ -218,6 +240,25 @@ export default {
throw error
})
},
onMenuToggle(open) {
if (open) {
console.debug('accounts menu opened, fetching quota')
this.fetchQuota()
}
},
async fetchQuota() {
const quota = await fetchQuota(this.account.id)
console.debug('quota fetched', {
quota,
})
if (quota === undefined) {
// Server does not support this
this.quota = false
} else {
this.quota = quota
}
},
},
}
</script>

Просмотреть файл

@ -1,5 +1,5 @@
import {generateUrl} from '@nextcloud/router'
import HttpClient from '@nextcloud/axios'
import axios from '@nextcloud/axios'
export const fixAccountId = (original) => {
return {
@ -11,7 +11,8 @@ export const fixAccountId = (original) => {
export const create = (data) => {
const url = generateUrl('/apps/mail/api/accounts')
return HttpClient.post(url, data)
return axios
.post(url, data)
.then((resp) => resp.data)
.then(fixAccountId)
.catch((e) => {
@ -28,7 +29,8 @@ export const patch = (account, data) => {
id: account.accountId,
})
return HttpClient.patch(url, data)
return axios
.patch(url, data)
.then((resp) => resp.data)
.then(fixAccountId)
}
@ -38,7 +40,8 @@ export const update = (data) => {
id: data.accountId,
})
return HttpClient.put(url, data)
return axios
.put(url, data)
.then((resp) => resp.data)
.then(fixAccountId)
}
@ -51,7 +54,8 @@ export const updateSignature = (account, signature) => {
signature,
}
return HttpClient.put(url, data)
return axios
.put(url, data)
.then((resp) => resp.data)
.then(fixAccountId)
}
@ -59,7 +63,7 @@ export const updateSignature = (account, signature) => {
export const fetchAll = () => {
const url = generateUrl('/apps/mail/api/accounts')
return HttpClient.get(url).then((resp) => resp.data.map(fixAccountId))
return axios.get(url).then((resp) => resp.data.map(fixAccountId))
}
export const fetch = (id) => {
@ -67,7 +71,26 @@ export const fetch = (id) => {
id,
})
return HttpClient.get(url).then((resp) => fixAccountId(resp.data))
return axios.get(url).then((resp) => fixAccountId(resp.data))
}
export const fetchQuota = async (id) => {
const url = generateUrl('/apps/mail/api/accounts/{id}/quota', {
id,
})
try {
const resp = await axios.get(url)
return resp.data.data
} catch (e) {
if ('response' in e && e.response.status === 501) {
// The server does not support quota
return false
}
// Something else
throw e
}
}
export const deleteAccount = (id) => {
@ -75,5 +98,5 @@ export const deleteAccount = (id) => {
id,
})
return HttpClient.delete(url).then((resp) => fixAccountId(resp.data))
return axios.delete(url).then((resp) => fixAccountId(resp.data))
}

Просмотреть файл

@ -28,6 +28,7 @@ use ChristophWurst\Nextcloud\Testing\TestCase;
use Exception;
use Horde_Exception;
use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailTransmission;
use OCA\Mail\Controller\AccountsController;
use OCA\Mail\Exception\ClientException;
@ -94,6 +95,9 @@ class AccountsControllerTest extends TestCase {
/** @var SetupService|MockObject */
private $setupService;
/** @var IMailManager|MockObject */
private $mailManager;
protected function setUp(): void {
parent::setUp();
@ -112,9 +116,21 @@ class AccountsControllerTest extends TestCase {
$this->aliasesService = $this->createMock(AliasesService::class);
$this->transmission = $this->createMock(IMailTransmission::class);
$this->setupService = $this->createMock(SetupService::class);
$this->mailManager = $this->createMock(IMailManager::class);
$this->controller = new AccountsController($this->appName, $this->request, $this->accountService, $this->groupsIntegration, $this->userId,
$this->logger, $this->l10n, $this->aliasesService, $this->transmission, $this->setupService);
$this->controller = new AccountsController(
$this->appName,
$this->request,
$this->accountService,
$this->groupsIntegration,
$this->userId,
$this->logger,
$this->l10n,
$this->aliasesService,
$this->transmission,
$this->setupService,
$this->mailManager
);
$this->account = $this->createMock(Account::class);
$this->accountId = 123;
}