71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Events\Conversation;
|
|
|
|
use App\Http\Controllers\Api\V1\Chat\Resources\MessageResource;
|
|
use App\Models\Chat\Conversation;
|
|
use App\Models\Chat\Message;
|
|
use App\Models\User;
|
|
use Illuminate\Broadcasting\Channel;
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
use Illuminate\Broadcasting\PrivateChannel;
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class MessageSent implements ShouldBroadcast
|
|
{
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
/**
|
|
* Create a new event instance.
|
|
*/
|
|
public function __construct(private Message $message)
|
|
{
|
|
$this->message->loadMissing(['user', 'conversation.channel']);
|
|
}
|
|
|
|
/**
|
|
* Get the channels the event should broadcast on.
|
|
*
|
|
* @return array<int, Channel>
|
|
*/
|
|
public function broadcastOn(): array
|
|
{
|
|
$channels = [
|
|
new PrivateChannel('chat.'.$this->message->conversation_id),
|
|
];
|
|
|
|
$conversation = $this->message->conversation;
|
|
|
|
if ($conversation instanceof Conversation && $conversation->isSupportThread()) {
|
|
$channels[] = new PrivateChannel('chat.support');
|
|
}
|
|
|
|
$channel = $conversation?->channel;
|
|
|
|
if (
|
|
$channel
|
|
&& $channel->channelables_type === User::class
|
|
&& $channel->channelables_id
|
|
) {
|
|
$channels[] = new PrivateChannel('chat.inbox.'.$channel->channelables_id);
|
|
}
|
|
|
|
return $channels;
|
|
}
|
|
|
|
public function broadcastAs(): string
|
|
{
|
|
return 'message.sent';
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function broadcastWith(): array
|
|
{
|
|
return (new MessageResource($this->message->loadMissing('user')))->resolve();
|
|
}
|
|
}
|