0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-05-10 02:50:39 +00:00

Reviewed and refactored additional editor draft save warnings

- Added testing to cover warning cases.
- Refactored logic to be simpler and move much of the business out of
  the controller.
- Added new message that's more suitable to the case this was handling.
- For detecting an outdated draft, checked the draft created_at time
  instead of updated_at to better fit the scenario being checked.
- Updated some method types to align with those potentially being used
  in the logic of the code.
- Added a cache of shown messages on the front-end to prevent them
  re-showing on every save during the session, even if dismissed.
This commit is contained in:
Dan Brown 2021-10-04 20:26:55 +01:00
parent 756b55bbff
commit f99af807d0
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
6 changed files with 96 additions and 39 deletions
app/Http/Controllers

View file

@ -4,6 +4,7 @@ namespace BookStack\Http\Controllers;
use BookStack\Actions\View;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Models\PageRevision;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\NextPreviousContentLocator;
@ -258,32 +259,14 @@ 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;
$warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
return response()->json([
'status' => 'success',
'message' => trans('entities.pages_edit_draft_save_at'),
'warning' => $jsonResponseWarning,
'timestamp' => $updateTime,
'warning' => implode("\n", $warnings),
'timestamp' => $draft->updated_at->timestamp,
]);
}