Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use JsonSerializable;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Service\Quota;
use ReturnTypeWillChange;

class Account implements JsonSerializable {
Expand Down Expand Up @@ -62,36 +61,4 @@ public function getUserId() {
return $this->account->getUserId();
}

/**
* @return bool
*/
public function getDebug(): bool {
return $this->account->getDebug();
}

public function getImipCreate(): bool {
return $this->account->getImipCreate();
}

/**
* Set the quota percentage
* @param Quota $quota
* @return void
*/
public function calculateAndSetQuotaPercentage(Quota $quota): void {
if ($quota->getLimit() === 0) {
$this->account->setQuotaPercentage(0);
return;
}
$percentage = (int)round($quota->getUsage() / $quota->getLimit() * 100);
$this->account->setQuotaPercentage($percentage);
}

public function getQuotaPercentage(): ?int {
return $this->account->getQuotaPercentage();
}

public function getClassificationEnabled(): bool {
return $this->account->getClassificationEnabled();
}
}
13 changes: 9 additions & 4 deletions lib/BackgroundJob/QuotaJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ protected function run($argument): void {
$this->logger->debug('Could not get quota information for account <' . $account->getEmail() . '>', ['app' => 'mail']);
return;
}
$previous = $account->getMailAccount()->getQuotaPercentage();
$account->calculateAndSetQuotaPercentage($quota);
$this->accountService->update($account->getMailAccount());
$current = $account->getQuotaPercentage();
$mailAccount = $account->getMailAccount();
$previous = $mailAccount->getQuotaPercentage();
if ($quota->getLimit() === 0) {
$mailAccount->setQuotaPercentage(0);
} else {
$mailAccount->setQuotaPercentage((int)round($quota->getUsage() / $quota->getLimit() * 100));
}
$this->accountService->update($mailAccount);
$current = $mailAccount->getQuotaPercentage();

// Only notify if we've reached the rising edge
if ($previous < $current && $previous <= 90 && $current > 90) {
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJob/TrainImportanceClassifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function run($argument) {
return;
}

if (!$account->getClassificationEnabled()) {
if (!$account->getMailAccount()->getClassificationEnabled()) {
$this->logger->debug("classification is turned off for account $accountId");
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/TrainAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

if (!$force && !$account->getClassificationEnabled()) {
if (!$force && !$account->getMailAccount()->getClassificationEnabled()) {
$output->writeln("<info>classification is turned off for account $accountId</info>");
return 2;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/IMAP/IMAPClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getClient(Account $account, bool $useCache = true): Horde_Imap_C
'backend' => $this->hordeCacheFactory->newCache($account),
];
}
if ($account->getDebug() || $this->config->getSystemValueBool('app.mail.debug')) {
if ($account->getMailAccount()->getDebug() || $this->config->getSystemValueBool('app.mail.debug')) {
$fn = 'mail-' . $account->getUserId() . '-' . $account->getId() . '-imap.log';
$params['debug'] = $this->config->getSystemValue('datadirectory') . '/' . $fn;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/SMTP/SmtpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function create(Account $account): Horde_Mail_Transport {
$decryptedAccessToken,
);
}
if ($account->getDebug() || $this->config->getSystemValueBool('app.mail.debug')) {
if ($account->getMailAccount()->getDebug() || $this->config->getSystemValueBool('app.mail.debug')) {
$fn = 'mail-' . $account->getUserId() . '-' . $account->getId() . '-smtp.log';
$params['debug'] = $this->config->getSystemValue('datadirectory') . '/' . $fn;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/Classification/NewMessagesClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function classifyNewMessages(
Account $account,
Tag $importantTag,
): bool {
if (!$account->getClassificationEnabled()) {
if (!$account->getMailAccount()->getClassificationEnabled()) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Service/IMipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function process(): void {

$userId = $account->getUserId();
$recipient = $account->getEmail();
$imipCreate = $account->getImipCreate();
$imipCreate = $account->getMailAccount()->getImipCreate();
$systemVersion = $this->serverVersion->getMajorVersion();

foreach ($filteredMessages as $message) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Sieve/SieveClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getClient(Account $account): ManageSieve {
$password = $account->getMailAccount()->getInboundPassword();
}

if ($account->getDebug() || $this->config->getSystemValueBool('app.mail.debug')) {
if ($account->getMailAccount()->getDebug() || $this->config->getSystemValueBool('app.mail.debug')) {
$logFile = $this->config->getSystemValue('datadirectory') . '/mail-' . $account->getUserId() . '-' . $account->getId() . '-sieve.log';
} else {
$logFile = null;
Expand Down
70 changes: 15 additions & 55 deletions tests/Unit/BackgroundJob/QuotaJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,6 @@ public function testQuotaNoAuthentication(): void {
$this->serviceMock->getParameter('mailManager')
->expects(self::never())
->method('getQuota');
$account->expects(self::never())
->method('calculateAndSetQuotaPercentage')
->with($quotaDTO);
$account->expects(self::never())
->method('getQuotaPercentage')
->willReturn($newQuota);
$this->serviceMock->getParameter('accountService')
->expects(self::never())
->method('update')
Expand All @@ -162,12 +156,12 @@ public function testQuotaNoAuthentication(): void {
}

public function testQuotaTooLow(): void {
$oldQuota = 10;
$newQuota = 20;
$quotaDTO = new Quota(20, 100);
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => true,
]);
$mailAccount = new MailAccount();
$mailAccount->setId(123);
$mailAccount->setUserId('user123');
$mailAccount->setInboundPassword('password');
$mailAccount->setQuotaPercentage(10);
$account = $this->createConfiguredMock(Account::class, [
'getId' => 123,
'getUserId' => 'user123',
Expand All @@ -182,8 +176,6 @@ public function testQuotaTooLow(): void {
->method('findById')
->with(123)
->willReturn($account);
$mailAccount->expects(self::once())
->method('canAuthenticateImap');
$this->serviceMock->getParameter('userManager')
->expects(self::once())
->method('get')
Expand All @@ -196,15 +188,6 @@ public function testQuotaTooLow(): void {
->expects(self::once())
->method('getQuota')
->willReturn($quotaDTO);
$account->expects(self::once())
->method('calculateAndSetQuotaPercentage')
->with($quotaDTO);
$account->expects(self::exactly(3))
->method('getMailAccount')
->willReturn($mailAccount);
$account->expects(self::once())
->method('getQuotaPercentage')
->willReturn($newQuota);
$this->serviceMock->getParameter('accountService')
->expects(self::once())
->method('update')
Expand All @@ -219,12 +202,13 @@ public function testQuotaTooLow(): void {
}

public function testQuotaWithNotification(): void {
$oldQuota = 85;
$newQuota = 95;
$quotaDTO = new Quota(95, 100);
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => true,
]);
$mailAccount = new MailAccount();
$mailAccount->setId(123);
$mailAccount->setUserId('user123');
$mailAccount->setInboundPassword('password');
$mailAccount->setQuotaPercentage(85);
$account = $this->createConfiguredMock(Account::class, [
'getId' => 123,
'getUserId' => 'user123',
Expand All @@ -250,21 +234,6 @@ public function testQuotaWithNotification(): void {
->expects(self::once())
->method('getQuota')
->willReturn($quotaDTO);
$account->expects(self::once())
->method('calculateAndSetQuotaPercentage')
->with($quotaDTO);
$account->expects(self::exactly(3))
->method('getMailAccount')
->willReturn($mailAccount);
$account->expects(self::once())
->method('getQuotaPercentage')
->willReturn($newQuota);
$account->expects(self::exactly(2))
->method('getUserId')
->willReturn('user123');
$account->expects(self::exactly(2))
->method('getEmail')
->willReturn('user123@test.com');
$this->serviceMock->getParameter('accountService')
->expects(self::once())
->method('update')
Expand Down Expand Up @@ -325,12 +294,12 @@ public function testQuotaWithNotification(): void {
}

public function testQuotaZero(): void {
$oldQuota = 0;
$newQuota = 0;
$quotaDTO = new Quota(0, 0);
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => true,
]);
$mailAccount = new MailAccount();
$mailAccount->setId(123);
$mailAccount->setUserId('user123');
$mailAccount->setInboundPassword('password');
$mailAccount->setQuotaPercentage(0);
$account = $this->createConfiguredMock(Account::class, [
'getId' => 123,
'getUserId' => 'user123',
Expand All @@ -357,15 +326,6 @@ public function testQuotaZero(): void {
->expects(self::once())
->method('getQuota')
->willReturn($quotaDTO);
$account->expects(self::once())
->method('calculateAndSetQuotaPercentage')
->with($quotaDTO);
$account->expects(self::exactly(3))
->method('getMailAccount')
->willReturn($mailAccount);
$account->expects(self::once())
->method('getQuotaPercentage')
->willReturn($newQuota);
$this->serviceMock->getParameter('accountService')
->expects(self::once())
->method('update')
Expand Down