]> BookStack Code Mirror - bookstack/blob - app/Activity/CommentRepo.php
Comments: Started archive display, created mode for tree node
[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\Exceptions\NotifyException;
8 use BookStack\Exceptions\PrettyException;
9 use BookStack\Facades\Activity as ActivityService;
10 use BookStack\Util\HtmlDescriptionFilter;
11
12 class CommentRepo
13 {
14     /**
15      * Get a comment by ID.
16      */
17     public function getById(int $id): Comment
18     {
19         return Comment::query()->findOrFail($id);
20     }
21
22     /**
23      * Create a new comment on an entity.
24      */
25     public function create(Entity $entity, string $html, ?int $parent_id, string $content_ref): Comment
26     {
27         $userId = user()->id;
28         $comment = new Comment();
29
30         $comment->html = HtmlDescriptionFilter::filterFromString($html);
31         $comment->created_by = $userId;
32         $comment->updated_by = $userId;
33         $comment->local_id = $this->getNextLocalId($entity);
34         $comment->parent_id = $parent_id;
35         $comment->content_ref = preg_match('/^bkmrk-(.*?):\d+:(\d*-\d*)?$/', $content_ref) === 1 ? $content_ref : '';
36
37         $entity->comments()->save($comment);
38         ActivityService::add(ActivityType::COMMENT_CREATE, $comment);
39         ActivityService::add(ActivityType::COMMENTED_ON, $entity);
40
41         return $comment;
42     }
43
44     /**
45      * Update an existing comment.
46      */
47     public function update(Comment $comment, string $html): Comment
48     {
49         $comment->updated_by = user()->id;
50         $comment->html = HtmlDescriptionFilter::filterFromString($html);
51         $comment->save();
52
53         ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
54
55         return $comment;
56     }
57
58
59     /**
60      * Archive an existing comment.
61      */
62     public function archive(Comment $comment): Comment
63     {
64         if ($comment->parent_id) {
65             throw new NotifyException('Only top-level comments can be archived.');
66         }
67
68         $comment->archived = true;
69         $comment->save();
70
71         ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
72
73         return $comment;
74     }
75
76     /**
77      * Un-archive an existing comment.
78      */
79     public function unarchive(Comment $comment): Comment
80     {
81         if ($comment->parent_id) {
82             throw new NotifyException('Only top-level comments can be un-archived.');
83         }
84
85         $comment->archived = false;
86         $comment->save();
87
88         ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
89
90         return $comment;
91     }
92
93     /**
94      * Delete a comment from the system.
95      */
96     public function delete(Comment $comment): void
97     {
98         $comment->delete();
99
100         ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
101     }
102
103     /**
104      * Get the next local ID relative to the linked entity.
105      */
106     protected function getNextLocalId(Entity $entity): int
107     {
108         $currentMaxId = $entity->comments()->max('local_id');
109
110         return $currentMaxId + 1;
111     }
112 }