From 537b1614c475ac1a9ff7455eae7e447c7d4a709f Mon Sep 17 00:00:00 2001 From: MatthieuParis <19178125+MatthieuParis@users.noreply.github.com> Date: Sun, 8 Aug 2021 19:20:15 +0200 Subject: [PATCH 1/3] Display warnings when saving draft if another user is editing the page or if the page was updated since the current user has started editing the page. --- app/Entities/Tools/PageEditActivity.php | 11 +++++++++++ app/Http/Controllers/PageController.php | 18 ++++++++++++++++++ resources/js/components/page-editor.js | 3 +++ 3 files changed, 32 insertions(+) diff --git a/app/Entities/Tools/PageEditActivity.php b/app/Entities/Tools/PageEditActivity.php index a88dea830..052427f6c 100644 --- a/app/Entities/Tools/PageEditActivity.php +++ b/app/Entities/Tools/PageEditActivity.php @@ -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. * diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index 853ac28fc..4818b5211 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -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, ]); } diff --git a/resources/js/components/page-editor.js b/resources/js/components/page-editor.js index f66e23b19..e753b58e1 100644 --- a/resources/js/components/page-editor.js +++ b/resources/js/components/page-editor.js @@ -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 { From c2e031ae3e012b82978424c5ea89eb416e283b82 Mon Sep 17 00:00:00 2001 From: MatthieuParis <19178125+MatthieuParis@users.noreply.github.com> Date: Sun, 8 Aug 2021 20:35:12 +0200 Subject: [PATCH 2/3] Testing command suppressed. --- app/Entities/Tools/PageEditActivity.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Entities/Tools/PageEditActivity.php b/app/Entities/Tools/PageEditActivity.php index 052427f6c..991882bef 100644 --- a/app/Entities/Tools/PageEditActivity.php +++ b/app/Entities/Tools/PageEditActivity.php @@ -1,4 +1,4 @@ -activePageEditingQuery(60)->count(); return $this->activePageEditingQuery(60)->count() > 0; } From 3c4415f3ff460ed7463ca7e674767dc570269b2f Mon Sep 17 00:00:00 2001 From: MatthieuParis <19178125+MatthieuParis@users.noreply.github.com> Date: Sun, 8 Aug 2021 21:59:04 +0200 Subject: [PATCH 3/3] Typo. --- app/Entities/Tools/PageEditActivity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Entities/Tools/PageEditActivity.php b/app/Entities/Tools/PageEditActivity.php index 991882bef..2e620aa2c 100644 --- a/app/Entities/Tools/PageEditActivity.php +++ b/app/Entities/Tools/PageEditActivity.php @@ -1,4 +1,4 @@ -a