Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit be778694 authored by Ronak Patel's avatar Ronak Patel
Browse files

Merge branch 'dev/slow-query' into 'main'

Slow cron job issue for recovery warning notification

See merge request !118
parents d4562b97 5d02fb4c
Loading
Loading
Loading
Loading
Loading
+48 −11
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ use OCA\EmailRecovery\Service\NotificationService;
use OCA\EmailRecovery\Service\RecoveryEmailService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Mail\IEMailTemplate;
@@ -22,6 +23,7 @@ use Symfony\Component\Console\Output\OutputInterface;
class RecoveryWarningNotificationCommand extends Command {
	private IConfig $config;
	private IUserManager $userManager;
	private IGroupManager $groupManager;
	private RecoveryEmailService $recoveryEmailService;

	private IMailer $mailer;
@@ -48,6 +50,7 @@ class RecoveryWarningNotificationCommand extends Command {
	public function __construct(
		IConfig $config,
		IUserManager $userManager,
		IGroupManager $groupManager,
		RecoveryEmailService $recoveryEmailService,
		IMailer $mailer,
		IManager $notificationManager,
@@ -58,6 +61,7 @@ class RecoveryWarningNotificationCommand extends Command {
		parent::__construct();
		$this->config = $config;
		$this->userManager = $userManager;
		$this->groupManager = $groupManager;
		$this->recoveryEmailService = $recoveryEmailService;
		$this->mailer = $mailer;
		$this->notificationManager = $notificationManager;
@@ -93,11 +97,16 @@ class RecoveryWarningNotificationCommand extends Command {
	}

	protected function execute(InputInterface $input, OutputInterface $output): int {
		$overallStart = microtime(true);
		$startTime = date('Y-m-d H:i:s');
		
		$disableAccounts = $input->getOption('disable-accounts');
		$deleteAccounts = $input->getOption('delete-accounts');
		$this->dryRun = $input->getOption('dry-run');
		$this->testMode = $input->getOption('test-mode');

		$output->writeln('<info>[' . $startTime . '] Starting email recovery warning notification command</info>');

		if ($this->dryRun) {
			$output->writeln('<info>Running in DRY-RUN mode - no actual actions will be taken</info>');
		}
@@ -129,15 +138,22 @@ class RecoveryWarningNotificationCommand extends Command {
				$this->deleteExpiredDisabledUsers($output);
			}

			$output->writeln('<info>=== Command Summary ===</info>');
			$overallDuration = microtime(true) - $overallStart;
			$endTime = date('Y-m-d H:i:s');

			$output->writeln('<info>[' . $endTime . '] === Command Summary ===</info>');
			$output->writeln('  - Users for notifications: ' . count($this->uids));
			$output->writeln('  - Users for disable: ' . count($this->disableUids));
			$output->writeln('  - Users for deletion: ' . count($this->deleteUids));
			$output->writeln('  - Total execution time: ' . round($overallDuration, 2) . 's');

			$output->writeln('<info>Recovery warning notification command completed successfully.</info>');
			$output->writeln('<info>[' . $endTime . '] Recovery warning notification command completed successfully.</info>');
			return Command::SUCCESS;
		} catch (\Throwable $e) {
			$output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
			$overallDuration = microtime(true) - $overallStart;
			$endTime = date('Y-m-d H:i:s');
			$output->writeln('<error>[' . $endTime . '] Error: ' . $e->getMessage() . '</error>');
			$output->writeln('<error>[' . $endTime . '] Command failed after ' . round($overallDuration, 2) . 's</error>');
			return Command::FAILURE;
		}
	}
@@ -161,32 +177,38 @@ class RecoveryWarningNotificationCommand extends Command {
		$skippedCount = 0;
		$invalidCount = 0;
		$errorCount = 0;
		$premiumSkippedCount = 0;

		foreach ($users as $username) {
			try {
				$user = $this->userManager->get($username);
				
				if (!$user) {
					$invalidCount++;
					continue;
				}

				if (!$this->isUserValid($user)) {
				$isValid = $this->isUserValid($user);
				
				if (!$isValid) {
					$invalidCount++;
					continue;
				}

				$isPremium = $this->isUserInPremiumGroup($user);
				
				// Check if user is in premium group - exclude from all processing
				if ($isPremium) {
					$premiumSkippedCount++;
					continue;
				}

				$uid = $user->getUID();
				$emailAddress = $user->getEMailAddress();

				// Check if user has a VERIFIED recovery email
				$verifiedRecoveryEmail = $this->recoveryEmailService->getRecoveryEmail($username);
				if (!empty($verifiedRecoveryEmail)) {
					$skippedCount++;
					continue;
				}
				
				// Check for active subscription
				if ($this->recoveryEmailService->hasActiveSubscription($emailAddress)) {
				if (!empty($verifiedRecoveryEmail)) {
					$skippedCount++;
					continue;
				}
@@ -232,6 +254,7 @@ class RecoveryWarningNotificationCommand extends Command {
		$output->writeln('  - Total users: ' . count($users));
		$output->writeln('  - Processed: ' . $processedCount);
		$output->writeln('  - Skipped: ' . $skippedCount);
		$output->writeln('  - Premium skipped: ' . $premiumSkippedCount);
		$output->writeln('  - Invalid: ' . $invalidCount);
		$output->writeln('  - Errors: ' . $errorCount);
		$output->writeln('  - For notifications: ' . count($this->uids));
@@ -307,6 +330,7 @@ class RecoveryWarningNotificationCommand extends Command {
		foreach ($this->uids as $uid) {
			try {
				$user = $this->userManager->get($uid);
				
				if (!$user) {
					$stats['notificationFailedCount']++;
					$stats['emailFailedCount']++;
@@ -513,4 +537,17 @@ class RecoveryWarningNotificationCommand extends Command {
		$emailAddress = $user->getEMailAddress();
		return $emailAddress && $this->mailer->validateMailAddress($emailAddress);
	}

	private function isUserInPremiumGroup($user): bool {
		// Check for 'premium' and 'premium-*' groups
		$userGroups = $this->groupManager->getUserGroups($user);
		foreach ($userGroups as $group) {
			$groupId = $group->getGID();
			if ($groupId === 'premium' || strpos($groupId, 'premium-') === 0) {
				return true;
			}
		}
		
		return false;
	}
}