2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Activity;
|
2017-01-13 11:15:48 -05:00
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Activity\Models\Comment;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2020-11-07 18:15:13 -05:00
|
|
|
use BookStack\Facades\Activity as ActivityService;
|
2024-01-30 10:16:58 -05:00
|
|
|
use BookStack\Util\HtmlDescriptionFilter;
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2023-06-07 08:24:49 -04:00
|
|
|
return Comment::query()->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.
|
|
|
|
*/
|
2024-01-30 10:16:58 -05:00
|
|
|
public function create(Entity $entity, string $html, ?int $parent_id): Comment
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
2017-04-18 15:51:45 -04:00
|
|
|
$userId = user()->id;
|
2023-06-07 08:24:49 -04:00
|
|
|
$comment = new Comment();
|
2020-05-01 18:24:11 -04:00
|
|
|
|
2024-01-30 10:16:58 -05:00
|
|
|
$comment->html = HtmlDescriptionFilter::filterFromString($html);
|
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);
|
2023-07-18 10:07:31 -04:00
|
|
|
ActivityService::add(ActivityType::COMMENT_CREATE, $comment);
|
2021-12-11 12:29:33 -05:00
|
|
|
ActivityService::add(ActivityType::COMMENTED_ON, $entity);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2017-06-04 09:22:44 -04:00
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Update an existing comment.
|
|
|
|
*/
|
2024-01-30 10:16:58 -05:00
|
|
|
public function update(Comment $comment, string $html): Comment
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
|
|
|
$comment->updated_by = user()->id;
|
2024-01-30 10:16:58 -05:00
|
|
|
$comment->html = HtmlDescriptionFilter::filterFromString($html);
|
2020-05-01 18:24:11 -04:00
|
|
|
$comment->save();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2023-07-18 10:07:31 -04:00
|
|
|
ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
|
|
|
|
|
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.
|
|
|
|
*/
|
2021-11-01 07:17:30 -04:00
|
|
|
public function delete(Comment $comment): void
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
2020-05-01 18:24:11 -04:00
|
|
|
$comment->delete();
|
2023-07-18 10:07:31 -04:00
|
|
|
|
|
|
|
ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
|
2020-05-01 18:24:11 -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
|
|
|
{
|
2023-06-07 08:24:49 -04:00
|
|
|
$currentMaxId = $entity->comments()->max('local_id');
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2023-06-07 08:24:49 -04:00
|
|
|
return $currentMaxId + 1;
|
2017-04-18 15:51:45 -04:00
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|