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:
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use App\Models\Post;
|
||||
use App\Models\PostComment;
|
||||
use App\Models\PostLike;
|
||||
use App\Support\Notifier;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
@@ -12,9 +13,9 @@ class PostController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$me = auth()->user();
|
||||
$me = auth()->user();
|
||||
$friendIds = $me->friendIds();
|
||||
$ids = array_merge($friendIds, [$me->id]);
|
||||
$ids = array_merge($friendIds, [$me->id]);
|
||||
|
||||
$posts = Post::whereIn('user_id', $ids)
|
||||
->with(['author', 'likes', 'comments.author'])
|
||||
@@ -51,15 +52,28 @@ class PostController extends Controller
|
||||
|
||||
public function like(Post $post)
|
||||
{
|
||||
$userId = auth()->id();
|
||||
$liked = PostLike::where('user_id', $userId)->where('post_id', $post->id)->first();
|
||||
$me = auth()->user();
|
||||
$liked = PostLike::where('user_id', $me->id)->where('post_id', $post->id)->first();
|
||||
|
||||
if ($liked) {
|
||||
$liked->delete();
|
||||
$liked = false;
|
||||
} else {
|
||||
PostLike::create(['user_id' => $userId, 'post_id' => $post->id]);
|
||||
PostLike::create(['user_id' => $me->id, 'post_id' => $post->id]);
|
||||
$liked = true;
|
||||
|
||||
// Notify post author (not if liking own post)
|
||||
if ($post->user_id !== $me->id) {
|
||||
$post->load('author');
|
||||
Notifier::send(
|
||||
recipient: $post->author,
|
||||
title: $me->name . ' le dio Me gusta a tu publicación',
|
||||
body: $post->body ? \Str::limit($post->body, 60) : '',
|
||||
icon: 'fa-heart',
|
||||
color: 'danger',
|
||||
url: route('feed') . '#post-' . $post->id,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
@@ -72,14 +86,28 @@ class PostController extends Controller
|
||||
{
|
||||
$request->validate(['body' => 'required|string|max:500']);
|
||||
|
||||
$me = auth()->user();
|
||||
$comment = PostComment::create([
|
||||
'user_id' => auth()->id(),
|
||||
'user_id' => $me->id,
|
||||
'post_id' => $post->id,
|
||||
'body' => $request->body,
|
||||
]);
|
||||
|
||||
$comment->load('author');
|
||||
|
||||
// Notify post author (not if commenting on own post)
|
||||
if ($post->user_id !== $me->id) {
|
||||
$post->load('author');
|
||||
Notifier::send(
|
||||
recipient: $post->author,
|
||||
title: $me->name . ' comentó en tu publicación',
|
||||
body: \Str::limit($request->body, 60),
|
||||
icon: 'fa-comment',
|
||||
color: 'info',
|
||||
url: route('feed') . '#post-' . $post->id,
|
||||
);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'id' => $comment->id,
|
||||
'body' => $comment->body,
|
||||
|
||||
Reference in New Issue
Block a user