]> BookStack Code Mirror - bookstack/blob - app/Activity/CommentRepo.php
Comments & Pointer: Converted components to typescript
[bookstack] / app / Activity / CommentRepo.php
1 <?php
2
3 namespace BookStack\Activity;
4
5 use BookStack\Activity\Models\Comment;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Facades\Activity as ActivityService;
8 use BookStack\Util\HtmlDescriptionFilter;
9
10 class CommentRepo
11 {
12     /**
13      * Get a comment by ID.
14      */
15     public function getById(int $id): Comment
16     {
17         return Comment::query()->findOrFail($id);
18     }
19
20     /**
21      * Create a new comment on an entity.
22      */
23     public function create(Entity $entity, string $html, ?int $parent_id): Comment
24     {
25         $userId = user()->id;
26         $comment = new Comment();
27
28         $comment->html = HtmlDescriptionFilter::filterFromString($html);
29         $comment->created_by = $userId;
30         $comment->updated_by = $userId;
31         $comment->local_id = $this->getNextLocalId($entity);
32         $comment->parent_id = $parent_id;
33
34         $entity->comments()->save($comment);
35         ActivityService::add(ActivityType::COMMENT_CREATE, $comment);
36         ActivityService::add(ActivityType::COMMENTED_ON, $entity);
37
38         return $comment;
39     }
40
41     /**
42      * Update an existing comment.
43      */
44     public function update(Comment $comment, string $html): Comment
45     {
46         $comment->updated_by = user()->id;
47         $comment->html = HtmlDescriptionFilter::filterFromString($html);
48         $comment->save();
49
50         ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
51
52         return $comment;
53     }
54
55     /**
56      * Delete a comment from the system.
57      */
58     public function delete(Comment $comment): void
59     {
60         $comment->delete();
61
62         ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
63     }
64
65     /**
66      * Get the next local ID relative to the linked entity.
67      */
68     protected function getNextLocalId(Entity $entity): int
69     {
70         $currentMaxId = $entity->comments()->max('local_id');
71
72         return $currentMaxId + 1;
73     }
74 }