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
40
public function __construct (ITimeFactory $ time ,
37
41
IUserManager $ userManager ,
@@ -40,7 +44,7 @@ public function __construct(ITimeFactory $time,
40
44
ImapToDbSynchronizer $ syncService ,
41
45
LoggerInterface $ logger ,
42
46
IJobList $ jobList ,
43
- IConfig $ config ) {
47
+ private readonly IConfig $ config ) {
44
48
parent ::__construct ($ time );
45
49
46
50
$ this ->userManager = $ userManager ;
@@ -50,12 +54,15 @@ public function __construct(ITimeFactory $time,
50
54
$ this ->logger = $ logger ;
51
55
$ this ->jobList = $ jobList ;
52
56
53
- $ this ->setInterval (
54
- max (
55
- 5 * 60 ,
56
- $ config ->getSystemValueInt ('app.mail.background-sync-interval ' , 3600 )
57
- ),
58
- );
57
+ $ configuredSyncInterval = $ config ->getSystemValueInt ('app.mail.background-sync-interval ' );
58
+ if ($ configuredSyncInterval > 0 ) {
59
+ $ this ->forcedSyncInterval = true ;
60
+ } else {
61
+ $ this ->forcedSyncInterval = false ;
62
+ $ configuredSyncInterval = self ::DEFAULT_SYNC_INTERVAL ;
63
+ }
64
+
65
+ $ this ->setInterval (max (5 * 60 , $ configuredSyncInterval ));
59
66
$ this ->setTimeSensitivity (self ::TIME_SENSITIVE );
60
67
}
61
68
@@ -79,6 +86,31 @@ protected function run($argument) {
79
86
return ;
80
87
}
81
88
89
+ // If an admin configured a custom sync interval, always abide by it
90
+ if (!$ this ->forcedSyncInterval ) {
91
+ $ now = $ this ->time ->getTime ();
92
+ $ heartbeat = (int )$ this ->config ->getUserValue (
93
+ $ account ->getUserId (),
94
+ Application::APP_ID ,
95
+ 'ui-heartbeat ' ,
96
+ 0 ,
97
+ );
98
+ $ lastUsed = $ now - $ heartbeat ;
99
+ if ($ lastUsed > 3 * 24 * 3600 ) {
100
+ // User did not open the app in more than three days -> defer sync
101
+ $ this ->setInterval (6 * 3600 );
102
+ } elseif ($ lastUsed > 24 * 3600 ) {
103
+ // User opened the app at least once within the last three days -> default sync
104
+ $ this ->setInterval (self ::DEFAULT_SYNC_INTERVAL );
105
+ } elseif ($ lastUsed > 0 ) {
106
+ // User opened the app at least once within the last 24 hours -> sync more often
107
+ $ this ->setInterval (15 * 60 );
108
+ } else {
109
+ // Default to the hourly interval in case there is no heartbeat
110
+ $ this ->setInterval (self ::DEFAULT_SYNC_INTERVAL );
111
+ }
112
+ }
113
+
82
114
$ user = $ this ->userManager ->get ($ account ->getUserId ());
83
115
if ($ user === null || !$ user ->isEnabled ()) {
84
116
$ this ->logger ->debug (sprintf (
0 commit comments