bookmarks/lib/Flow/CreateBookmark.php

174 lines
4.6 KiB
PHP
Raw Normal View History

2020-08-05 15:35:43 +00:00
<?php
2020-09-21 12:25:50 +00:00
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
2020-09-21 12:25:50 +00:00
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
2020-08-05 15:35:43 +00:00
namespace OCA\Bookmarks\Flow;
2020-09-21 12:17:46 +00:00
use OC;
use OCA\Bookmarks\Exception\AlreadyExistsError;
use OCA\Bookmarks\Exception\UnsupportedOperation;
use OCA\Bookmarks\Exception\UrlParseError;
use OCA\Bookmarks\Exception\UserLimitExceededError;
2020-08-05 15:35:43 +00:00
use OCA\Bookmarks\Service\BookmarkService;
use OCA\WorkflowEngine\Entity\File;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
2020-08-05 15:35:43 +00:00
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
2020-08-05 15:35:43 +00:00
use OCP\Files\Node;
use OCP\Files\NotPermittedException;
2020-08-05 15:35:43 +00:00
use OCP\IL10N;
2020-09-21 12:17:46 +00:00
use OCP\IURLGenerator;
2020-08-05 15:35:43 +00:00
use OCP\IUser;
use OCP\IUserSession;
use OCP\Lock\LockedException;
2020-08-05 15:35:43 +00:00
use OCP\Util;
use OCP\WorkflowEngine\EntityContext\IUrl;
use OCP\WorkflowEngine\Events\RegisterEntitiesEvent;
use OCP\WorkflowEngine\Events\RegisterOperationsEvent;
2020-09-21 12:17:46 +00:00
use OCP\WorkflowEngine\IManager;
2020-08-05 15:35:43 +00:00
use OCP\WorkflowEngine\IOperation;
use OCP\WorkflowEngine\IRuleMatcher;
use Psr\Log\LoggerInterface;
2020-08-05 15:35:43 +00:00
class CreateBookmark implements IOperation {
private const REGEX_URL = "%(https?|ftp)://(\S+(:\S*)?@|\d{1,3}(\.\d{1,3}){3}|(([a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(\.([a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(\.[a-z\x{00a1}-\x{ffff}]{2,6}))(:\d+)?([^\s]*)?%ium";
2020-08-05 15:35:43 +00:00
/**
* @var IL10N
*/
private $l;
/**
* @var BookmarkService
*/
private $bookmarks;
/**
* @var IUserSession
*/
private $session;
/**
2020-09-21 12:17:46 +00:00
* @var IURLGenerator
2020-08-05 15:35:43 +00:00
*/
private $urlGenerator;
/**
* @var LoggerInterface
2020-08-05 15:35:43 +00:00
*/
private $logger;
private IRootFolder $rootFolder;
2020-08-05 15:35:43 +00:00
public function __construct(IL10N $l, BookmarkService $bookmarks, IUserSession $session, IURLGenerator $urlGenerator, LoggerInterface $logger, IRootFolder $rootFolder) {
2020-08-05 15:35:43 +00:00
$this->l = $l;
$this->bookmarks = $bookmarks;
$this->session = $session;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->rootFolder = $rootFolder;
2020-08-05 15:35:43 +00:00
}
public static function register(IEventDispatcher $dispatcher): void {
2020-09-21 12:17:46 +00:00
if (interface_exists(IManager::class)) {
$dispatcher->addListener(RegisterOperationsEvent::class, static function (RegisterOperationsEvent $event) {
2020-09-21 12:17:46 +00:00
$operation = OC::$server->query(CreateBookmark::class);
$event->registerOperation($operation);
Util::addScript('bookmarks', 'bookmarks-flow');
2020-08-05 15:35:43 +00:00
});
$dispatcher->addListener(RegisterEntitiesEvent::class, static function (RegisterEntitiesEvent $event) {
2020-09-21 12:17:46 +00:00
$entity = OC::$server->query(Bookmark::class);
$event->registerEntity($entity);
Util::addScript('bookmarks', 'bookmarks-flow');
2020-08-05 15:35:43 +00:00
});
}
}
/**
* @inheritDoc
*/
public function getDisplayName(): string {
return $this->l->t('Create bookmark');
}
/**
* @inheritDoc
*/
public function getDescription(): string {
return $this->l->t('Takes a link and adds it to your collection of bookmarks.');
}
/**
* @inheritDoc
*/
public function getIcon(): string {
return $this->urlGenerator->imagePath('bookmarks', 'bookmarks.svg');
}
/**
* @inheritDoc
*/
public function isAvailableForScope(int $scope): bool {
return true;
}
/**
* @inheritDoc
*/
public function validateOperation(string $name, array $checks, string $operation): void {
// TODO: Implement validateOperation() method.
}
/**
* @inheritDoc
*/
public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void {
$flows = $ruleMatcher->getFlows(true);
foreach ($flows as $flow) {
$user = $this->session->getUser();
if ($user === null) {
continue;
}
$entity = $ruleMatcher->getEntity();
if ($entity instanceof File) {
$node = current($this->rootFolder->getById($entity->exportContextIDs()['nodeId']));
if ($node !== null) {
$this->handleFile($node, $user);
}
2020-08-05 15:35:43 +00:00
continue;
}
if ($entity instanceof IUrl) {
$this->bookmarks->create($user->getUID(), $entity->getUrl());
continue;
}
}
}
private function handleFile(Node $node, IUser $user): void {
if (!$node instanceof \OCP\Files\File) {
2020-08-05 15:35:43 +00:00
return;
}
try {
$text = $node->getContent();
} catch (NotPermittedException|LockedException $e) {
2020-08-05 15:35:43 +00:00
return;
}
2020-08-05 15:37:36 +00:00
if (preg_match_all(self::REGEX_URL, $text, $matches) === false) {
2020-08-05 15:35:43 +00:00
return;
}
2020-08-05 15:37:36 +00:00
foreach ($matches[0] as $url) {
try {
$this->bookmarks->create($user->getUID(), $url);
} catch (AlreadyExistsError|UnsupportedOperation|UrlParseError|UserLimitExceededError|DoesNotExistException|MultipleObjectsReturnedException $e) {
return;
}
2020-08-05 15:35:43 +00:00
}
}
}