Skip to content

Commit

Permalink
feat(Contexts): return received tables and views
Browse files Browse the repository at this point in the history
- that is through the internal endpoints /tables/table and /tables/view

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
  • Loading branch information
blizzz committed Apr 18, 2024
1 parent 980a2d9 commit 1929d95
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/Service/ContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ protected function insertNodesFromArray(Context $context, array $nodes): void {
if (!$this->permissionsService->canManageNodeById($node['type'], $node['id'], $userId)) {
throw new PermissionError(sprintf('Owner cannot manage node %d (type %d)', $node['id'], $node['type']));
}
$contextNodeRel = $this->addNodeToContext($context, $node['id'], $node['type'], $node['permissions'] ?? 660);
$contextNodeRel = $this->addNodeToContext($context, $node['id'], $node['type'], $node['permissions'] ?? 0);
$addedNodes[] = $contextNodeRel->jsonSerialize();
} catch (Exception $e) {
$this->logger->warning('Could not add node {ntype}/{nid} to context {cid}, skipping.', [
Expand Down
38 changes: 25 additions & 13 deletions lib/Service/TableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TableService extends SuperService {


protected IL10N $l;
private ContextService $contextService;

public function __construct(
PermissionsService $permissionsService,
Expand All @@ -56,7 +57,8 @@ public function __construct(
ShareService $shareService,
UserHelper $userHelper,
FavoritesService $favoritesService,
IL10N $l
ContextService $contextService,
IL10N $l,
) {
parent::__construct($logger, $userId, $permissionsService);
$this->mapper = $mapper;
Expand All @@ -68,6 +70,7 @@ public function __construct(
$this->userHelper = $userHelper;
$this->favoritesService = $favoritesService;
$this->l = $l;
$this->contextService = $contextService;
}

/**
Expand All @@ -88,7 +91,10 @@ public function findAll(?string $userId = null, bool $skipTableEnhancement = fal
$userId = $this->permissionsService->preCheckUserId($userId); // $userId can be set or ''

try {
$allTables = $this->mapper->findAll($userId); // get own tables
$ownedTables = $this->mapper->findAll($userId); // get own tables
foreach ($ownedTables as $ownedTable) {
$allTables[$ownedTable->getId()] = $ownedTable;
}
} catch (OcpDbException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError($e->getMessage());
Expand All @@ -97,7 +103,8 @@ public function findAll(?string $userId = null, bool $skipTableEnhancement = fal
// if there are no own tables found, create the tutorial table
if (count($allTables) === 0 && $createTutorial) {
try {
$allTables = [$this->create($this->l->t('Tutorial'), 'tutorial', '🚀')];
$tutorialTable = $this->create($this->l->t('Tutorial'), 'tutorial', '🚀');
$allTables = [$tutorialTable->getId() => $tutorialTable];
} catch (InternalError|PermissionError|DoesNotExistException|MultipleObjectsReturnedException|OcpDbException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
Expand All @@ -109,16 +116,22 @@ public function findAll(?string $userId = null, bool $skipTableEnhancement = fal

// clean duplicates
foreach ($sharedTables as $sharedTable) {
$found = false;
foreach ($allTables as $table) {
if ($sharedTable->getId() === $table->getId()) {
$found = true;
break;
}
if (isset($allTables[$sharedTable->getId()])) {
$allTables[$sharedTable->getId()] = $sharedTable;
}
if (!$found) {
$allTables[] = $sharedTable;
}
}

$contexts = $this->contextService->findAll($userId);
foreach ($contexts as $context) {
$nodes = $context->getNodes();
foreach ($nodes as $node) {
if ($node['node_type'] !== Application::NODE_TYPE_TABLE
|| isset($allTables[$node['node_id']])
) {
continue;
}
$allTables[$node['node_id']] = $this->find($node['node_id'], $skipTableEnhancement, $userId);
}
}

Expand All @@ -139,8 +152,7 @@ public function findAll(?string $userId = null, bool $skipTableEnhancement = fal
}
}


return $allTables;
return array_values($allTables);
}

/**
Expand Down
27 changes: 25 additions & 2 deletions lib/Service/ViewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ViewService extends SuperService {
protected FavoritesService $favoritesService;

protected IL10N $l;
private ContextService $contextService;

public function __construct(
PermissionsService $permissionsService,
Expand All @@ -48,6 +49,7 @@ public function __construct(
RowService $rowService,
UserHelper $userHelper,
FavoritesService $favoritesService,
ContextService $contextService,
IL10N $l
) {
parent::__construct($logger, $userId, $permissionsService);
Expand All @@ -57,6 +59,7 @@ public function __construct(
$this->rowService = $rowService;
$this->userHelper = $userHelper;
$this->favoritesService = $favoritesService;
$this->contextService = $contextService;
}


Expand Down Expand Up @@ -142,11 +145,31 @@ public function findSharedViewsWithMe(?string $userId = null): array {
if ($userId === '') {
return [];
}

$allViews = [];

$sharedViews = $this->shareService->findViewsSharedWithMe($userId);
foreach ($sharedViews as $view) {
foreach ($sharedViews as $sharedView) {
$allViews[$sharedView->getId()] = $sharedView;
}

$contexts = $this->contextService->findAll($userId);
foreach ($contexts as $context) {
$nodes = $context->getNodes();
foreach ($nodes as $node) {
if ($node['node_type'] !== Application::NODE_TYPE_VIEW
|| isset($allViews[$node['node_id']])
) {
continue;
}
$allViews[$node['node_id']] = $this->find($node['node_id'], false, $userId);
}
}

foreach ($allViews as $view) {
$this->enhanceView($view, $userId);
}
return $sharedViews;
return array_values($allViews);
}


Expand Down

0 comments on commit 1929d95

Please sign in to comment.