Skip to content

Commit

Permalink
fixes
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 May 31, 2022
1 parent aa7a8a4 commit a89622c
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 114 deletions.
8 changes: 0 additions & 8 deletions lib/CircleSharesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,6 @@ public function updateItem(
);

try {

// // 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,
Expand Down
141 changes: 138 additions & 3 deletions lib/InternalAsync/AsyncItemUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,157 @@

namespace OCA\Circles\InternalAsync;

use OCA\Circles\Db\CircleRequest;
use OCA\Circles\Db\SyncedItemLockRequest;
use OCA\Circles\Db\SyncedItemRequest;
use OCA\Circles\Db\SyncedShareRequest;
use OCA\Circles\Exceptions\FederatedEventException;
use OCA\Circles\Exceptions\FederatedItemException;
use OCA\Circles\Exceptions\FederatedSyncManagerNotFoundException;
use OCA\Circles\Exceptions\InitiatorNotConfirmedException;
use OCA\Circles\Exceptions\OwnerNotFoundException;
use OCA\Circles\Exceptions\RemoteInstanceException;
use OCA\Circles\Exceptions\RemoteNotFoundException;
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\SyncedItemNotFoundException;
use OCA\Circles\Exceptions\UnknownRemoteException;
use OCA\Circles\FederatedItems\FederatedSync\ItemUpdate;
use OCA\Circles\IInternalAsync;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\Federated\FederatedEvent;
use OCA\Circles\Model\SyncedItem;
use OCA\Circles\Model\SyncedItemLock;
use OCA\Circles\Model\SyncedShare;
use OCA\Circles\Service\DebugService;
use OCA\Circles\Service\FederatedEventService;
use OCA\Circles\Service\FederatedSyncService;
use OCA\Circles\Tools\Exceptions\InvalidItemException;
use OCA\Circles\Tools\Model\ReferencedDataStore;


class AsyncItemUpdate implements IInternalAsync {

private CircleRequest $circleRequest;
private SyncedItemRequest $syncedItemRequest;
private SyncedShareRequest $syncedShareRequest;
private SyncedItemLockRequest $syncedItemLockRequest;
private FederatedEventService $federatedEventService;
private FederatedSyncService $federatedSyncService;
private DebugService $debugService;

public function __construct() {
public function __construct(
CircleRequest $circleRequest,
SyncedItemRequest $syncedItemRequest,
SyncedShareRequest $syncedShareRequest,
SyncedItemLockRequest $syncedItemLockRequest,
FederatedEventService $federatedEventService,
FederatedSyncService $federatedSyncService,
DebugService $debugService
) {
$this->circleRequest = $circleRequest;
$this->syncedItemRequest = $syncedItemRequest;
$this->syncedShareRequest = $syncedShareRequest;
$this->syncedItemLockRequest = $syncedItemLockRequest;
$this->federatedEventService = $federatedEventService;
$this->federatedSyncService = $federatedSyncService;
$this->debugService = $debugService;
}


/**
* @param ReferencedDataStore $store
*
* @throws FederatedSyncManagerNotFoundException
* @throws SyncedItemNotFoundException
* @throws InvalidItemException
*/
public function runAsynced(ReferencedDataStore $store): void {
// update Checksum
/** @var SyncedItem $syncedItem */
$syncedItem = $store->gObj('syncedItem');
/** @var SyncedItemLock $syncedLock */
$syncedLock = $store->gObj('syncedItemLock');

// broadcast update
$item = $this->federatedSyncService->initSyncManager($syncedItem)
->serializeItem($syncedItem->getItemId());
$syncedItem->setSerialized($item);

$this->updateChecksum($syncedItem);
$this->broadcastItemUpdate($syncedItem);

$this->removeLock($syncedLock);
}


/**
* @param Circle $circle
* @param SyncedItem $syncedItem
*
* @throws FederatedEventException
* @throws FederatedItemException
* @throws InitiatorNotConfirmedException
* @throws OwnerNotFoundException
* @throws RemoteInstanceException
* @throws RemoteNotFoundException
* @throws RemoteResourceNotFoundException
* @throws RequestBuilderException
* @throws UnknownRemoteException
*/
private function broadcastItemUpdate(SyncedItem $item): void {
$item->setSerialized();

foreach ($this->getAffectedCircles($item->getSingleId()) as $circle) {
$event = new FederatedEvent(ItemUpdate::class);
$event->setCircle($circle)
->setSyncedItem($item);

$this->debugService->info(
'generating {`IFederatedEvent} using {event.class}',
$circle->getSingleId(),
[
'event' => $event,
'syncedItem' => $item,
'circle' => $circle
]
);

// TODO: confirm there is no re-async as we are already on a // thread (even with multiple circles)
$this->federatedEventService->newEvent($event);
}
}


/**
* @param string $singleId
*
* @return Circle[]
* @throws RequestBuilderException
*/
private function getAffectedCircles(string $singleId): array {
$circleIds = array_map(
function (SyncedShare $share): string {
return $share->getCircleId();
}, $this->syncedShareRequest->getshares($singleId)
);

return $this->circleRequest->getCirclesByIds($circleIds);
}


/**
* @param SyncedItem $syncedItem
*/
private function updateChecksum(SyncedItem $syncedItem): void {
$sum = md5(json_encode($syncedItem->getSerialized()));

$this->syncedItemRequest->updateChecksum($syncedItem->getSingleId(), $sum);
}


/**
* @param SyncedItemLock $syncedLock
*/
private function removeLock(SyncedItemLock $syncedLock): void {
$this->syncedItemLockRequest->remove($syncedLock);
}
}
Loading

0 comments on commit a89622c

Please sign in to comment.