From 09c08f3feaa9ecdcb7b6b87eb06b592fb60a2172 Mon Sep 17 00:00:00 2001 From: w33b Date: Wed, 6 May 2026 21:15:15 +0200 Subject: [PATCH] Add comment restore button --- app/Livewire/Comment.php | 19 +++++++++++++++++++ app/Policies/CommentPolicy.php | 15 +++++++++++++++ resources/views/livewire/comment.blade.php | 10 ++++++++++ 3 files changed, 44 insertions(+) diff --git a/app/Livewire/Comment.php b/app/Livewire/Comment.php index 73ca329..093f4fd 100644 --- a/app/Livewire/Comment.php +++ b/app/Livewire/Comment.php @@ -89,6 +89,25 @@ class Comment extends Component $this->dispatch('refresh'); } + public function restoreComment() + { + $this->authorize('restore', $this->comment); + + $user = Auth::user(); + + if ($user->hasRole(UserRole::ADMINISTRATOR) || $user->hasRole(UserRole::MODERATOR)) { + // Log to ModLog + ModLog::create([ + 'moderator' => $user->name, + 'data' => "Restored comment {$this->comment->id} written by {$this->comment->user->id} with contents: {$this->comment->body}", + ]); + + $this->comment->deleted_by_moderator_id = null; + $this->comment->save(); + $this->dispatch('refresh'); + } + } + public function postReply() { if (! ($this->comment->depth() < 2)) { diff --git a/app/Policies/CommentPolicy.php b/app/Policies/CommentPolicy.php index 7a653fc..64eddfa 100644 --- a/app/Policies/CommentPolicy.php +++ b/app/Policies/CommentPolicy.php @@ -25,4 +25,19 @@ class CommentPolicy return $user->id === $comment->user_id; } + + public function restore(User $user, Comment $comment): bool + { + // Comment not deleted + if ($comment->deleted_by_moderator_id === null) { + return false; + } + + if ($user->hasRole(UserRole::ADMINISTRATOR) || + $user->hasRole(UserRole::MODERATOR)) { + return true; + } + + return false; + } } diff --git a/resources/views/livewire/comment.blade.php b/resources/views/livewire/comment.blade.php index d8d739c..3363940 100644 --- a/resources/views/livewire/comment.blade.php +++ b/resources/views/livewire/comment.blade.php @@ -109,6 +109,16 @@ Delete @endcan + + @can ('restore', $comment) + + @endcan @endauth