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 League\CommonMark\CommonMarkConverter;
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 $text, ?int $parent_id): Comment
26 $comment = new Comment();
28 $comment->text = $text;
29 $comment->html = $this->commentToHtml($text);
30 $comment->created_by = $userId;
31 $comment->updated_by = $userId;
32 $comment->local_id = $this->getNextLocalId($entity);
33 $comment->parent_id = $parent_id;
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 $text): Comment
47 $comment->updated_by = user()->id;
48 $comment->text = $text;
49 $comment->html = $this->commentToHtml($text);
52 ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
58 * Delete a comment from the system.
60 public function delete(Comment $comment): void
64 ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
68 * Convert the given comment Markdown to HTML.
70 public function commentToHtml(string $commentText): string
72 $converter = new CommonMarkConverter([
73 'html_input' => 'strip',
74 'max_nesting_level' => 10,
75 'allow_unsafe_links' => false,
78 return $converter->convert($commentText);
82 * Get the next local ID relative to the linked entity.
84 protected function getNextLocalId(Entity $entity): int
86 $currentMaxId = $entity->comments()->max('local_id');
88 return $currentMaxId + 1;