1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Forms; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Livewire\Attributes\Locked; |
9
|
|
|
use Livewire\Attributes\Validate; |
10
|
|
|
use Livewire\Form; |
11
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
12
|
|
|
use Xetaravel\Events\Discuss\ConversationWasLockedEvent; |
13
|
|
|
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent; |
14
|
|
|
use Xetaravel\Models\DiscussConversation; |
15
|
|
|
use Xetaravel\Models\DiscussPost; |
16
|
|
|
use Xetaravel\Models\DiscussUser; |
17
|
|
|
|
18
|
|
|
class DiscussPostForm extends Form |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The conversation id where the post belong to. |
22
|
|
|
* |
23
|
|
|
* @var int|null |
24
|
|
|
*/ |
25
|
|
|
#[Locked] |
26
|
|
|
public ?int $conversation_id = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The content of the post. |
30
|
|
|
* |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
#[Validate('required|min:10')] |
34
|
|
|
public ?string $content = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Whatever the conversation is pinned |
38
|
|
|
* |
39
|
|
|
* @var bool|null |
40
|
|
|
*/ |
41
|
|
|
#[Validate('boolean')] |
42
|
|
|
public ?bool $is_pinned = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Whatever the conversation is locked |
46
|
|
|
* |
47
|
|
|
* @var bool|null |
48
|
|
|
*/ |
49
|
|
|
#[Validate('boolean')] |
50
|
|
|
public ?bool $is_locked = false; |
51
|
|
|
|
52
|
|
|
private function createPost(array $properties): DiscussPost |
53
|
|
|
{ |
54
|
|
|
$post = DiscussPost::create($this->only(['conversation_id', 'content'])); |
55
|
|
|
|
56
|
|
|
$conversation = DiscussConversation::find($properties['conversation_id']); |
57
|
|
|
|
58
|
|
|
if (Auth::user()->hasPermissionTo('pin discuss conversation')) { |
|
|
|
|
59
|
|
|
if ($conversation->is_pinned !== $properties['is_pinned'] && $properties['is_pinned'] === true) { |
|
|
|
|
60
|
|
|
event(new ConversationWasPinnedEvent($conversation)); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
$conversation->is_pinned = $properties['is_pinned']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (Auth::user()->hasPermissionTo('lock discuss conversation')) { |
66
|
|
|
if ($conversation->is_locked !== $properties['is_locked'] && $properties['is_locked'] === true) { |
|
|
|
|
67
|
|
|
event(new ConversationWasLockedEvent($conversation)); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
$conversation->is_locked = $properties['is_locked']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$conversation->last_post_id = $post->getKey(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$conversation->save(); |
75
|
|
|
|
76
|
|
|
return $post; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function createUser(array $properties): void |
80
|
|
|
{ |
81
|
|
|
DiscussUser::updateOrCreate( |
82
|
|
|
[ |
83
|
|
|
'user_id' => Auth::id(), |
84
|
|
|
'conversation_id' => $properties['conversation_id'] |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
'conversation_id' => $properties['conversation_id'] |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Function to store the model. |
95
|
|
|
* |
96
|
|
|
* @return DiscussPost |
97
|
|
|
*/ |
98
|
|
|
public function store(): DiscussPost |
99
|
|
|
{ |
100
|
|
|
$properties = [ |
101
|
|
|
'conversation_id', |
102
|
|
|
'content' |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
if (Auth::user()->hasPermissionTo('pin discuss conversation')) { |
106
|
|
|
$properties[] = 'is_pinned'; |
107
|
|
|
} |
108
|
|
|
if (Auth::user()->hasPermissionTo('lock discuss conversation')) { |
109
|
|
|
$properties[] = 'is_locked'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$post = $this->createPost($this->only($properties)); |
113
|
|
|
$this->createUser($this->only($properties)); |
114
|
|
|
|
115
|
|
|
$parser = new MentionParser($post, [ |
116
|
|
|
'regex' => config('mentions.regex') |
117
|
|
|
]); |
118
|
|
|
$content = $parser->parse($post->content); |
119
|
|
|
|
120
|
|
|
$post->content = $content; |
121
|
|
|
$post->save(); |
122
|
|
|
|
123
|
|
|
return $post; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|