3 namespace BookStack\Activity;
5 use BookStack\Activity\Models\Comment;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Facades\Activity as ActivityService;
8 use BookStack\Util\HtmlDescriptionFilter;
13 * Get a comment by ID.
15 public function getById(int $id): Comment
17 return Comment::query()->findOrFail($id);
21 * Create a new comment on an entity.
23 public function create(Entity $entity, string $html, ?int $parent_id, string $content_ref): Comment
26 $comment = new Comment();
28 $comment->html = HtmlDescriptionFilter::filterFromString($html);
29 $comment->created_by = $userId;
30 $comment->updated_by = $userId;
31 $comment->local_id = $this->getNextLocalId($entity);
32 $comment->parent_id = $parent_id;
33 $comment->content_ref = preg_match('/^bkmrk-(.*?):\d+:(\d*-\d*)?$/', $content_ref) === 1 ? $content_ref : '';
35 $entity->comments()->save($comment);
36 ActivityService::add(ActivityType::COMMENT_CREATE, $comment);
37 ActivityService::add(ActivityType::COMMENTED_ON, $entity);
43 * Update an existing comment.
45 public function update(Comment $comment, string $html): Comment
47 $comment->updated_by = user()->id;
48 $comment->html = HtmlDescriptionFilter::filterFromString($html);
51 ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
57 * Delete a comment from the system.
59 public function delete(Comment $comment): void
63 ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
67 * Get the next local ID relative to the linked entity.
69 protected function getNextLocalId(Entity $entity): int
71 $currentMaxId = $entity->comments()->max('local_id');
73 return $currentMaxId + 1;