Skip to content

Commit d7081be

Browse files
committed
Prevent users from creating more than 5 forum threads per day #1161
1 parent c0cd5bd commit d7081be

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

app/Http/Controllers/Forum/ThreadsController.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ public function create(): View
9191

9292
public function store(ThreadRequest $request): RedirectResponse
9393
{
94+
$userId = Auth::id(); // Get the authenticated user's ID
95+
$midnight = now()->endOfDay();
96+
$remainingSeconds = $midnight->diffInSeconds(now());
97+
98+
// Count the threads posted by the user today
99+
$cacheKey = "user_threads_count_{$userId}";
100+
$threadCount = Cache::remember($cacheKey, $remainingSeconds, function () use ($userId) {
101+
return Thread::where('author_id', $userId)
102+
->where('created_at', '>=', now()->startOfDay())
103+
->count();
104+
});
105+
106+
// Check if the user has reached the limit
107+
if ($threadCount >= 1) {
108+
$this->error('You can only post a maximum of 5 threads per day.');
109+
return redirect()->route('forum');
110+
}
111+
112+
94113
$this->dispatchSync(CreateThread::fromRequest($request, $uuid = Str::uuid()));
95114

96115
$thread = Thread::findByUuidOrFail($uuid);

0 commit comments

Comments
 (0)