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:
Mekan1206
2026-08-15 16:07:23 +05:00
parent b52b567388
commit ccfffe6173
23 changed files with 425 additions and 198 deletions

View File

@@ -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();
}
}