Skip to content
Prev Previous commit
Inject all dependnencies and increase cache timeout
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Aug 31, 2022
commit 1ab66988bc6e5dca0b0b18ad9366880124fb28e1
3 changes: 2 additions & 1 deletion core/Controller/ReferenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace OC\Core\Controller;

use OCP\AppFramework\Http\Response;
use OCP\Collaboration\Reference\IReferenceManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
Expand All @@ -48,7 +49,7 @@ public function __construct(string $appName, IRequest $request, IReferenceManage
* @PublicPage
* @NoCSRFRequired
*/
public function preview(string $referenceId) {
public function preview(string $referenceId): Response {
$reference = $this->referenceManager->getReferenceByCacheKey($referenceId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get a hit here locally so I don't have any previews. Is there something else needed to configure? Is a Redis required? Or is the method not loading the providers or something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, would require redis/apcu in its current state. I'd need to think how we can make this work without, as currently we have no reference from the hashed reference that is used for the preview to the original one.

if ($reference === null) {
return new DataResponse('', Http::STATUS_NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function resolveReference(string $referenceText): ?IReference {
/**
* @throws NotFoundException
*/
private function fetchReference(Reference $reference) {
private function fetchReference(Reference $reference): void {
if ($this->userId === null) {
throw new NotFoundException();
}
Expand Down
14 changes: 10 additions & 4 deletions lib/private/Collaboration/Reference/LinkReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\NotFoundException;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

class LinkReferenceProvider implements IReferenceProvider {
Expand All @@ -56,14 +58,18 @@ class LinkReferenceProvider implements IReferenceProvider {
private IAppDataFactory $appDataFactory;
private IURLGenerator $urlGenerator;
private Limiter $limiter;
private IUserSession $userSession;
private IRequest $request;

public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator, Limiter $limiter) {
public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator, Limiter $limiter, IUserSession $userSession, IRequest $request) {
$this->clientService = $clientService;
$this->logger = $logger;
$this->systemConfig = $systemConfig;
$this->appDataFactory = $appDataFactory;
$this->urlGenerator = $urlGenerator;
$this->limiter = $limiter;
$this->userSession = $userSession;
$this->request = $request;
}

public function matchReference(string $referenceText): bool {
Expand All @@ -84,13 +90,13 @@ public function resolveReference(string $referenceText): ?IReference {
return null;
}

private function fetchReference(Reference $reference) {
private function fetchReference(Reference $reference): void {
try {
$user = \OC::$server->getUserSession()->getUser();
$user = $this->userSession->getUser();
if ($user) {
$this->limiter->registerUserRequest('opengraph', 10, 120, $user);
} else {
$this->limiter->registerAnonRequest('opengraph', 10, 120, \OC::$server->getRequest()->getRemoteAddress());
$this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress());
}
} catch (RateLimitExceededException $e) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Collaboration/Reference/ReferenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
use Throwable;

class ReferenceManager implements IReferenceManager {
public const CACHE_TTL = 60;
public const CACHE_TTL = 3600;

/** @var IReferenceProvider[]|null */
private ?array $providers = null;
Expand Down