user(); $key = 'react_post:' . $user->id; if (RateLimiter::tooManyAttempts($key, 60)) { return response()->json(['message' => 'Too many reactions. Please slow down.'], 429); } RateLimiter::hit($key, 3600); $post = Post::findOrFail($id); $reaction = $request->input('reaction', 'like'); $existing = PostReaction::where('post_id', $post->id) ->where('user_id', $user->id) ->where('reaction', $reaction) ->first(); if ($existing) { return response()->json(['message' => 'Already reacted.', 'reactions_count' => $post->reactions_count], 200); } PostReaction::create([ 'post_id' => $post->id, 'user_id' => $user->id, 'reaction' => $reaction, ]); $this->counters->incrementReactions($post); $post->refresh(); return response()->json(['reactions_count' => $post->reactions_count, 'viewer_liked' => true], 201); } /** * DELETE /api/posts/{id}/reactions/{reaction} */ public function destroy(Request $request, int $id, string $reaction = 'like'): JsonResponse { $user = $request->user(); $post = Post::findOrFail($id); $deleted = PostReaction::where('post_id', $post->id) ->where('user_id', $user->id) ->where('reaction', $reaction) ->delete(); if ($deleted) { $this->counters->decrementReactions($post); $post->refresh(); } return response()->json(['reactions_count' => $post->reactions_count, 'viewer_liked' => false]); } }