X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/919660678bec2b94eaa84ac60d0313f5ef07dfb7..refs/pull/2283/head:/app/Actions/CommentRepo.php diff --git a/app/Actions/CommentRepo.php b/app/Actions/CommentRepo.php index 3422d141b..4dfe3ddb6 100644 --- a/app/Actions/CommentRepo.php +++ b/app/Actions/CommentRepo.php @@ -1,24 +1,20 @@ comment = $comment; @@ -26,65 +22,71 @@ class CommentRepo /** * Get a comment by ID. - * @param $id - * @return \BookStack\Actions\Comment|\Illuminate\Database\Eloquent\Model */ - public function getById($id) + public function getById(int $id): Comment { return $this->comment->newQuery()->findOrFail($id); } /** * Create a new comment on an entity. - * @param \BookStack\Entities\Entity $entity - * @param array $data - * @return \BookStack\Actions\Comment */ - public function create(Entity $entity, $data = []) + public function create(Entity $entity, string $text, ?int $parent_id): Comment { $userId = user()->id; - $comment = $this->comment->newInstance($data); + $comment = $this->comment->newInstance(); + + $comment->text = $text; + $comment->html = $this->commentToHtml($text); $comment->created_by = $userId; $comment->updated_by = $userId; $comment->local_id = $this->getNextLocalId($entity); + $comment->parent_id = $parent_id; + $entity->comments()->save($comment); return $comment; } /** * Update an existing comment. - * @param \BookStack\Actions\Comment $comment - * @param array $input - * @return mixed */ - public function update($comment, $input) + public function update(Comment $comment, string $text): Comment { $comment->updated_by = user()->id; - $comment->update($input); + $comment->text = $text; + $comment->html = $this->commentToHtml($text); + $comment->save(); return $comment; } /** * Delete a comment from the system. - * @param \BookStack\Actions\Comment $comment - * @return mixed */ - public function delete($comment) + public function delete(Comment $comment) { - return $comment->delete(); + $comment->delete(); + } + + /** + * Convert the given comment markdown text to HTML. + */ + public function commentToHtml(string $commentText): string + { + $converter = new CommonMarkConverter([ + 'html_input' => 'strip', + 'max_nesting_level' => 10, + 'allow_unsafe_links' => false, + ]); + + return $converter->convertToHtml($commentText); } /** * Get the next local ID relative to the linked entity. - * @param \BookStack\Entities\Entity $entity - * @return int */ - protected function getNextLocalId(Entity $entity) + protected function getNextLocalId(Entity $entity): int { $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first(); - if ($comments === null) { - return 1; - } - return $comments->local_id + 1; + return ($comments->local_id ?? 0) + 1; } }