Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 23 additions & 0 deletions lib/Service/SessionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down