allowing configurable flood gate (#1411)

* allowing configurable flood gate

* fixed review comments
This commit is contained in:
Daniël Klabbers 2018-05-11 11:57:37 +02:00 committed by Toby Zerner
parent 49c643609c
commit 3c41011548
2 changed files with 49 additions and 3 deletions

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Post\Event;
use Flarum\User\User;
class CheckingForFlooding
{
/**
* @var User
*/
public $actor;
/**
* @param User|null $actor
*/
public function __construct(User $actor = null)
{
$this->actor = $actor;
}
}

View File

@ -12,14 +12,26 @@
namespace Flarum\Post;
use DateTime;
use Flarum\Post\Event\CheckingForFlooding;
use Flarum\Post\Exception\FloodingException;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
class Floodgate
{
/**
* @var Dispatcher
*/
protected $events;
public function __construct(Dispatcher $events)
{
$this->events = $events;
}
/**
* @param User $actor
* @throws \Flarum\Post\Exception\FloodingException
* @throws FloodingException
*/
public function assertNotFlooding(User $actor)
{
@ -32,8 +44,12 @@ class Floodgate
* @param User $actor
* @return bool
*/
public function isFlooding(User $actor)
public function isFlooding(User $actor): bool
{
return Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();
$isFlooding = $this->events->until(
new CheckingForFlooding($actor)
);
return $isFlooding ?? Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();
}
}