|
4 | 4 |
|
5 | 5 | namespace App\Livewire\Comments; |
6 | 6 |
|
| 7 | +use App\Enums\CommentStateEnum; |
7 | 8 | use App\Enums\LivewireEventEnum; |
| 9 | +use App\Enums\ModerationTypeEnum; |
8 | 10 | use App\Models\Comment; |
9 | | -use App\Models\Flag; |
10 | | -use App\Models\Post; |
11 | | -use App\Models\User; |
12 | 11 | use App\Traits\CommentComponentTrait; |
13 | 12 | use Illuminate\Contracts\View\View; |
14 | | -use Illuminate\Support\Facades\DB; |
| 13 | +use Illuminate\Support\Collection; |
| 14 | +use Livewire\Attributes\Computed; |
15 | 15 | use Livewire\Attributes\On; |
16 | 16 | use Livewire\Component; |
17 | 17 |
|
18 | 18 | final class CommentComponent extends Component |
19 | 19 | { |
20 | 20 | use CommentComponentTrait; |
21 | 21 |
|
22 | | - // Data |
23 | | - public ?int $authorizedUserId; |
24 | | - public string $body = ''; |
25 | | - public ?int $parentId = null; |
26 | | - public int $flagCount = 0; |
27 | | - public string $flagIconFilename = 'flag'; |
28 | | - public string $flagButtonText = ''; |
29 | | - public bool $userFlagged = false; |
30 | | - |
31 | 22 | // State |
32 | | - public bool $isEditing = false; |
33 | | - public bool $isFlagging = false; |
34 | | - public bool $isReplying = false; |
35 | | - |
36 | | - public Comment $comment; |
37 | | - public CommentForm $commentForm; |
38 | | - public Post $post; |
39 | | - public ?User $user; |
| 23 | + public CommentStateEnum $state = CommentStateEnum::Viewing; |
40 | 24 |
|
41 | | - public function mount(Comment $comment, Post $post): void |
| 25 | + #[Computed] |
| 26 | + public function isInitiallyBlurred(): bool |
42 | 27 | { |
43 | | - $this->authorizedUserId = auth()->id() ?? null; |
44 | | - |
45 | | - $this->comment = $comment; |
46 | | - $this->commentForm->setComment($comment); |
47 | | - |
48 | | - $this->post = $post; |
49 | | - |
50 | | - $this->body = $comment->body; |
51 | | - |
52 | | - $this->user = auth()->user() ?? null; |
53 | | - |
54 | | - $this->updateFlagCount(); |
55 | | - $this->hasUserFlagged(); |
56 | | - |
57 | | - $this->flagIconFilename = $this->getFlagIconFilename(); |
58 | | - $this->flagButtonText = $this->getFlagTitleText(); |
59 | | - } |
60 | | - |
61 | | - public function render(): View |
62 | | - { |
63 | | - return view('livewire.comments.comment-component'); |
| 28 | + return $this->moderatorCommentsByType?->get(ModerationTypeEnum::Blur->value) !== null; |
64 | 29 | } |
65 | 30 |
|
66 | | - private function getFlagIconFilename(): string |
| 31 | + public function mount(int $commentId, ?Comment $comment, ?Collection $childComments): void |
67 | 32 | { |
68 | | - return $this->userFlagged ? 'flag-fill' : 'flag'; |
| 33 | + // On mount we expect the comment list to provide the comment model |
| 34 | + // and the moderator comments collection. |
| 35 | + $this->commentId = $commentId; |
| 36 | + $this->comment = $comment ?? Comment::find($commentId); |
| 37 | + $this->childComments = $childComments; |
| 38 | + |
| 39 | + // Decide whether to blur the component initially. |
| 40 | + $this->isBlurred = $this->isInitiallyBlurred; |
69 | 41 | } |
70 | 42 |
|
71 | | - private function getFlagTitleText(): string |
| 43 | + public function render(): View |
72 | 44 | { |
73 | | - return $this->userFlagged ? trans('Remove flag') : trans('Flag this comment'); |
| 45 | + // If the comment has been replaced, just render the moderation message. |
| 46 | + // TODO: if it is possible to have a top-level comment be marked with a |
| 47 | + // moderation type, we may want to render it via this view. But it may |
| 48 | + // also just be about changing the border for a regular rendering. |
| 49 | + $moderatorReplaceComment = $this->moderatorCommentsByType?->get(ModerationTypeEnum::Replace->value); |
| 50 | + $moderatorWrapComment = $this->moderatorCommentsByType?->get(ModerationTypeEnum::Wrap->value); |
| 51 | + $moderatorBlurComment = $this->moderatorCommentsByType?->get(ModerationTypeEnum::Blur->value); |
| 52 | + $moderationType = null; |
| 53 | + |
| 54 | + if ($moderatorReplaceComment !== null && |
| 55 | + ($moderatorWrapComment === null || $moderatorReplaceComment->created_at > $moderatorWrapComment->created_at)) { |
| 56 | + $moderationType = ModerationTypeEnum::Replace; |
| 57 | + } elseif ($moderatorWrapComment !== null) { |
| 58 | + $moderationType = ModerationTypeEnum::Wrap; |
| 59 | + } |
| 60 | + |
| 61 | + // If there are no decorations to apply, just render the basic comment component. |
| 62 | + return view('livewire.comments.comment-component', [ |
| 63 | + 'comment' => $this->comment, |
| 64 | + 'childComments' => $this->childComments, |
| 65 | + 'moderationType' => $moderationType, |
| 66 | + 'isInitiallyBlurred' => $this->isInitiallyBlurred, |
| 67 | + 'replacedByCommentId' => $moderatorReplaceComment?->id, |
| 68 | + 'wrappedByCommentId' => $moderatorWrapComment?->id, |
| 69 | + 'blurredByCommentId' => $moderatorBlurComment?->id, |
| 70 | + 'isEditing' => $this->state === CommentStateEnum::Editing, |
| 71 | + 'isFlagging' => $this->state === CommentStateEnum::Flagging, |
| 72 | + 'isReplying' => $this->state === CommentStateEnum::Replying, |
| 73 | + 'isModerating' => $this->state === CommentStateEnum::Moderating, |
| 74 | + ]); |
74 | 75 | } |
75 | 76 |
|
76 | | - private function hasUserFlagged(): void |
| 77 | + #[On([ |
| 78 | + LivewireEventEnum::EscapeKeyClicked->value, |
| 79 | + ])] |
| 80 | + public function closeForm(): void |
77 | 81 | { |
78 | | - $userFlagCount = DB::table(table: 'markable_flags') |
79 | | - ->where(column: 'user_id', operator: '=', value: auth()->id()) |
80 | | - ->where(column: 'markable_id', operator: '=', value: $this->comment->id) |
81 | | - ->where(column: 'markable_type', operator: 'LIKE', value: '%Comment%') |
82 | | - ->count(); |
83 | | - |
84 | | - $this->userFlagged = $userFlagCount > 0; |
| 82 | + $this->state = CommentStateEnum::Viewing; |
85 | 83 | } |
86 | 84 |
|
87 | 85 | #[On([ |
88 | 86 | LivewireEventEnum::CommentStored->value, |
89 | | - LivewireEventEnum::CommentDeleted->value, |
90 | | - LivewireEventEnum::CommentFlagged->value, |
91 | 87 | LivewireEventEnum::CommentUpdated->value, |
92 | | - LivewireEventEnum::EscapeKeyClicked->value, |
93 | 88 | ])] |
94 | | - public function closeForm(): void |
| 89 | + public function reloadChildComments(int $id, ?int $parentId): void |
95 | 90 | { |
96 | | - $this->reset([ |
97 | | - 'body', |
98 | | - ]); |
| 91 | + if ($parentId === $this->commentId) { |
| 92 | + $this->childComments = $this->commentRepository->getCommentsByParentId($parentId); |
| 93 | + unset($this->moderatorCommentsByType, $this->isInitiallyBlurred); |
99 | 94 |
|
100 | | - $this->stopEditing(); |
101 | | - $this->stopFlagging(); |
102 | | - $this->stopReplying(); |
| 95 | + // Re-evaluate whether the comment should be blurred. |
| 96 | + $this->isBlurred = $this->isInitiallyBlurred; |
| 97 | + } |
103 | 98 | } |
104 | 99 |
|
105 | | - private function updateFlagCount(): void |
| 100 | + public function setState(CommentStateEnum $state): void |
106 | 101 | { |
107 | | - $this->flagCount = Flag::count($this->comment); |
| 102 | + $this->state = $state; |
108 | 103 | } |
109 | 104 |
|
110 | | - #[On('comment-flagged.{comment.id}')] |
111 | | - public function addUserFlag(): void |
| 105 | + public function addUserFlag(int $id): void |
112 | 106 | { |
113 | | - \Log::debug('CommentComponent::addUserFlag'); |
114 | | - $this->userFlagged = true; |
115 | | - $this->flagCount++; |
| 107 | + if ($id !== $this->comment->id) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + unset($this->userFlagged, $this->flagCount); |
| 112 | + |
| 113 | + $this->state = CommentStateEnum::Viewing; |
116 | 114 | } |
117 | 115 |
|
118 | | - #[On('comment-flag-deleted.{comment.id}')] |
119 | | - public function removeUserFlag(): void |
| 116 | + public function removeUserFlag(int $id): void |
120 | 117 | { |
121 | | - $this->userFlagged = false; |
122 | | - $this->flagCount--; |
| 118 | + if ($id !== $this->comment->id) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + unset($this->userFlagged, $this->flagCount); |
| 123 | + |
| 124 | + $this->state = CommentStateEnum::Viewing; |
123 | 125 | } |
124 | 126 | } |
0 commit comments