mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig-portal.git
synced 2025-12-11 06:46:47 +00:00
plebchat added
This commit is contained in:
32
app/Events/ChatMessageSentEvent.php
Normal file
32
app/Events/ChatMessageSentEvent.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ChatMessageSentEvent implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
* @return \Illuminate\Broadcasting\Channel|array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new Channel('plebchannel');
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,6 @@ class PlebLoggedInEvent implements ShouldBroadcast
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new Channel('login');
|
||||
return new Channel('plebchannel');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,11 @@ class HighscoreTable extends Component
|
||||
]);
|
||||
}
|
||||
|
||||
public function toggleChat()
|
||||
{
|
||||
$this->emit('toggleHighscoreChat');
|
||||
}
|
||||
|
||||
public function openModal($id)
|
||||
{
|
||||
$this->modal = User::query()
|
||||
|
||||
68
app/Http/Livewire/Chat/HighscoreChat.php
Normal file
68
app/Http/Livewire/Chat/HighscoreChat.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Chat;
|
||||
|
||||
use App\Events\ChatMessageSentEvent;
|
||||
use Livewire\Component;
|
||||
|
||||
class HighscoreChat extends Component
|
||||
{
|
||||
public bool $open = false;
|
||||
|
||||
public array $messages = [];
|
||||
public string $myNewMessage = '';
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'myNewMessage' => 'required|min:1|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
public function getListeners()
|
||||
{
|
||||
return [
|
||||
'toggleHighscoreChat' => 'toggle',
|
||||
'echo:plebchannel,.App\Events\ChatMessageSentEvent' => 'chatMessageSent',
|
||||
];
|
||||
}
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->messages = cache()->get('highscore_chat_messages', []);
|
||||
}
|
||||
|
||||
public function toggle()
|
||||
{
|
||||
$this->open = !$this->open;
|
||||
}
|
||||
|
||||
public function chatMessageSent()
|
||||
{
|
||||
$this->messages = cache()->get('highscore_chat_messages', []);
|
||||
$this->dispatchBrowserEvent('chat-updated');
|
||||
}
|
||||
|
||||
public function sendMessage()
|
||||
{
|
||||
$this->validate();
|
||||
$newMessages = collect($this->messages)
|
||||
->push([
|
||||
'fromId' => auth()->id(),
|
||||
'fromName' => str(auth()->user()->name)->initials(),
|
||||
'userImg' => str(auth()->user()->profile_photo_url)->replace('background=EBF4FF', 'background=F7931A'),
|
||||
'message' => $this->myNewMessage,
|
||||
'time' => now()->asDateTime(),
|
||||
])
|
||||
->toArray();
|
||||
cache()->set('highscore_chat_messages', $newMessages);
|
||||
event(new ChatMessageSentEvent());
|
||||
$this->messages = $newMessages;
|
||||
$this->myNewMessage = '';
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.chat.highscore-chat');
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ class LaravelEcho extends Component
|
||||
{
|
||||
use Actions;
|
||||
|
||||
protected $listeners = ['echo:login,.App\Events\PlebLoggedInEvent' => 'plebLoggedIn'];
|
||||
protected $listeners = ['echo:plebchannel,.App\Events\PlebLoggedInEvent' => 'plebLoggedIn'];
|
||||
|
||||
public function plebLoggedIn($data)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user