Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Federated Sync #1034

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 11 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
],

'routes' => [
['name' => 'EventWrapper#asyncBroadcast', 'url' => '/async/{token}/', 'verb' => 'POST'],
['name' => 'EventWrapper#asyncBroadcast', 'url' => '/async/broadcast/{token}', 'verb' => 'POST'],
['name' => 'EventWrapper#asyncInternal', 'url' => '/async/internal/{token}', 'verb' => 'POST'],

['name' => 'Remote#appService', 'url' => '/', 'verb' => 'GET'],
['name' => 'Remote#test', 'url' => '/test', 'verb' => 'GET'],
Expand All @@ -112,6 +113,14 @@
['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' => 'Sync#syncItem', 'url' => '/sync/item', 'verb' => 'GET'],
['name' => 'Sync#updateSyncedItem', 'url' => '/sync/item', 'verb' => 'PUT'],
// ['name' => 'Remote#syncItem', 'url' => '/sync/item/{singleId}', 'verb' => 'GET'],
['name' => 'Sync#syncShare', 'url' => '/sync/share', 'verb' => 'GET'],
['name' => 'Sync#createSyncedShare', 'url' => '/sync/share', 'verb' => 'PUT'],
['name' => 'Debug#debugDaemon', 'url' => '/debug', 'verb' => 'POST']

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

Large diffs are not rendered by default.

Binary file added drafts/flowchart_updateitem_31052022.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
318 changes: 318 additions & 0 deletions lib/CircleSharesManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
<?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\Exceptions\FederatedUserNotFoundException;
use OCA\Circles\Model\SyncedItemLock;
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 OCA\Circles\Service\FederatedUserService;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

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


private CircleService $circleService;
private FederatedUserService $federatedUserService;
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 FederatedUserService $federatedUserService
* @param FederatedSyncService $federatedSyncService
* @param FederatedSyncItemService $federatedSyncItemService
* @param FederatedSyncShareService $federatedSyncShareService
* @param ConfigService $configService
* @param DebugService $debugService
*/
public function __construct(
CircleService $circleService,
FederatedUserService $federatedUserService,
FederatedSyncService $federatedSyncService,
FederatedSyncItemService $federatedSyncItemService,
FederatedSyncShareService $federatedSyncShareService,
ConfigService $configService,
DebugService $debugService
) {
$this->circleService = $circleService;
$this->federatedUserService = $federatedUserService;
$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 SyncedShare} based on {appId}.{itemType}.{itemId}',
$circleId,
[
'appId' => $this->originAppId,
'itemType' => $this->originItemType,
'itemId' => $itemId,
'extraData' => $extraData
]
);

try {
$this->mustHaveOrigin();

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

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

$federatedUser = $this->federatedUserService->getCurrentEntity();
if (!$federatedUser->isLocal()) {
throw new FederatedUserNotFoundException('unknown local user');
}

$this->federatedSyncShareService->requestSyncedShareCreation(
$federatedUser,
$syncedItem,
$circleId,
$extraData
);
} catch (Exception $e) {
$this->debugService->exception($e, $circleId);
throw $e;
}
}

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

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

/**
* @param string $itemId
* @param string $updateType
* @param string $updateTypeId
* @param array $extraData
* @param bool $sumCheck
*
* @throws CircleSharesManagerException
* @throws Exceptions\FederatedEventException
* @throws Exceptions\FederatedItemException
* @throws Exceptions\FederatedSyncConflictException
* @throws Exceptions\FederatedSyncManagerNotFoundException
* @throws Exceptions\InitiatorNotConfirmedException
* @throws Exceptions\OwnerNotFoundException
* @throws Exceptions\RemoteInstanceException
* @throws Exceptions\RemoteNotFoundException
* @throws Exceptions\RemoteResourceNotFoundException
* @throws Exceptions\RequestBuilderException
* @throws Exceptions\UnknownRemoteException
*/
public function updateItem(
string $itemId,
string $updateType = '',
string $updateTypeId = '',
array $extraData = [],
bool $sumCheck = true
): void {
$this->mustHaveOrigin();

$this->debugService->setDebugType('federated_sync');
$this->debugService->info(
'{~New request to update a SyncedItem} based on {appId}.{itemType}.{itemId}',
'',
[
'appId' => $this->originAppId,
'itemType' => $this->originItemType,
'itemId' => $itemId,
'updateType' => $updateType,
'updateTypeId' => $updateTypeId,
'extraData' => $extraData
]
);

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

$this->debugService->info(
'initiating the process of updating SyncedItem {syncedItem.singleId}',
'', [
'itemId' => $itemId,
'syncedItem' => $syncedItem,
'extraData' => $extraData,
'isLocal' => $syncedItem->isLocal()
]
);

$federatedUser = $this->federatedUserService->getCurrentEntity();
if (!$federatedUser->isLocal()) {
throw new FederatedUserNotFoundException('unknown local user');
}

$this->federatedSyncItemService->requestSyncedItemUpdate(
$federatedUser,
$syncedItem,
new SyncedItemLock($updateType, $updateTypeId, $sumCheck),
$extraData
);
} catch (Exception $e) {
$this->debugService->exception($e);
throw $e;
}
}


/**
* @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'
);
}
}
Loading