Skip to content

Commit

Permalink
get circle from remote instance
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Feb 8, 2021
1 parent ce45b71 commit 702a347
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 67 deletions.
7 changes: 3 additions & 4 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@
['name' => 'Remote#test', 'url' => '/test', 'verb' => 'GET'],
['name' => 'Remote#event', 'url' => '/event', 'verb' => 'POST'],
['name' => 'Remote#incoming', 'url' => '/incoming', 'verb' => 'POST'],
['name' => 'Remote#circles', 'url' => '/circles', 'verb' => 'GET'],
['name' => 'Remote#circle', 'url' => '/circles/{circleId}', 'verb' => 'GET'],
['name' => 'Remote#members', 'url' => '/members', 'verb' => 'GET'],
['name' => 'Remote#member', 'url' => '/members/{memberId}', 'verb' => 'GET'],
['name' => 'Remote#circles', 'url' => '/circles/', 'verb' => 'GET'],
['name' => 'Remote#circle', 'url' => '/circle/{circleId}/', 'verb' => 'GET'],
['name' => 'Remote#members', 'url' => '/members/', 'verb' => 'GET'],
]
];
93 changes: 61 additions & 32 deletions lib/Command/CirclesSync.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);


/**
Expand Down Expand Up @@ -29,13 +31,15 @@

namespace OCA\Circles\Command;

use Exception;
use OC\Core\Command\Base;
use OCA\Circles\Exceptions\GSStatusException;
use OCA\Circles\Service\CirclesService;
use OCA\Circles\Service\GSUpstreamService;
use OCA\Circles\Service\MembersService;
use OCP\IL10N;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\FederatedUserService;
use OCA\Circles\Service\MemberService;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;


Expand All @@ -47,36 +51,36 @@
class CirclesSync extends Base {


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

/** @var MembersService */
private $membersService;
/** @var MemberService */
private $memberService;

/** @var CirclesService */
private $circlesService;
/** @var CircleService */
private $circleService;

/** @var GSUpstreamService */
private $gsUpstreamService;
/** @var ConfigService */
private $configService;


/**
* CirclesSync constructor.
*
* @param IL10N $l10n
* @param CirclesService $circlesService
* @param MembersService $membersService
* @param GSUpstreamService $gsUpstreamService
* @param FederatedUserService $federatedUserService
* @param CircleService $circlesService
* @param MemberService $membersService
* @param ConfigService $configService
*/
public function __construct(
IL10N $l10n, CirclesService $circlesService, MembersService $membersService,
GSUpstreamService $gsUpstreamService
FederatedUserService $federatedUserService, CircleService $circlesService,
MemberService $membersService, ConfigService $configService
) {
parent::__construct();
$this->l10n = $l10n;
$this->circlesService = $circlesService;
$this->membersService = $membersService;
$this->gsUpstreamService = $gsUpstreamService;
$this->federatedUserService = $federatedUserService;
$this->circleService = $circlesService;
$this->memberService = $membersService;
$this->configService = $configService;
}


Expand All @@ -86,7 +90,10 @@ public function __construct(
protected function configure() {
parent::configure();
$this->setName('circles:manage:sync')
->setDescription('sync circles and members');
->setDescription('Sync circles and members')
->addArgument('circle_id', InputArgument::OPTIONAL, 'ID of the circle', '')
->addOption('instance', '', InputOption::VALUE_REQUIRED, ' Instance of the circle', '')
->addOption('all', '', InputOption::VALUE_NONE, 'Sync all local circles');
}


Expand All @@ -95,20 +102,42 @@ protected function configure() {
* @param OutputInterface $output
*
* @return int
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$circles = $this->circlesService->getCirclesToSync();
foreach ($circles as $circle) {
$this->membersService->updateCachedFromCircle($circle);
$this->federatedUserService->bypassCurrentUserCondition(true);

// if ($input->getOption('all')) {
// $circles = [];
// foreach ($circles as $circle) {
// //$this->syncCircle($circle->getId());
// }
// } else {
// if ($circleId === '') {
// throw new Exception('missing circle_id or use --all option');
// }

$circleId = $input->getArgument('circle_id');
$instance = $input->getOption('instance');
if ($instance !== '') {
// $circle = $this->circleService->getCircle($circleId);
$this->circleService->syncRemoteCircle($circleId, $instance);
}

try {
$this->gsUpstreamService->synchronize($circles);
} catch (GSStatusException $e) {
}

// $circles = $this->circleService->getCirclesToSync();
// foreach ($circles as $circle) {
// $this->memberService->updateCachedFromCircle($circle);
// }
//
// try {
// $this->gsUpstreamService->synchronize($circles);
// } catch (GSStatusException $e) {
// }

return 0;
}


}

56 changes: 53 additions & 3 deletions lib/Controller/RemoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
use OCA\Circles\Exceptions\FederatedItemException;
use OCA\Circles\Model\Federated\FederatedEvent;
use OCA\Circles\Model\Federated\RemoteInstance;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\FederatedUserService;
use OCA\Circles\Service\RemoteDownstreamService;
use OCA\Circles\Service\RemoteService;
use OCP\AppFramework\Controller;
Expand Down Expand Up @@ -73,6 +75,12 @@ class RemoteController extends Controller {
/** @var RemoteDownstreamService */
private $remoteDownstreamService;

/** @var FederatedUserService */
private $federatedUserService;

/** @var CircleService */
private $circleService;

/** @var ConfigService */
private $configService;

Expand All @@ -85,16 +93,21 @@ class RemoteController extends Controller {
* @param CircleRequest $circleRequest
* @param RemoteService $remoteService
* @param RemoteDownstreamService $remoteDownstreamService
* @param FederatedUserService $federatedUserService
* @param CircleService $circleService
* @param ConfigService $configService
*/
public function __construct(
string $appName, IRequest $request, CircleRequest $circleRequest, RemoteService $remoteService,
RemoteDownstreamService $remoteDownstreamService, ConfigService $configService
RemoteDownstreamService $remoteDownstreamService, FederatedUserService $federatedUserService,
CircleService $circleService, ConfigService $configService
) {
parent::__construct($appName, $request);
$this->circleRequest = $circleRequest;
$this->remoteService = $remoteService;
$this->remoteDownstreamService = $remoteDownstreamService;
$this->federatedUserService = $federatedUserService;
$this->circleService = $circleService;
$this->configService = $configService;

$this->setup('app', 'circles');
Expand Down Expand Up @@ -140,6 +153,7 @@ public function event(): DataResponse {
public function incoming(): DataResponse {
try {
$event = $this->extractEventFromRequest();

$result = $this->remoteDownstreamService->incomingEvent($event);

return $this->success($result);
Expand Down Expand Up @@ -173,9 +187,44 @@ public function test(): DataResponse {
* @return DataResponse
*/
public function circles(): DataResponse {
$circles = $this->circleRequest->getFederated();
// $circles = $this->circleRequest->getFederated();
//
// try {
// /** @var Circle $circle */
// $circle = $this->extractItemFromRequest(Circle::class, $signed);
//// $event->setIncomingOrigin($signed->getOrigin());
//
// //$result = $this->remoteDownstreamService->incomingEvent($event);
//
// return $this->successObj($circle);
// } catch (Exception $e) {
// return $this->fail($e);
// }
}


return $this->success($circles, false);
/**
* @PublicPage
* @NoCSRFRequired
*
* @param string $circleId
*
* @return DataResponse
*/
public function circle(string $circleId): DataResponse {
try {
$signed = $this->remoteService->incomingSignedRequest($this->configService->getLocalInstance());

$remoteInstance = $this->remoteService->getCachedRemoteInstance($signed->getOrigin());
$this->federatedUserService->setRemoteInstance($remoteInstance);
$this->federatedUserService->bypassCurrentUserCondition(true);

$circle = $this->circleService->getCircle($circleId);

return $this->successObj($circle);
} catch (Exception $e) {
return $this->fail($e);
}
}


Expand All @@ -196,6 +245,7 @@ private function confirmRemoteInstance(NC21SignedRequest $signedRequest) {


/**
* @return FederatedEvent
* @throws InvalidOriginException
* @throws MalformedArrayException
* @throws SignatoryException
Expand Down
10 changes: 9 additions & 1 deletion lib/Db/CircleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCA\Circles\Exceptions\InvalidIdException;
use OCA\Circles\IFederatedUser;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\Federated\RemoteInstance;
use OCA\Circles\Model\Member;


Expand Down Expand Up @@ -111,17 +112,24 @@ public function getCircles(?Member $filter = null, ?IFederatedUser $initiator =
/**
* @param string $id
* @param IFederatedUser|null $initiator
* @param RemoteInstance|null $instance
*
* @return Circle
* @throws CircleNotFoundException
*/
public function getCircle(string $id, ?IFederatedUser $initiator = null): Circle {
public function getCircle(
string $id,
?IFederatedUser $initiator = null,
?RemoteInstance $instance = null
): Circle {
$qb = $this->getCircleSelectSql();
$qb->limitToUniqueId($id);
$qb->leftJoinOwner();

if (!is_null($initiator)) {
$qb->limitToInitiator($initiator);
} else if (!is_null($instance)) {
// TODO: filter based on RemoteInstance $instance
}

return $this->getItemFromRequest($qb);
Expand Down
25 changes: 25 additions & 0 deletions lib/Model/Federated/RemoteInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class RemoteInstance extends NC21Signatory implements INC21QueryRow, JsonSeriali
const INCOMING = 'incoming';
const EVENT = 'event';
const CIRCLES = 'circles';
const CIRCLE = 'circle';


/** @var int */
Expand All @@ -78,6 +79,9 @@ class RemoteInstance extends NC21Signatory implements INC21QueryRow, JsonSeriali
/** @var string */
private $circles = '';

/** @var string */
private $circle = '';

/** @var string */
private $members = '';

Expand Down Expand Up @@ -205,6 +209,25 @@ public function setCircles(string $circles): self {
}


/**
* @return string
*/
public function getCircle(): string {
return $this->circle;
}

/**
* @param string $circle
*
* @return self
*/
public function setCircle(string $circle): self {
$this->circle = $circle;

return $this;
}


/**
* @return string
*/
Expand Down Expand Up @@ -317,6 +340,7 @@ public function import(array $data): NC21Signatory {
$this->setEvent($this->get('event', $data));
$this->setIncoming($this->get('incoming', $data));
$this->setCircles($this->get('circles', $data));
$this->setCircle($this->get('circle', $data));
$this->setMembers($this->get('members', $data));
$this->setAuthSigned($this->get('auth-signed', $data));
$this->setUid($this->get('uid', $data));
Expand All @@ -335,6 +359,7 @@ public function jsonSerialize(): array {
'incoming' => $this->getIncoming(),
'test' => $this->getTest(),
'circles' => $this->getCircles(),
'circle' => $this->getCircle(),
'members' => $this->getMembers(),
];

Expand Down
Loading

0 comments on commit 702a347

Please sign in to comment.