Files
Sistema-Educativo-Laravel/app/Http/Controllers/ChatController.php
T
fernando f81237b23c fix/feat: búsqueda móvil, chat solo amigos y bloqueo si se eliminó amistad
- Header: búsqueda móvil con panel deslizable (fixed bajo la topbar), comparte la lógica AJAX con desktop via initSearchWidget()
- ChatController: index muestra solo amigos; show pasa isFriend y friendship a la vista; send valida amistad con abort_unless
- chat/index: muestra amigos con @forelse y mensaje vacío si no tiene
- chat/show: si no son amigos muestra banner de bloqueo con botón contextual (enviar solicitud / cancelar / aceptar según estado)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 17:40:16 -06:00

94 lines
3.0 KiB
PHP

<?php
namespace App\Http\Controllers;
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
{
// Lista de amigos con quienes puedo chatear
public function index()
{
$me = auth()->user();
$friends = User::whereIn('id', $me->friendIds())->orderBy('name')->get();
return view('chat.index', compact('friends'));
}
// Mensajes entre yo y otro usuario
public function show(User $user)
{
$me = auth()->user();
$isFriend = $me->isFriendWith($user);
$friendship = $me->friendshipWith($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();
// marcar como leídos solo si son amigos
if ($isFriend) {
Message::where('user_id', $user->id)
->where('receiver_id', $me->id)
->whereNull('read_at')
->update(['read_at' => now()]);
}
return view('chat.show', compact('user', 'messages', 'isFriend', 'friendship'));
}
// Enviar mensaje
public function send(Request $request)
{
$request->validate([
'receiver_id' => 'required|exists:users,id',
'body' => 'nullable|string',
'attachment' => 'nullable|file|max:20480',
]);
$receiver = User::findOrFail($request->receiver_id);
abort_unless(auth()->user()->isFriendWith($receiver), 403, 'No puedes enviar mensajes a esta persona.');
$attachment = null;
$attachmentType = null;
if ($request->hasFile('attachment')) {
$file = $request->file('attachment');
$attachment = $file->store('chat', 'local');
$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';
}
$message = Message::create([
'user_id' => auth()->id(),
'receiver_id' => $request->receiver_id,
'body' => $request->body,
'attachment' => $attachment,
'attachment_type' => $attachmentType,
]);
$message->load('sender');
broadcast(new MessageSent($message))->toOthers();
$message->created_at = $message->created_at->format('H:i');
return response()->json($message);
}
}