Skip to content

Commit

Permalink
refactor: Consolidate account classes
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Sep 17, 2024
1 parent b1c5609 commit 880e2f8
Show file tree
Hide file tree
Showing 101 changed files with 1,621 additions and 1,235 deletions.
59 changes: 1 addition & 58 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,14 @@
*/
namespace OCA\Mail;

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

class Account implements JsonSerializable {
class Account {
public function __construct(private MailAccount $account) {
}

public function getMailAccount(): MailAccount {
return $this->account;
}

/**
* @return int
*/
public function getId() {
return $this->account->getId();
}

/**
* @return string
*/
public function getName() {
return $this->account->getName();
}

/**
* @return string
*/
public function getEMailAddress() {
return $this->account->getEmail();
}

#[ReturnTypeWillChange]
public function jsonSerialize() {
return $this->account->toJson();
}

public function getEmail(): string {
return $this->account->getEmail();
}

/**
* @return string
*/
public function getUserId() {
return $this->account->getUserId();
}

/**
* 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();
}
}
13 changes: 5 additions & 8 deletions lib/BackgroundJob/MigrateImportantJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Account;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Db\MailboxMapper;

Expand Down Expand Up @@ -65,27 +64,25 @@ public function run($argument) {
try {
$mailAccount = $this->mailAccountMapper->findById($accountId);
} catch (DoesNotExistException $e) {
$this->logger->debug('Could not find account <' . $accountId . '>');
$this->logger->debug('Could not find mailAccount <' . $accountId . '>');
return;
}

$account = new Account($mailAccount);
$client = $this->imapClientFactory->getClient($account);

$client = $this->imapClientFactory->getClient($mailAccount);
try {
if ($this->mailManager->isPermflagsEnabled($client, $account, $mailbox->getName()) === false) {
if ($this->mailManager->isPermflagsEnabled($client, $mailAccount, $mailbox->getName()) === false) {
$this->logger->debug('Permflags not enabled for <' . $accountId . '>');
return;
}

try {
$this->migration->migrateImportantOnImap($client, $account, $mailbox);
$this->migration->migrateImportantOnImap($client, $mailbox);
} catch (ServiceException $e) {
$this->logger->debug('Could not flag messages on IMAP for mailbox <' . $mailboxId . '>.');
}

try {
$this->migration->migrateImportantFromDb($client, $account, $mailbox);
$this->migration->migrateImportantFromDb($client, $mailbox);
} catch (ServiceException $e) {
$this->logger->debug('Could not flag messages from DB on IMAP for mailbox <' . $mailboxId . '>.');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJob/PreviewEnhancementProcessingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function run($argument) {
return;
}

if (!$account->getMailAccount()->canAuthenticateImap()) {
if (!$account->canAuthenticateImap()) {
$this->logger->info('Ignoring preprocessing job for provisioned account as athentication on IMAP not possible');
return;
}
Expand Down
16 changes: 10 additions & 6 deletions lib/BackgroundJob/QuotaJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Service\AccountService;
use OCP\AppFramework\Db\DoesNotExistException;
Expand All @@ -18,6 +17,7 @@
use OCP\IUserManager;
use OCP\Notification\IManager;
use Psr\Log\LoggerInterface;
use function round;
use function sprintf;

class QuotaJob extends TimedJob {
Expand Down Expand Up @@ -54,15 +54,14 @@ public function __construct(ITimeFactory $time,
protected function run($argument): void {
$accountId = (int)$argument['accountId'];
try {
/** @var Account $account */
$account = $this->accountService->findById($accountId);
} catch (DoesNotExistException $e) {
$this->logger->debug('Could not find account <' . $accountId . '> removing from jobs');
$this->jobList->remove(self::class, $argument);
return;
}

if(!$account->getMailAccount()->canAuthenticateImap()) {
if(!$account->canAuthenticateImap()) {
$this->logger->debug('No authentication on IMAP possible, skipping quota job');
return;
}
Expand All @@ -82,9 +81,14 @@ 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());
$previous = $account->getQuotaPercentage();
if ($quota->getLimit() === 0) {
$account->setQuotaPercentage(0);
return;
}
$percentage = (int)round($quota->getUsage() / $quota->getLimit() * 100);
$account->setQuotaPercentage($percentage);
$this->accountService->update($account);
$current = $account->getQuotaPercentage();

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

if(!$account->getMailAccount()->canAuthenticateImap()) {
if(!$account->canAuthenticateImap()) {
$this->logger->debug('No authentication on IMAP possible, skipping background sync job');
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJob/TrainImportanceClassifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function run($argument) {
return;
}

if(!$account->getMailAccount()->canAuthenticateImap()) {
if(!$account->canAuthenticateImap()) {
$this->logger->debug('Cron importance classifier training not possible: no authentication on IMAP possible');
return;
}
Expand Down
12 changes: 5 additions & 7 deletions lib/BackgroundJob/TrashRetentionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Db\MessageMapper;
Expand Down Expand Up @@ -46,9 +46,7 @@ public function __construct(
public function run($argument) {
$accounts = $this->accountMapper->getAllAccounts();
foreach ($accounts as $account) {
$account = new Account($account);

$retentionDays = $account->getMailAccount()->getTrashRetentionDays();
$retentionDays = $account->getTrashRetentionDays();
if ($retentionDays === null || $retentionDays <= 0) {
continue;
}
Expand All @@ -62,7 +60,7 @@ public function run($argument) {
'exception' => $e,
'userId' => $account->getUserId(),
'accountId' => $account->getId(),
'trashMailboxId' => $account->getMailAccount()->getTrashMailboxId(),
'trashMailboxId' => $account->getTrashMailboxId(),
]);
}
}
Expand All @@ -73,8 +71,8 @@ public function run($argument) {
* @throws ClientException
* @throws ServiceException
*/
private function cleanTrash(Account $account, int $retentionSeconds): void {
$trashMailboxId = $account->getMailAccount()->getTrashMailboxId();
private function cleanTrash(MailAccount $account, int $retentionSeconds): void {
$trashMailboxId = $account->getTrashMailboxId();
if ($trashMailboxId === null) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Command/DeleteAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OCA\Mail\Command;

use OCA\Mail\Account;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Service\AccountService;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -53,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
$output->writeLn('<info>Found account with email: ' . $account->getEmail() . '</info>');

if (!is_null($account->getMailAccount()->getProvisioningId())) {
if (!is_null($account->getProvisioningId())) {
$output->writeLn('<error>This is a provisioned account which can not be deleted from CLI. Use the Provisioning UI instead.</error>');
return 2;
}
Expand All @@ -63,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

private function delete(Account $account, OutputInterface $output): void {
private function delete(MailAccount $account, OutputInterface $output): void {
$id = $account->getId();
try {
$this->accountService->deleteByAccountId($account->getId());
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/DiagnoseAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

if ($account->getMailAccount()->getInboundPassword() === null) {
if ($account->getInboundPassword() === null) {
$output->writeln('<error>No IMAP passwort set. The user might have to log into their account to set it.</error>');
}
$imapClient = $this->clientFactory->getClient($account);
Expand Down
Loading

0 comments on commit 880e2f8

Please sign in to comment.