From 1929d959d65b07ff315eadc6334ba8d4ac37e639 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 18 Apr 2024 21:43:38 +0200 Subject: [PATCH] feat(Contexts): return received tables and views - that is through the internal endpoints /tables/table and /tables/view Signed-off-by: Arthur Schiwon --- lib/Service/ContextService.php | 2 +- lib/Service/TableService.php | 38 ++++++++++++++++++++++------------ lib/Service/ViewService.php | 27 ++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/lib/Service/ContextService.php b/lib/Service/ContextService.php index a0e247bd8..a8fdf944b 100644 --- a/lib/Service/ContextService.php +++ b/lib/Service/ContextService.php @@ -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.', [ diff --git a/lib/Service/TableService.php b/lib/Service/TableService.php index 03659e594..e61d262be 100644 --- a/lib/Service/TableService.php +++ b/lib/Service/TableService.php @@ -43,6 +43,7 @@ class TableService extends SuperService { protected IL10N $l; + private ContextService $contextService; public function __construct( PermissionsService $permissionsService, @@ -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; @@ -68,6 +70,7 @@ public function __construct( $this->userHelper = $userHelper; $this->favoritesService = $favoritesService; $this->l = $l; + $this->contextService = $contextService; } /** @@ -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()); @@ -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()); @@ -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); } } @@ -139,8 +152,7 @@ public function findAll(?string $userId = null, bool $skipTableEnhancement = fal } } - - return $allTables; + return array_values($allTables); } /** diff --git a/lib/Service/ViewService.php b/lib/Service/ViewService.php index d509fa85c..8eed370d8 100644 --- a/lib/Service/ViewService.php +++ b/lib/Service/ViewService.php @@ -38,6 +38,7 @@ class ViewService extends SuperService { protected FavoritesService $favoritesService; protected IL10N $l; + private ContextService $contextService; public function __construct( PermissionsService $permissionsService, @@ -48,6 +49,7 @@ public function __construct( RowService $rowService, UserHelper $userHelper, FavoritesService $favoritesService, + ContextService $contextService, IL10N $l ) { parent::__construct($logger, $userId, $permissionsService); @@ -57,6 +59,7 @@ public function __construct( $this->rowService = $rowService; $this->userHelper = $userHelper; $this->favoritesService = $favoritesService; + $this->contextService = $contextService; } @@ -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); }