Skip to content

Commit

Permalink
feat(Contexts): add Contexts to navigation when applicable
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
  • Loading branch information
blizzz committed Mar 7, 2024
1 parent 2269f3d commit 47729eb
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use OCA\Analytics\Datasource\DatasourceEvent;
use OCA\Tables\Capabilities;
use OCA\Tables\Db\Context;
use OCA\Tables\Listener\AnalyticsDatasourceListener;
use OCA\Tables\Listener\TablesReferenceListener;
use OCA\Tables\Listener\UserDeletedListener;
Expand All @@ -13,12 +14,16 @@
use OCA\Tables\Reference\LegacyReferenceProvider;
use OCA\Tables\Reference\ReferenceProvider;
use OCA\Tables\Search\SearchTablesProvider;
use OCA\Tables\Service\ContextService;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\IConfig;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Server;
use OCP\User\Events\BeforeUserDeletedEvent;
use Psr\Container\ContainerExceptionInterface;
Expand All @@ -32,6 +37,10 @@ class Application extends App implements IBootstrap {

public const OWNER_TYPE_USER = 0;

public const NAV_ENTRY_MODE_HIDDEN = 0;
public const NAV_ENTRY_MODE_RECIPIENTS = 1;
public const NAV_ENTRY_MODE_ALL = 2;

public function __construct() {
parent::__construct(self::APP_ID);
}
Expand Down Expand Up @@ -68,5 +77,61 @@ public function register(IRegistrationContext $context): void {
}

public function boot(IBootContext $context): void {
$context->injectFn([$this, 'addPersonalNavigationEntries']);
}

public function addPersonalNavigationEntries(
INavigationManager $navigationManager,
IURLGenerator $urlGenerator
): void
{
$userSession = \OCP\Server::get(IUserSession::class);
$user = $userSession->getUser();
if ($user === null) {
return;
}

$contextService = \OCP\Server::get(ContextService::class);
$contexts = $contextService->findForNavigation($user->getUID());

foreach ($contexts as $context) {
/** @var Context $context */
if ($context->getOwnerType() === self::OWNER_TYPE_USER
&& $context->getOwnerId() === $user->getUID()) {

// filter out entries for owners unless it is set to be visible
$skipEntry = true;
foreach ($context->getSharing() as $shareInfo) {
// TODO: integrate into DB query in Mapper

if (isset($shareInfo['display_mode']) && $shareInfo['display_mode'] === self::NAV_ENTRY_MODE_ALL) {
// a custom override makes it visible
$skipEntry = false;
break;
} elseif (!isset($shareInfo['display_mode']) && $shareInfo['display_mode'] === self::NAV_ENTRY_MODE_ALL) {

Check failure on line 111 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TypeDoesNotContainType

lib/AppInfo/Application.php:111:16: TypeDoesNotContainType: Type null for $shareInfo['display_mode'] is never =int(2) (see https://psalm.dev/056)

Check failure on line 111 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TypeDoesNotContainType

lib/AppInfo/Application.php:111:54: TypeDoesNotContainType: 2 cannot be identical to null (see https://psalm.dev/056)

Check failure on line 111 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

TypeDoesNotContainType

lib/AppInfo/Application.php:111:16: TypeDoesNotContainType: Type null for $shareInfo['display_mode'] is never =int(2) (see https://psalm.dev/056)

Check failure on line 111 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

TypeDoesNotContainType

lib/AppInfo/Application.php:111:54: TypeDoesNotContainType: 2 cannot be identical to null (see https://psalm.dev/056)
// no custom override, and visible also for owner by default
$skipEntry = false;
break;
}
}
if ($skipEntry) {
continue;
}
}


$iconRelPath = 'material/' . $context->getIcon() . '.svg';
$icon = file_exists(__DIR__ . '/../../img/' . $iconRelPath) ? $iconRelPath : 'app.svg';


$navigationManager->add([
'id' => self::APP_ID . '::' . $context->getId(),
'name' => $context->getName(),
'href' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
'icon' => $urlGenerator->imagePath(self::APP_ID, $icon),
'order' => 8,
'type' => 'link',
]);
}
}
}
37 changes: 37 additions & 0 deletions lib/Db/ContextMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OCA\Tables\Db;

use OCA\Tables\AppInfo\Application;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Helper\UserHelper;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -163,6 +164,42 @@ public function findAll(?string $userId = null): array {
return $resultEntities;
}

public function findForNavBar(string $userId): array {
$qb = $this->getFindContextBaseQuery($userId);
$qb->andWhere($qb->expr()->andX(
// default
$qb->expr()->gt('n.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_HIDDEN, IQueryBuilder::PARAM_INT)),
// user override
$qb->expr()->orX(
$qb->expr()->gt('n2.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_HIDDEN, IQueryBuilder::PARAM_INT)),
$qb->expr()->isNull('n2.display_mode'),
)
));

$result = $qb->executeQuery();
$r = $result->fetchAll();

$contextIds = [];
foreach ($r as $row) {
$contextIds[$row['id']] = 1;
}
$contextIds = array_keys($contextIds);
unset($row);

$resultEntities = [];
foreach ($contextIds as $contextId) {
$workArray = [];
foreach ($r as $row) {
if ($row['id'] === $contextId) {
$workArray[] = $row;
}
}
$resultEntities[] = $this->formatResultRows($workArray, $userId);
}

return $resultEntities;
}

/**
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
Expand Down
4 changes: 4 additions & 0 deletions lib/Service/ContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public function findAll(?string $userId): array {
return $this->contextMapper->findAll($userId);
}

public function findForNavigation(string $userId): array {
return $this->contextMapper->findForNavBar($userId);
}

/**
* @return Context
* @throws InternalError
Expand Down

0 comments on commit 47729eb

Please sign in to comment.