Merge pull request #184 from nextcloud/fill-follows-table

check installation on upgrade or account creation
pull/242/head
Maxence Lange 2018-12-18 15:40:10 -01:00 zatwierdzone przez GitHub
commit 50bb9cd0fc
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
6 zmienionych plików z 186 dodań i 11 usunięć

Wyświetl plik

@ -33,6 +33,12 @@
<nextcloud min-version="15" max-version="16"/>
</dependencies>
<repair-steps>
<post-migration>
<step>OCA\Social\Migration\CheckInstallation</step>
</post-migration>
</repair-steps>
<background-jobs>
<job>OCA\Social\Cron\Cache</job>
<job>OCA\Social\Cron\Queue</job>

8
composer.lock wygenerowano
Wyświetl plik

@ -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": [

Wyświetl plik

@ -151,6 +151,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;

Wyświetl plik

@ -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
*

Wyświetl plik

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Social Support
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
* @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 <http://www.gnu.org/licenses/>.
*
*/
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();
}
}

Wyświetl plik

@ -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;
}