// throttle all members
(new \Flarum\Extend\ThrottleApi())
->set('throttle-members', function (\Psr\Http\Message\ServerRequestInterface$request) {
// Get the route name being requested.
$routeName = $request->getAttribute('routeName');
// Ignore requests to routes we don't want to apply throttling to.
if ($routeName !== 'posts.create') return;
// The current user.
/** @var \Flarum\User\User $actor */
$actor = $request->getAttribute('actor');
// Ignore users with more groups than one.
if ($actor->groups()->count() > 1 ) return;
// Restrict the number of comment posts to one in this time frame.
// Eg once per minute: $after = Carbon::now()->subMinute();
$after = \Carbon\Carbon::now()->subMinutes(2);
// The function needs to return true if we want to throttle and false if not
// We will load the posts by this user and if there's at least one
// we will throttle the user.
return \Flarum\Post\CommentPost::query()
// remove any global scopes that might restrict the query, eg approvals
->withoutGlobalScopes()
// which are by this user
->where('user_id', $actor->id)
// created in the time frame since $after
->where('created_at', '>=', $after)
// if there are more than 0, we'll return true to throttle, or null to ignore this throttler
->count() > 0 ? true : null;
}),
Make a backup of your extend.php before doing this.
This should be added inside extend.php
in your Flarum installation directory, make sure to put it inside the return [
and ];
.
This is untested! If it fails, remove the code 😉