Skip to content

Commit

Permalink
Following logic
Browse files Browse the repository at this point in the history
Signed-off-by: Hoang Pham <[email protected]>
  • Loading branch information
hweihwang committed Jan 7, 2025
1 parent 4aadede commit a68f186
Show file tree
Hide file tree
Showing 12 changed files with 1,718 additions and 157 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/vendor/
/node_modules/
/backup/
/recordings/

.php-cs-fixer.cache
.phpunit.result.cache
Expand Down
9 changes: 9 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
use OCA\Whiteboard\Listener\AddContentSecurityPolicyListener;
use OCA\Whiteboard\Listener\BeforeTemplateRenderedListener;
use OCA\Whiteboard\Listener\LoadViewerListener;
use OCA\Whiteboard\Listener\NodeCreatedListener;
use OCA\Whiteboard\Listener\RegisterTemplateCreatorListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Template\ITemplateManager;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\IL10N;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use OCP\Util;
use Psr\Container\ContainerExceptionInterface;
use Throwable;

/**
* @psalm-suppress UndefinedClass
Expand All @@ -44,8 +48,13 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadViewer::class, LoadViewerListener::class);
$context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerEventListener(NodeCreatedEvent::class, NodeCreatedListener::class);
}

/**
* @throws ContainerExceptionInterface
* @throws Throwable
*/
public function boot(IBootContext $context): void {
[$major] = Util::getVersion();
if ($major < 30) {
Expand Down
61 changes: 61 additions & 0 deletions lib/Listener/NodeCreatedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Whiteboard\Listener;

use OC\Share\Constants as ShareConstants;
use OCP\AppFramework\Http\Response;
use OCP\Constants;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\NotFoundException;
use OCP\Share;
use Psr\Log\LoggerInterface;
use OCP\Files\Events\Node\NodeCreatedEvent;


/** @template-implements IEventListener<NodeCreatedEvent|Event> */
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress MissingTemplateParam
*/
class NodeCreatedListener implements IEventListener {
public function __construct(
private Share\IManager $shareManager,
private Response $response,
private LoggerInterface $logger
) {

}

/**
* @throws NotFoundException
* @throws \Exception
*/
public function handle(Event $event): void {
if (!($event instanceof NodeCreatedEvent)) {
return;
}

$node = $event->getNode();
$existingShares = $this->shareManager->getSharedWith(null, ShareConstants::SHARE_TYPE_LINK, $node->getId());

Check failure on line 47 in lib/Listener/NodeCreatedListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

NullArgument

lib/Listener/NodeCreatedListener.php:47:56: NullArgument: Argument 1 of OCP\Share\IManager::getSharedWith cannot be null, null value provided to parameter with type string (see https://psalm.dev/057)

Check failure on line 47 in lib/Listener/NodeCreatedListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidArgument

lib/Listener/NodeCreatedListener.php:47:95: InvalidArgument: Argument 3 of OCP\Share\IManager::getSharedWith expects OCP\Files\Node|null, but int provided (see https://psalm.dev/004)
if(!empty($existingShares)) {
$this->logger->critical('Whiteboard share already exists for node ' . $node->getId());
return;
}

$this->logger->critical('Creating whiteboard share for node ' . $node->getId());

$share = $this->shareManager->newShare();
$share->setNode($node);
$share->setShareType(ShareConstants::SHARE_TYPE_LINK);
$share->setPermissions(Constants::PERMISSION_READ);
$this->shareManager->createShare($share);
}
}
Loading

0 comments on commit a68f186

Please sign in to comment.