Skip to content

Commit

Permalink
updateItem
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed May 25, 2022
1 parent 93b0e96 commit 6f46d3f
Show file tree
Hide file tree
Showing 19 changed files with 982 additions and 173 deletions.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
['name' => 'Remote#memberships', 'url' => '/memberships/{circleId}/', 'verb' => 'GET'],

['name' => 'Sync#getSyncedItem', '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' => 'POST'],
['name' => 'Debug#debugDaemon', 'url' => '/debug', 'verb' => 'POST']
Expand Down
84 changes: 72 additions & 12 deletions lib/CircleSharesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;

Expand All @@ -52,6 +53,7 @@ class CircleSharesManager implements ICircleSharesManager {


private CircleService $circleService;
private FederatedUserService $federatedUserService;
private FederatedSyncService $federatedSyncService;
private FederatedSyncItemService $federatedSyncItemService;
private FederatedSyncShareService $federatedSyncShareService;
Expand All @@ -64,19 +66,24 @@ class CircleSharesManager implements ICircleSharesManager {

/**
* @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;
Expand Down Expand Up @@ -125,12 +132,14 @@ public function createShare(
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
]);
$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();
Expand All @@ -143,10 +152,11 @@ public function createShare(
$circle = $this->circleService->getCircle($circleId, $probe);

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

$this->debugService->info(
Expand All @@ -170,7 +180,6 @@ public function createShare(
$this->debugService->exception($e, $circleId);
throw $e;
}
// this->$this->federatedItemService->getSharedItem
}

/**
Expand All @@ -185,7 +194,6 @@ public function updateShare(
string $circleId,
array $extraData = []
): void {
$this->mustHaveOrigin();
}

/**
Expand All @@ -200,15 +208,67 @@ public function deleteShare(string $itemId, string $circleId): void {

/**
* @param string $itemId
* @param array $serializedData
* @param array $extraData
*
* @throws CircleSharesManagerException
*/
public function updateItem(
string $itemId,
array $serializedData
array $extraData = []
): 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,
'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->initSyncedItem(
$this->originAppId,
$this->originItemType,
$itemId
);

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

$this->federatedSyncItemService->requestSyncedItemUpdate(
$this->federatedUserService->getCurrentEntity(),
$syncedItem,
$extraData
);
} catch (Exception $e) {
$this->debugService->exception($e);
throw $e;
}
}


/**
* @param string $itemId
*
Expand Down
10 changes: 6 additions & 4 deletions lib/Command/CirclesDebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
namespace OCA\Circles\Command;

use Exception;
use JetBrains\PhpStorm\Pure;
use OC\Core\Command\Base;
use OCA\Circles\Model\Debug;
use OCA\Circles\Service\ConfigService;
Expand Down Expand Up @@ -63,6 +62,7 @@ class CirclesDebug extends Base {
private BottomRightPanel $bottomRightPanel;
private ProgressBar $display;

private string $localDisplayName;
/** @var Panel[] $panels */
private array $panels = [];
/** @var Debug[] $debugs */
Expand All @@ -72,6 +72,7 @@ class CirclesDebug extends Base {

/**
* @param DebugService $debugService
* @param ConfigService $configService
*/
public function __construct(DebugService $debugService, ConfigService $configService) {
parent::__construct();
Expand All @@ -88,6 +89,7 @@ protected function configure() {
->addOption('history', '', InputOption::VALUE_REQUIRED, 'last history', '50')
->addOption('size', '', InputOption::VALUE_REQUIRED, 'height', '0')
->addOption('ping', '', InputOption::VALUE_NONE, 'ping debug daemon')
->addOption('local', '', InputOption::VALUE_REQUIRED, 'set displayed address', 'local')
->addOption('instance', '', InputOption::VALUE_REQUIRED, 'filter instance', '');
}

Expand All @@ -105,6 +107,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$this->localDisplayName = $input->getOption('local');

$this->init();
$this->initTerminal((int)$input->getOption('size'));
$this->initPanel($output);
Expand Down Expand Up @@ -273,7 +277,7 @@ private function refreshHistory(): void {
$debug = $item->getDebug();

$instance = ($this->configService->isLocalInstance($item->getInstance())) ?
'local' : $item->getInstance();
$this->localDisplayName : $item->getInstance();

$instanceColor = $this->configService->getAppValue('debug_instance.' . $instance);
if ($instanceColor === '') {
Expand Down Expand Up @@ -428,7 +432,6 @@ private function refresh(): void {
/**
* @return bool
*/
#[Pure]
private function isRefreshNeeded(): bool {
if ($this->refresh) {
return true;
Expand All @@ -454,7 +457,6 @@ private function cleanRefresh(): void {
/**
* @return Debug
*/
#[Pure]
private function getSelectedEntry(): Debug {
return $this->debugs[$this->topPanel->getCurrentPage() + $this->topPanel->getCurrentLine()];
}
Expand Down
60 changes: 60 additions & 0 deletions lib/Controller/SyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
namespace OCA\Circles\Controller;

use Exception;
use OCA\Circles\Exceptions\FederatedItemBadRequestException;
use OCA\Circles\Model\SyncedItem;
use OCA\Circles\Model\SyncedWrapper;
use OCA\Circles\Service\DebugService;
use OCA\Circles\Service\FederatedSyncItemService;
use OCA\Circles\Service\FederatedSyncShareService;
Expand Down Expand Up @@ -116,4 +118,62 @@ public function getSyncedItem(): DataResponse {
}
}


/**
* @PublicPage
* @NoCSRFRequired
*
* @return DataResponse
*/
public function updateSyncedItem(): DataResponse {
try {
/** @var SyncedWrapper $wrapper */
$wrapper = $this->signedControllerService->extractObjectFromRequest(
SyncedWrapper::class,
$signed
);

$this->debugService->info(
'{instance} is requesting an update on SyncedItem {syncedItem.singleId}', '',
[
'instance' => $signed->getOrigin(),
'syncedWrapper' => $wrapper
]
);

if (!$wrapper->hasItem() || !$wrapper->hasFederatedUser()) {
throw new FederatedItemBadRequestException();
}

$item = $wrapper->getItem();
$local = $this->federatedSyncItemService->getLocalSyncedItem($item->getSingleId());

// confirm that remote is in a circle with a share on the item
$this->federatedSyncShareService->confirmRemoteInstanceAccess(
$local->getSingleId(),
$signed->getOrigin()
);

$this->debugService->info(
'SyncedItem exists, is local, and {instance} have access to the SyncedItem.', '',
[
'instance' => $signed->getOrigin(),
'local' => $local,
]
);

$updated = $this->federatedSyncItemService->requestSyncedItemUpdate(
$wrapper->getFederatedUser(),
$local,
$wrapper->getExtraData()
);

return new DataResponse($updated);
} catch (Exception $e) {
$this->e($e);

return $this->signedControllerService->exceptionResponse($e, Http::STATUS_UNAUTHORIZED);
}
}

}
15 changes: 14 additions & 1 deletion lib/Db/SyncedItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use OCA\Circles\Exceptions\InvalidIdException;
use OCA\Circles\Exceptions\SyncedItemNotFoundException;
use OCA\Circles\Model\SyncedItem;
use OCA\Circles\Tools\Exceptions\InvalidItemException;

/**
* Class ShareRequest
Expand Down Expand Up @@ -65,6 +64,19 @@ public function save(SyncedItem $item): void {
}


/**
* @param string $singleId
* @param string $checksum
*/
public function updateChecksum(string $singleId, string $checksum): void {
$qb = $this->getSyncedItemUpdateSql();
$qb->set('checksum', $qb->createNamedParameter($checksum));
$qb->limitToSingleId($singleId);

$qb->executeStatement();
}


/**
* @param string $singleId
*
Expand Down Expand Up @@ -97,4 +109,5 @@ public function getSyncedItem(string $appId, string $itemType, string $itemId):

return $this->getItemFromRequest($qb);
}

}
Loading

0 comments on commit 6f46d3f

Please sign in to comment.