1 <?php namespace BookStack\Actions;
3 use BookStack\Entities\Entity;
4 use League\CommonMark\CommonMarkConverter;
13 * @var Comment $comment
18 public function __construct(Comment $comment)
20 $this->comment = $comment;
24 * Get a comment by ID.
26 public function getById(int $id): Comment
28 return $this->comment->newQuery()->findOrFail($id);
32 * Create a new comment on an entity.
34 public function create(Entity $entity, string $text, ?int $parent_id): Comment
37 $comment = $this->comment->newInstance();
39 $comment->text = $text;
40 $comment->html = $this->commentToHtml($text);
41 $comment->created_by = $userId;
42 $comment->updated_by = $userId;
43 $comment->local_id = $this->getNextLocalId($entity);
44 $comment->parent_id = $parent_id;
46 $entity->comments()->save($comment);
47 Activity::add($entity, ActivityType::COMMENTED_ON, $entity->book->id);
52 * Update an existing comment.
54 public function update(Comment $comment, string $text): Comment
56 $comment->updated_by = user()->id;
57 $comment->text = $text;
58 $comment->html = $this->commentToHtml($text);
64 * Delete a comment from the system.
66 public function delete(Comment $comment)
72 * Convert the given comment markdown text to HTML.
74 public function commentToHtml(string $commentText): string
76 $converter = new CommonMarkConverter([
77 'html_input' => 'strip',
78 'max_nesting_level' => 10,
79 'allow_unsafe_links' => false,
82 return $converter->convertToHtml($commentText);
86 * Get the next local ID relative to the linked entity.
88 protected function getNextLocalId(Entity $entity): int
90 $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first();
91 return ($comments->local_id ?? 0) + 1;