1 <?php namespace BookStack\Actions;
3 use BookStack\Entities\Models\Entity;
4 use League\CommonMark\CommonMarkConverter;
5 use BookStack\Facades\Activity as ActivityService;
14 * @var Comment $comment
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);
53 * Update an existing comment.
55 public function update(Comment $comment, string $text): Comment
57 $comment->updated_by = user()->id;
58 $comment->text = $text;
59 $comment->html = $this->commentToHtml($text);
65 * Delete a comment from the system.
67 public function delete(Comment $comment)
73 * Convert the given comment markdown text to HTML.
75 public function commentToHtml(string $commentText): string
77 $converter = new CommonMarkConverter([
78 'html_input' => 'strip',
79 'max_nesting_level' => 10,
80 'allow_unsafe_links' => false,
83 return $converter->convertToHtml($commentText);
87 * Get the next local ID relative to the linked entity.
89 protected function getNextLocalId(Entity $entity): int
91 $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first();
92 return ($comments->local_id ?? 0) + 1;