Skip to content

[test] feat: adjust background sync on user activity #11340

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 13 additions & 2 deletions lib/BackgroundJob/SyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,14 +35,17 @@
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;
Expand Down Expand Up @@ -79,6 +84,12 @@
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;

Check warning on line 90 in lib/BackgroundJob/SyncJob.php

View check run for this annotation

Codecov / codecov/patch

lib/BackgroundJob/SyncJob.php#L88-L90

Added lines #L88 - L90 were not covered by tests
}

$user = $this->userManager->get($account->getUserId());
if ($user === null || !$user->isEnabled()) {
$this->logger->debug(sprintf(
Expand Down
28 changes: 13 additions & 15 deletions lib/Controller/MailboxesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -140,6 +136,8 @@
$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());

Check warning on line 139 in lib/Controller/MailboxesController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/MailboxesController.php#L139

Added line #L139 was not covered by tests

try {
$syncResponse = $this->syncService->syncMailbox(
$account,
Expand Down
23 changes: 18 additions & 5 deletions tests/Unit/Controller/MailboxesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -45,20 +46,32 @@ class MailboxesControllerTest extends TestCase {
/** @var SyncService|MockObject */
private $syncService;

private IUserSession|MockObject $userSession;
private IUserConfig|MockObject $userConfig;

public function setUp(): void {
parent::setUp();

$this->request = $this->createMock(IRequest::class);
$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
);
}

Expand Down
Loading