]> BookStack Code Mirror - bookstack/commitdiff
Display warnings when saving draft if another user is editing the page or if the...
authorMatthieuParis <redacted>
Sun, 8 Aug 2021 17:20:15 +0000 (19:20 +0200)
committerMatthieuParis <redacted>
Sun, 8 Aug 2021 17:20:15 +0000 (19:20 +0200)
app/Entities/Tools/PageEditActivity.php
app/Http/Controllers/PageController.php
resources/js/components/page-editor.js

index a88dea8307767b2afe5a770f30f60296271634e0..052427f6ca3fae34133a7a66dfaf1a783547cb19 100644 (file)
@@ -26,6 +26,7 @@ class PageEditActivity
      */
     public function hasActiveEditing(): bool
     {
+        $value = $this->activePageEditingQuery(60)->count();
         return $this->activePageEditingQuery(60)->count() > 0;
     }
 
@@ -43,6 +44,16 @@ class PageEditActivity
         return trans('entities.pages_draft_edit_active.message', ['start' => $userMessage, 'time' => $timeMessage]);
     }
 
+    /**
+     * Check if the page has been updated since the draft has been saved.
+     *
+     * @return bool
+     */
+    public function hasPageBeenUpdatedSinceDraftSaved(PageRevision $draft): bool
+    {
+        return $draft->page->updated_at->timestamp >= $draft->updated_at->timestamp;
+    }
+
     /**
      * Get the message to show when the user will be editing one of their drafts.
      *
index 853ac28fc4c770501f056d4972acfc2c4399f4aa..4818b52116ce968f9533fe712400565a8f96d6d8 100644 (file)
@@ -258,6 +258,23 @@ class PageController extends Controller
             return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
         }
 
+        // Check for active editing or time conflict
+        $warnings = [];
+        $jsonResponseWarning = '';
+        $editActivity = new PageEditActivity($page);
+        if ($editActivity->hasActiveEditing()) {
+            $warnings[] = $editActivity->activeEditingMessage();
+        }
+        $userDraft = $this->pageRepo->getUserDraft($page);
+        if ($userDraft !== null) {
+            if ($editActivity->hasPageBeenUpdatedSinceDraftSaved($userDraft)) {
+                $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
+            }
+        }
+        if (count($warnings) > 0) {
+            $jsonResponseWarning = implode("\n", $warnings);
+        }
+
         $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
 
         $updateTime = $draft->updated_at->timestamp;
@@ -265,6 +282,7 @@ class PageController extends Controller
         return response()->json([
             'status'    => 'success',
             'message'   => trans('entities.pages_edit_draft_save_at'),
+            'warning'   => $jsonResponseWarning,
             'timestamp' => $updateTime,
         ]);
     }
index f66e23b19923d2814b12ea5e2da844892c563557..e753b58e18309879e3ddfbfdfa5ec78702830e8b 100644 (file)
@@ -119,6 +119,9 @@ class PageEditor {
             }
             this.draftNotifyChange(`${resp.data.message} ${Dates.utcTimeStampToLocalTime(resp.data.timestamp)}`);
             this.autoSave.last = Date.now();
+            if (resp.data.warning.length > 0) {
+                window.$events.emit('warning', resp.data.warning);
+            }
         } catch (err) {
             // Save the editor content in LocalStorage as a last resort, just in case.
             try {