From 84af64c321711a31ecef9458085462e2ebedfd27 Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 29 Sep 2026 12:01:35 +0200 Subject: [PATCH] perf(session): fetch document only once in SessionMiddleware Endpoints that require both a document session and a matching base version etag (push, sync, save) looked up the document twice per request: once in assertDocumentBaseVersionEtag() and again in assertDocumentSession(). Keep the fetched document in the middleware for the rest of the request so both checks share one query. Supersedes #5773. Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5.5 --- lib/Middleware/SessionMiddleware.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/Middleware/SessionMiddleware.php b/lib/Middleware/SessionMiddleware.php index 2c0852be7fc..72f19602133 100644 --- a/lib/Middleware/SessionMiddleware.php +++ b/lib/Middleware/SessionMiddleware.php @@ -9,6 +9,7 @@ use OC\User\NoUserException; use OCA\Text\Controller\ISessionAwareController; +use OCA\Text\Db\Document; use OCA\Text\Exception\AccountDisabledException; use OCA\Text\Exception\InvalidDocumentBaseVersionEtagException; use OCA\Text\Exception\InvalidSessionException; @@ -37,6 +38,7 @@ use ReflectionException; class SessionMiddleware extends Middleware { + private ?Document $document = null; public function __construct( private IRequest $request, @@ -88,7 +90,7 @@ private function assertDocumentBaseVersionEtag(): void { $documentId = (int)$this->request->getParam('documentId'); $baseVersionEtag = $this->request->getParam('baseVersionEtag'); - $document = $this->documentService->getDocument($documentId); + $document = $this->getDocument($documentId); if ($baseVersionEtag && $document?->getBaseVersionEtag() !== $baseVersionEtag) { throw new InvalidDocumentBaseVersionEtagException(); } @@ -116,7 +118,7 @@ private function assertDocumentSession(ISessionAwareController $controller): voi } } - $document = $this->documentService->getDocument($documentId); + $document = $this->getDocument($documentId); if (!$document) { throw new InvalidSessionException(); } @@ -194,6 +196,13 @@ private function assertUserOrShareToken(ISessionAwareController $controller): vo throw new InvalidSessionException(); } + private function getDocument(int $documentId): ?Document { + if ($this->document?->getId() !== $documentId) { + $this->document = $this->documentService->getDocument($documentId); + } + return $this->document; + } + public function afterException($controller, $methodName, \Exception $exception): JSONResponse|Response { if ($exception instanceof InvalidDocumentBaseVersionEtagException) { return new JSONResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED);