11<?php
2+ declare (strict_types=1 );
23/**
34 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected] > 5+ * @copyright Copyright (c) 2019 Joas Schilling <[email protected] > 46 *
57 * @license GNU AGPL version 3 or any later version
68 *
2224namespace OCA \LookupServerConnector \BackgroundJobs ;
2325
2426
25- use OC \BackgroundJob \Job ;
26- use OC \BackgroundJob \JobList ;
27+ use OC \Security \IdentityProof \Signer ;
28+ use OCP \Accounts \IAccountManager ;
29+ use OCP \AppFramework \Utility \ITimeFactory ;
30+ use OCP \BackgroundJob \Job ;
2731use OCP \BackgroundJob \IJobList ;
2832use OCP \Http \Client \IClientService ;
2933use OCP \IConfig ;
3034use OCP \ILogger ;
35+ use OCP \IUser ;
36+ use OCP \IUserManager ;
3137
3238class RetryJob extends Job {
3339 /** @var IClientService */
3440 private $ clientService ;
35- /** @var IJobList */
36- private $ jobList ;
3741 /** @var string */
3842 private $ lookupServer ;
39- /** @var int how much time should be between two, will be increased for each retry */
40- private $ interval = 100 ;
4143 /** @var IConfig */
4244 private $ config ;
45+ /** @var IUserManager */
46+ private $ userManager ;
47+ /** @var IAccountManager */
48+ private $ accountManager ;
49+ /** @var Signer */
50+ private $ signer ;
51+ /** @var int */
52+ protected $ retries = 0 ;
53+ /** @var bool */
54+ protected $ retainJob = false ;
4355
4456 /**
57+ * @param ITimeFactory $time
4558 * @param IClientService $clientService
46- * @param IJobList $jobList
4759 * @param IConfig $config
60+ * @param IUserManager $userManager
61+ * @param IAccountManager $accountManager
62+ * @param Signer $signer
4863 */
49- public function __construct (IClientService $ clientService ,
50- IJobList $ jobList ,
51- IConfig $ config ) {
64+ public function __construct (ITimeFactory $ time ,
65+ IClientService $ clientService ,
66+ IConfig $ config ,
67+ IUserManager $ userManager ,
68+ IAccountManager $ accountManager ,
69+ Signer $ signer ) {
70+ parent ::__construct ($ time );
5271 $ this ->clientService = $ clientService ;
53- $ this ->jobList = $ jobList ;
5472 $ this ->config = $ config ;
55-
56- if ($ config ->getSystemValue ('has_internet_connection ' , true ) === false ) {
57- return ;
58- }
73+ $ this ->userManager = $ userManager ;
74+ $ this ->accountManager = $ accountManager ;
75+ $ this ->signer = $ signer ;
5976
6077 $ this ->lookupServer = $ config ->getSystemValue ('lookup_server ' , 'https://lookup.nextcloud.com ' );
6178 if (!empty ($ this ->lookupServer )) {
@@ -67,71 +84,129 @@ public function __construct(IClientService $clientService,
6784 /**
6885 * run the job, then remove it from the jobList
6986 *
70- * @param JobList $jobList
87+ * @param IJobList $jobList
7188 * @param ILogger|null $logger
7289 */
73- public function execute ($ jobList , ILogger $ logger = null ) {
74- if ($ this -> shouldRun ($ this ->argument )) {
75- parent :: execute ( $ jobList , $ logger );
90+ public function execute ($ jobList , ILogger $ logger = null ): void {
91+ if (! isset ($ this ->argument [ ' userId ' ] )) {
92+ // Old background job without user id, just drop it.
7693 $ jobList ->remove ($ this , $ this ->argument );
94+ return ;
95+ }
96+
97+ $ this ->retries = (int ) $ this ->config ->getUserValue ($ this ->argument ['userId ' ], 'lookup_server_connector ' , 'update_retries ' , 0 );
98+
99+ if ($ this ->shouldRemoveBackgroundJob ()) {
100+ $ jobList ->remove ($ this , $ this ->argument );
101+ return ;
102+ }
103+
104+ if ($ this ->shouldRun ()) {
105+ parent ::execute ($ jobList , $ logger );
106+ if (!$ this ->retainJob ) {
107+ $ jobList ->remove ($ this , $ this ->argument );
108+ }
77109 }
78110 }
79111
80- protected function run ($ argument ) {
81- if ($ this ->killBackgroundJob ((int )$ argument ['retryNo ' ])) {
112+ /**
113+ * Check if we should kill the background job:
114+ *
115+ * - internet connection is disabled
116+ * - no valid lookup server URL given
117+ * - lookup server was disabled by the admin
118+ * - max retries are reached (set to 5)
119+ *
120+ * @return bool
121+ */
122+ protected function shouldRemoveBackgroundJob (): bool {
123+ return $ this ->config ->getSystemValueBool ('has_internet_connection ' , true ) === false ||
124+ $ this ->config ->getSystemValueString ('lookup_server ' , 'https://lookup.nextcloud.com ' ) === '' ||
125+ $ this ->config ->getAppValue ('files_sharing ' , 'lookupServerUploadEnabled ' , 'yes ' ) !== 'yes ' ||
126+ $ this ->retries >= 5 ;
127+ }
128+
129+ protected function shouldRun (): bool {
130+ $ delay = 100 * 6 ** $ this ->retries ;
131+ return ($ this ->time ->getTime () - $ this ->lastRun ) > $ delay ;
132+ }
133+
134+ protected function run ($ argument ): void {
135+ $ user = $ this ->userManager ->get ($ this ->argument ['userId ' ]);
136+ if (!$ user instanceof IUser) {
137+ // User does not exist anymore
82138 return ;
83139 }
84140
141+ $ data = $ this ->getUserAccountData ($ user );
142+ $ signedData = $ this ->signer ->sign ('lookupserver ' , $ data , $ user );
85143 $ client = $ this ->clientService ->newClient ();
86144
87145 try {
88- $ client ->post ($ this ->lookupServer ,
89- [
90- 'body ' => json_encode ($ argument ['dataArray ' ]),
91- 'timeout ' => 10 ,
92- 'connect_timeout ' => 3 ,
93- ]
146+ if (count ($ data ) === 1 ) {
147+ // No public data, just the federation Id
148+ $ client ->delete ($ this ->lookupServer ,
149+ [
150+ 'body ' => json_encode ($ signedData ),
151+ 'timeout ' => 10 ,
152+ 'connect_timeout ' => 3 ,
153+ ]
154+ );
155+ } else {
156+ $ client ->post ($ this ->lookupServer ,
157+ [
158+ 'body ' => json_encode ($ signedData ),
159+ 'timeout ' => 10 ,
160+ 'connect_timeout ' => 3 ,
161+ ]
162+ );
163+ }
164+
165+ // Reset retry counter
166+ $ this ->config ->deleteUserValue (
167+ $ user ->getUID (),
168+ 'lookup_server_connector ' ,
169+ 'update_retries '
94170 );
171+
95172 } catch (\Exception $ e ) {
96- $ this ->jobList ->add (RetryJob::class,
97- [
98- 'dataArray ' => $ argument ['dataArray ' ],
99- 'retryNo ' => $ argument ['retryNo ' ] + 1 ,
100- 'lastRun ' => time (),
101- ]
173+ // An error occurred, retry later
174+ $ this ->retainJob = true ;
175+ $ this ->config ->setUserValue (
176+ $ user ->getUID (),
177+ 'lookup_server_connector ' ,
178+ 'update_retries ' ,
179+ $ this ->retries + 1
102180 );
103-
104181 }
105182 }
106183
107- /**
108- * test if it is time for the next run
109- *
110- * @param array $argument
111- * @return bool
112- */
113- protected function shouldRun ($ argument ) {
114- $ retryNo = (int )$ argument ['retryNo ' ];
115- $ delay = $ this ->interval * 6 ** $ retryNo ;
116- return !isset ($ argument ['lastRun ' ]) || ((time () - $ argument ['lastRun ' ]) > $ delay );
117- }
184+ protected function getUserAccountData (IUser $ user ): array {
185+ $ account = $ this ->accountManager ->getAccount ($ user );
118186
119- /**
120- * check if we should kill the background job
121- *
122- * The lookup server should no longer be contacted if:
123- *
124- * - max retries are reached (set to 5)
125- * - lookup server was disabled by the admin
126- * - no valid lookup server URL given
127- *
128- * @param int $retryCount
129- * @return bool
130- */
131- protected function killBackgroundJob ($ retryCount ) {
132- $ maxTriesReached = $ retryCount >= 5 ;
133- $ lookupServerDisabled = $ this ->config ->getAppValue ('files_sharing ' , 'lookupServerUploadEnabled ' , 'yes ' ) !== 'yes ' ;
187+ $ publicData = [];
188+ foreach ($ account ->getProperties () as $ property ) {
189+ if ($ property ->getScope () === IAccountManager::VISIBILITY_PUBLIC ) {
190+ $ publicData [$ property ->getName ()] = $ property ->getValue ();
191+ }
192+ }
193+
194+ $ data = ['federationId ' => $ user ->getCloudId ()];
195+ if (!empty ($ publicData )) {
196+ $ data ['name ' ] = $ publicData [IAccountManager::PROPERTY_DISPLAYNAME ]['value ' ] ?? '' ;
197+ $ data ['email ' ] = $ publicData [IAccountManager::PROPERTY_EMAIL ]['value ' ] ?? '' ;
198+ $ data ['address ' ] = $ publicData [IAccountManager::PROPERTY_ADDRESS ]['value ' ] ?? '' ;
199+ $ data ['website ' ] = $ publicData [IAccountManager::PROPERTY_WEBSITE ]['value ' ] ?? '' ;
200+ $ data ['twitter ' ] = $ publicData [IAccountManager::PROPERTY_TWITTER ]['value ' ] ?? '' ;
201+ $ data ['phone ' ] = $ publicData [IAccountManager::PROPERTY_PHONE ]['value ' ] ?? '' ;
202+ $ data ['twitter_signature ' ] = $ publicData [IAccountManager::PROPERTY_TWITTER ]['signature ' ] ?? '' ;
203+ $ data ['website_signature ' ] = $ publicData [IAccountManager::PROPERTY_WEBSITE ]['signature ' ] ?? '' ;
204+ $ data ['verificationStatus ' ] = [
205+ IAccountManager::PROPERTY_WEBSITE => $ publicData [IAccountManager::PROPERTY_WEBSITE ]['verified ' ] ?? '' ,
206+ IAccountManager::PROPERTY_TWITTER => $ publicData [IAccountManager::PROPERTY_TWITTER ]['verified ' ] ?? '' ,
207+ ];
208+ }
134209
135- return $ maxTriesReached || $ lookupServerDisabled || empty ( $ this -> lookupServer ) ;
210+ return $ data ;
136211 }
137212}
0 commit comments