feat: funcionalidad de red social (feed, amigos, búsqueda)
- Migraciones: posts, post_likes, post_comments - Modelos Post, PostLike, PostComment con relaciones - User: métodos friendshipWith, isFriendWith, friendIds, posts - PostController: feed, crear publicación con imagen, like toggle, comentarios, eliminar - FriendshipController: listar solicitudes, enviar, aceptar, rechazar, toggle AJAX - SocialProfileController: perfil público con publicaciones y estado de amistad - UserSearchController: búsqueda AJAX por nombre con estado de amistad - FriendRequestNotification: notificación al recibir solicitud - Vistas: feed/index, feed/_post, friendships/index, social/profile - Header: barra de búsqueda con autocomplete AJAX (agregar amigo / mensaje) - Menú lateral: Comunidad, Amigos, Mensajes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Friendship;
|
||||
use App\Models\User;
|
||||
use App\Notifications\FriendRequestNotification;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FriendshipController extends Controller
|
||||
{
|
||||
// Solicitudes pendientes recibidas
|
||||
public function index()
|
||||
{
|
||||
$requests = auth()->user()
|
||||
->receivedFriendRequests()
|
||||
->where('status', 'pending')
|
||||
->with('sender')
|
||||
->latest()
|
||||
->get();
|
||||
|
||||
return view('friendships.index', compact('requests'));
|
||||
}
|
||||
|
||||
// Enviar solicitud
|
||||
public function send(User $user)
|
||||
{
|
||||
$me = auth()->user();
|
||||
|
||||
if ($me->id === $user->id) {
|
||||
return back()->with('error', 'No puedes enviarte una solicitud a ti mismo.');
|
||||
}
|
||||
|
||||
$existing = $me->friendshipWith($user);
|
||||
if ($existing) {
|
||||
return back()->with('error', 'Ya existe una relación con este usuario.');
|
||||
}
|
||||
|
||||
$friendship = Friendship::create([
|
||||
'sender_id' => $me->id,
|
||||
'receiver_id' => $user->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$user->notify(new FriendRequestNotification($me));
|
||||
|
||||
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']);
|
||||
|
||||
return back()->with('success', 'Solicitud aceptada.');
|
||||
}
|
||||
|
||||
// Rechazar o cancelar solicitud
|
||||
public function destroy(Friendship $friendship)
|
||||
{
|
||||
$me = auth()->id();
|
||||
abort_unless(
|
||||
$friendship->sender_id === $me || $friendship->receiver_id === $me,
|
||||
403
|
||||
);
|
||||
|
||||
$friendship->delete();
|
||||
|
||||
return back()->with('success', 'Solicitud eliminada.');
|
||||
}
|
||||
|
||||
// AJAX: enviar/cancelar solicitud desde búsqueda o perfil
|
||||
public function toggle(User $user)
|
||||
{
|
||||
$me = auth()->user();
|
||||
|
||||
if ($me->id === $user->id) {
|
||||
return response()->json(['error' => 'No permitido'], 422);
|
||||
}
|
||||
|
||||
$existing = $me->friendshipWith($user);
|
||||
|
||||
if ($existing) {
|
||||
$existing->delete();
|
||||
return response()->json(['status' => 'none']);
|
||||
}
|
||||
|
||||
Friendship::create([
|
||||
'sender_id' => $me->id,
|
||||
'receiver_id' => $user->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$user->notify(new FriendRequestNotification($me));
|
||||
|
||||
return response()->json(['status' => 'pending']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user