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): 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;
34 $entity->comments()->save($comment);
35 ActivityService::add(ActivityType::COMMENT_CREATE, $comment);
36 ActivityService::add(ActivityType::COMMENTED_ON, $entity);
42 * Update an existing comment.
44 public function update(Comment $comment, string $html): Comment
46 $comment->updated_by = user()->id;
47 $comment->html = HtmlDescriptionFilter::filterFromString($html);
50 ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
56 * Delete a comment from the system.
58 public function delete(Comment $comment): void
62 ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
66 * Get the next local ID relative to the linked entity.
68 protected function getNextLocalId(Entity $entity): int
70 $currentMaxId = $entity->comments()->max('local_id');
72 return $currentMaxId + 1;