Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Inline account class delegations #10164

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
60 changes: 1 addition & 59 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,13 @@
*/
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();
}
}
6 changes: 3 additions & 3 deletions lib/BackgroundJob/PreviewEnhancementProcessingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public function run($argument) {
return;
}

$user = $this->userManager->get($account->getUserId());
$user = $this->userManager->get($account->getMailAccount()->getUserId());
if ($user === null || !$user->isEnabled()) {
$this->logger->debug(sprintf(
'Account %d of user %s could not be found or was disabled, skipping preprocessing of messages',
$account->getId(),
$account->getUserId()
$account->getMailAccount()->getId(),
$account->getMailAccount()->getUserId()
));
return;
}
Expand Down
24 changes: 15 additions & 9 deletions lib/BackgroundJob/QuotaJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -67,38 +68,43 @@ protected function run($argument): void {
return;
}

$user = $this->userManager->get($account->getUserId());
$user = $this->userManager->get($account->getMailAccount()->getUserId());
if ($user === null || !$user->isEnabled()) {
$this->logger->debug(sprintf(
'Account %d of user %s could not be found or was disabled, skipping quota query',
$account->getId(),
$account->getUserId()
$account->getMailAccount()->getId(),
$account->getMailAccount()->getUserId()
));
return;
}

$quota = $this->mailManager->getQuota($account);
if ($quota === null) {
$this->logger->debug('Could not get quota information for account <' . $account->getEmail() . '>', ['app' => 'mail']);
$this->logger->debug('Could not get quota information for account <' . $account->getMailAccount()->getEmail() . '>', ['app' => 'mail']);
return;
}
$previous = $account->getMailAccount()->getQuotaPercentage();
$account->calculateAndSetQuotaPercentage($quota);
if ($quota->getLimit() === 0) {
$account->getMailAccount()->setQuotaPercentage(0);
} else {
$percentage = (int)round($quota->getUsage() / $quota->getLimit() * 100);
$account->getMailAccount()->setQuotaPercentage($percentage);
}
$this->accountService->update($account->getMailAccount());
$current = $account->getQuotaPercentage();
$current = $account->getMailAccount()->getQuotaPercentage();

// Only notify if we've reached the rising edge
if ($previous < $current && $previous <= 90 && $current > 90) {
$this->logger->debug('New quota information for <' . $account->getEmail() . '> - previous: ' . $previous . ', current: ' . $current);
$this->logger->debug('New quota information for <' . $account->getMailAccount()->getEmail() . '> - previous: ' . $previous . ', current: ' . $current);
$time = $this->time->getDateTime('now');
$notification = $this->notificationManager->createNotification();
$notification
->setApp('mail')
->setUser($account->getUserId())
->setUser($account->getMailAccount()->getUserId())
->setObject('quota', (string)$accountId)
->setSubject('quota_depleted', [
'id' => $accountId,
'account_email' => $account->getEmail()
'account_email' => $account->getMailAccount()->getEmail()
])
->setDateTime($time)
->setMessage('percentage', [
Expand Down
6 changes: 3 additions & 3 deletions lib/BackgroundJob/SyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ protected function run($argument) {
return;
}

$user = $this->userManager->get($account->getUserId());
$user = $this->userManager->get($account->getMailAccount()->getUserId());
if ($user === null || !$user->isEnabled()) {
$this->logger->debug(sprintf(
'Account %d of user %s could not be found or was disabled, skipping background sync',
$account->getId(),
$account->getUserId()
$account->getMailAccount()->getId(),
$account->getMailAccount()->getUserId()
));
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 @@ -63,7 +63,7 @@ protected function run($argument) {
return;
}

if (!$this->classificationSettingsService->isClassificationEnabled($account->getUserId())) {
if (!$this->classificationSettingsService->isClassificationEnabled($account->getMailAccount()->getUserId())) {
$this->logger->debug("classification is turned off for account $accountId");
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/BackgroundJob/TrashRetentionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function run($argument) {
} catch (ServiceException|ClientException $e) {
$this->logger->error('Could not clean trash mailbox', [
'exception' => $e,
'userId' => $account->getUserId(),
'accountId' => $account->getId(),
'userId' => $account->getMailAccount()->getUserId(),
'accountId' => $account->getMailAccount()->getId(),
'trashMailboxId' => $account->getMailAccount()->getTrashMailboxId(),
]);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Command/DeleteAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeLn('<error>This account does not exist</error>');
return 1;
}
$output->writeLn('<info>Found account with email: ' . $account->getEmail() . '</info>');
$output->writeLn('<info>Found account with email: ' . $account->getMailAccount()->getEmail() . '</info>');

if (!is_null($account->getMailAccount()->getProvisioningId())) {
$output->writeLn('<error>This is a provisioned account which can not be deleted from CLI. Use the Provisioning UI instead.</error>');
return 2;
}
$output->writeLn('<info>Deleting ' . $account->getEmail() . '</info>');
$output->writeLn('<info>Deleting ' . $account->getMailAccount()->getEmail() . '</info>');
$this->delete($account, $output);

return 0;
}

private function delete(Account $account, OutputInterface $output): void {
$id = $account->getId();
$id = $account->getMailAccount()->getId();
try {
$this->accountService->deleteByAccountId($account->getId());
$this->accountService->deleteByAccountId($account->getMailAccount()->getId());
} catch (ClientException $e) {
throw $e;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Command/ExportAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function configure() {

private function getAccountsData($accounts) {
$accountsData = [];

foreach ($accounts as $account) {
$accountsData[] = [
'id' => $account->getId(),
Expand All @@ -65,7 +65,7 @@ private function getAccountsData($accounts) {
]
];
}

return $accountsData;
}

Expand All @@ -80,9 +80,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln(json_encode($this->getAccountsData($accounts), JSON_PRETTY_PRINT));
} else {
foreach ($accounts as $account) {
$output->writeln('<info>Account ' . $account->getId() . ':</info>');
$output->writeln('- E-Mail: ' . $account->getEmail());
$output->writeln('- Name: ' . $account->getName());
$output->writeln('<info>Account ' . $account->getMailAccount()->getId() . ':</info>');
$output->writeln('- E-Mail: ' . $account->getMailAccount()->getEmail());
$output->writeln('- Name: ' . $account->getMailAccount()->getName());
$output->writeln('- Provision: ' . ($account->getMailAccount()->getProvisioningId() ? 'set' : 'none'). ' ID: ' . ($account->getMailAccount()->getProvisioningId() ?: 'N/A'));
$output->writeln('- IMAP user: ' . $account->getMailAccount()->getInboundUser());
$output->writeln('- IMAP host: ' . $account->getMailAccount()->getInboundHost() . ':' . $account->getMailAccount()->getInboundPort() . ', security: ' . $account->getMailAccount()->getInboundSslMode());
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/TrainAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln("<error>account $accountId does not exist</error>");
return 1;
}
if (!$this->classificationSettingsService->isClassificationEnabled($account->getUserId())) {
if (!$this->classificationSettingsService->isClassificationEnabled($account->getMailAccount()->getUserId())) {
$output->writeln("<info>classification is turned off for account $accountId</info>");
return 2;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/Controller/AccountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace OCA\Mail\Controller;

use Horde_Imap_Client;
use OCA\Mail\Account;
use OCA\Mail\AppInfo\Application;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailTransmission;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function index(): JSONResponse {

$json = [];
foreach ($mailAccounts as $mailAccount) {
$conf = $mailAccount->jsonSerialize();
$conf = $mailAccount->getMailAccount()->jsonSerialize();
$conf['aliases'] = $this->aliasesService->findAll($conf['accountId'], $this->currentUserId);
$json[] = $conf;
}
Expand Down Expand Up @@ -177,7 +178,7 @@ public function update(int $id,

try {
return MailJsonResponse::success(
$this->setup->createNewAccount($accountName, $emailAddress, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $this->currentUserId, $authMethod, $id)
$this->setup->createNewAccount($accountName, $emailAddress, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $this->currentUserId, $authMethod, $id)->getMailAccount()
);
} catch (CouldNotConnectException $e) {
$data = [
Expand Down Expand Up @@ -398,7 +399,7 @@ public function create(string $accountName,
}
}
return MailJsonResponse::success(
$account, Http::STATUS_CREATED
$account->getMailAccount(), Http::STATUS_CREATED
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/MailboxesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function index(int $accountId): JSONResponse {
$mailboxes = $this->mailManager->getMailboxes($account);
return new JSONResponse([
'id' => $accountId,
'email' => $account->getEmail(),
'email' => $account->getMailAccount()->getEmail(),
'mailboxes' => $mailboxes,
'delimiter' => reset($mailboxes)->getDelimiter(),
]);
Expand Down
3 changes: 2 additions & 1 deletion lib/Controller/MessageApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\Mail\Controller;

use OCA\Mail\Account;
use OCA\Mail\Contracts\IDkimService;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Exception\ClientException;
Expand Down Expand Up @@ -126,7 +127,7 @@ public function send(
return new DataResponse('Account not found.', Http::STATUS_NOT_FOUND);
}

if ($fromEmail !== $mailAccount->getEmail()) {
if ($fromEmail !== $mailAccount->getMailAccount()->getEmail()) {
try {
$alias = $this->aliasesService->findByAliasAndUserId($fromEmail, $this->userId);
} catch (DoesNotExistException $e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function getBody(int $id): JSONResponse {
$a
);
}, $json['attachments']);
$json['accountId'] = $account->getId();
$json['accountId'] = $account->getMailAccount()->getId();
$json['mailboxId'] = $mailbox->getId();
$json['databaseId'] = $message->getId();
$json['isSenderTrusted'] = $this->isSenderTrusted($message);
Expand Down
6 changes: 3 additions & 3 deletions lib/Controller/OutOfOfficeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getState(int $accountId): JsonResponse {
}

$account = $this->accountService->findById($accountId);
if ($account->getUserId() !== $user->getUID()) {
if ($account->getMailAccount()->getUserId() !== $user->getUID()) {
return JsonResponse::fail([], Http::STATUS_NOT_FOUND);
}

Expand All @@ -72,7 +72,7 @@ public function followSystem(int $accountId) {
}

$account = $this->accountService->findById($accountId);
if ($account->getUserId() !== $user->getUID()) {
if ($account->getMailAccount()->getUserId() !== $user->getUID()) {
return JsonResponse::fail([], Http::STATUS_NOT_FOUND);
}

Expand Down Expand Up @@ -104,7 +104,7 @@ public function update(
}

$account = $this->accountService->findById($accountId);
if ($account->getUserId() !== $user->getUID()) {
if ($account->getMailAccount()->getUserId() !== $user->getUID()) {
return JsonResponse::fail([], Http::STATUS_NOT_FOUND);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public function index(): TemplateResponse {
$mailAccounts = $this->accountService->findByUserId($this->currentUserId);
$accountsJson = [];
foreach ($mailAccounts as $mailAccount) {
$json = $mailAccount->jsonSerialize();
$json['aliases'] = $this->aliasesService->findAll($mailAccount->getId(),
$json = $mailAccount->getMailAccount()->jsonSerialize();
$json['aliases'] = $this->aliasesService->findAll($mailAccount->getMailAccount()->getId(),
$this->currentUserId);
try {
$mailboxes = $this->mailManager->getMailboxes($mailAccount);
Expand Down
8 changes: 7 additions & 1 deletion lib/Db/MailAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\Mail\Db;

use JsonSerializable;
use OCP\AppFramework\Db\Entity;

/**
Expand Down Expand Up @@ -102,7 +103,7 @@
* @method bool|null getOooFollowsSystem()
* @method void setOooFollowsSystem(bool $oooFollowsSystem)
*/
class MailAccount extends Entity {
class MailAccount extends Entity implements JSONSerializable {
public const SIGNATURE_MODE_PLAIN = 0;
public const SIGNATURE_MODE_HTML = 1;

Expand Down Expand Up @@ -278,6 +279,7 @@ public function canAuthenticateImap(): bool {
}

/**
* @deprecated
* @return array
*/
public function toJson() {
Expand Down Expand Up @@ -328,4 +330,8 @@ public function toJson() {

return $result;
}

public function jsonSerialize(): mixed {
return $this->toJson();
}
}
Loading
Loading