3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Models\PageRevision;
8 use Illuminate\Database\Eloquent\Builder;
10 class PageEditActivity
15 * PageEditActivity constructor.
17 public function __construct(Page $page)
23 * Check if there's active editing being performed on this page.
27 public function hasActiveEditing(): bool
29 return $this->activePageEditingQuery(60)->count() > 0;
33 * Get a notification message concerning the editing activity on the page.
35 public function activeEditingMessage(): string
37 $pageDraftEdits = $this->activePageEditingQuery(60)->get();
38 $count = $pageDraftEdits->count();
40 $userMessage = $count > 1 ? trans('entities.pages_draft_edit_active.start_a', ['count' => $count]) : trans('entities.pages_draft_edit_active.start_b', ['userName' => $pageDraftEdits->first()->createdBy->name]);
41 $timeMessage = trans('entities.pages_draft_edit_active.time_b', ['minCount'=> 60]);
43 return trans('entities.pages_draft_edit_active.message', ['start' => $userMessage, 'time' => $timeMessage]);
47 * Get the message to show when the user will be editing one of their drafts.
49 * @param PageRevision $draft
53 public function getEditingActiveDraftMessage(PageRevision $draft): string
55 $message = trans('entities.pages_editing_draft_notification', ['timeDiff' => $draft->updated_at->diffForHumans()]);
56 if ($draft->page->updated_at->timestamp <= $draft->updated_at->timestamp) {
60 return $message . "\n" . trans('entities.pages_draft_edited_notification');
64 * A query to check for active update drafts on a particular page
65 * within the last given many minutes.
67 protected function activePageEditingQuery(int $withinMinutes): Builder
69 $checkTime = Carbon::now()->subMinutes($withinMinutes);
70 $query = PageRevision::query()
71 ->where('type', '=', 'update_draft')
72 ->where('page_id', '=', $this->page->id)
73 ->where('updated_at', '>', $this->page->updated_at)
74 ->where('created_by', '!=', user()->id)
75 ->where('updated_at', '>=', $checkTime)