From 995d611060b6d38808bc1eefe44f29fcd562a050 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Sat, 8 Dec 2018 18:43:34 -0100 Subject: [PATCH] check installation on upgrade or account creation --- appinfo/info.xml | 7 ++ composer.lock | 8 +-- lib/Controller/NavigationController.php | 1 + lib/Db/FollowsRequest.php | 15 ++++- lib/Migration/CheckInstallation.php | 80 +++++++++++++++++++++++ lib/Service/CheckService.php | 87 +++++++++++++++++++++++-- 6 files changed, 187 insertions(+), 11 deletions(-) create mode 100644 lib/Migration/CheckInstallation.php diff --git a/appinfo/info.xml b/appinfo/info.xml index f8d819ed..56754da6 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -32,6 +32,13 @@ + + + + OCA\Social\Migration\CheckInstallation + + + Social diff --git a/composer.lock b/composer.lock index 9b755307..5bc891fc 100644 --- a/composer.lock +++ b/composer.lock @@ -12,12 +12,12 @@ "source": { "type": "git", "url": "https://github.com/daita/my-small-php-tools.git", - "reference": "7a61e966ac2a01db6fd0096f77620a7db978af18" + "reference": "405a5e6afadfd7c0630cf33b9e48a39f6938f605" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/daita/my-small-php-tools/zipball/7a61e966ac2a01db6fd0096f77620a7db978af18", - "reference": "7a61e966ac2a01db6fd0096f77620a7db978af18", + "url": "https://api.github.com/repos/daita/my-small-php-tools/zipball/405a5e6afadfd7c0630cf33b9e48a39f6938f605", + "reference": "405a5e6afadfd7c0630cf33b9e48a39f6938f605", "shasum": "" }, "require": { @@ -40,7 +40,7 @@ } ], "description": "My small PHP Tools", - "time": "2018-12-04T10:09:31+00:00" + "time": "2018-12-08T15:17:26+00:00" } ], "packages-dev": [], diff --git a/lib/Controller/NavigationController.php b/lib/Controller/NavigationController.php index 33880792..4c7e6e67 100644 --- a/lib/Controller/NavigationController.php +++ b/lib/Controller/NavigationController.php @@ -154,6 +154,7 @@ class NavigationController extends Controller { try { $data['serverData']['cloudAddress'] = $this->configService->getCloudAddress(); } catch (SocialAppConfigException $e) { + $this->checkService->checkInstallationStatus(); $cloudAddress = $this->setupCloudAddress(); if ($cloudAddress !== ''){ $data['serverData']['cloudAddress'] = $cloudAddress; diff --git a/lib/Db/FollowsRequest.php b/lib/Db/FollowsRequest.php index fac47cdb..6124fc56 100644 --- a/lib/Db/FollowsRequest.php +++ b/lib/Db/FollowsRequest.php @@ -34,7 +34,6 @@ namespace OCA\Social\Db; use daita\MySmallPhpTools\Traits\TArrayTools; use DateTime; use OCA\Social\Exceptions\FollowDoesNotExistException; -use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\ActivityPub\Follow; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -144,6 +143,20 @@ class FollowsRequest extends FollowsRequestBuilder { } + /** + * @return int + */ + public function countFollows() { + $qb = $this->countFollowsSelectSql(); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + return $this->getInt('count', $data, 0); + } + + /** * @param string $followId * diff --git a/lib/Migration/CheckInstallation.php b/lib/Migration/CheckInstallation.php new file mode 100644 index 00000000..a3b58915 --- /dev/null +++ b/lib/Migration/CheckInstallation.php @@ -0,0 +1,80 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Migration; + + +use OCA\Social\Service\CheckService; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; + + +/** + * Class CheckInstallation + * + * @package OCA\Social\Migration + */ +class CheckInstallation implements IRepairStep { + + + /** @var CheckService */ + protected $checkService; + + + /** + * CheckInstallation constructor. + * + * @param CheckService $checkService + */ + public function __construct(CheckService $checkService) { + $this->checkService = $checkService; + } + + + /** + * Returns the step's name + * + * @return string + * @since 9.1.0 + */ + public function getName() { + return 'Check the installation of the Social app.'; + } + + + /** + * @param IOutput $output + */ + public function run(IOutput $output) { + $this->checkService->checkInstallationStatus(); + } + + +} diff --git a/lib/Service/CheckService.php b/lib/Service/CheckService.php index dea98418..306f13e0 100644 --- a/lib/Service/CheckService.php +++ b/lib/Service/CheckService.php @@ -24,6 +24,9 @@ namespace OCA\Social\Service; +use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Db\FollowsRequest; +use OCA\Social\Model\ActivityPub\Follow; use OCP\AppFramework\Http; use OCP\Http\Client\IClientService; use OCP\ICache; @@ -31,25 +34,56 @@ use OCP\IConfig; use OCP\IRequest; use OCP\IURLGenerator; + +/** + * Class CheckService + * + * @package OCA\Social\Service + */ class CheckService { + + use TStringTools; + + private $cache; private $config; private $clientService; private $request; private $urlGenerator; + /** @var FollowsRequest */ + private $followRequest; + const CACHE_PREFIX = 'social_check_'; - public function __construct(ICache $cache, IConfig $config, IClientService $clientService, IRequest $request, IURLGenerator $urlGenerator) { + /** + * CheckService constructor. + * + * @param ICache $cache + * @param IConfig $config + * @param IClientService $clientService + * @param IRequest $request + * @param IURLGenerator $urlGenerator + * @param FollowsRequest $followRequest + */ + public function __construct( + ICache $cache, IConfig $config, IClientService $clientService, IRequest $request, + IURLGenerator $urlGenerator, FollowsRequest $followRequest + ) { $this->cache = $cache; $this->config = $config; $this->clientService = $clientService; $this->request = $request; $this->urlGenerator = $urlGenerator; + $this->followRequest = $followRequest; } + + /** + * @return array + */ public function checkDefault(): array { $checks = []; $checks['wellknown'] = $this->checkWellKnown(); @@ -60,13 +94,19 @@ class CheckService { $success = false; } } + return [ 'success' => $success, - 'checks' => $checks + 'checks' => $checks ]; } + + + /** + * @return bool + */ public function checkWellKnown(): bool { - $state = (bool) ($this->cache->get(self::CACHE_PREFIX . 'wellknown') === 'true'); + $state = (bool)($this->cache->get(self::CACHE_PREFIX . 'wellknown') === 'true'); if ($state === true) { return true; } @@ -77,7 +117,9 @@ class CheckService { return true; } - if ($this->requestWellKnown($this->request->getServerProtocol() . '://' . $this->request->getServerHost())) { + if ($this->requestWellKnown( + $this->request->getServerProtocol() . '://' . $this->request->getServerHost() + )) { return true; } @@ -88,17 +130,50 @@ class CheckService { return false; } - private function requestWellKnown($base) { + + /** + * + */ + public function checkInstallationStatus() { + $this->checkStatusTableFollows(); + } + + + public function checkStatusTableFollows() { + if ($this->followRequest->countFollows() > 0) { + return; + } + + $follow = new Follow(); + $follow->setId($this->uuid()); + $follow->setType('Unknown'); + $follow->setActorId($this->uuid()); + $follow->setObjectId($this->uuid()); + $follow->setFollowId($this->uuid()); + + $this->followRequest->save($follow); + } + + + /** + * @param string $base + * + * @return bool + */ + private function requestWellKnown(string $base) { try { $url = $base . '/.well-known/webfinger'; - $response = $this->clientService->newClient()->get($url); + $response = $this->clientService->newClient() + ->get($url); if ($response->getStatusCode() === Http::STATUS_OK) { $this->cache->set(self::CACHE_PREFIX . 'wellknown', 'true', 3600); + return true; } } catch (\GuzzleHttp\Exception\ClientException $e) { } catch (\Exception $e) { } + return false; }