1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Discuss; |
6
|
|
|
|
7
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Livewire\Attributes\On; |
10
|
|
|
use Livewire\Component; |
11
|
|
|
use Masmerise\Toaster\Toastable; |
12
|
|
|
use Xetaravel\Events\Discuss\PostWasCreatedEvent; |
13
|
|
|
use Xetaravel\Livewire\Forms\DiscussPostForm; |
14
|
|
|
use Xetaravel\Models\DiscussConversation; |
15
|
|
|
use Xetaravel\Models\DiscussPost; |
16
|
|
|
|
17
|
|
|
class CreatePost extends Component |
18
|
|
|
{ |
19
|
|
|
use AuthorizesRequests; |
20
|
|
|
use Toastable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The form used to create/update a model. |
24
|
|
|
* |
25
|
|
|
* @var DiscussPostForm |
26
|
|
|
*/ |
27
|
|
|
public DiscussPostForm $form; |
28
|
|
|
|
29
|
|
|
public function mount(DiscussConversation $discussConversation): void |
30
|
|
|
{ |
31
|
|
|
$this->form->conversation_id = $discussConversation->getKey(); |
32
|
|
|
$this->form->is_pinned = $discussConversation->is_pinned; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a blank model and assign it to the model. (Used in create modal) |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function create(): void |
41
|
|
|
{ |
42
|
|
|
$this->authorize('create', DiscussPost::class); |
43
|
|
|
|
44
|
|
|
$this->validate(); |
45
|
|
|
|
46
|
|
|
// Users that have the permission "manage discuss conversation" can bypass this rule. (Default to Developer) |
47
|
|
|
if (DiscussPost::isFlooding('xetaravel.flood.discuss.post') && !Auth::user()->hasPermissionTo('manage discuss conversation')) { |
|
|
|
|
48
|
|
|
$this->error('Wow, keep calm bro, and try to not flood !'); |
49
|
|
|
|
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
$discussPost = $this->form->store(); |
53
|
|
|
|
54
|
|
|
event(new PostWasCreatedEvent(Auth::user(), $discussPost)); |
|
|
|
|
55
|
|
|
|
56
|
|
|
redirect() |
57
|
|
|
->route('discuss.post.show', ['id' => $discussPost->getKey()]) |
|
|
|
|
58
|
|
|
->success('Your reply has been posted successfully !'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
public function render() |
63
|
|
|
{ |
64
|
|
|
return view('livewire.discuss.create-post'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
#[On('post-reply')] |
68
|
|
|
public function updatePostContent($content): void |
69
|
|
|
{ |
70
|
|
|
$this->form->content = $content . $this->form->content; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|