Skip to content

Commit e3db4bd

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

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ LOG_STACK=single
5353
SESSION_ENCRYPT=false
5454
SESSION_PATH=/
5555
SESSION_DOMAIN=null
56+
57+
APP_MAX_THREAD_COUNT = 5

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 >= getenv('APP_MAX_THREAD_COUNT')) {
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)