2021-06-26 15:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-05-17 17:56:55 +01:00
|
|
|
namespace BookStack\Activity;
|
2017-01-13 21:45:48 +05:30
|
|
|
|
2023-05-17 17:56:55 +01:00
|
|
|
use BookStack\Activity\Models\Comment;
|
2020-11-22 00:17:45 +00:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2025-10-23 10:21:33 +01:00
|
|
|
use BookStack\Entities\Models\Page;
|
2025-04-28 20:09:18 +01:00
|
|
|
use BookStack\Exceptions\NotifyException;
|
2020-11-07 23:15:13 +00:00
|
|
|
use BookStack\Facades\Activity as ActivityService;
|
2024-01-30 15:16:58 +00:00
|
|
|
use BookStack\Util\HtmlDescriptionFilter;
|
2025-10-22 14:58:29 +01:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2017-01-13 21:45:48 +05:30
|
|
|
|
2018-01-28 16:58:52 +00:00
|
|
|
class CommentRepo
|
|
|
|
|
{
|
2017-09-03 16:37:51 +01:00
|
|
|
/**
|
|
|
|
|
* Get a comment by ID.
|
|
|
|
|
*/
|
2020-05-01 23:24:11 +01:00
|
|
|
public function getById(int $id): Comment
|
2017-09-03 16:37:51 +01:00
|
|
|
{
|
2023-06-07 13:24:49 +01:00
|
|
|
return Comment::query()->findOrFail($id);
|
2017-04-19 01:21:45 +05:30
|
|
|
}
|
|
|
|
|
|
2025-10-23 10:21:33 +01:00
|
|
|
/**
|
|
|
|
|
* Get a comment by ID, ensuring it is visible to the user based upon access to the page
|
|
|
|
|
* which the comment is attached to.
|
|
|
|
|
*/
|
|
|
|
|
public function getVisibleById(int $id): Comment
|
|
|
|
|
{
|
|
|
|
|
return $this->getQueryForVisible()->findOrFail($id);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 14:58:29 +01:00
|
|
|
/**
|
|
|
|
|
* Start a query for comments visible to the user.
|
2025-10-24 14:22:53 +01:00
|
|
|
* @return Builder<Comment>
|
2025-10-22 14:58:29 +01:00
|
|
|
*/
|
|
|
|
|
public function getQueryForVisible(): Builder
|
|
|
|
|
{
|
|
|
|
|
return Comment::query()->scopes('visible');
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 16:37:51 +01:00
|
|
|
/**
|
|
|
|
|
* Create a new comment on an entity.
|
|
|
|
|
*/
|
2025-05-12 15:31:55 +01:00
|
|
|
public function create(Entity $entity, string $html, ?int $parentId, string $contentRef): Comment
|
2017-09-03 16:37:51 +01:00
|
|
|
{
|
2025-10-23 10:21:33 +01:00
|
|
|
// Prevent comments being added to draft pages
|
|
|
|
|
if ($entity instanceof Page && $entity->draft) {
|
|
|
|
|
throw new \Exception(trans('errors.cannot_add_comment_to_draft'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate parent ID
|
|
|
|
|
if ($parentId !== null) {
|
|
|
|
|
$parentCommentExists = Comment::query()
|
2025-10-23 16:52:29 +01:00
|
|
|
->where('commentable_id', '=', $entity->id)
|
|
|
|
|
->where('commentable_type', '=', $entity->getMorphClass())
|
2025-10-23 10:21:33 +01:00
|
|
|
->where('local_id', '=', $parentId)
|
|
|
|
|
->exists();
|
|
|
|
|
if (!$parentCommentExists) {
|
|
|
|
|
$parentId = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 01:21:45 +05:30
|
|
|
$userId = user()->id;
|
2023-06-07 13:24:49 +01:00
|
|
|
$comment = new Comment();
|
2020-05-01 23:24:11 +01:00
|
|
|
|
2024-01-30 15:16:58 +00:00
|
|
|
$comment->html = HtmlDescriptionFilter::filterFromString($html);
|
2017-09-03 16:37:51 +01:00
|
|
|
$comment->created_by = $userId;
|
2017-04-19 01:21:45 +05:30
|
|
|
$comment->updated_by = $userId;
|
2017-09-03 16:37:51 +01:00
|
|
|
$comment->local_id = $this->getNextLocalId($entity);
|
2025-05-12 15:31:55 +01:00
|
|
|
$comment->parent_id = $parentId;
|
|
|
|
|
$comment->content_ref = preg_match('/^bkmrk-(.*?):\d+:(\d*-\d*)?$/', $contentRef) === 1 ? $contentRef : '';
|
2020-05-01 23:24:11 +01:00
|
|
|
|
2017-09-03 16:37:51 +01:00
|
|
|
$entity->comments()->save($comment);
|
2023-07-18 15:07:31 +01:00
|
|
|
ActivityService::add(ActivityType::COMMENT_CREATE, $comment);
|
2021-12-11 17:29:33 +00:00
|
|
|
ActivityService::add(ActivityType::COMMENTED_ON, $entity);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2025-10-24 15:14:25 +01:00
|
|
|
$comment->refresh()->unsetRelations();
|
2017-06-04 18:52:44 +05:30
|
|
|
return $comment;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 16:37:51 +01:00
|
|
|
/**
|
|
|
|
|
* Update an existing comment.
|
|
|
|
|
*/
|
2024-01-30 15:16:58 +00:00
|
|
|
public function update(Comment $comment, string $html): Comment
|
2017-09-03 16:37:51 +01:00
|
|
|
{
|
|
|
|
|
$comment->updated_by = user()->id;
|
2024-01-30 15:16:58 +00:00
|
|
|
$comment->html = HtmlDescriptionFilter::filterFromString($html);
|
2020-05-01 23:24:11 +01:00
|
|
|
$comment->save();
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2023-07-18 15:07:31 +01:00
|
|
|
ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
|
|
|
|
|
|
2017-04-19 01:21:45 +05:30
|
|
|
return $comment;
|
|
|
|
|
}
|
2017-05-16 00:40:14 +05:30
|
|
|
|
2025-04-28 15:37:09 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Archive an existing comment.
|
|
|
|
|
*/
|
2025-10-23 10:21:33 +01:00
|
|
|
public function archive(Comment $comment, bool $log = true): Comment
|
2025-04-28 15:37:09 +01:00
|
|
|
{
|
2025-04-28 20:09:18 +01:00
|
|
|
if ($comment->parent_id) {
|
2025-05-12 14:26:09 +01:00
|
|
|
throw new NotifyException('Only top-level comments can be archived.', '/', 400);
|
2025-04-28 20:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-28 15:37:09 +01:00
|
|
|
$comment->archived = true;
|
|
|
|
|
$comment->save();
|
|
|
|
|
|
2025-10-23 10:21:33 +01:00
|
|
|
if ($log) {
|
|
|
|
|
ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
|
|
|
|
|
}
|
2025-04-28 15:37:09 +01:00
|
|
|
|
|
|
|
|
return $comment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Un-archive an existing comment.
|
|
|
|
|
*/
|
2025-10-23 10:21:33 +01:00
|
|
|
public function unarchive(Comment $comment, bool $log = true): Comment
|
2025-04-28 15:37:09 +01:00
|
|
|
{
|
2025-04-28 20:09:18 +01:00
|
|
|
if ($comment->parent_id) {
|
2025-05-12 14:26:09 +01:00
|
|
|
throw new NotifyException('Only top-level comments can be un-archived.', '/', 400);
|
2025-04-28 20:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-28 15:37:09 +01:00
|
|
|
$comment->archived = false;
|
|
|
|
|
$comment->save();
|
|
|
|
|
|
2025-10-23 10:21:33 +01:00
|
|
|
if ($log) {
|
|
|
|
|
ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
|
|
|
|
|
}
|
2025-04-28 15:37:09 +01:00
|
|
|
|
|
|
|
|
return $comment;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 16:37:51 +01:00
|
|
|
/**
|
|
|
|
|
* Delete a comment from the system.
|
|
|
|
|
*/
|
2021-11-01 11:17:30 +00:00
|
|
|
public function delete(Comment $comment): void
|
2017-09-03 16:37:51 +01:00
|
|
|
{
|
2020-05-01 23:24:11 +01:00
|
|
|
$comment->delete();
|
2023-07-18 15:07:31 +01:00
|
|
|
|
|
|
|
|
ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
|
2020-05-01 23:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
2017-09-03 16:37:51 +01:00
|
|
|
/**
|
|
|
|
|
* Get the next local ID relative to the linked entity.
|
|
|
|
|
*/
|
2020-05-01 23:24:11 +01:00
|
|
|
protected function getNextLocalId(Entity $entity): int
|
2017-09-03 16:37:51 +01:00
|
|
|
{
|
2023-06-07 13:24:49 +01:00
|
|
|
$currentMaxId = $entity->comments()->max('local_id');
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2023-06-07 13:24:49 +01:00
|
|
|
return $currentMaxId + 1;
|
2017-04-19 01:21:45 +05:30
|
|
|
}
|
2018-01-28 16:58:52 +00:00
|
|
|
}
|