RetryJob.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  5. * @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com>
  6. *
  7. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  8. * @author Bjoern Schiessle <bjoern@schiessle.org>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license GNU AGPL version 3 or any later version
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as
  19. * published by the Free Software Foundation, either version 3 of the
  20. * License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. */
  31. namespace OCA\LookupServerConnector\BackgroundJobs;
  32. use OC\Security\IdentityProof\Signer;
  33. use OCP\Accounts\IAccountManager;
  34. use OCP\AppFramework\Utility\ITimeFactory;
  35. use OCP\BackgroundJob\IJobList;
  36. use OCP\BackgroundJob\Job;
  37. use OCP\Http\Client\IClientService;
  38. use OCP\IConfig;
  39. use OCP\IUser;
  40. use OCP\IUserManager;
  41. class RetryJob extends Job {
  42. private IClientService $clientService;
  43. private string $lookupServer;
  44. private IConfig $config;
  45. private IUserManager $userManager;
  46. private IAccountManager $accountManager;
  47. private Signer $signer;
  48. protected int $retries = 0;
  49. protected bool $retainJob = false;
  50. /**
  51. * @param ITimeFactory $time
  52. * @param IClientService $clientService
  53. * @param IConfig $config
  54. * @param IUserManager $userManager
  55. * @param IAccountManager $accountManager
  56. * @param Signer $signer
  57. */
  58. public function __construct(ITimeFactory $time,
  59. IClientService $clientService,
  60. IConfig $config,
  61. IUserManager $userManager,
  62. IAccountManager $accountManager,
  63. Signer $signer) {
  64. parent::__construct($time);
  65. $this->clientService = $clientService;
  66. $this->config = $config;
  67. $this->userManager = $userManager;
  68. $this->accountManager = $accountManager;
  69. $this->signer = $signer;
  70. $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
  71. if (!empty($this->lookupServer)) {
  72. $this->lookupServer = rtrim($this->lookupServer, '/');
  73. $this->lookupServer .= '/users';
  74. }
  75. }
  76. /**
  77. * Run the job, then remove it from the jobList
  78. */
  79. public function start(IJobList $jobList): void {
  80. if (!isset($this->argument['userId'])) {
  81. // Old background job without user id, just drop it.
  82. $jobList->remove($this, $this->argument);
  83. return;
  84. }
  85. $this->retries = (int) $this->config->getUserValue($this->argument['userId'], 'lookup_server_connector', 'update_retries', '0');
  86. if ($this->shouldRemoveBackgroundJob()) {
  87. $jobList->remove($this, $this->argument);
  88. return;
  89. }
  90. if ($this->shouldRun()) {
  91. parent::start($jobList);
  92. if (!$this->retainJob) {
  93. $jobList->remove($this, $this->argument);
  94. }
  95. }
  96. }
  97. /**
  98. * Check if we should kill the background job:
  99. *
  100. * - internet connection is disabled
  101. * - no valid lookup server URL given
  102. * - lookup server was disabled by the admin
  103. * - max retries are reached (set to 5)
  104. */
  105. protected function shouldRemoveBackgroundJob(): bool {
  106. return $this->config->getSystemValueBool('has_internet_connection', true) === false ||
  107. $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') === '' ||
  108. $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' ||
  109. $this->retries >= 5;
  110. }
  111. protected function shouldRun(): bool {
  112. $delay = 100 * 6 ** $this->retries;
  113. return ($this->time->getTime() - $this->lastRun) > $delay;
  114. }
  115. protected function run($argument): void {
  116. $user = $this->userManager->get($this->argument['userId']);
  117. if (!$user instanceof IUser) {
  118. // User does not exist anymore
  119. return;
  120. }
  121. $data = $this->getUserAccountData($user);
  122. $signedData = $this->signer->sign('lookupserver', $data, $user);
  123. $client = $this->clientService->newClient();
  124. try {
  125. if (count($data) === 1) {
  126. $dataOnLookupServer = $this->config->getUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '0') === '1';
  127. if (!$dataOnLookupServer) {
  128. // We never send data to the lookupserver so no need to delete it
  129. return;
  130. }
  131. // There is data on the lookup server so we must delete it
  132. $client->delete($this->lookupServer,
  133. [
  134. 'body' => json_encode($signedData),
  135. 'timeout' => 10,
  136. 'connect_timeout' => 3,
  137. ]
  138. );
  139. $this->config->setUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '0');
  140. } else {
  141. $client->post($this->lookupServer,
  142. [
  143. 'body' => json_encode($signedData),
  144. 'timeout' => 10,
  145. 'connect_timeout' => 3,
  146. ]
  147. );
  148. $this->config->setUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '1');
  149. }
  150. // Reset retry counter
  151. $this->config->deleteUserValue(
  152. $user->getUID(),
  153. 'lookup_server_connector',
  154. 'update_retries'
  155. );
  156. } catch (\Exception $e) {
  157. // An error occurred, retry later
  158. $this->retainJob = true;
  159. $this->config->setUserValue(
  160. $user->getUID(),
  161. 'lookup_server_connector',
  162. 'update_retries',
  163. $this->retries + 1
  164. );
  165. }
  166. }
  167. protected function getUserAccountData(IUser $user): array {
  168. $account = $this->accountManager->getAccount($user);
  169. $publicData = [];
  170. foreach ($account->getProperties() as $property) {
  171. if ($property->getScope() === IAccountManager::SCOPE_PUBLISHED) {
  172. $publicData[$property->getName()] = $property->getValue();
  173. }
  174. }
  175. $data = ['federationId' => $user->getCloudId()];
  176. if (!empty($publicData)) {
  177. $data['name'] = $publicData[IAccountManager::PROPERTY_DISPLAYNAME]['value'] ?? '';
  178. $data['email'] = $publicData[IAccountManager::PROPERTY_EMAIL]['value'] ?? '';
  179. $data['address'] = $publicData[IAccountManager::PROPERTY_ADDRESS]['value'] ?? '';
  180. $data['website'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['value'] ?? '';
  181. $data['twitter'] = $publicData[IAccountManager::PROPERTY_TWITTER]['value'] ?? '';
  182. $data['phone'] = $publicData[IAccountManager::PROPERTY_PHONE]['value'] ?? '';
  183. $data['twitter_signature'] = $publicData[IAccountManager::PROPERTY_TWITTER]['signature'] ?? '';
  184. $data['website_signature'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['signature'] ?? '';
  185. $data['verificationStatus'] = [
  186. IAccountManager::PROPERTY_WEBSITE => $publicData[IAccountManager::PROPERTY_WEBSITE]['verified'] ?? '',
  187. IAccountManager::PROPERTY_TWITTER => $publicData[IAccountManager::PROPERTY_TWITTER]['verified'] ?? '',
  188. ];
  189. }
  190. return $data;
  191. }
  192. }