2021-08-08 15:59:04 -04:00
|
|
|
<?php
|
2021-06-26 11:23:15 -04:00
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools;
|
2019-10-05 07:55:01 -04:00
|
|
|
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use BookStack\Entities\Models\PageRevision;
|
2019-10-05 07:55:01 -04:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
|
|
|
class PageEditActivity
|
|
|
|
{
|
2022-04-17 18:01:14 -04:00
|
|
|
protected Page $page;
|
2019-10-05 07:55:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PageEditActivity constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(Page $page)
|
|
|
|
{
|
|
|
|
$this->page = $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if there's active editing being performed on this page.
|
|
|
|
*/
|
|
|
|
public function hasActiveEditing(): bool
|
|
|
|
{
|
|
|
|
return $this->activePageEditingQuery(60)->count() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a notification message concerning the editing activity on the page.
|
|
|
|
*/
|
|
|
|
public function activeEditingMessage(): string
|
|
|
|
{
|
|
|
|
$pageDraftEdits = $this->activePageEditingQuery(60)->get();
|
|
|
|
$count = $pageDraftEdits->count();
|
|
|
|
|
2021-11-05 20:32:01 -04:00
|
|
|
$userMessage = trans('entities.pages_draft_edit_active.start_a', ['count' => $count]);
|
|
|
|
if ($count === 1) {
|
|
|
|
/** @var PageRevision $firstDraft */
|
|
|
|
$firstDraft = $pageDraftEdits->first();
|
|
|
|
$userMessage = trans('entities.pages_draft_edit_active.start_b', ['userName' => $firstDraft->createdBy->name ?? '']);
|
|
|
|
}
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$timeMessage = trans('entities.pages_draft_edit_active.time_b', ['minCount'=> 60]);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return trans('entities.pages_draft_edit_active.message', ['start' => $userMessage, 'time' => $timeMessage]);
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:26:55 -04:00
|
|
|
/**
|
|
|
|
* Get any editor clash warning messages to show for the given draft revision.
|
2021-10-08 17:23:17 -04:00
|
|
|
*
|
2021-10-04 15:26:55 -04:00
|
|
|
* @param PageRevision|Page $draft
|
2021-10-08 17:23:17 -04:00
|
|
|
*
|
2021-10-04 15:26:55 -04:00
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getWarningMessagesForDraft($draft): array
|
|
|
|
{
|
|
|
|
$warnings = [];
|
|
|
|
|
|
|
|
if ($this->hasActiveEditing()) {
|
|
|
|
$warnings[] = $this->activeEditingMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($draft instanceof PageRevision && $this->hasPageBeenUpdatedSinceDraftCreated($draft)) {
|
|
|
|
$warnings[] = trans('entities.pages_draft_page_changed_since_creation');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $warnings;
|
|
|
|
}
|
|
|
|
|
2021-08-08 13:20:15 -04:00
|
|
|
/**
|
|
|
|
* Check if the page has been updated since the draft has been saved.
|
|
|
|
*/
|
2021-10-04 15:26:55 -04:00
|
|
|
protected function hasPageBeenUpdatedSinceDraftCreated(PageRevision $draft): bool
|
2021-08-08 13:20:15 -04:00
|
|
|
{
|
2021-10-04 15:26:55 -04:00
|
|
|
return $draft->page->updated_at->timestamp > $draft->created_at->timestamp;
|
2021-08-08 13:20:15 -04:00
|
|
|
}
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
/**
|
|
|
|
* Get the message to show when the user will be editing one of their drafts.
|
|
|
|
*/
|
|
|
|
public function getEditingActiveDraftMessage(PageRevision $draft): string
|
|
|
|
{
|
|
|
|
$message = trans('entities.pages_editing_draft_notification', ['timeDiff' => $draft->updated_at->diffForHumans()]);
|
|
|
|
if ($draft->page->updated_at->timestamp <= $draft->updated_at->timestamp) {
|
|
|
|
return $message;
|
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return $message . "\n" . trans('entities.pages_draft_edited_notification');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A query to check for active update drafts on a particular page
|
|
|
|
* within the last given many minutes.
|
|
|
|
*/
|
|
|
|
protected function activePageEditingQuery(int $withinMinutes): Builder
|
|
|
|
{
|
|
|
|
$checkTime = Carbon::now()->subMinutes($withinMinutes);
|
|
|
|
$query = PageRevision::query()
|
|
|
|
->where('type', '=', 'update_draft')
|
|
|
|
->where('page_id', '=', $this->page->id)
|
|
|
|
->where('updated_at', '>', $this->page->updated_at)
|
|
|
|
->where('created_by', '!=', user()->id)
|
|
|
|
->where('updated_at', '>=', $checkTime)
|
|
|
|
->with('createdBy');
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|