@@ -112,6 +112,14 @@ public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapp
112112 }
113113 }
114114
115+ public function getDocument (File $ file ): ?Document {
116+ try {
117+ return $ this ->documentMapper ->find ($ file ->getId ());
118+ } catch (DoesNotExistException |NotFoundException $ e ) {
119+ return null ;
120+ }
121+ }
122+
115123 /**
116124 * @param File $file
117125 * @return Entity
@@ -127,7 +135,7 @@ public function createDocument(File $file): Document {
127135 // This way the user can still resolve conflicts in the editor view
128136 $ stepsVersion = $ this ->stepMapper ->getLatestVersion ($ document ->getId ());
129137 if ($ stepsVersion && ($ document ->getLastSavedVersion () !== $ stepsVersion )) {
130- $ this ->logger ->debug ('Unsaved steps but collission with file , continue collaborative editing ' );
138+ $ this ->logger ->debug ('Unsaved steps, continue collaborative editing ' );
131139 return $ document ;
132140 }
133141 return $ document ;
@@ -258,6 +266,7 @@ private function insertSteps($documentId, $sessionId, $steps, $version): int {
258266 }
259267 $ stepsVersion = $ this ->stepMapper ->getLatestVersion ($ document ->getId ());
260268 $ newVersion = $ stepsVersion + count ($ steps );
269+ $ this ->logger ->debug ("Adding steps to $ documentId: bumping version from $ stepsVersion to $ newVersion " );
261270 $ this ->cache ->set ('document-version- ' . $ document ->getId (), $ newVersion );
262271 $ step = new Step ();
263272 $ step ->setData ($ stepsJson );
@@ -332,6 +341,22 @@ public function autosave(?File $file, int $documentId, int $version, ?string $au
332341 return $ document ;
333342 }
334343
344+ if (empty ($ autoaveDocument )) {
345+ $ this ->logger ->debug ('Saving empty document ' , [
346+ 'requestVersion ' => $ version ,
347+ 'requestAutosaveDocument ' => $ autoaveDocument ,
348+ 'requestDocumentState ' => $ documentState ,
349+ 'document ' => $ document ->jsonSerialize (),
350+ 'fileSizeBeforeSave ' => $ file ->getSize (),
351+ 'steps ' => array_map (function (Step $ step ) {
352+ return $ step ->jsonSerialize ();
353+ }, $ this ->stepMapper ->find ($ documentId , 0 )),
354+ 'sessions ' => array_map (function (Session $ session ) {
355+ return $ session ->jsonSerialize ();
356+ }, $ this ->sessionMapper ->findAll ($ documentId ))
357+ ]);
358+ }
359+
335360 $ this ->cache ->set ('document-save-lock- ' . $ documentId , true , 10 );
336361 try {
337362 $ this ->lockManager ->runInScope (new LockContext (
@@ -344,46 +369,49 @@ public function autosave(?File $file, int $documentId, int $version, ?string $au
344369 $ this ->writeDocumentState ($ file ->getId (), $ documentState );
345370 }
346371 });
372+ $ document ->setLastSavedVersion ($ stepsVersion );
373+ $ document ->setLastSavedVersionTime (time ());
374+ $ document ->setLastSavedVersionEtag ($ file ->getEtag ());
375+ $ this ->documentMapper ->update ($ document );
347376 } catch (LockedException $ e ) {
348377 // Ignore lock since it might occur when multiple people save at the same time
349378 return $ document ;
379+ } finally {
380+ $ this ->cache ->remove ('document-save-lock- ' . $ documentId );
350381 }
351- $ document ->setLastSavedVersion ($ stepsVersion );
352- $ document ->setLastSavedVersionTime (time ());
353- $ document ->setLastSavedVersionEtag ($ file ->getEtag ());
354- $ this ->documentMapper ->update ($ document );
355- $ this ->cache ->remove ('document-save-lock- ' . $ documentId );
356382 return $ document ;
357383 }
358384
359385 /**
360- * @param $documentId
361- * @param bool $force
362386 * @throws DocumentHasUnsavedChangesException
387+ * @throws Exception
388+ * @throws NotPermittedException
363389 */
364390 public function resetDocument (int $ documentId , bool $ force = false ): void {
365391 try {
366- $ this ->unlock ($ documentId );
367-
368392 $ document = $ this ->documentMapper ->find ($ documentId );
369-
370- if ($ force || !$ this ->hasUnsavedChanges ($ document )) {
371- $ this ->stepMapper ->deleteAll ($ documentId );
372- $ this ->sessionMapper ->deleteByDocumentId ($ documentId );
373- $ this ->documentMapper ->delete ($ document );
374-
375- try {
376- $ this ->getStateFile ($ documentId )->delete ();
377- } catch (NotFoundException $ e ) {
378- } catch (NotPermittedException $ e ) {
379- }
380- } elseif ($ this ->hasUnsavedChanges ($ document )) {
393+ if (!$ force && $ this ->hasUnsavedChanges ($ document )) {
394+ $ this ->logger ->debug ('did not reset document for ' . $ documentId );
381395 throw new DocumentHasUnsavedChangesException ('Did not reset document, as it has unsaved changes ' );
382396 }
383- } catch (DoesNotExistException $ e ) {
397+
398+ $ this ->unlock ($ documentId );
399+
400+ $ this ->stepMapper ->deleteAll ($ documentId );
401+ $ this ->sessionMapper ->deleteByDocumentId ($ documentId );
402+ $ this ->documentMapper ->delete ($ document );
403+
404+ $ this ->getStateFile ($ documentId )->delete ();
405+ $ this ->logger ->debug ('document reset for ' . $ documentId );
406+ } catch (DoesNotExistException |NotFoundException $ e ) {
407+ // Ignore if document not found or state file not found
384408 }
385409 }
386410
411+ public function getAll () {
412+ return $ this ->documentMapper ->findAll ();
413+ }
414+
387415 /**
388416 * @param Session $session
389417 * @param $shareToken
0 commit comments