1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Björn Schießle <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
namespace OCA\FederatedFileSharing\BackgroundJob; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
use OC\BackgroundJob\Job; |
27
|
|
|
use OC\BackgroundJob\JobList; |
28
|
|
|
use OCA\FederatedFileSharing\AddressHandler; |
29
|
|
|
use OCA\FederatedFileSharing\DiscoveryManager; |
30
|
|
|
use OCA\FederatedFileSharing\Notifications; |
31
|
|
|
use OCP\BackgroundJob\IJobList; |
32
|
|
|
use OCP\ILogger; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class UnShare |
36
|
|
|
* |
37
|
|
|
* Background job to re-send the un-share notification to the remote server in |
38
|
|
|
* case the server was not available on the first try |
39
|
|
|
* |
40
|
|
|
* @package OCA\FederatedFileSharing\BackgroundJob |
41
|
|
|
*/ |
42
|
|
|
class UnShare extends Job { |
43
|
|
|
|
44
|
|
|
/** @var bool */ |
45
|
|
|
private $retainJob = true; |
46
|
|
|
|
47
|
|
|
/** @var Notifications */ |
48
|
|
|
private $notifications; |
49
|
|
|
|
50
|
|
|
/** @var int max number of attempts to send the un-share request */ |
51
|
|
|
private $maxTry = 10; |
52
|
|
|
|
53
|
|
|
/** @var int how much time should be between two tries (12 hours) */ |
54
|
|
|
private $interval = 43200; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* UnShare constructor. |
58
|
|
|
* |
59
|
|
|
* @param Notifications $notifications |
60
|
|
|
*/ |
61
|
|
|
public function __construct(Notifications $notifications = null) { |
62
|
|
|
if ($notifications) { |
63
|
|
|
$this->notifications = $notifications; |
64
|
|
|
} else { |
65
|
|
|
$addressHandler = new AddressHandler( |
66
|
|
|
\OC::$server->getURLGenerator(), |
67
|
|
|
\OC::$server->getL10N('federatedfilesharing') |
68
|
|
|
); |
69
|
|
|
$discoveryManager = new DiscoveryManager( |
70
|
|
|
\OC::$server->getMemCacheFactory(), |
71
|
|
|
\OC::$server->getHTTPClientService() |
72
|
|
|
); |
73
|
|
|
$this->notifications = new Notifications( |
74
|
|
|
$addressHandler, |
75
|
|
|
\OC::$server->getHTTPClientService(), |
76
|
|
|
$discoveryManager, |
77
|
|
|
\OC::$server->getJobList() |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* run the job, then remove it from the jobList |
85
|
|
|
* |
86
|
|
|
* @param JobList $jobList |
87
|
|
|
* @param ILogger $logger |
88
|
|
|
*/ |
89
|
|
|
public function execute($jobList, ILogger $logger = null) { |
90
|
|
|
|
91
|
|
|
if ($this->shouldRun($this->argument)) { |
92
|
|
|
parent::execute($jobList, $logger); |
93
|
|
|
$jobList->remove($this, $this->argument); |
94
|
|
|
if ($this->retainJob) { |
95
|
|
|
$this->reAddJob($jobList, $this->argument); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function run($argument) { |
101
|
|
|
$remote = $argument['remote']; |
102
|
|
|
$id = (int)$argument['id']; |
103
|
|
|
$token = $argument['token']; |
104
|
|
|
$try = (int)$argument['try'] + 1; |
105
|
|
|
|
106
|
|
|
$result = $this->notifications->sendRemoteUnShare($remote, $id, $token, $try); |
107
|
|
|
|
108
|
|
|
if ($result === true || $try > $this->maxTry) { |
109
|
|
|
$this->retainJob = false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* re-add background job with new arguments |
115
|
|
|
* |
116
|
|
|
* @param IJobList $jobList |
117
|
|
|
* @param array $argument |
118
|
|
|
*/ |
119
|
|
|
protected function reAddJob(IJobList $jobList, array $argument) { |
120
|
|
|
$jobList->add('OCA\FederatedFileSharing\BackgroundJob\UnShare', |
121
|
|
|
[ |
122
|
|
|
'remote' => $argument['remote'], |
123
|
|
|
'id' => $argument['id'], |
124
|
|
|
'token' => $argument['token'], |
125
|
|
|
'try' => (int)$argument['try'] + 1, |
126
|
|
|
'lastRun' => time() |
127
|
|
|
] |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* test if it is time for the next run |
133
|
|
|
* |
134
|
|
|
* @param array $argument |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
protected function shouldRun(array $argument) { |
138
|
|
|
$lastRun = (int)$argument['lastRun']; |
139
|
|
|
return ((time() - $lastRun) > $this->interval); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|