File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed
app/Http/Controllers/Forum Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -53,3 +53,5 @@ LOG_STACK=single
53
53
SESSION_ENCRYPT = false
54
54
SESSION_PATH = /
55
55
SESSION_DOMAIN = null
56
+
57
+ APP_MAX_THREAD_COUNT = 5
Original file line number Diff line number Diff line change @@ -91,6 +91,25 @@ public function create(): View
91
91
92
92
public function store (ThreadRequest $ request ): RedirectResponse
93
93
{
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
+
94
113
$ this ->dispatchSync (CreateThread::fromRequest ($ request , $ uuid = Str::uuid ()));
95
114
96
115
$ thread = Thread::findByUuidOrFail ($ uuid );
You can’t perform that action at this time.
0 commit comments