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