Browse Source

Cleanup

Signed-off-by: Julius Härtl <jus@bitgrid.net>
pull/33494/head
Julius Härtl 3 years ago
parent
commit
a392235e23
No known key found for this signature in database GPG Key ID: 4C614C6ED2CDE6DF
  1. 19
      core/Controller/ReferenceApiController.php
  2. 19
      core/Controller/ReferenceController.php
  3. 2
      core/src/OCP/comments.js
  4. 2
      dist/core-main.js.map
  5. 9
      lib/composer/composer/autoload_classmap.php
  6. 9
      lib/composer/composer/autoload_static.php
  7. 62
      lib/private/Collaboration/Reference/FileReferenceProvider.php
  8. 23
      lib/private/Collaboration/Reference/LinkReferenceProvider.php
  9. 12
      lib/private/Collaboration/Reference/Reference.php
  10. 17
      lib/private/Collaboration/Reference/ReferenceManager.php
  11. 4
      lib/private/Server.php
  12. 31
      lib/public/Collaboration/Reference/IReference.php
  13. 5
      lib/public/Collaboration/Reference/IReferenceManager.php
  14. 2
      lib/public/Collaboration/Reference/IReferenceProvider.php
  15. 10
      lib/public/IURLGenerator.php

19
core/Controller/ReferenceApiController.php

@ -25,23 +25,19 @@ declare(strict_types=1);
namespace OC\Core\Controller;
use OCP\AppFramework\Http\DataResponse;
use OC\Collaboration\Reference\ReferenceManager;
use OCP\Collaboration\Reference\IReferenceManager;
use OCP\IRequest;
class ReferenceApiController extends \OCP\AppFramework\OCSController {
private ReferenceManager $referenceManager;
private IReferenceManager $referenceManager;
public function __construct($appName, IRequest $request, ReferenceManager $referenceManager) {
public function __construct(string $appName, IRequest $request, IReferenceManager $referenceManager) {
parent::__construct($appName, $request);
$this->referenceManager = $referenceManager;
}
/**
* @NoAdminRequired
*
* @param string $text
* @param bool $resolve
* @return DataResponse
*/
public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse {
$references = $this->referenceManager->extractReferences($text);
@ -63,16 +59,17 @@ class ReferenceApiController extends \OCP\AppFramework\OCSController {
/**
* @NoAdminRequired
*
* @param array $references
* @return DataResponse
* @param string[] $references
*/
public function resolve(array $references, int $limit = 1): DataResponse {
$result = [];
$index = 0;
foreach ($references as $reference) {
if ($index++ < $limit) {
$result[$reference] = $this->referenceManager->resolveReference($reference);
if ($index++ >= $limit) {
break;
}
$result[$reference] = $this->referenceManager->resolveReference($reference);
}
return new DataResponse([

19
core/Controller/ReferenceController.php

@ -24,18 +24,21 @@ declare(strict_types=1);
namespace OC\Core\Controller;
use OC\Collaboration\Reference\ReferenceManager;
use OCP\Collaboration\Reference\IReferenceManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IRequest;
class ReferenceController extends \OCP\AppFramework\Controller {
private ReferenceManager $referenceManager;
class ReferenceController extends Controller {
private IReferenceManager $referenceManager;
private IAppDataFactory $appDataFactory;
public function __construct($appName, IRequest $request, ReferenceManager $referenceManager, IAppDataFactory $appDataFactory) {
public function __construct(string $appName, IRequest $request, IReferenceManager $referenceManager, IAppDataFactory $appDataFactory) {
parent::__construct($appName, $request);
$this->referenceManager = $referenceManager;
$this->appDataFactory = $appDataFactory;
@ -44,10 +47,8 @@ class ReferenceController extends \OCP\AppFramework\Controller {
/**
* @PublicPage
* @NoCSRFRequired
* @param $referenceId
* @throws \OCP\Files\NotFoundException
*/
public function preview($referenceId) {
public function preview(string $referenceId) {
$reference = $this->referenceManager->getReferenceByCacheKey($referenceId);
if ($reference === null) {
return new DataResponse('', Http::STATUS_NOT_FOUND);
@ -57,9 +58,9 @@ class ReferenceController extends \OCP\AppFramework\Controller {
$appData = $this->appDataFactory->get('core');
$folder = $appData->getFolder('opengraph');
$file = $folder->getFile($referenceId);
} catch (NotFoundException $e) {
return new DataDownloadResponse($file->getContent(), $referenceId, $reference->getImageContentType());
} catch (NotFoundException|NotPermittedException $e) {
return new DataResponse('', Http::STATUS_NOT_FOUND);
}
return new DataDownloadResponse($file->getContent(), $referenceId, $reference->getImageContentType());
}
}

2
core/src/OCP/comments.js

@ -31,6 +31,8 @@ import $ from 'jquery'
*
* The downside: anything not ascii is excluded. Not sure how common it is in areas using different
* alphabets the upside: fake domains with similar looking characters won't be formatted as links
*
* This is a copy of the backend regex in IURLGenerator, make sure to adjust both when changing
*/
const urlRegex = /(\s|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig

2
dist/core-main.js.map
File diff suppressed because it is too large
View File

9
lib/composer/composer/autoload_classmap.php

@ -142,6 +142,9 @@ return array(
'OCP\\Collaboration\\Collaborators\\ISearchPlugin' => $baseDir . '/lib/public/Collaboration/Collaborators/ISearchPlugin.php',
'OCP\\Collaboration\\Collaborators\\ISearchResult' => $baseDir . '/lib/public/Collaboration/Collaborators/ISearchResult.php',
'OCP\\Collaboration\\Collaborators\\SearchResultType' => $baseDir . '/lib/public/Collaboration/Collaborators/SearchResultType.php',
'OCP\\Collaboration\\Reference\\IReference' => $baseDir . '/lib/public/Collaboration/Reference/IReference.php',
'OCP\\Collaboration\\Reference\\IReferenceManager' => $baseDir . '/lib/public/Collaboration/Reference/IReferenceManager.php',
'OCP\\Collaboration\\Reference\\IReferenceProvider' => $baseDir . '/lib/public/Collaboration/Reference/IReferenceProvider.php',
'OCP\\Collaboration\\Resources\\CollectionException' => $baseDir . '/lib/public/Collaboration/Resources/CollectionException.php',
'OCP\\Collaboration\\Resources\\ICollection' => $baseDir . '/lib/public/Collaboration/Resources/ICollection.php',
'OCP\\Collaboration\\Resources\\IManager' => $baseDir . '/lib/public/Collaboration/Resources/IManager.php',
@ -823,6 +826,10 @@ return array(
'OC\\Collaboration\\Collaborators\\Search' => $baseDir . '/lib/private/Collaboration/Collaborators/Search.php',
'OC\\Collaboration\\Collaborators\\SearchResult' => $baseDir . '/lib/private/Collaboration/Collaborators/SearchResult.php',
'OC\\Collaboration\\Collaborators\\UserPlugin' => $baseDir . '/lib/private/Collaboration/Collaborators/UserPlugin.php',
'OC\\Collaboration\\Reference\\FileReferenceProvider' => $baseDir . '/lib/private/Collaboration/Reference/FileReferenceProvider.php',
'OC\\Collaboration\\Reference\\LinkReferenceProvider' => $baseDir . '/lib/private/Collaboration/Reference/LinkReferenceProvider.php',
'OC\\Collaboration\\Reference\\Reference' => $baseDir . '/lib/private/Collaboration/Reference/Reference.php',
'OC\\Collaboration\\Reference\\ReferenceManager' => $baseDir . '/lib/private/Collaboration/Reference/ReferenceManager.php',
'OC\\Collaboration\\Resources\\Collection' => $baseDir . '/lib/private/Collaboration/Resources/Collection.php',
'OC\\Collaboration\\Resources\\Listener' => $baseDir . '/lib/private/Collaboration/Resources/Listener.php',
'OC\\Collaboration\\Resources\\Manager' => $baseDir . '/lib/private/Collaboration/Resources/Manager.php',
@ -975,6 +982,8 @@ return array(
'OC\\Core\\Controller\\ProfileApiController' => $baseDir . '/core/Controller/ProfileApiController.php',
'OC\\Core\\Controller\\ProfilePageController' => $baseDir . '/core/Controller/ProfilePageController.php',
'OC\\Core\\Controller\\RecommendedAppsController' => $baseDir . '/core/Controller/RecommendedAppsController.php',
'OC\\Core\\Controller\\ReferenceApiController' => $baseDir . '/core/Controller/ReferenceApiController.php',
'OC\\Core\\Controller\\ReferenceController' => $baseDir . '/core/Controller/ReferenceController.php',
'OC\\Core\\Controller\\SearchController' => $baseDir . '/core/Controller/SearchController.php',
'OC\\Core\\Controller\\SetupController' => $baseDir . '/core/Controller/SetupController.php',
'OC\\Core\\Controller\\TwoFactorChallengeController' => $baseDir . '/core/Controller/TwoFactorChallengeController.php',

9
lib/composer/composer/autoload_static.php

@ -175,6 +175,9 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Collaboration\\Collaborators\\ISearchPlugin' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Collaborators/ISearchPlugin.php',
'OCP\\Collaboration\\Collaborators\\ISearchResult' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Collaborators/ISearchResult.php',
'OCP\\Collaboration\\Collaborators\\SearchResultType' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Collaborators/SearchResultType.php',
'OCP\\Collaboration\\Reference\\IReference' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReference.php',
'OCP\\Collaboration\\Reference\\IReferenceManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReferenceManager.php',
'OCP\\Collaboration\\Reference\\IReferenceProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReferenceProvider.php',
'OCP\\Collaboration\\Resources\\CollectionException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/CollectionException.php',
'OCP\\Collaboration\\Resources\\ICollection' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ICollection.php',
'OCP\\Collaboration\\Resources\\IManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IManager.php',
@ -856,6 +859,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Collaboration\\Collaborators\\Search' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Collaborators/Search.php',
'OC\\Collaboration\\Collaborators\\SearchResult' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Collaborators/SearchResult.php',
'OC\\Collaboration\\Collaborators\\UserPlugin' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Collaborators/UserPlugin.php',
'OC\\Collaboration\\Reference\\FileReferenceProvider' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Reference/FileReferenceProvider.php',
'OC\\Collaboration\\Reference\\LinkReferenceProvider' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Reference/LinkReferenceProvider.php',
'OC\\Collaboration\\Reference\\Reference' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Reference/Reference.php',
'OC\\Collaboration\\Reference\\ReferenceManager' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Reference/ReferenceManager.php',
'OC\\Collaboration\\Resources\\Collection' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/Collection.php',
'OC\\Collaboration\\Resources\\Listener' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/Listener.php',
'OC\\Collaboration\\Resources\\Manager' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/Manager.php',
@ -1008,6 +1015,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Controller\\ProfileApiController' => __DIR__ . '/../../..' . '/core/Controller/ProfileApiController.php',
'OC\\Core\\Controller\\ProfilePageController' => __DIR__ . '/../../..' . '/core/Controller/ProfilePageController.php',
'OC\\Core\\Controller\\RecommendedAppsController' => __DIR__ . '/../../..' . '/core/Controller/RecommendedAppsController.php',
'OC\\Core\\Controller\\ReferenceApiController' => __DIR__ . '/../../..' . '/core/Controller/ReferenceApiController.php',
'OC\\Core\\Controller\\ReferenceController' => __DIR__ . '/../../..' . '/core/Controller/ReferenceController.php',
'OC\\Core\\Controller\\SearchController' => __DIR__ . '/../../..' . '/core/Controller/SearchController.php',
'OC\\Core\\Controller\\SetupController' => __DIR__ . '/../../..' . '/core/Controller/SetupController.php',
'OC\\Core\\Controller\\TwoFactorChallengeController' => __DIR__ . '/../../..' . '/core/Controller/TwoFactorChallengeController.php',

62
lib/private/Collaboration/Reference/FileReferenceProvider.php

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
@ -22,11 +24,15 @@
namespace OC\Collaboration\Reference;
use OC\User\NoUserException;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\IReferenceProvider;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IPreview;
use OCP\IURLGenerator;
use OCP\IUserSession;
@ -34,11 +40,13 @@ class FileReferenceProvider implements IReferenceProvider {
private IURLGenerator $urlGenerator;
private IRootFolder $rootFolder;
private ?string $userId;
private IPreview $previewManager;
public function __construct(IURLGenerator $urlGenerator, IRootFolder $rootFolder, IUserSession $userSession) {
public function __construct(IURLGenerator $urlGenerator, IRootFolder $rootFolder, IUserSession $userSession, IPreview $previewManager) {
$this->urlGenerator = $urlGenerator;
$this->rootFolder = $rootFolder;
$this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
$this->previewManager = $previewManager;
}
public function matchReference(string $referenceText): bool {
@ -52,6 +60,7 @@ class FileReferenceProvider implements IReferenceProvider {
try {
$this->fetchReference($reference);
} catch (NotFoundException $e) {
$reference->setRichObject('file', null);
$reference->setAccessible(false);
}
return $reference;
@ -61,10 +70,7 @@ class FileReferenceProvider implements IReferenceProvider {
}
/**
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Files\InvalidPathException
* @throws NotFoundException
* @throws \OC\User\NoUserException
*/
private function fetchReference(Reference $reference) {
if ($this->userId === null) {
@ -74,30 +80,34 @@ class FileReferenceProvider implements IReferenceProvider {
$fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $reference->getId());
$fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $fileId);
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$files = $userFolder->getById((int)$fileId);
try {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$files = $userFolder->getById((int)$fileId);
if (empty($files)) {
throw new NotFoundException();
}
if (empty($files)) {
/** @var Node $file */
$file = array_shift($files);
$reference->setTitle($file->getName());
$reference->setDescription($file->getMimetype());
$reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId));
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId]));
$reference->setRichObject('file', [
'id' => $file->getId(),
'name' => $file->getName(),
'size' => $file->getSize(),
'path' => $file->getPath(),
'link' => $reference->getUrl(),
'mimetype' => $file->getMimetype(),
'preview-available' => $this->previewManager->isAvailable($file)
]);
} catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) {
throw new NotFoundException();
}
/** @var Node $file */
$file = array_shift($files);
$reference->setTitle($file->getName());
$reference->setDescription($file->getMimetype());
$reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId));
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId]));
$reference->setRichObject('file', [
'id' => $file->getId(),
'name' => $file->getName(),
'size' => $file->getSize(),
'path' => $file->getPath(),
'link' => $reference->getUrl(),
'mimetype' => $file->getMimetype(),
'preview-available' => false
]);
}
public function isGloballyCacheable(): bool {
@ -105,6 +115,6 @@ class FileReferenceProvider implements IReferenceProvider {
}
public function getCacheKey(string $referenceId): string {
return $this->userId;
return $this->userId ?? '';
}
}

23
lib/private/Collaboration/Reference/LinkReferenceProvider.php

@ -27,6 +27,8 @@ namespace OC\Collaboration\Reference;
use Fusonic\OpenGraph\Consumer;
use GuzzleHttp\Psr7\LimitStream;
use GuzzleHttp\Psr7\Utils;
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
use OC\Security\RateLimiting\Limiter;
use OC\SystemConfig;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\IReferenceProvider;
@ -37,7 +39,6 @@ use OCP\IURLGenerator;
use Psr\Log\LoggerInterface;
class LinkReferenceProvider implements IReferenceProvider {
public const URL_PATTERN = '/(\s|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/i';
public const MAX_PREVIEW_SIZE = 1024 * 1024;
public const ALLOWED_CONTENT_TYPES = [
@ -54,13 +55,15 @@ class LinkReferenceProvider implements IReferenceProvider {
private SystemConfig $systemConfig;
private IAppDataFactory $appDataFactory;
private IURLGenerator $urlGenerator;
private Limiter $limiter;
public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator) {
public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator, Limiter $limiter) {
$this->clientService = $clientService;
$this->logger = $logger;
$this->systemConfig = $systemConfig;
$this->appDataFactory = $appDataFactory;
$this->urlGenerator = $urlGenerator;
$this->limiter = $limiter;
}
public function matchReference(string $referenceText): bool {
@ -68,7 +71,7 @@ class LinkReferenceProvider implements IReferenceProvider {
return false;
}
return (bool)preg_match(self::URL_PATTERN, $referenceText);
return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText);
}
public function resolveReference(string $referenceText): ?IReference {
@ -82,6 +85,17 @@ class LinkReferenceProvider implements IReferenceProvider {
}
private function fetchReference(Reference $reference) {
try {
$user = \OC::$server->getUserSession()->getUser();
if ($user) {
$this->limiter->registerUserRequest('opengraph', 10, 120, $user);
} else {
$this->limiter->registerAnonRequest('opengraph', 10, 120, \OC::$server->getRequest()->getRemoteAddress());
}
} catch (RateLimitExceededException $e) {
return;
}
$client = $this->clientService->newClient();
try {
$response = $client->get($reference->getId(), [ 'timeout' => 10 ]);
@ -118,12 +132,11 @@ class LinkReferenceProvider implements IReferenceProvider {
$contentType = $response->getHeader('Content-Type');
$contentLength = $response->getHeader('Content-Length');
if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) {
$stream = Utils::streamFor($response->getBody());
$bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0);
$reference->setImageContentType($contentType);
$folder->newFile(md5($reference->getId()), $bodyStream);
$folder->newFile(md5($reference->getId()), $bodyStream->getContents());
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())]));
}
} catch (\Throwable $e) {

12
lib/private/Collaboration/Reference/Reference.php

@ -53,6 +53,10 @@ class Reference implements IReference {
$this->accessible = $accessible;
}
public function getAccessible(): bool {
return $this->accessible;
}
public function setTitle(string $title): void {
$this->title = $title;
}
@ -93,20 +97,20 @@ class Reference implements IReference {
return $this->url;
}
public function setRichObject(string $type, array $richObject): void {
public function setRichObject(string $type, ?array $richObject): void {
$this->richObjectType = $type;
$this->richObject = $richObject;
}
public function getRichObjectType(): string {
if (!$this->richObjectType) {
if ($this->richObjectType === null) {
return 'open-graph';
}
return $this->richObjectType;
}
public function getRichObject(): array {
if (!$this->richObject) {
if ($this->richObject === null) {
return $this->getOpenGraphObject();
}
return $this->richObject;
@ -130,7 +134,7 @@ class Reference implements IReference {
'imageContentType' => $reference->getImageContentType(),
'description' => $reference->getDescription(),
'link' => $reference->getUrl(),
'accessible' => $reference->accessible,
'accessible' => $reference->getAccessible(),
'richObjectType' => $reference->getRichObjectType(),
'richObject' => $reference->getRichObject(),
];

17
lib/private/Collaboration/Reference/ReferenceManager.php

@ -30,12 +30,12 @@ use OCP\Collaboration\Reference\IReferenceManager;
use OCP\Collaboration\Reference\IReferenceProvider;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IURLGenerator;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Throwable;
use function OCP\Log\logger;
class ReferenceManager implements IReferenceManager {
public const URL_PATTERN = '/(\s|\n|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|\n|$)/mi';
public const CACHE_TTL = 60;
/** @var IReferenceProvider[]|null */
@ -43,16 +43,19 @@ class ReferenceManager implements IReferenceManager {
private ICache $cache;
private Coordinator $coordinator;
private ContainerInterface $container;
private LinkReferenceProvider $linkReferenceProvider;
private LoggerInterface $logger;
public function __construct(LinkReferenceProvider $linkReferenceProvider, ICacheFactory $cacheFactory, Coordinator $coordinator, ContainerInterface $container) {
public function __construct(LinkReferenceProvider $linkReferenceProvider, ICacheFactory $cacheFactory, Coordinator $coordinator, ContainerInterface $container, LoggerInterface $logger) {
$this->linkReferenceProvider = $linkReferenceProvider;
$this->cache = $cacheFactory->createDistributed('reference');
$this->coordinator = $coordinator;
$this->container = $container;
$this->logger = $logger;
}
public function extractReferences(string $text): array {
preg_match_all(self::URL_PATTERN, $text, $matches);
preg_match_all(IURLGenerator::URL_REGEX, $text, $matches);
$references = $matches[0] ?? [];
return array_map(function ($reference) {
return trim($reference);
@ -94,6 +97,9 @@ class ReferenceManager implements IReferenceManager {
$matchedProvider = null;
foreach ($this->getProviders() as $provider) {
$matchedProvider = $provider->matchReference($referenceId) ? $provider : null;
if ($matchedProvider !== null) {
break;
}
}
if ($matchedProvider === null && $this->linkReferenceProvider->matchReference($referenceId)) {
@ -141,7 +147,7 @@ class ReferenceManager implements IReferenceManager {
/** @var IReferenceProvider $provider */
$provider = $this->container->get($registration->getService());
} catch (Throwable $e) {
logger()->error('Could not load reference provider ' . $registration->getService() . ': ' . $e->getMessage(), [
$this->logger->error('Could not load reference provider ' . $registration->getService() . ': ' . $e->getMessage(), [
'exception' => $e,
]);
return null;
@ -150,7 +156,6 @@ class ReferenceManager implements IReferenceManager {
return $provider;
}, $context->getReferenceProviders()));
// TODO: Move to files app
$this->providers[] = $this->container->get(FileReferenceProvider::class);
}

4
lib/private/Server.php

@ -73,6 +73,7 @@ use OC\Collaboration\Collaborators\MailPlugin;
use OC\Collaboration\Collaborators\RemoteGroupPlugin;
use OC\Collaboration\Collaborators\RemotePlugin;
use OC\Collaboration\Collaborators\UserPlugin;
use OC\Collaboration\Reference\ReferenceManager;
use OC\Command\CronBus;
use OC\Comments\ManagerFactory as CommentsManagerFactory;
use OC\Contacts\ContactsMenu\ActionFactory;
@ -162,6 +163,7 @@ use OCP\App\IAppManager;
use OCP\Authentication\LoginCredentials\IStore;
use OCP\BackgroundJob\IJobList;
use OCP\Collaboration\AutoComplete\IManager;
use OCP\Collaboration\Reference\IReferenceManager;
use OCP\Command\IBus;
use OCP\Comments\ICommentsManager;
use OCP\Contacts\ContactsMenu\IActionFactory;
@ -1338,6 +1340,8 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias(\OCP\Collaboration\Resources\IProviderManager::class, \OC\Collaboration\Resources\ProviderManager::class);
$this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class);
$this->registerAlias(IReferenceManager::class, ReferenceManager::class);
$this->registerDeprecatedAlias('SettingsManager', \OC\Settings\Manager::class);
$this->registerAlias(\OCP\Settings\IManager::class, \OC\Settings\Manager::class);
$this->registerService(\OC\Files\AppData\Factory::class, function (ContainerInterface $c) {

31
lib/public/Collaboration/Reference/IReference.php

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
@ -35,10 +37,19 @@ interface IReference extends JsonSerializable {
public function getId(): string;
/**
* Accessible flag indicates if the user has access to the provided reference
*
* @since 25.0.0
*/
public function setAccessible(bool $accessible): void;
/**
* Accessible flag indicates if the user has access to the provided reference
*
* @since 25.0.0
*/
public function getAccessible(): bool;
/**
* @since 25.0.0
*/
@ -90,32 +101,30 @@ interface IReference extends JsonSerializable {
public function getUrl(): ?string;
/**
* Set the reference specific rich object representation
*
* @since 25.0.0
*/
public function setRichObject(string $type, array $richObject): void;
public function setRichObject(string $type, ?array $richObject): void;
/**
* Returns the type of the reference specific rich object
*
* @since 25.0.0
*/
public function getRichObjectType(): string;
/**
* Returns the reference specific rich object representation
*
* @since 25.0.0
*/
public function getRichObject(): array;
/**
* Returns the opengraph rich object representation
*
* @since 25.0.0
*/
public function getOpenGraphObject(): array;
/**
* @since 25.0.0
*/
public static function toCache(IReference $reference): array;
/**
* @since 25.0.0
*/
public static function fromCache(array $cache): IReference;
}

5
lib/public/Collaboration/Reference/IReferenceManager.php

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
@ -38,7 +40,8 @@ interface IReferenceManager {
*
* This method has a fallback to always provide the open graph metadata,
* but may still return null in case this is disabled or the fetching fails
*
* @since 25.0.0
*/
public function resolveReference(string $reference): ?IReference;
public function resolveReference(string $referenceId): ?IReference;
}

2
lib/public/Collaboration/Reference/IReferenceProvider.php

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*

10
lib/public/IURLGenerator.php

@ -35,6 +35,16 @@ namespace OCP;
* @since 6.0.0
*/
interface IURLGenerator {
/**
* Regex for matching http(s) urls
*
* This is a copy of the frontend regex in core/src/OCP/comments.js, make sure to adjust both when changing
*
* @since 25.0.0
*/
public const URL_REGEX = '/(\s|\n|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|\n|$)/mi';
/**
* Returns the URL for a route
* @param string $routeName the name of the route

Loading…
Cancel
Save