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::COMMENTED_ON, $entity);
42 * Update an existing comment.
44 public function update(Comment $comment, string $text): Comment
46 $comment->updated_by = user()->id;
47 $comment->text = $text;
48 $comment->html = $this->commentToHtml($text);
55 * Delete a comment from the system.
57 public function delete(Comment $comment): void
63 * Convert the given comment Markdown to HTML.
65 public function commentToHtml(string $commentText): string
67 $converter = new CommonMarkConverter([
68 'html_input' => 'strip',
69 'max_nesting_level' => 10,
70 'allow_unsafe_links' => false,
73 return $converter->convert($commentText);
77 * Get the next local ID relative to the linked entity.
79 protected function getNextLocalId(Entity $entity): int
81 $currentMaxId = $entity->comments()->max('local_id');
83 return $currentMaxId + 1;