2016-01-02 12:52:16 +08:00
|
|
|
<?php
|
2016-11-29 13:03:53 +08:00
|
|
|
|
2016-01-02 12:52:16 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-06-24 19:43:33 +08:00
|
|
|
namespace Flarum\Post;
|
2016-01-02 12:52:16 +08:00
|
|
|
|
|
|
|
use DateTime;
|
2018-05-11 17:57:37 +08:00
|
|
|
use Flarum\Post\Event\CheckingForFlooding;
|
2017-06-24 19:43:33 +08:00
|
|
|
use Flarum\Post\Exception\FloodingException;
|
2017-06-24 18:55:22 +08:00
|
|
|
use Flarum\User\User;
|
2018-05-11 17:57:37 +08:00
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
2016-01-02 12:52:16 +08:00
|
|
|
|
|
|
|
class Floodgate
|
|
|
|
{
|
2018-05-11 17:57:37 +08:00
|
|
|
/**
|
|
|
|
* @var Dispatcher
|
|
|
|
*/
|
|
|
|
protected $events;
|
|
|
|
|
|
|
|
public function __construct(Dispatcher $events)
|
|
|
|
{
|
|
|
|
$this->events = $events;
|
|
|
|
}
|
|
|
|
|
2016-01-02 12:52:16 +08:00
|
|
|
/**
|
|
|
|
* @param User $actor
|
2018-05-11 17:57:37 +08:00
|
|
|
* @throws FloodingException
|
2016-01-02 12:52:16 +08:00
|
|
|
*/
|
|
|
|
public function assertNotFlooding(User $actor)
|
|
|
|
{
|
|
|
|
if ($this->isFlooding($actor)) {
|
|
|
|
throw new FloodingException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $actor
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-05-11 17:57:37 +08:00
|
|
|
public function isFlooding(User $actor): bool
|
2016-01-02 12:52:16 +08:00
|
|
|
{
|
2018-05-11 17:57:37 +08:00
|
|
|
$isFlooding = $this->events->until(
|
|
|
|
new CheckingForFlooding($actor)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $isFlooding ?? Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();
|
2016-01-02 12:52:16 +08:00
|
|
|
}
|
|
|
|
}
|