Skip to content

Commit a1daaca

Browse files
SebastianKrupinskikesselb
authored andcommitted
feat: adjust background sync on user activity
Signed-off-by: SebastianKrupinski <[email protected]>
1 parent d468d0e commit a1daaca

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

lib/BackgroundJob/SyncJob.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace OCA\Mail\BackgroundJob;
1010

1111
use Horde_Imap_Client_Exception;
12+
use NCU\Config\IUserConfig;
13+
use OCA\Mail\AppInfo\Application;
1214
use OCA\Mail\Exception\IncompleteSyncException;
1315
use OCA\Mail\Exception\ServiceException;
1416
use OCA\Mail\IMAP\MailboxSync;
@@ -33,14 +35,17 @@ class SyncJob extends TimedJob {
3335
private LoggerInterface $logger;
3436
private IJobList $jobList;
3537

36-
public function __construct(ITimeFactory $time,
38+
public function __construct(
39+
ITimeFactory $time,
3740
IUserManager $userManager,
3841
AccountService $accountService,
3942
MailboxSync $mailboxSync,
4043
ImapToDbSynchronizer $syncService,
4144
LoggerInterface $logger,
4245
IJobList $jobList,
43-
IConfig $config) {
46+
IConfig $config,
47+
private IUserConfig $userConfig,
48+
) {
4449
parent::__construct($time);
4550

4651
$this->userManager = $userManager;
@@ -79,6 +84,12 @@ protected function run($argument) {
7984
return;
8085
}
8186

87+
if (($this->userConfig->getValueInt($account->getUserId(), Application::APP_ID, 'ui-hearthbeat') - $this->time->getTime()) > 900) {
88+
$this->logger->debug('Detected user activity, skipping background sync job');
89+
$this->setInterval(900);
90+
return;
91+
}
92+
8293
$user = $this->userManager->get($account->getUserId());
8394
if ($user === null || !$user->isEnabled()) {
8495
$this->logger->debug(sprintf(

lib/Controller/MailboxesController.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace OCA\Mail\Controller;
1212

1313
use Horde_Imap_Client;
14+
use NCU\Config\IUserConfig;
15+
use OCA\Mail\AppInfo\Application;
1416
use OCA\Mail\Contracts\IMailManager;
1517
use OCA\Mail\Contracts\IMailSearch;
1618
use OCA\Mail\Exception\ClientException;
@@ -28,34 +30,28 @@
2830
use OCP\AppFramework\Http\Attribute\UserRateLimit;
2931
use OCP\AppFramework\Http\JSONResponse;
3032
use OCP\IRequest;
33+
use OCP\IUserSession;
3134

3235
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
3336
class MailboxesController extends Controller {
3437
private AccountService $accountService;
35-
private ?string $currentUserId;
3638
private IMailManager $mailManager;
3739
private SyncService $syncService;
40+
private ?string $currentUserId;
3841

39-
/**
40-
* @param string $appName
41-
* @param IRequest $request
42-
* @param AccountService $accountService
43-
* @param string|null $UserId
44-
* @param IMailManager $mailManager
45-
* @param SyncService $syncService
46-
*/
47-
public function __construct(string $appName,
42+
public function __construct(
4843
IRequest $request,
4944
AccountService $accountService,
50-
?string $UserId,
5145
IMailManager $mailManager,
52-
SyncService $syncService) {
53-
parent::__construct($appName, $request);
54-
46+
SyncService $syncService,
47+
private IUserSession $userSession,
48+
private IUserConfig $userConfig,
49+
) {
50+
parent::__construct(Application::APP_ID, $request);
5551
$this->accountService = $accountService;
56-
$this->currentUserId = $UserId;
5752
$this->mailManager = $mailManager;
5853
$this->syncService = $syncService;
54+
$this->currentUserId = $userSession->getUser()?->getUID();
5955
}
6056

6157
/**
@@ -140,6 +136,8 @@ public function sync(int $id, array $ids = [], ?int $lastMessageTimestamp = null
140136
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
141137
$order = $sortOrder === 'newest' ? IMailSearch::ORDER_NEWEST_FIRST: IMailSearch::ORDER_OLDEST_FIRST;
142138

139+
$this->userConfig->setValueInt($this->currentUserId, Application::APP_ID, 'ui-hearthbeat', time());
140+
143141
try {
144142
$syncResponse = $this->syncService->syncMailbox(
145143
$account,

tests/Unit/Controller/MailboxesControllerTest.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OCA\Mail\Tests\Unit\Controller;
1111

1212
use ChristophWurst\Nextcloud\Testing\TestCase;
13+
use NCU\Config\IUserConfig;
1314
use OCA\Mail\Account;
1415
use OCA\Mail\Contracts\IMailManager;
1516
use OCA\Mail\Controller\MailboxesController;
@@ -21,11 +22,11 @@
2122
use OCA\Mail\Service\Sync\SyncService;
2223
use OCP\AppFramework\Http\JSONResponse;
2324
use OCP\IRequest;
25+
use OCP\IUser;
26+
use OCP\IUserSession;
2427
use PHPUnit\Framework\MockObject\MockObject;
2528

2629
class MailboxesControllerTest extends TestCase {
27-
/** @var string */
28-
private $appName = 'mail';
2930

3031
/** @var IRequest|MockObject */
3132
private $request;
@@ -45,20 +46,32 @@ class MailboxesControllerTest extends TestCase {
4546
/** @var SyncService|MockObject */
4647
private $syncService;
4748

49+
private IUserSession|MockObject $userSession;
50+
private IUserConfig|MockObject $userConfig;
51+
4852
public function setUp(): void {
4953
parent::setUp();
5054

5155
$this->request = $this->createMock(IRequest::class);
5256
$this->accountService = $this->createMock(AccountService::class);
5357
$this->mailManager = $this->createMock(IMailManager::class);
5458
$this->syncService = $this->createMock(SyncService::class);
59+
$this->userSession = $this->createMock(IUserSession::class);
60+
$this->userConfig = $this->createMock(IUserConfig::class);
61+
62+
$userObject = $this->createMock(IUser::class);
63+
$userObject->method('getUID')
64+
->willReturn('john');
65+
$this->userSession->method('getUser')
66+
->willReturn($userObject);
67+
5568
$this->controller = new MailboxesController(
56-
$this->appName,
5769
$this->request,
5870
$this->accountService,
59-
$this->userId,
6071
$this->mailManager,
61-
$this->syncService
72+
$this->syncService,
73+
$this->userSession,
74+
$this->userConfig
6275
);
6376
}
6477

0 commit comments

Comments
 (0)