2018-09-25 07:30:50 -04:00
|
|
|
<?php namespace BookStack\Actions;
|
2017-01-13 11:15:48 -05:00
|
|
|
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Entities\Entity;
|
2020-05-01 18:24:11 -04:00
|
|
|
use League\CommonMark\CommonMarkConverter;
|
2017-01-13 11:15:48 -05:00
|
|
|
|
|
|
|
/**
|
2017-09-03 11:37:51 -04:00
|
|
|
* Class CommentRepo
|
2017-01-13 11:15:48 -05:00
|
|
|
*/
|
2018-01-28 11:58:52 -05:00
|
|
|
class CommentRepo
|
|
|
|
{
|
2017-09-03 11:37:51 -04:00
|
|
|
|
2017-01-13 11:15:48 -05:00
|
|
|
/**
|
2020-05-01 18:24:11 -04:00
|
|
|
* @var Comment $comment
|
2017-01-13 11:15:48 -05:00
|
|
|
*/
|
|
|
|
protected $comment;
|
2017-04-18 15:51:45 -04:00
|
|
|
|
2020-05-01 18:24:11 -04:00
|
|
|
|
2017-04-18 15:51:45 -04:00
|
|
|
public function __construct(Comment $comment)
|
|
|
|
{
|
|
|
|
$this->comment = $comment;
|
|
|
|
}
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Get a comment by ID.
|
|
|
|
*/
|
2020-05-01 18:24:11 -04:00
|
|
|
public function getById(int $id): Comment
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
|
|
|
return $this->comment->newQuery()->findOrFail($id);
|
2017-04-18 15:51:45 -04:00
|
|
|
}
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Create a new comment on an entity.
|
|
|
|
*/
|
2020-05-01 18:24:11 -04:00
|
|
|
public function create(Entity $entity, string $text, ?int $parent_id): Comment
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
2017-04-18 15:51:45 -04:00
|
|
|
$userId = user()->id;
|
2020-05-01 18:24:11 -04:00
|
|
|
$comment = $this->comment->newInstance();
|
|
|
|
|
|
|
|
$comment->text = $text;
|
|
|
|
$comment->html = $this->commentToHtml($text);
|
2017-09-03 11:37:51 -04:00
|
|
|
$comment->created_by = $userId;
|
2017-04-18 15:51:45 -04:00
|
|
|
$comment->updated_by = $userId;
|
2017-09-03 11:37:51 -04:00
|
|
|
$comment->local_id = $this->getNextLocalId($entity);
|
2020-05-01 18:24:11 -04:00
|
|
|
$comment->parent_id = $parent_id;
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
$entity->comments()->save($comment);
|
2017-06-04 09:22:44 -04:00
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Update an existing comment.
|
|
|
|
*/
|
2020-05-01 18:24:11 -04:00
|
|
|
public function update(Comment $comment, string $text): Comment
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
|
|
|
$comment->updated_by = user()->id;
|
2020-05-01 18:24:11 -04:00
|
|
|
$comment->text = $text;
|
|
|
|
$comment->html = $this->commentToHtml($text);
|
|
|
|
$comment->save();
|
2017-04-18 15:51:45 -04:00
|
|
|
return $comment;
|
|
|
|
}
|
2017-05-15 15:10:14 -04:00
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Delete a comment from the system.
|
|
|
|
*/
|
2020-05-01 18:24:11 -04:00
|
|
|
public function delete(Comment $comment)
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
2020-05-01 18:24:11 -04:00
|
|
|
$comment->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the given comment markdown text to HTML.
|
|
|
|
*/
|
2020-05-01 18:41:47 -04:00
|
|
|
public function commentToHtml(string $commentText): string
|
2020-05-01 18:24:11 -04:00
|
|
|
{
|
|
|
|
$converter = new CommonMarkConverter([
|
|
|
|
'html_input' => 'strip',
|
|
|
|
'max_nesting_level' => 10,
|
|
|
|
'allow_unsafe_links' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $converter->convertToHtml($commentText);
|
2017-04-26 17:05:29 -04:00
|
|
|
}
|
2017-05-15 15:10:14 -04:00
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Get the next local ID relative to the linked entity.
|
|
|
|
*/
|
2020-05-01 18:24:11 -04:00
|
|
|
protected function getNextLocalId(Entity $entity): int
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
2017-09-09 10:56:24 -04:00
|
|
|
$comments = $entity->comments(false)->orderBy('local_id', 'desc')->first();
|
2020-05-01 18:24:11 -04:00
|
|
|
return ($comments->local_id ?? 0) + 1;
|
2017-04-18 15:51:45 -04:00
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|