Skip to content

Prevent users from creating more than 5 forum threads per day #1161 #1162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/Http/Controllers/Forum/ThreadsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ public function show(Thread $thread)
return view('forum.threads.show', compact('thread', 'moderators'));
}

public function create(): View
public function create(): RedirectResponse|View
{
if (Auth::user()->hasTooManyThreadsToday()) {
$this->error('You can only post a maximum of 5 threads per day.');

return redirect()->route('forum');
}

$tags = Tag::all();
$selectedTags = old('tags') ?: [];

Expand All @@ -91,6 +97,12 @@ public function create(): View

public function store(ThreadRequest $request): RedirectResponse
{
if (Auth::user()->hasTooManyThreadsToday()) {
$this->error('You can only post a maximum of 5 threads per day.');

return redirect()->route('forum');
}

$this->dispatchSync(CreateThread::fromRequest($request, $uuid = Str::uuid()));

$thread = Thread::findByUuidOrFail($uuid);
Expand Down
15 changes: 15 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Concerns\HasTimestamps;
use App\Concerns\PreparesSearch;
use App\Enums\NotificationType;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -196,6 +197,20 @@ public function countThreads(): int
return $this->threadsRelation()->count();
}

public function countThreadsFromToday(): int
{
$today = Carbon::today();

return $this->threadsRelation()
->whereBetween('created_at', [$today, $today->copy()->endOfDay()])
->count();
}

public function hasTooManyThreadsToday(): bool
{
return $this->countThreadsFromToday() >= 5;
}

/**
* @return \Illuminate\Database\Eloquent\Collection
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/ForumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@
->assertSessionHas('success', 'Thread successfully created!');
});

test('users cannot create more than 5 threads per day', function () {
$tag = Tag::factory()->create(['name' => 'Test Tag']);

$user = $this->login();

Thread::factory()->count(5)->create(['author_id' => $user->id(), 'created_at' => now()]);

$this->post('/forum/create-thread', [
'subject' => 'How to work with Eloquent?',
'body' => 'This text explains how to work with Eloquent.',
'tags' => [$tag->id()],
])
->assertRedirect('/forum')
->assertSessionHas('error', 'You can only post a maximum of 5 threads per day.');
})->only();

test('users can edit a thread', function () {
$user = $this->createUser();
$tag = Tag::factory()->create(['name' => 'Test Tag']);
Expand Down