$this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
}
+ public function matchReference(string $referenceText): bool {
+ return str_starts_with($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/'))
+ || str_starts_with($referenceText, $this->urlGenerator->getAbsoluteURL('/f/'));
+ }
+
public function resolveReference(string $referenceText): ?IReference {
- if (str_starts_with($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) || str_starts_with($referenceText, $this->urlGenerator->getAbsoluteURL('/f/'))) {
+ if ($this->matchReference($referenceText)) {
$reference = new Reference($referenceText);
try {
$this->fetchReference($reference);
* @throws \OC\User\NoUserException
*/
private function fetchReference(Reference $reference) {
+ if ($this->userId === null) {
+ throw new NotFoundException();
+ }
+
$fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $reference->getId());
$fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $fileId);
'preview-available' => false
]);
}
+
+ public function isGloballyCachable(): bool {
+ return false;
+ }
+
+ public function getCacheKey(string $referenceId): string {
+ return $this->userId;
+ }
}
$this->systemConfig = $systemConfig;
}
- public function resolveReference(string $referenceText): ?IReference {
+ public function matchReference(string $referenceText): bool {
if ($this->systemConfig->getValue('reference_opengraph', true) !== true) {
- return null;
+ return false;
}
- if (preg_match(self::URL_PATTERN, $referenceText)) {
+ return (bool)preg_match(self::URL_PATTERN, $referenceText);
+ }
+
+ public function resolveReference(string $referenceText): ?IReference {
+ if ($this->matchReference($referenceText)) {
$reference = new Reference($referenceText);
$this->fetchReference($reference);
return $reference;
$reference->setImageUrl($object->images[0]->url);
}
}
+
+ public function isGloballyCachable(): bool {
+ return true;
+ }
+
+ public function getCacheKey(string $referenceId): string {
+ return '';
+ }
}
}
public function resolveReference(string $referenceId): ?IReference {
- $cached = $this->cache->get($referenceId);
+ $matchedProvider = null;
+ foreach ($this->providers as $provider) {
+ $matchedProvider = $provider->matchReference($referenceId) ? $provider : null;
+ }
+
+ if ($matchedProvider === null) {
+ $matchedProvider = $this->linkReferenceProvider;
+ }
+
+ $cacheKey = md5(serialize([
+ $matchedProvider->isGloballyCachable() ? 0 : $matchedProvider->getCacheKey($referenceId),
+ $referenceId
+ ]));
+ $cached = $this->cache->get($cacheKey);
if ($cached) {
- // TODO: Figure out caching for references that depend on the viewer user
return Reference::fromCache($cached);
}
- foreach ($this->providers as $provider) {
- $reference = $provider->resolveReference($referenceId);
- if ($reference) {
- $this->cache->set($referenceId, Reference::toCache($reference), 60);
- return $reference;
- }
+
+ $reference = $matchedProvider->resolveReference($referenceId);
+ if ($reference) {
+ $this->cache->set($cacheKey, Reference::toCache($reference), 60);
+ return $reference;
}
- return $this->linkReferenceProvider->resolveReference($referenceId);
+ return null;
}
public function registerReferenceProvider(IReferenceProvider $provider): void {
namespace OCP\Collaboration\Reference;
interface IReferenceProvider {
+ public function matchReference(string $referenceText): bool;
public function resolveReference(string $referenceText): ?IReference;
+ public function isGloballyCachable(): bool;
+ public function getCacheKey(string $referenceId): string;
}