diff --git a/lib/Service/ApiService.php b/lib/Service/ApiService.php index 3a49d6be8ac..f7a972a5a25 100644 --- a/lib/Service/ApiService.php +++ b/lib/Service/ApiService.php @@ -94,7 +94,13 @@ public function close(string $documentId, int $sessionId, string $sessionToken, $this->sessionService->closeSession($documentId, $sessionId, $sessionToken); $this->sessionService->removeInactiveSessionsWithoutSteps($documentId); $activeSessions = $this->sessionService->getActiveSessions($documentId); - if (count($activeSessions) === 0) { + // "No session left" is not the same as "nobody is editing": a reload of the editor leaves + // rows behind that are still inside SESSION_VALID_TIME (5 min), and a close whose row cannot + // be matched leaves its own row too. Either way the lock was never released and the file + // stayed unwritable for everybody else. The lock protects other people, so ask about them. + $closer = $auth instanceof IUser ? $auth->getUID() : null; + $othersEditing = $this->sessionService->hasRecentSessionsOfOtherUsers($documentId, $closer); + if (count($activeSessions) === 0 || !$othersEditing) { $document = $this->documentService->getDocument($documentId); if ($document !== null) { $type = $document->getContextType(); diff --git a/lib/Service/SessionService.php b/lib/Service/SessionService.php index 6ebbb984f74..ef6051096e7 100644 --- a/lib/Service/SessionService.php +++ b/lib/Service/SessionService.php @@ -162,6 +162,29 @@ public function getSession(string $documentId, int $sessionId, string $token): ? return $this->session; } + /** + * Is another user still working on this document? + * + * The file lock exists to warn other people, so that is the question that decides whether it may + * be released. Leftover rows of the same user — one per reload of the editor, plus any row whose + * close could not be matched — must not keep a file locked. + * + * A live editor pushes every few seconds, so a short window separates "somebody is typing" from + * "these rows are leftovers". + */ + public function hasRecentSessionsOfOtherUsers(string $documentId, ?string $userId, int $graceSeconds = 60): bool { + $threshold = $this->timeFactory->now()->getTimestamp() - $graceSeconds; + foreach ($this->sessionMapper->findAll($documentId) as $session) { + if ($session->getLastContact() < $threshold || $session->isGuest()) { + continue; + } + if ($userId === null || $session->getUserId() !== $userId) { + return true; + } + } + return false; + } + public function getValidSession(string $documentId, int $sessionId, string $token): ?Session { $session = $this->getSession($documentId, $sessionId, $token); if ($session === null) {