-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChatPanel.php
More file actions
52 lines (39 loc) · 1.28 KB
/
ChatPanel.php
File metadata and controls
52 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace App\Livewire;
use App\Models\Enums\ChatMessageType;
use App\Models\Legacy\ChatMessage;
use Illuminate\Support\Collection;
use Livewire\Component;
class ChatPanel extends Component
{
public $content = '';
public $targetType;
public $targetId;
public function mount($targetType, $targetId): void
{
$this->targetType = $targetType;
$this->targetId = $targetId;
}
public function render()
{
/** @var Collection<ChatMessage> $messages */
$messages = ChatMessage::where('target', $this->targetType)
->where('target_id', $this->targetId)->get();
return view('livewire.chat-panel', ['messages' => $messages]);
}
public function save()
{
$this->validate(['content' => 'required|min:1']);
$cleanContent = strip_tags((string) $this->content, '<p><br><strong><em><ul><ol><li><a><h1><h2><h3>');
ChatMessage::create([
'text' => $cleanContent,
'type' => ChatMessageType::PUBLIC,
'target' => $this->targetType,
'target_id' => $this->targetId,
'creator' => Auth()->user()->username,
'creator_alias' => Auth()->user()->name,
'timestamp' => now(),
]);
$this->content = '';
}
}