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

Enh/reference widgets #434

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use OCA\Tables\Listener\AnalyticsDatasourceListener;
use OCA\Tables\Listener\TablesReferenceListener;
use OCA\Tables\Listener\UserDeletedListener;
use OCA\Tables\Reference\RowReferenceProvider;
use OCA\Tables\Reference\SearchableTableReferenceProvider;
use OCA\Tables\Reference\AdvancedTableReferenceProvider;
use OCA\Tables\Reference\TableContentReferenceProvider;
use OCA\Tables\Reference\TableReferenceProvider;
use OCA\Tables\Search\SearchTablesProvider;
use OCP\AppFramework\App;
Expand Down Expand Up @@ -49,7 +52,9 @@ public function register(IRegistrationContext $context): void {
if (version_compare($config->getSystemValueString('version', '0.0.0'), '26.0.0', '<')) {
$context->registerReferenceProvider(TableReferenceProvider::class);
} else {
$context->registerReferenceProvider(SearchableTableReferenceProvider::class);
$context->registerReferenceProvider(AdvancedTableReferenceProvider::class);
$context->registerReferenceProvider(TableContentReferenceProvider::class);
$context->registerReferenceProvider(RowReferenceProvider::class);
}
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
}
Expand Down
111 changes: 111 additions & 0 deletions lib/Reference/AdvancedTableReferenceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace OCA\Tables\Reference;

use OC\Collaboration\Reference\ReferenceManager;
use OCA\Tables\AppInfo\Application;
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\Collaboration\Reference\IReference;
use OCP\IL10N;
use OCP\IURLGenerator;

class AdvancedTableReferenceProvider extends ADiscoverableReferenceProvider {
private TableReferenceHelper $referenceHelper;
private IL10N $l10n;
private IURLGenerator $urlGenerator;

private ReferenceManager $referenceManager;

public function __construct(
IL10N $l10n,
IURLGenerator $urlGenerator,
TableReferenceHelper $referenceHelper,
ReferenceManager $referenceManager
) {
$this->referenceHelper = $referenceHelper;
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->referenceManager = $referenceManager;
}

/**
* @inheritDoc
*/
public function getId(): string {
return Application::APP_ID . '-ref-tables';
}

/**
* @inheritDoc
*/
public function getTitle(): string {
return $this->l10n->t('Nextcloud tables');
}

/**
* @inheritDoc
*/
public function getOrder(): int {
return 10;
}

/**
* @inheritDoc
*/
public function getIconUrl(): string {
return $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg')
);
}

/**
* @inheritDoc
*/
public function getSupportedSearchProviderIds(): array {
return ['tables-search-tables'];
}

/**
* @inheritDoc
*/
public function matchReference(string $referenceText): bool {
return $this->referenceHelper->matchReference($referenceText);
}

/**
* @inheritDoc
*/
public function resolveReference(string $referenceText): ?IReference {
return $this->referenceHelper->resolveReference($referenceText);
}

/**
* @param string $url
* @return int|null
*/
public function getTableIdFromLink(string $url): ?int {
return $this->referenceHelper->getTableIdFromLink($url);
}

/**
* @inheritDoc
*/
public function getCachePrefix(string $referenceId): string {
return $this->referenceHelper->getCachePrefix($referenceId);
}

/**
* @inheritDoc
*/
public function getCacheKey(string $referenceId): ?string {
return $this->referenceHelper->getCacheKey($referenceId);
}

/**
* @param string $userId
* @return void
*/
public function invalidateUserCache(string $userId): void {
$this->referenceManager->invalidateCache($userId);
}
}
91 changes: 14 additions & 77 deletions lib/Reference/ReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use OCA\Tables\AppInfo\Application;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\PermissionError;
use OCA\Tables\Service\ColumnService;
use OCA\Tables\Service\RowService;
use OCA\Tables\Service\ViewService;
use OCP\Collaboration\Reference\IReference;
Expand All @@ -16,18 +17,18 @@
use Throwable;

class ReferenceHelper {
private const RICH_OBJECT_TYPE = Application::APP_ID . '_table';
protected ?string $userId;
protected IURLGenerator $urlGenerator;
protected LinkReferenceProvider $linkReferenceProvider;
protected ViewService $viewService;
protected ColumnService $columnService;
protected RowService $rowService;

private ?string $userId;
private IURLGenerator $urlGenerator;
private LinkReferenceProvider $linkReferenceProvider;
private ViewService $viewService;
private RowService $rowService;

private IConfig $config;
protected IConfig $config;

public function __construct(IURLGenerator $urlGenerator,
ViewService $viewService,
ColumnService $columnService,
RowService $rowService,
LinkReferenceProvider $linkReferenceProvider,
?string $userId,
Expand All @@ -37,82 +38,18 @@ public function __construct(IURLGenerator $urlGenerator,
$this->linkReferenceProvider = $linkReferenceProvider;
$this->viewService = $viewService;
$this->rowService = $rowService;
$this->columnService = $columnService;
$this->config = $config;
}

public function matchReference(string $referenceText): bool {
if ($this->userId === null) {
return false;
}
$start = $this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID);
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);

// link example:
// https://nextcloud.local/apps/tables/#/table/3
$noIndexMatch = preg_match('/^' . preg_quote($start, '/') . '\/#\/view\/\d+$/i', $referenceText) === 1;
$indexMatch = preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/view\/\d+$/i', $referenceText) === 1;

return $noIndexMatch || $indexMatch;
return false;
}

/** @noinspection PhpUndefinedMethodInspection */
/** @psalm-suppress InvalidReturnType */
public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$viewId = $this->getTableIdFromLink($referenceText);
if ($viewId === null || $this->userId === null) {
// fallback to opengraph if it matches, but somehow we can't resolve
/** @psalm-suppress InvalidReturnStatement */
return $this->linkReferenceProvider->resolveReference($referenceText);
}
try {
$view = $this->viewService->find($viewId, $this->userId);
} catch (Exception | Throwable $e) {
/** @psalm-suppress InvalidReturnStatement */
return $this->linkReferenceProvider->resolveReference($referenceText);
}

$reference = new Reference($referenceText);
$viewReferenceInfo = [];

if ($view->getEmoji()) {
$reference->setDescription($view->getEmoji() . ' ' . $view->getTitle());
$viewReferenceInfo['title'] = $view->getTitle();
$viewReferenceInfo['emoji'] = $view->getEmoji();
} else {
$reference->setTitle($view->getTitle());
$viewReferenceInfo['title'] = $view->getTitle();
}

$reference->setDescription($view->getOwnerDisplayName() ?? $view->getOwnership());

$viewReferenceInfo['ownership'] = $view->getOwnership();
$viewReferenceInfo['ownerDisplayName'] = $view->getOwnerDisplayName();
$viewReferenceInfo['rowsCount'] = $view->getRowsCount();

$imageUrl = $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg')
);
$reference->setImageUrl($imageUrl);

$viewReferenceInfo['link'] = $referenceText;
$reference->setUrl($referenceText);

// add rows data
try {
$viewReferenceInfo['rows'] = $this->rowService->findAllByView($viewId, 10, 0);
} catch (InternalError $e) {
} catch (PermissionError $e) {
}

$reference->setRichObject(
$this::RICH_OBJECT_TYPE,
$viewReferenceInfo,
);
return $reference;
}

return null;
return $this->linkReferenceProvider->resolveReference($referenceText);
}

/**
Expand All @@ -123,9 +60,9 @@ public function getTableIdFromLink(string $url): ?int {
$start = $this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID);
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);

preg_match('/^' . preg_quote($start, '/') . '\/#\/view\/(\d+)$/i', $url, $matches);
preg_match('/^' . preg_quote($start, '/') . '\/#\/view\/(\d+)(?:\/[^\/]+)*$/i', $url, $matches);
if (!$matches || count($matches) < 2) {
preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/view\/(\d+)$/i', $url, $matches);
preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/view\/(\d+)(?:\/[^\/]+)*$/i', $url, $matches);
}
if ($matches && count($matches) > 1) {
return (int) $matches[1];
Expand Down
130 changes: 130 additions & 0 deletions lib/Reference/RowReferenceHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace OCA\Tables\Reference;

use Exception;
use OC\Collaboration\Reference\LinkReferenceProvider;
use OCA\Tables\AppInfo\Application;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\PermissionError;
use OCA\Tables\Service\ColumnService;
use OCA\Tables\Service\RowService;
use OCA\Tables\Service\ViewService;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\Reference;
use OCP\IConfig;
use OCP\IURLGenerator;
use Throwable;

class RowReferenceHelper extends ReferenceHelper {
protected const RICH_OBJECT_TYPE = Application::APP_ID . '_row';

public function __construct(IURLGenerator $urlGenerator,
ViewService $viewService,
ColumnService $columnService,
RowService $rowService,
LinkReferenceProvider $linkReferenceProvider,
?string $userId,
IConfig $config) {
parent::__construct($urlGenerator, $viewService, $columnService, $rowService, $linkReferenceProvider, $userId, $config);
}

public function matchReference(string $referenceText): bool {
if ($this->userId === null) {
return false;
}
$start = $this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID);
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);

// link example:
// https://nextcloud.local/apps/tables/#/table/3
$noIndexMatch = preg_match('/^' . preg_quote($start, '/') . '\/#\/view\/\d+\/row\/\d+$/i', $referenceText) === 1;
$indexMatch = preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/view\/\d+\/row\/\d+$/i', $referenceText) === 1;

return $noIndexMatch || $indexMatch;
}

/** @noinspection PhpUndefinedMethodInspection */
/** @psalm-suppress InvalidReturnType */
public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$viewId = $this->getTableIdFromLink($referenceText);
$rowId = $this->getRowIdFromLink($referenceText);
if ($viewId === null || $this->userId === null) {
// fallback to opengraph if it matches, but somehow we can't resolve
/** @psalm-suppress InvalidReturnStatement */
return $this->linkReferenceProvider->resolveReference($referenceText);
}
try {
$view = $this->viewService->find($viewId, false, $this->userId);
} catch (Exception | Throwable $e) {
/** @psalm-suppress InvalidReturnStatement */
return $this->linkReferenceProvider->resolveReference($referenceText);
}

$reference = new Reference($referenceText);
$viewReferenceInfo = [];

if ($view->getEmoji()) {
$reference->setDescription($view->getEmoji() . ' ' . $view->getTitle());
$viewReferenceInfo['title'] = $view->getTitle();
$viewReferenceInfo['emoji'] = $view->getEmoji();
} else {
$reference->setTitle($view->getTitle());
$viewReferenceInfo['title'] = $view->getTitle();
}

$reference->setDescription($view->getOwnerDisplayName() ?? $view->getOwnership());

$viewReferenceInfo['ownership'] = $view->getOwnership();
$viewReferenceInfo['ownerDisplayName'] = $view->getOwnerDisplayName();
$viewReferenceInfo['rowsCount'] = $view->getRowsCount();
$viewReferenceInfo['columnIds'] = $view->getColumnsArray();

$imageUrl = $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg')
);
$reference->setImageUrl($imageUrl);

$viewReferenceInfo['link'] = $referenceText;
$reference->setUrl($referenceText);


// add Columns
$viewReferenceInfo['columns'] = $this->columnService->findAllByView($viewId, $this->userId);
// add rows data
try {
$viewReferenceInfo['rows'] = [$this->rowService->find($rowId)];
} catch (InternalError $e) {
} catch (PermissionError $e) {
}

$reference->setRichObject(
$this::RICH_OBJECT_TYPE,
$viewReferenceInfo,
);
return $reference;
}

return null;
}

/**
* @param string $url
* @return int|null
*/
private function getRowIdFromLink(string $url): ?int {
$start = $this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID);
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);

preg_match('/^' . preg_quote($start, '/') . '\/#\/view\/\d+\/row\/(\d+)(?:\/[^\/]+)*$/i', $url, $matches);
if (!$matches || count($matches) < 2) {
preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/view\/\d+\/row\/(\d+)(?:\/[^\/]+)*$/i', $url, $matches);
}
if ($matches && count($matches) > 1) {
return (int) $matches[1];
}

return null;
}
}
Loading
Loading