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 committed 2026-08-15 16:07:23 +05:00
1 parent b52b567388
commit ccfffe6173
23 files changed
+425 -198

No files matched your search

@@ -0,0 +1,20 @@
<?php
namespace App\Http\Controllers\Api\V1\Chat\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ChatUserResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->fullname,
];
}
}
@@ -0,0 +1,31 @@
<?php
namespace App\Http\Controllers\Api\V1\Chat\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ConversationResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$otherUser = $this->relationLoaded('users')
? $this->users->firstWhere('id', '!=', $request->user()?->id)
: null;
return [
'id' => $this->id,
'user' => $this->when($otherUser, fn () => new ChatUserResource($otherUser)),
'last_message' => $this->when(
$this->relationLoaded('latestMessage') && $this->latestMessage,
fn () => new MessageResource($this->latestMessage)
),
'unread_count' => (int) ($this->unread_count ?? 0),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Http\Controllers\Api\V1\Chat\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class MessageResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'conversation_id' => $this->conversation_id,
'body' => $this->body,
'seen_at' => $this->seen_at,
'user' => new ChatUserResource($this->whenLoaded('user')),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}