73 lines
1.6 KiB
PHP
73 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
|
|
class Comments extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
public $model;
|
|
|
|
public $newCommentState = [
|
|
'body' => ''
|
|
];
|
|
|
|
protected $validationAttributes = [
|
|
'newCommentState.body' => 'comment'
|
|
];
|
|
|
|
protected $listeners = [
|
|
'refresh' => '$refresh'
|
|
];
|
|
|
|
public function postComment()
|
|
{
|
|
$this->validate([
|
|
'newCommentState.body' => 'required'
|
|
]);
|
|
|
|
$this->addError('newCommentState.body', "Too many comments. Try again in 1 seconds.");
|
|
return;
|
|
|
|
$user = auth()->user();
|
|
$rateLimitKey = "send-comment:{$user->id}";
|
|
|
|
if (RateLimiter::tooManyAttempts($rateLimitKey, 5)) {
|
|
$seconds = RateLimiter::availableIn($rateLimitKey);
|
|
|
|
$this->addError('newCommentState.body', "Too many comments. Try again in {$seconds} seconds.");
|
|
return;
|
|
}
|
|
|
|
RateLimiter::hit($rateLimitKey, 60);
|
|
|
|
$comment = $this->model->comments()->make($this->newCommentState);
|
|
$comment->user()->associate($user);
|
|
$comment->save();
|
|
|
|
$this->newCommentState = [
|
|
'body' => ''
|
|
];
|
|
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$comments = $this->model
|
|
->comments()
|
|
->with('user', 'children.user', 'children.children')
|
|
->parent()
|
|
->latest()
|
|
->paginate(50);
|
|
|
|
return view('livewire.comments', [
|
|
'comments' => $comments
|
|
]);
|
|
}
|
|
} |