RetryJob.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\ILogger;
  40. use OCP\IUser;
  41. use OCP\IUserManager;
  42. class RetryJob extends Job {
  43. private IClientService $clientService;
  44. private string $lookupServer;
  45. private IConfig $config;
  46. private IUserManager $userManager;
  47. private IAccountManager $accountManager;
  48. private Signer $signer;
  49. protected int $retries = 0;
  50. protected bool $retainJob = false;
  51. /**
  52. * @param ITimeFactory $time
  53. * @param IClientService $clientService
  54. * @param IConfig $config
  55. * @param IUserManager $userManager
  56. * @param IAccountManager $accountManager
  57. * @param Signer $signer
  58. */
  59. public function __construct(ITimeFactory $time,
  60. IClientService $clientService,
  61. IConfig $config,
  62. IUserManager $userManager,
  63. IAccountManager $accountManager,
  64. Signer $signer) {
  65. parent::__construct($time);
  66. $this->clientService = $clientService;
  67. $this->config = $config;
  68. $this->userManager = $userManager;
  69. $this->accountManager = $accountManager;
  70. $this->signer = $signer;
  71. $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
  72. if (!empty($this->lookupServer)) {
  73. $this->lookupServer = rtrim($this->lookupServer, '/');
  74. $this->lookupServer .= '/users';
  75. }
  76. }
  77. /**
  78. * Run the job, then remove it from the jobList
  79. */
  80. public function start(IJobList $jobList): void {
  81. if (!isset($this->argument['userId'])) {
  82. // Old background job without user id, just drop it.
  83. $jobList->remove($this, $this->argument);
  84. return;
  85. }
  86. $this->retries = (int) $this->config->getUserValue($this->argument['userId'], 'lookup_server_connector', 'update_retries', '0');
  87. if ($this->shouldRemoveBackgroundJob()) {
  88. $jobList->remove($this, $this->argument);
  89. return;
  90. }
  91. if ($this->shouldRun()) {
  92. parent::start($jobList);
  93. if (!$this->retainJob) {
  94. $jobList->remove($this, $this->argument);
  95. }
  96. }
  97. }
  98. /**
  99. * Check if we should kill the background job:
  100. *
  101. * - internet connection is disabled
  102. * - no valid lookup server URL given
  103. * - lookup server was disabled by the admin
  104. * - max retries are reached (set to 5)
  105. */
  106. protected function shouldRemoveBackgroundJob(): bool {
  107. return $this->config->getSystemValueBool('has_internet_connection', true) === false ||
  108. $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') === '' ||
  109. $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' ||
  110. $this->retries >= 5;
  111. }
  112. protected function shouldRun(): bool {
  113. $delay = 100 * 6 ** $this->retries;
  114. return ($this->time->getTime() - $this->lastRun) > $delay;
  115. }
  116. protected function run($argument): void {
  117. $user = $this->userManager->get($this->argument['userId']);
  118. if (!$user instanceof IUser) {
  119. // User does not exist anymore
  120. return;
  121. }
  122. $data = $this->getUserAccountData($user);
  123. $signedData = $this->signer->sign('lookupserver', $data, $user);
  124. $client = $this->clientService->newClient();
  125. try {
  126. if (count($data) === 1) {
  127. $dataOnLookupServer = $this->config->getUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '0') === '1';
  128. if (!$dataOnLookupServer) {
  129. // We never send data to the lookupserver so no need to delete it
  130. return;
  131. }
  132. // There is data on the lookup server so we must delete it
  133. $client->delete($this->lookupServer,
  134. [
  135. 'body' => json_encode($signedData),
  136. 'timeout' => 10,
  137. 'connect_timeout' => 3,
  138. ]
  139. );
  140. $this->config->setUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '0');
  141. } else {
  142. $client->post($this->lookupServer,
  143. [
  144. 'body' => json_encode($signedData),
  145. 'timeout' => 10,
  146. 'connect_timeout' => 3,
  147. ]
  148. );
  149. $this->config->setUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '1');
  150. }
  151. // Reset retry counter
  152. $this->config->deleteUserValue(
  153. $user->getUID(),
  154. 'lookup_server_connector',
  155. 'update_retries'
  156. );
  157. } catch (\Exception $e) {
  158. // An error occurred, retry later
  159. $this->retainJob = true;
  160. $this->config->setUserValue(
  161. $user->getUID(),
  162. 'lookup_server_connector',
  163. 'update_retries',
  164. $this->retries + 1
  165. );
  166. }
  167. }
  168. protected function getUserAccountData(IUser $user): array {
  169. $account = $this->accountManager->getAccount($user);
  170. $publicData = [];
  171. foreach ($account->getProperties() as $property) {
  172. if ($property->getScope() === IAccountManager::SCOPE_PUBLISHED) {
  173. $publicData[$property->getName()] = $property->getValue();
  174. }
  175. }
  176. $data = ['federationId' => $user->getCloudId()];
  177. if (!empty($publicData)) {
  178. $data['name'] = $publicData[IAccountManager::PROPERTY_DISPLAYNAME]['value'] ?? '';
  179. $data['email'] = $publicData[IAccountManager::PROPERTY_EMAIL]['value'] ?? '';
  180. $data['address'] = $publicData[IAccountManager::PROPERTY_ADDRESS]['value'] ?? '';
  181. $data['website'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['value'] ?? '';
  182. $data['twitter'] = $publicData[IAccountManager::PROPERTY_TWITTER]['value'] ?? '';
  183. $data['phone'] = $publicData[IAccountManager::PROPERTY_PHONE]['value'] ?? '';
  184. $data['twitter_signature'] = $publicData[IAccountManager::PROPERTY_TWITTER]['signature'] ?? '';
  185. $data['website_signature'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['signature'] ?? '';
  186. $data['verificationStatus'] = [
  187. IAccountManager::PROPERTY_WEBSITE => $publicData[IAccountManager::PROPERTY_WEBSITE]['verified'] ?? '',
  188. IAccountManager::PROPERTY_TWITTER => $publicData[IAccountManager::PROPERTY_TWITTER]['verified'] ?? '',
  189. ];
  190. }
  191. return $data;
  192. }
  193. }