Skip to content

Commit

Permalink
fixing local cloud id
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Aug 7, 2020
1 parent 1371b01 commit 58393fc
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 36 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 60 additions & 12 deletions lib/Command/CirclesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@

namespace OCA\Circles\Command;

use daita\MySmallPhpTools\Exceptions\RequestContentException;
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
use daita\MySmallPhpTools\Exceptions\RequestServerException;
use daita\MySmallPhpTools\Model\Request;
use daita\MySmallPhpTools\Traits\TArrayTools;
use daita\MySmallPhpTools\Traits\TRequest;
use Exception;
use OC\Core\Command\Base;
use OCA\Circles\Model\GlobalScale\GSEvent;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\GlobalScaleService;
use OCA\Circles\Service\GSUpstreamService;
use OCP\IL10N;
use Symfony\Component\Console\Input\InputArgument;
use OCP\IURLGenerator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -52,11 +58,15 @@ class CirclesTest extends Base {


use TArrayTools;
use TRequest;


/** @var IL10N */
private $l10n;

/** @var IURLGenerator */
private $urlGenerator;

/** @var GlobalScaleService */
private $globalScaleService;

Expand All @@ -67,21 +77,27 @@ class CirclesTest extends Base {
private $configService;


/** @var int */
private $delay = 5;


/**
* CirclesList constructor.
*
* @param IL10N $l10n
* @param IURLGenerator $urlGenerator
* @param GlobalScaleService $globalScaleService
* @param GSUpstreamService $gsUpstreamService
* @param ConfigService $configService
*/
public function __construct(
IL10N $l10n, GlobalScaleService $globalScaleService, GSUpstreamService $gsUpstreamService,
ConfigService $configService
IL10N $l10n, IURLGenerator $urlGenerator, GlobalScaleService $globalScaleService,
GSUpstreamService $gsUpstreamService, ConfigService $configService
) {
parent::__construct();

$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->gsUpstreamService = $gsUpstreamService;
$this->globalScaleService = $globalScaleService;
$this->configService = $configService;
Expand All @@ -92,7 +108,6 @@ protected function configure() {
parent::configure();
$this->setName('circles:test')
->setDescription('testing some features')
->addArgument('local', InputArgument::OPTIONAL, 'testing with a specific local cloud id')
->addOption('delay', 'd', InputOption::VALUE_REQUIRED, 'delay before checking result');
}

Expand All @@ -105,13 +120,12 @@ protected function configure() {
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
if ($input->getArgument('local')) {
define('TEMP_LOCAL_CLOUD_ID', $input->getArgument('local'));
if ($input->getOption('delay')) {
$this->delay = (int)$input->getOption('delay');
}

$delay = 5;
if ($input->getOption('delay')) {
$delay = (int)$input->getOption('delay');
if (!$this->testLocalAddress($output)) {
return 0;
}

$instances =
Expand All @@ -121,9 +135,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$test->setAsync(true);
$wrapper = $this->gsUpstreamService->newEvent($test);

$output->writeln('Async request is sent, now waiting ' . $delay . ' seconds');
sleep($delay);
$output->writeln('Pause is over, checking results for ' . $wrapper->getToken());
$output->writeln('- Async request is sent, now waiting ' . $this->delay . ' seconds');
sleep($this->delay);
$output->writeln('- Pause is over, checking results for ' . $wrapper->getToken());

$wrappers = $this->gsUpstreamService->getEventsByToken($wrapper->getToken());

Expand All @@ -146,5 +160,39 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}


/**
* @param OutputInterface $output
*
* @return bool
* @throws RequestContentException
* @throws RequestNetworkException
* @throws RequestResultSizeException
* @throws RequestServerException
*/
private function testLocalAddress(OutputInterface $output): bool {
$absolute = $this->urlGenerator->linkToRouteAbsolute('core.CSRFToken.index');
$output->write('- Simple request on ' . $absolute . ': ');

$request = new Request('', Request::TYPE_GET);
$request->setAddressFromUrl($absolute);
if (method_exists($request, 'setFollowLocation')) {
$request->setFollowLocation(false);
}

$this->doRequest($request);
$color = 'error';
if ($request->getResultCode() === 200) {
$color = 'info';
}
$output->writeln('<' . $color . '>' . $request->getResultCode() . '</' . $color . '>');

if ($request->getResultCode() === 200) {
return true;
}

return false;
}

}

4 changes: 0 additions & 4 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,6 @@ public function getTrustedDomains(): array {
* @return string
*/
public function getLocalCloudId(): string {
if (defined('TEMP_LOCAL_CLOUD_ID')) {
return TEMP_LOCAL_CLOUD_ID;
}

$localCloudId = $this->getAppValue(self::LOCAL_CLOUD_ID);
if ($localCloudId === '') {
return $this->getTrustedDomains()[0];
Expand Down
41 changes: 26 additions & 15 deletions lib/Service/GlobalScaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,12 @@ public function asyncBroadcast(GSEvent $event): GSWrapper {
$wrapper = $this->gsEventsRequest->create($wrapper);
}

$path = $this->urlGenerator->linkToRoute(
$absolute = $this->urlGenerator->linkToRouteAbsolute(
'circles.GlobalScale.asyncBroadcast', ['token' => $wrapper->getToken()]
);

$request = new Request($path, Request::TYPE_PUT);

$baseUrl = $this->urlGenerator->getBaseUrl();
if (substr($baseUrl, 0, 16) === 'http://localhost') {
$request->setBaseUrl(substr($baseUrl, 16));
$request->setAddress($this->configService->getLocalCloudId());
$request->setProtocols(['https', 'http']);
} else {
$request->setAddressFromUrl($baseUrl);
}
$request = new Request('', Request::TYPE_PUT);
$request->setAddressFromUrl($absolute);

try {
$this->doRequest($request);
Expand Down Expand Up @@ -228,7 +220,10 @@ public function getInstances(bool $all = false): array {
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
$request = new Request(ConfigService::GS_LOOKUP_INSTANCES, Request::TYPE_POST);

$user = $this->getRandomUser();
try {
$user = $this->getRandomUser();
} catch (NoUserException $e) {
}
$data = $this->signer->sign('lookupserver', ['federationId' => $user->getCloudId()], $user);
$request->setData($data);
$request->setAddressFromUrl($lookup);
Expand All @@ -242,8 +237,8 @@ public function getInstances(bool $all = false): array {

return [];
}
} catch (NoUserException | GSStatusException $e) {
$instances = [$this->configService->getLocalCloudId()];
} catch (GSStatusException $e) {
return $this->getLocalInstance($all);
}

if ($all) {
Expand All @@ -254,6 +249,22 @@ public function getInstances(bool $all = false): array {
}


/**
* @param bool $all
*
* @return array
*/
private function getLocalInstance(bool $all): array {
if (!$all) {
return [];
}

$absolute = $this->urlGenerator->linkToRouteAbsolute('circles.Navigation.navigate');

return [parse_url($absolute, PHP_URL_HOST)];
}


/**
* @param GSEvent $event
*
Expand Down Expand Up @@ -287,7 +298,7 @@ private function getRandomUser(): IUser {
return array_shift($random);
}

throw new NoUserException();
throw new NoUserException();
}

}
Expand Down
6 changes: 5 additions & 1 deletion lib/Service/MembersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function addMember($circleUniqueId, $ident, $type, string $instance, bool
}

$curr = $this->membersRequest->getMembers($circle->getUniqueId(), $circle->getHigherViewer(), $force);

$new = $this->addMassiveMembers($circle, $ident, $type);
if (empty($new)) {
$new = [$this->addSingleMember($circle, $ident, $type, $instance, $force)];
Expand Down Expand Up @@ -204,6 +204,10 @@ private function addSingleMember(Circle $circle, $ident, $type, $instance = '',
$new = $event->getMember();
$new->setJoined($this->l10n->t('now'));

if ($new->getInstance() === $this->configService->getLocalCloudId()) {
$new->setInstance('');
}

return $new;
}

Expand Down

0 comments on commit 58393fc

Please sign in to comment.