Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?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\Core\Exception; | ||
|
||
use Exception; | ||
use Tobscure\JsonApi\Exception\JsonApiSerializableInterface; | ||
|
||
class FloodingException extends Exception implements JsonApiSerializableInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getStatusCode() | ||
{ | ||
return 429; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getErrors() | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?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\Core\Listener; | ||
|
||
use DateTime; | ||
use Flarum\Core\Exception\FloodingException; | ||
use Flarum\Core\Post; | ||
use Flarum\Core\User; | ||
use Flarum\Event\DiscussionWillBeSaved; | ||
use Flarum\Event\PostWillBeSaved; | ||
use Illuminate\Contracts\Events\Dispatcher; | ||
|
||
class FloodController | ||
{ | ||
/** | ||
* @param Dispatcher $events | ||
*/ | ||
public function subscribe(Dispatcher $events) | ||
{ | ||
$events->listen(DiscussionWillBeSaved::class, [$this, 'whenDiscussionWillBeSaved']); | ||
$events->listen(PostWillBeSaved::class, [$this, 'whenPostWillBeSaved']); | ||
} | ||
|
||
/** | ||
* @param DiscussionWillBeSaved $event | ||
*/ | ||
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event) | ||
{ | ||
if ($event->discussion->exists) { | ||
return; | ||
} | ||
|
||
$this->assertNotFlooding($event->actor); | ||
} | ||
|
||
/** | ||
* @param PostWillBeSaved $event | ||
*/ | ||
public function whenPostWillBeSaved(PostWillBeSaved $event) | ||
{ | ||
if ($event->post->exists) { | ||
return; | ||
} | ||
|
||
$this->assertNotFlooding($event->actor); | ||
} | ||
|
||
/** | ||
* @param User $actor | ||
* @throws FloodingException | ||
*/ | ||
protected function assertNotFlooding(User $actor) | ||
{ | ||
if ($this->isFlooding($actor)) { | ||
throw new FloodingException; | ||
} | ||
} | ||
|
||
/** | ||
* @param User $actor | ||
* @return bool | ||
*/ | ||
protected function isFlooding(User $actor) | ||
{ | ||
return Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
} |
Is ten seconds enough? Should that be configurable?