feat: notificaciones en tiempo real para social (likes, comentarios, solicitudes)
- Notifier helper: guarda en BD + broadcast en un solo llamado - AppNotification: notificación genérica para canal database - FriendshipController: notifica al recibir solicitud y al aceptarla - PostController: notifica al autor cuando le dan like o comentan (excepto en propio post) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,12 +4,11 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Friendship;
|
||||
use App\Models\User;
|
||||
use App\Notifications\FriendRequestNotification;
|
||||
use App\Support\Notifier;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FriendshipController extends Controller
|
||||
{
|
||||
// Solicitudes pendientes recibidas
|
||||
public function index()
|
||||
{
|
||||
$requests = auth()->user()
|
||||
@@ -22,7 +21,6 @@ class FriendshipController extends Controller
|
||||
return view('friendships.index', compact('requests'));
|
||||
}
|
||||
|
||||
// Enviar solicitud
|
||||
public function send(User $user)
|
||||
{
|
||||
$me = auth()->user();
|
||||
@@ -31,33 +29,45 @@ class FriendshipController extends Controller
|
||||
return back()->with('error', 'No puedes enviarte una solicitud a ti mismo.');
|
||||
}
|
||||
|
||||
$existing = $me->friendshipWith($user);
|
||||
if ($existing) {
|
||||
if ($me->friendshipWith($user)) {
|
||||
return back()->with('error', 'Ya existe una relación con este usuario.');
|
||||
}
|
||||
|
||||
$friendship = Friendship::create([
|
||||
Friendship::create([
|
||||
'sender_id' => $me->id,
|
||||
'receiver_id' => $user->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$user->notify(new FriendRequestNotification($me));
|
||||
Notifier::send(
|
||||
recipient: $user,
|
||||
title: $me->name . ' te envió una solicitud de amistad',
|
||||
icon: 'fa-user-plus',
|
||||
color: 'primary',
|
||||
url: route('friendships.index'),
|
||||
);
|
||||
|
||||
return back()->with('success', 'Solicitud de amistad enviada.');
|
||||
}
|
||||
|
||||
// Aceptar solicitud
|
||||
public function accept(Friendship $friendship)
|
||||
{
|
||||
abort_unless($friendship->receiver_id === auth()->id(), 403);
|
||||
|
||||
$friendship->update(['status' => 'accepted']);
|
||||
$friendship->load('receiver');
|
||||
|
||||
Notifier::send(
|
||||
recipient: $friendship->sender,
|
||||
title: $friendship->receiver->name . ' aceptó tu solicitud de amistad',
|
||||
icon: 'fa-user-check',
|
||||
color: 'success',
|
||||
url: route('social.profile', $friendship->receiver),
|
||||
);
|
||||
|
||||
return back()->with('success', 'Solicitud aceptada.');
|
||||
}
|
||||
|
||||
// Rechazar o cancelar solicitud
|
||||
public function destroy(Friendship $friendship)
|
||||
{
|
||||
$me = auth()->id();
|
||||
@@ -71,7 +81,7 @@ class FriendshipController extends Controller
|
||||
return back()->with('success', 'Solicitud eliminada.');
|
||||
}
|
||||
|
||||
// AJAX: enviar/cancelar solicitud desde búsqueda o perfil
|
||||
// AJAX desde búsqueda/perfil
|
||||
public function toggle(User $user)
|
||||
{
|
||||
$me = auth()->user();
|
||||
@@ -93,7 +103,13 @@ class FriendshipController extends Controller
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$user->notify(new FriendRequestNotification($me));
|
||||
Notifier::send(
|
||||
recipient: $user,
|
||||
title: $me->name . ' te envió una solicitud de amistad',
|
||||
icon: 'fa-user-plus',
|
||||
color: 'primary',
|
||||
url: route('friendships.index'),
|
||||
);
|
||||
|
||||
return response()->json(['status' => 'pending']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user