Refactor chat functionality: remove ChatController, enhance MessageSent event with broadcasting capabilities, and update Conversation and Message models for improved relationships. Update API routes for chat operations and adjust CORS settings for broadcasting authentication.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Events\Conversation;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\MessageResource;
|
||||
use App\Models\Chat\Message;
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
@@ -13,14 +15,12 @@ class MessageSent implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public $message;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct($message)
|
||||
public function __construct(private Message $message)
|
||||
{
|
||||
$this->message = $message->load('user');
|
||||
$this->message->loadMissing('user');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,4 +34,17 @@ class MessageSent implements ShouldBroadcast
|
||||
new PrivateChannel('chat.'.$this->message->conversation_id),
|
||||
];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'message.sent';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return (new MessageResource($this->message->loadMissing('user')))->resolve();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user