]> source.dussan.org Git - nextcloud-server.git/commitdiff
Make reference fetching configurable
authorJulius Härtl <jus@bitgrid.net>
Fri, 12 Aug 2022 15:59:18 +0000 (17:59 +0200)
committerJulius Härtl <jus@bitgrid.net>
Wed, 31 Aug 2022 14:20:06 +0000 (16:20 +0200)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
lib/private/Collaboration/Reference/LinkReferenceProvider.php
lib/private/Collaboration/Reference/ReferenceManager.php

index 75d6717de10eaa1ac897db5ec66c4143d0fa8136..b4c44344476587890a76c732fbfb79a1b2a2e746 100644 (file)
@@ -25,6 +25,7 @@ declare(strict_types=1);
 namespace OC\Collaboration\Reference;
 
 use Fusonic\OpenGraph\Consumer;
+use OC\SystemConfig;
 use OCP\Collaboration\Reference\IReference;
 use OCP\Collaboration\Reference\IReferenceProvider;
 use OCP\Http\Client\IClientService;
@@ -35,13 +36,19 @@ class LinkReferenceProvider implements IReferenceProvider {
 
        private IClientService $clientService;
        private LoggerInterface $logger;
+       private SystemConfig $systemConfig;
 
-       public function __construct(IClientService $clientService, LoggerInterface $logger) {
+       public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig) {
                $this->clientService = $clientService;
                $this->logger = $logger;
+               $this->systemConfig = $systemConfig;
        }
 
        public function resolveReference(string $referenceText): ?IReference {
+               if ($this->systemConfig->getValue('reference_opengraph', true) !== true) {
+                       return null;
+               }
+
                if (preg_match(self::URL_PATTERN, $referenceText)) {
                        $reference = new Reference($referenceText);
                        $this->fetchReference($reference);
@@ -51,7 +58,7 @@ class LinkReferenceProvider implements IReferenceProvider {
                return null;
        }
 
-       public function fetchReference(Reference $reference) {
+       private function fetchReference(Reference $reference) {
                $client = $this->clientService->newClient();
                try {
                        $response = $client->get($reference->getId());
index dc2f01416fef61292511aa8f0c49aea3c979fc24..6f0ef37198e29579b427c990124b5a907ffd6090 100644 (file)
@@ -32,25 +32,22 @@ use OCP\ICacheFactory;
 
 class ReferenceManager implements IReferenceManager {
        public const URL_PATTERN = '/(\s|\n|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|\n|$)/mi';
-       public const REF_PATTERN = '/#[A-z0-9_]+\/[A-z0-9_]+/i';
 
        /** @var IReferenceProvider[] */
        private array $providers = [];
        private ICache $cache;
 
+       private LinkReferenceProvider $linkReferenceProvider;
+
        public function __construct(LinkReferenceProvider $linkReferenceProvider, FileReferenceProvider $fileReferenceProvider, ICacheFactory $cacheFactory) {
                $this->registerReferenceProvider($fileReferenceProvider);
-               $this->registerReferenceProvider($linkReferenceProvider);
+               $this->linkReferenceProvider = $linkReferenceProvider;
                $this->cache = $cacheFactory->createDistributed('reference');
        }
 
        public function extractReferences(string $text): array {
-               $matches = [];
-               preg_match_all(self::REF_PATTERN, $text, $matches);
-               $references = $matches[0] ?? [];
-
                preg_match_all(self::URL_PATTERN, $text, $matches);
-               $references = array_merge($references, $matches[0] ?? []);
+               $references = $matches[0] ?? [];
                return array_map(function ($reference) {
                        return trim($reference);
                }, $references);
@@ -70,7 +67,7 @@ class ReferenceManager implements IReferenceManager {
                        }
                }
 
-               return null;
+               return $this->linkReferenceProvider->resolveReference($referenceId);
        }
 
        public function registerReferenceProvider(IReferenceProvider $provider): void {