Skip to content

Commit

Permalink
first draft
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed May 17, 2022
1 parent 323c8c4 commit fb59325
Show file tree
Hide file tree
Showing 52 changed files with 5,878 additions and 479 deletions.
1 change: 1 addition & 0 deletions .php_cs.cache

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Those groups of users (or "circles") can then be used by any other app for shari
<command>OCA\Circles\Command\MembersDetails</command>
<command>OCA\Circles\Command\MembersLevel</command>
<command>OCA\Circles\Command\MembersRemove</command>
<!-- <command>OCA\Circles\Command\SyncContact</command>-->
<command>OCA\Circles\Command\CirclesDebug</command>
</commands>

<!-- <activity>-->
Expand Down
7 changes: 6 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
['name' => 'Remote#members', 'url' => '/members/{circleId}/', 'verb' => 'GET'],
['name' => 'Remote#member', 'url' => '/member/{type}/{userId}/', 'verb' => 'GET'],
['name' => 'Remote#inherited', 'url' => '/inherited/{circleId}/', 'verb' => 'GET'],
['name' => 'Remote#memberships', 'url' => '/memberships/{circleId}/', 'verb' => 'GET']
['name' => 'Remote#memberships', 'url' => '/memberships/{circleId}/', 'verb' => 'GET'],

['name' => 'Remote#syncItem', 'url' => '/sync/item', 'verb' => 'POST'],
['name' => 'Remote#syncShare', 'url' => '/sync/share', 'verb' => 'POST'],
['name' => 'Remote#debugDaemon', 'url' => '/debug', 'verb' => 'POST']

]
];
493 changes: 493 additions & 0 deletions drafts/federated_sync.md

Large diffs are not rendered by default.

242 changes: 242 additions & 0 deletions lib/CircleSharesManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<?php

declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <[email protected]>
* @copyright 2022
* @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\Circles;

use Exception;
use OCA\Circles\Exceptions\CircleSharesManagerException;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\DebugService;
use OCA\Circles\Service\FederatedSyncItemService;
use OCA\Circles\Service\FederatedSyncService;
use OCA\Circles\Service\FederatedSyncShareService;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
* Class CircleSharesManager
*
* @package OCA\Circles
*/
class CircleSharesManager implements ICircleSharesManager {


private CircleService $circleService;
private FederatedSyncService $federatedSyncService;
private FederatedSyncItemService $federatedSyncItemService;
private FederatedSyncShareService $federatedSyncShareService;
private ConfigService $configService;
private DebugService $debugService;

private string $originAppId = '';
private string $originItemType = '';


/**
* @param CircleService $circleService
* @param FederatedSyncItemService $federatedSyncItemService
* @param FederatedSyncShareService $federatedSyncShareService
* @param ConfigService $configService
*/
public function __construct(
CircleService $circleService,
FederatedSyncService $federatedSyncService,
FederatedSyncItemService $federatedSyncItemService,
FederatedSyncShareService $federatedSyncShareService,
ConfigService $configService,
DebugService $debugService
) {
$this->circleService = $circleService;
$this->federatedSyncService = $federatedSyncService;
$this->federatedSyncItemService = $federatedSyncItemService;
$this->federatedSyncShareService = $federatedSyncShareService;
$this->configService = $configService;
$this->debugService = $debugService;
}


/**
* @param string $syncManager
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function registerFederatedSyncManager(string $syncManager): void {
if ($this->originAppId !== '' || $this->originItemType !== '') {
return;
}

$federatedSyncManager = \OC::$server->get($syncManager);
if (!($federatedSyncManager instanceof IFederatedSyncManager)) {
// log something
return;
}

$this->federatedSyncService->addFederatedSyncManager($federatedSyncManager);
}


/**
* @param string $itemId
* @param string $circleId
* @param array $extraData
*
* @throws CircleSharesManagerException
* @throws Exceptions\CircleNotFoundException
* @throws Exceptions\FederatedSyncConflictException
* @throws Exceptions\FederatedSyncManagerNotFoundException
* @throws Exceptions\InitiatorNotFoundException
* @throws Exceptions\RequestBuilderException
* @throws Exceptions\SyncedSharedAlreadyExistException
*/
public function createShare(
string $itemId,
string $circleId,
array $extraData = []
): void {
$this->debugService->setDebugType('federated_sync');
$this->debugService->info('New request to create a share', $circleId, [
'appId' => $this->originAppId,
'itemType' => $this->originItemType,
'itemId' => $itemId,
'extraData' => $extraData
]);

try {
$this->mustHaveOrigin();

// TODO: verify rules that apply when sharing to a circle
$probe = new CircleProbe();
$probe->includeSystemCircles()
->mustBeMember();

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

// get valid SyncedItem based on appId, itemType, itemId
$syncedItem = $this->federatedSyncItemService->getSyncedItem(
$this->originAppId,
$this->originItemType,
$itemId
);

$this->debugService->info(
'Initiating the process of sharing {syncedItem.singleId} to {circle.id}',
$circleId, [
'circle' => $circle,
'syncedItem' => $syncedItem,
'extraData' => $extraData
]
);

// confirm item is local
if (!$syncedItem->isLocal()) {
// TODO: sharing a remote item
return;
}

$this->federatedSyncShareService->createShare($syncedItem, $circle, $extraData);
} catch (Exception $e) {
$this->debugService->exception($e, $circleId);
throw $e;
}
// this->$this->federatedItemService->getSharedItem
}

/**
* @param string $itemId
* @param string $circleId
* @param array $extraData
*
* @throws CircleSharesManagerException
*/
public function updateShare(
string $itemId,
string $circleId,
array $extraData = []
): void {
$this->mustHaveOrigin();
}

/**
* @param string $itemId
* @param string $circleId
*
* @throws CircleSharesManagerException
*/
public function deleteShare(string $itemId, string $circleId): void {
$this->mustHaveOrigin();
}

/**
* @param string $itemId
* @param array $serializedData
*/
public function updateItem(
string $itemId,
array $serializedData
): void {
$this->mustHaveOrigin();
}

/**
* @param string $itemId
*
* @throws CircleSharesManagerException
*/
public function deleteItem(string $itemId): void {
$this->mustHaveOrigin();
}


/**
* @param string $appId
* @param string $itemType
*/
public function setOrigin(string $appId, string $itemType) {
$this->originAppId = $appId;
$this->originItemType = $itemType;
}

/**
* @throws CircleSharesManagerException
*/
private function mustHaveOrigin(): void {
if ($this->originAppId !== '' && $this->originItemType !== '') {
return;
}

throw new CircleSharesManagerException(
'ICirclesManager::getShareManager(appId, itemType) used empty params'
);
}
}
52 changes: 29 additions & 23 deletions lib/CirclesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,22 @@
use OCA\Circles\Service\MembershipService;
use OCA\Circles\Tools\Exceptions\InvalidItemException;

/**
* Class CirclesManager
*
* @package OCA\Circles
*/
class CirclesManager {


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

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

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

/** @var MembershipService */
private $membershipService;

/** @var ConfigService */
private $configService;
class CirclesManager {

/** @var CirclesQueryHelper */
private $circlesQueryHelper;
private CircleSharesManager $circleSharesManager;
private FederatedUserService $federatedUserService;
private CircleService $circleService;
private MemberService $memberService;
private MembershipService $membershipService;
private ConfigService $configService;
private CirclesQueryHelper $circlesQueryHelper;


/**
* CirclesManager constructor.
*
* @param CircleSharesManager $circleSharesManager
* @param FederatedUserService $federatedUserService
* @param CircleService $circleService
* @param MemberService $memberService
Expand All @@ -102,13 +88,15 @@ class CirclesManager {
* @param CirclesQueryHelper $circlesQueryHelper
*/
public function __construct(
CircleSharesManager $circleSharesManager,
FederatedUserService $federatedUserService,
CircleService $circleService,
MemberService $memberService,
MembershipService $membershipService,
ConfigService $configService,
CirclesQueryHelper $circlesQueryHelper
) {
$this->circleSharesManager = $circleSharesManager;
$this->federatedUserService = $federatedUserService;
$this->circleService = $circleService;
$this->memberService = $memberService;
Expand All @@ -118,6 +106,24 @@ public function __construct(
}


/**
* @param string $appId
* @param string $itemType
*
* @return CircleSharesManager
*/
public function getShareManager(string $appId = '', string $itemType = ''): ICircleSharesManager {
if ($appId === '') {
return $this->circleSharesManager;
}

$clone = clone $this->circleSharesManager;
$clone->setOrigin($appId, $itemType);

return $clone;
}


/**
* @param string $federatedId
* @param int $type
Expand Down
Loading

0 comments on commit fb59325

Please sign in to comment.