forked from eatvio/eatvio-infra
04 10 2023 snapshot
This commit is contained in:
109
app/Livewire/Chat/MessageWindow.php
Normal file
109
app/Livewire/Chat/MessageWindow.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Chat;
|
||||
use App\Models\User;
|
||||
use Livewire\Component;
|
||||
use App\Events\ChatMessageUpdated;
|
||||
|
||||
class MessageWindow extends Component
|
||||
{
|
||||
public $messages = [];
|
||||
public $curMessage = "";
|
||||
public $curUserId;
|
||||
public $recipientUserId;
|
||||
public $recipientUser;
|
||||
public $chanId;
|
||||
public $curUser;
|
||||
|
||||
public function getListeners()
|
||||
{
|
||||
return [
|
||||
'echo-private:direct-chat.' . $this->chanId . ',ChatMessageUpdated' => 'notifyNewMessage',
|
||||
'updateRecipientId',
|
||||
'recipientUserIdUpdated' => '$refresh',
|
||||
// 'reloadUserEcho'
|
||||
];
|
||||
}
|
||||
|
||||
public function notifyNewMessage($e): void
|
||||
{
|
||||
$message = $e['message'];
|
||||
|
||||
$this->messages = collect($this->messages)
|
||||
->prepend($message);
|
||||
}
|
||||
|
||||
private function saveThisMessage(array $message): void
|
||||
{
|
||||
$newMessage = new Chat();
|
||||
$newMessage->from_user_id = $message['from_user_id'];
|
||||
$newMessage->to_user_id = $message['to_user_id'];
|
||||
$newMessage->data = $message['data'];
|
||||
$newMessage->save();
|
||||
}
|
||||
public function updateRecipientId($id = null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function whisperTypeing()
|
||||
{
|
||||
$this->dispatch(
|
||||
'typing',
|
||||
[
|
||||
'chanId' => $this->chanId,
|
||||
'activePeer' => $this->curUserId,
|
||||
'activePeerName' => $this->curUser->name
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// FIXME
|
||||
public function mount($recipientUserId)
|
||||
{
|
||||
|
||||
$this->recipientUserId = $recipientUserId;
|
||||
$this->recipientUser = User::find($recipientUserId);
|
||||
$this->curUser = auth()->user();
|
||||
$this->curUserId = $this->curUser->id;
|
||||
// $this->chanId = (int) $recipientUserId < (int) $this->curUserId
|
||||
// ? $recipientUserId . "_" . $this->curUserId
|
||||
// : $this->curUserId . "_" . $recipientUserId;
|
||||
if (is_null($recipientUserId)) {
|
||||
$this->messages = [];
|
||||
} else {
|
||||
$this->messages = (new Chat())->withLastTenMessages($this->curUserId, $recipientUserId)->get();
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.chat.message-window');
|
||||
}
|
||||
|
||||
public function sendChatMessage()
|
||||
{
|
||||
$chatMessage = [
|
||||
'data' => $this->curMessage,
|
||||
'created_at' => now()->setTimezone('UTC')->format('Y-m-d H:i:s'),
|
||||
'from_user_id' => $this->curUserId,
|
||||
'to_user_id' => $this->recipientUserId
|
||||
];
|
||||
|
||||
if (mb_strlen(trim($this->curMessage)) < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->saveThisMessage($chatMessage);
|
||||
|
||||
ChatMessageUpdated::dispatch($chatMessage);
|
||||
|
||||
$this->messages = collect($this->messages)
|
||||
->prepend($chatMessage);
|
||||
|
||||
$this->reset('curMessage');
|
||||
$this->curMessage = '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user