9
9
namespace OCA \Mail \BackgroundJob ;
10
10
11
11
use Horde_Imap_Client_Exception ;
12
+ use OCA \Mail \AppInfo \Application ;
12
13
use OCA \Mail \Exception \IncompleteSyncException ;
13
14
use OCA \Mail \Exception \ServiceException ;
14
15
use OCA \Mail \IMAP \MailboxSync ;
26
27
use function sprintf ;
27
28
28
29
class SyncJob extends TimedJob {
30
+ private const DEFAULT_SYNC_INTERVAL = 3600 ;
31
+
29
32
private IUserManager $ userManager ;
30
33
private AccountService $ accountService ;
31
34
private ImapToDbSynchronizer $ syncService ;
32
35
private MailboxSync $ mailboxSync ;
33
36
private LoggerInterface $ logger ;
34
37
private IJobList $ jobList ;
38
+ private readonly bool $ forcedSyncInterval ;
35
39
36
- public function __construct (ITimeFactory $ time ,
40
+ public function __construct (
41
+ ITimeFactory $ time ,
37
42
IUserManager $ userManager ,
38
43
AccountService $ accountService ,
39
44
MailboxSync $ mailboxSync ,
40
45
ImapToDbSynchronizer $ syncService ,
41
46
LoggerInterface $ logger ,
42
47
IJobList $ jobList ,
43
- IConfig $ config ) {
48
+ private readonly IConfig $ config ,
49
+ ) {
44
50
parent ::__construct ($ time );
45
51
46
52
$ this ->userManager = $ userManager ;
@@ -50,12 +56,15 @@ public function __construct(ITimeFactory $time,
50
56
$ this ->logger = $ logger ;
51
57
$ this ->jobList = $ jobList ;
52
58
53
- $ this ->setInterval (
54
- max (
55
- 5 * 60 ,
56
- $ config ->getSystemValueInt ('app.mail.background-sync-interval ' , 3600 )
57
- ),
58
- );
59
+ $ configuredSyncInterval = $ config ->getSystemValueInt ('app.mail.background-sync-interval ' );
60
+ if ($ configuredSyncInterval > 0 ) {
61
+ $ this ->forcedSyncInterval = true ;
62
+ } else {
63
+ $ this ->forcedSyncInterval = false ;
64
+ $ configuredSyncInterval = self ::DEFAULT_SYNC_INTERVAL ;
65
+ }
66
+
67
+ $ this ->setInterval (max (5 * 60 , $ configuredSyncInterval ));
59
68
$ this ->setTimeSensitivity (self ::TIME_SENSITIVE );
60
69
}
61
70
@@ -79,6 +88,31 @@ protected function run($argument) {
79
88
return ;
80
89
}
81
90
91
+ // If an admin configured a custom sync interval, always abide by it
92
+ if (!$ this ->forcedSyncInterval ) {
93
+ $ now = $ this ->time ->getTime ();
94
+ $ heartbeat = (int )$ this ->config ->getUserValue (
95
+ $ account ->getUserId (),
96
+ Application::APP_ID ,
97
+ 'ui-heartbeat ' ,
98
+ $ now + 1 , // Force negative value for $lastUsed in case of no heartbeat
99
+ );
100
+ $ lastUsed = $ now - $ heartbeat ;
101
+ if ($ lastUsed > 3 * 24 * 3600 ) {
102
+ // User did not open the app in more than three days -> defer sync
103
+ $ this ->setInterval (6 * 3600 );
104
+ } elseif ($ lastUsed > 24 * 3600 ) {
105
+ // User opened the app at least once within the last three days -> default sync
106
+ $ this ->setInterval (self ::DEFAULT_SYNC_INTERVAL );
107
+ } elseif ($ lastUsed > 0 ) {
108
+ // User opened the app at least once within the last 24 hours -> sync more often
109
+ $ this ->setInterval (15 * 60 );
110
+ } else {
111
+ // Default to the hourly interval in case there is no heartbeat
112
+ $ this ->setInterval (self ::DEFAULT_SYNC_INTERVAL );
113
+ }
114
+ }
115
+
82
116
$ user = $ this ->userManager ->get ($ account ->getUserId ());
83
117
if ($ user === null || !$ user ->isEnabled ()) {
84
118
$ this ->logger ->debug (sprintf (
0 commit comments