diff --git a/lib/BackgroundJob/SyncJob.php b/lib/BackgroundJob/SyncJob.php index c9e3600918..8404633794 100644 --- a/lib/BackgroundJob/SyncJob.php +++ b/lib/BackgroundJob/SyncJob.php @@ -9,6 +9,8 @@ namespace OCA\Mail\BackgroundJob; use Horde_Imap_Client_Exception; +use NCU\Config\IUserConfig; +use OCA\Mail\AppInfo\Application; use OCA\Mail\Exception\IncompleteSyncException; use OCA\Mail\Exception\ServiceException; use OCA\Mail\IMAP\MailboxSync; @@ -33,14 +35,17 @@ class SyncJob extends TimedJob { private LoggerInterface $logger; private IJobList $jobList; - public function __construct(ITimeFactory $time, + public function __construct( + ITimeFactory $time, IUserManager $userManager, AccountService $accountService, MailboxSync $mailboxSync, ImapToDbSynchronizer $syncService, LoggerInterface $logger, IJobList $jobList, - IConfig $config) { + IConfig $config, + private IUserConfig $userConfig, + ) { parent::__construct($time); $this->userManager = $userManager; @@ -79,6 +84,12 @@ protected function run($argument) { return; } + if (($this->userConfig->getValueInt($account->getUserId(), Application::APP_ID, 'ui-hearthbeat') - $this->time->getTime()) > 900) { + $this->logger->debug('Detected user activity, skipping background sync job'); + $this->setInterval(900); + return; + } + $user = $this->userManager->get($account->getUserId()); if ($user === null || !$user->isEnabled()) { $this->logger->debug(sprintf( diff --git a/lib/Controller/MailboxesController.php b/lib/Controller/MailboxesController.php index b3485df6d6..416b0d9891 100644 --- a/lib/Controller/MailboxesController.php +++ b/lib/Controller/MailboxesController.php @@ -11,6 +11,8 @@ namespace OCA\Mail\Controller; use Horde_Imap_Client; +use NCU\Config\IUserConfig; +use OCA\Mail\AppInfo\Application; use OCA\Mail\Contracts\IMailManager; use OCA\Mail\Contracts\IMailSearch; use OCA\Mail\Exception\ClientException; @@ -28,34 +30,28 @@ use OCP\AppFramework\Http\Attribute\UserRateLimit; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; +use OCP\IUserSession; #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)] class MailboxesController extends Controller { private AccountService $accountService; - private ?string $currentUserId; private IMailManager $mailManager; private SyncService $syncService; + private ?string $currentUserId; - /** - * @param string $appName - * @param IRequest $request - * @param AccountService $accountService - * @param string|null $UserId - * @param IMailManager $mailManager - * @param SyncService $syncService - */ - public function __construct(string $appName, + public function __construct( IRequest $request, AccountService $accountService, - ?string $UserId, IMailManager $mailManager, - SyncService $syncService) { - parent::__construct($appName, $request); - + SyncService $syncService, + private IUserSession $userSession, + private IUserConfig $userConfig, + ) { + parent::__construct(Application::APP_ID, $request); $this->accountService = $accountService; - $this->currentUserId = $UserId; $this->mailManager = $mailManager; $this->syncService = $syncService; + $this->currentUserId = $userSession->getUser()?->getUID(); } /** @@ -140,6 +136,8 @@ public function sync(int $id, array $ids = [], ?int $lastMessageTimestamp = null $account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId()); $order = $sortOrder === 'newest' ? IMailSearch::ORDER_NEWEST_FIRST: IMailSearch::ORDER_OLDEST_FIRST; + $this->userConfig->setValueInt($this->currentUserId, Application::APP_ID, 'ui-hearthbeat', time()); + try { $syncResponse = $this->syncService->syncMailbox( $account, diff --git a/tests/Unit/Controller/MailboxesControllerTest.php b/tests/Unit/Controller/MailboxesControllerTest.php index d2c1884216..ba02b4999a 100644 --- a/tests/Unit/Controller/MailboxesControllerTest.php +++ b/tests/Unit/Controller/MailboxesControllerTest.php @@ -10,6 +10,7 @@ namespace OCA\Mail\Tests\Unit\Controller; use ChristophWurst\Nextcloud\Testing\TestCase; +use NCU\Config\IUserConfig; use OCA\Mail\Account; use OCA\Mail\Contracts\IMailManager; use OCA\Mail\Controller\MailboxesController; @@ -21,11 +22,11 @@ use OCA\Mail\Service\Sync\SyncService; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; +use OCP\IUser; +use OCP\IUserSession; use PHPUnit\Framework\MockObject\MockObject; class MailboxesControllerTest extends TestCase { - /** @var string */ - private $appName = 'mail'; /** @var IRequest|MockObject */ private $request; @@ -45,6 +46,9 @@ class MailboxesControllerTest extends TestCase { /** @var SyncService|MockObject */ private $syncService; + private IUserSession|MockObject $userSession; + private IUserConfig|MockObject $userConfig; + public function setUp(): void { parent::setUp(); @@ -52,13 +56,22 @@ public function setUp(): void { $this->accountService = $this->createMock(AccountService::class); $this->mailManager = $this->createMock(IMailManager::class); $this->syncService = $this->createMock(SyncService::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->userConfig = $this->createMock(IUserConfig::class); + + $userObject = $this->createMock(IUser::class); + $userObject->method('getUID') + ->willReturn('john'); + $this->userSession->method('getUser') + ->willReturn($userObject); + $this->controller = new MailboxesController( - $this->appName, $this->request, $this->accountService, - $this->userId, $this->mailManager, - $this->syncService + $this->syncService, + $this->userSession, + $this->userConfig ); }