144 lines
3.0 KiB
PHP
144 lines
3.0 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\User;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
|
|
use Maize\Markable\Models\Like;
|
|
|
|
class Comment extends Component
|
|
{
|
|
use AuthorizesRequests;
|
|
|
|
public $comment;
|
|
|
|
public $isReplying = false;
|
|
|
|
public $likeCount = 0;
|
|
|
|
public $liked = false;
|
|
|
|
public $replyState = [
|
|
'body' => ''
|
|
];
|
|
|
|
public $isEditing = false;
|
|
|
|
public $editState = [
|
|
'body' => ''
|
|
];
|
|
|
|
protected $listeners = [
|
|
'refresh' => '$refresh'
|
|
];
|
|
|
|
protected $validationAttributes = [
|
|
'replyState.body' => 'reply'
|
|
];
|
|
|
|
public function updatedIsEditing($isEditing)
|
|
{
|
|
if (! $isEditing) {
|
|
return;
|
|
}
|
|
|
|
$this->editState = [
|
|
'body' => $this->comment->body
|
|
];
|
|
}
|
|
|
|
public function editComment()
|
|
{
|
|
$this->authorize('update', $this->comment);
|
|
|
|
$this->comment->update($this->editState);
|
|
|
|
$this->isEditing = false;
|
|
}
|
|
|
|
public function deleteComment()
|
|
{
|
|
$this->authorize('destroy', $this->comment);
|
|
|
|
$this->comment->delete();
|
|
|
|
$this->dispatch('refresh');
|
|
}
|
|
|
|
public function postReply()
|
|
{
|
|
if (! $this->comment->depth() < 2) {
|
|
return;
|
|
}
|
|
|
|
$user = auth()->user();
|
|
$rateLimitKey = "send-comment:{$user->id}";
|
|
|
|
if (RateLimiter::tooManyAttempts($rateLimitKey, 5)) {
|
|
$seconds = RateLimiter::availableIn($rateLimitKey);
|
|
|
|
$this->addError('replyState.body', "Too many comments. Try again in {$seconds} seconds.");
|
|
return;
|
|
}
|
|
|
|
RateLimiter::hit($rateLimitKey, 60);
|
|
|
|
$this->validate([
|
|
'replyState.body' => 'required'
|
|
]);
|
|
|
|
$reply = $this->comment->children()->make($this->replyState);
|
|
$reply->user()->associate($user);
|
|
$reply->commentable()->associate($this->comment->commentable);
|
|
|
|
$reply->save();
|
|
|
|
$this->replyState = [
|
|
'body' => ''
|
|
];
|
|
|
|
$this->isReplying = false;
|
|
|
|
$this->dispatch('refresh')->self();
|
|
}
|
|
|
|
public function like()
|
|
{
|
|
if (! Auth::check()) {
|
|
return;
|
|
}
|
|
|
|
Like::toggle($this->comment, User::where('id', Auth::user()->id)->firstOrFail());
|
|
|
|
Cache::forget('commentLikes'.$this->comment->id);
|
|
|
|
if ($this->liked) {
|
|
$this->liked = false;
|
|
$this->likeCount--;
|
|
return;
|
|
}
|
|
|
|
$this->liked = true;
|
|
$this->likeCount++;
|
|
}
|
|
|
|
public function mount()
|
|
{
|
|
if (Auth::check()) {
|
|
$this->likeCount = $this->comment->likeCount();
|
|
$this->liked = Like::has($this->comment, User::where('id', Auth::user()->id)->firstOrFail());
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.comment');
|
|
}
|
|
} |