3 namespace BookStack\Actions;
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Facades\Activity as ActivityService;
7 use League\CommonMark\CommonMarkConverter;
19 public function __construct(Comment $comment)
21 $this->comment = $comment;
25 * Get a comment by ID.
27 public function getById(int $id): Comment
29 return $this->comment->newQuery()->findOrFail($id);
33 * Create a new comment on an entity.
35 public function create(Entity $entity, string $text, ?int $parent_id): Comment
38 $comment = $this->comment->newInstance();
40 $comment->text = $text;
41 $comment->html = $this->commentToHtml($text);
42 $comment->created_by = $userId;
43 $comment->updated_by = $userId;
44 $comment->local_id = $this->getNextLocalId($entity);
45 $comment->parent_id = $parent_id;
47 $entity->comments()->save($comment);
48 ActivityService::addForEntity($entity, ActivityType::COMMENTED_ON);
54 * Update an existing comment.
56 public function update(Comment $comment, string $text): Comment
58 $comment->updated_by = user()->id;
59 $comment->text = $text;
60 $comment->html = $this->commentToHtml($text);
67 * Delete a comment from the system.
69 public function delete(Comment $comment): void
75 * Convert the given comment Markdown to HTML.
77 public function commentToHtml(string $commentText): string
79 $converter = new CommonMarkConverter([
80 'html_input' => 'strip',
81 'max_nesting_level' => 10,
82 'allow_unsafe_links' => false,
85 return $converter->convertToHtml($commentText);
89 * Get the next local ID relative to the linked entity.
91 protected function getNextLocalId(Entity $entity): int
93 /** @var Comment $comment */
94 $comment = $entity->comments(false)->orderBy('local_id', 'desc')->first();
96 return ($comment->local_id ?? 0) + 1;