feat: chat en tiempo real bidireccional funcionando con Reverb
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Message;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
|
||||
class ChatMessageSent implements ShouldBroadcastNow
|
||||
{
|
||||
public $message;
|
||||
|
||||
public function __construct(Message $message)
|
||||
{
|
||||
$this->message = $message->load('user');
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel(
|
||||
'chat.' . $this->message->conversation_id
|
||||
);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'ChatMessageSent';
|
||||
}
|
||||
}
|
||||
+21
-12
@@ -2,30 +2,39 @@
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Message;
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class MessageSent implements ShouldBroadcast
|
||||
class MessageSent implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public $message;
|
||||
public function __construct(public Message $message){}
|
||||
|
||||
public function __construct($message)
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
$this->message = $message;
|
||||
return [
|
||||
new Channel('chat.' . $this->message->receiver_id),
|
||||
];
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return new Channel('chat');
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'MessageSent';
|
||||
return [
|
||||
'id' => $this->message->id,
|
||||
'body' => $this->message->body,
|
||||
'attachment' => $this->message->attachment,
|
||||
'attachment_type' => $this->message->attachment_type,
|
||||
'created_at' => $this->message->created_at->format('H:i'),
|
||||
'sender' => [
|
||||
'id' => $this->message->sender->id,
|
||||
'name' => $this->message->sender->name,
|
||||
'avatar' => $this->message->sender->profile_photo_url ?? null,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
|
||||
class UserStoppedTyping implements ShouldBroadcast
|
||||
{
|
||||
public $conversationId;
|
||||
public $user;
|
||||
|
||||
public function __construct($conversationId, $user)
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('chat.' . $this->conversationId);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'UserStoppedTyping';
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class UserTyping implements ShouldBroadcast
|
||||
{
|
||||
public $conversationId;
|
||||
public $user;
|
||||
|
||||
public function __construct($conversationId, $user)
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('chat.' . $this->conversationId);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'UserTyping';
|
||||
}
|
||||
}
|
||||
@@ -2,74 +2,82 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\ChatMessageSent;
|
||||
use App\Models\Conversation;
|
||||
use App\Events\MessageSent;
|
||||
use App\Models\Message;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ChatController extends Controller
|
||||
{
|
||||
|
||||
public function send(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'conversation_id' => 'required|exists:conversations,id',
|
||||
'message' => 'required|string'
|
||||
]);
|
||||
|
||||
$message = Message::create([
|
||||
'conversation_id' => $request->conversation_id,
|
||||
'user_id' => auth()->id(),
|
||||
'message' => $request->message
|
||||
]);
|
||||
|
||||
// cargar usuario para que venga en el JSON
|
||||
$message->load('user');
|
||||
|
||||
// enviar a los demás usuarios por websocket
|
||||
broadcast(new ChatMessageSent($message))->toOthers();
|
||||
|
||||
return response()->json([
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
|
||||
public function start(User $user)
|
||||
// Lista de usuarios con quienes puedo chatear
|
||||
public function index()
|
||||
{
|
||||
$authUser = auth()->user();
|
||||
$users = User::where('id', '!=', auth()->id())->get();
|
||||
return view('chat.index', compact('users'));
|
||||
}
|
||||
|
||||
// buscar conversación existente
|
||||
$conversation = Conversation::whereHas('users', function ($q) use ($authUser) {
|
||||
$q->where('user_id', $authUser->id);
|
||||
})
|
||||
->whereHas('users', function ($q) use ($user) {
|
||||
$q->where('user_id', $user->id);
|
||||
})
|
||||
->first();
|
||||
// Mensajes entre yo y otro usuario
|
||||
public function show(User $user)
|
||||
{
|
||||
$messages = Message::where(function ($q) use ($user) {
|
||||
$q->where('user_id', auth()->id())
|
||||
->where('receiver_id', $user->id);
|
||||
})
|
||||
->orWhere(function ($q) use ($user) {
|
||||
$q->where('user_id', $user->id)
|
||||
->where('receiver_id', auth()->id());
|
||||
})
|
||||
->with('sender')
|
||||
->orderBy('created_at', 'asc')
|
||||
->get();
|
||||
|
||||
// si no existe → crear
|
||||
if (!$conversation) {
|
||||
$conversation = Conversation::create();
|
||||
// marcar como leídos
|
||||
Message::where('user_id', $user->id)
|
||||
->where('receiver_id', auth()->id())
|
||||
->whereNull('read_at')
|
||||
->update(['read_at' => now()]);
|
||||
|
||||
$conversation->users()->attach([
|
||||
$authUser->id,
|
||||
$user->id
|
||||
]);
|
||||
return view('chat.show', compact('user', 'messages'));
|
||||
}
|
||||
|
||||
// Enviar mensaje
|
||||
public function send(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'receiver_id' => 'required|exists:users,id',
|
||||
'body' => 'nullable|string',
|
||||
'attachment' => 'nullable|file|max:20480',
|
||||
]);
|
||||
|
||||
$attachment = null;
|
||||
$attachmentType = null;
|
||||
|
||||
if ($request->hasFile('attachment')) {
|
||||
$file = $request->file('attachment');
|
||||
$attachment = $file->store('chat', 'public');
|
||||
$mime = $file->getMimeType();
|
||||
|
||||
if (str_starts_with($mime, 'image')) $attachmentType = 'image';
|
||||
elseif (str_starts_with($mime, 'video')) $attachmentType = 'video';
|
||||
elseif (str_starts_with($mime, 'audio')) $attachmentType = 'audio';
|
||||
else $attachmentType = 'file';
|
||||
}
|
||||
|
||||
return response()->json($conversation);
|
||||
}
|
||||
$message = Message::create([
|
||||
'user_id' => auth()->id(),
|
||||
'receiver_id' => $request->receiver_id,
|
||||
'body' => $request->body,
|
||||
'attachment' => $attachment,
|
||||
'attachment_type' => $attachmentType,
|
||||
]);
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$conversation = Conversation::with([
|
||||
'messages' => function ($query) {
|
||||
$query->orderBy('created_at', 'asc');
|
||||
},
|
||||
'messages.user'
|
||||
])->findOrFail($id);
|
||||
$message->load('sender');
|
||||
|
||||
return view('chat.show', compact('conversation'));
|
||||
broadcast(new MessageSent($message))->toOthers();
|
||||
|
||||
$message->created_at = $message->created_at->format('H:i');
|
||||
|
||||
return response()->json($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Friendship extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'sender_id',
|
||||
'receiver_id',
|
||||
'status',
|
||||
];
|
||||
|
||||
public function sender()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'sender_id');
|
||||
}
|
||||
|
||||
public function receiver()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'receiver_id');
|
||||
}
|
||||
}
|
||||
+16
-4
@@ -7,13 +7,25 @@ use Illuminate\Database\Eloquent\Model;
|
||||
class Message extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'conversation_id',
|
||||
'user_id',
|
||||
'message'
|
||||
'receiver_id',
|
||||
'body',
|
||||
'attachment',
|
||||
'attachment_type',
|
||||
'read_at',
|
||||
];
|
||||
|
||||
public function user()
|
||||
protected $casts = [
|
||||
'read_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function sender()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
public function receiver()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'receiver_id');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user