mirror of
https://github.com/openvk/openvk
synced 2025-01-09 01:09:46 +03:00
Rate limit certain notifications
Likes and reposts will not emit notifications more than once per 2 minutes
This commit is contained in:
parent
342429b505
commit
5b86b1fab7
3 changed files with 18 additions and 3 deletions
|
@ -5,6 +5,7 @@ use openvk\Web\Models\Entities\{User, Post};
|
||||||
final class LikeNotification extends Notification
|
final class LikeNotification extends Notification
|
||||||
{
|
{
|
||||||
protected $actionCode = 0;
|
protected $actionCode = 0;
|
||||||
|
protected $threshold = 120;
|
||||||
|
|
||||||
function __construct(User $recipient, Post $post, User $liker)
|
function __construct(User $recipient, Post $post, User $liker)
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,6 +13,7 @@ class Notification
|
||||||
private $data;
|
private $data;
|
||||||
|
|
||||||
protected $actionCode = NULL;
|
protected $actionCode = NULL;
|
||||||
|
protected $threshold = -1;
|
||||||
|
|
||||||
function __construct(User $recipient, $originModel, $targetModel, ?int $time = NULL, string $data = "")
|
function __construct(User $recipient, $originModel, $targetModel, ?int $time = NULL, string $data = "")
|
||||||
{
|
{
|
||||||
|
@ -78,8 +79,7 @@ class Notification
|
||||||
if(!($e = eventdb()))
|
if(!($e = eventdb()))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
$edb = $e->getConnection();
|
$data = [
|
||||||
$edb->query("INSERT INTO notifications VALUES (0, ?, ?, ?, ?, ?, ?, ?, ?)", ...[
|
|
||||||
$this->recipient->getId(),
|
$this->recipient->getId(),
|
||||||
$this->encodeType($this->originModel),
|
$this->encodeType($this->originModel),
|
||||||
$this->originModel->getId(),
|
$this->originModel->getId(),
|
||||||
|
@ -88,7 +88,20 @@ class Notification
|
||||||
$this->actionCode,
|
$this->actionCode,
|
||||||
$this->data,
|
$this->data,
|
||||||
$this->time,
|
$this->time,
|
||||||
]);
|
];
|
||||||
|
|
||||||
|
$edb = $e->getConnection();
|
||||||
|
if($this->threshold !== -1) {
|
||||||
|
# Event is thersholded, check if there is similar event
|
||||||
|
$query = <<<'QUERY'
|
||||||
|
SELECT * FROM `notifications` WHERE `recipientType`=0 AND `recipientId`=? AND `originModelType`=? AND `originModelId`=? AND `targetModelType`=? AND `targetModelId`=? AND `modelAction`=? AND `additionalData`=? AND `timestamp` > (? - ?)
|
||||||
|
QUERY;
|
||||||
|
$result = $edb->query($query, ...array_merge($data, [ $this->threshold ]));
|
||||||
|
if($result->getRowCount() > 0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$edb->query("INSERT INTO notifications VALUES (0, ?, ?, ?, ?, ?, ?, ?, ?)", ...$data);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ use openvk\Web\Models\Entities\{User, Post};
|
||||||
final class RepostNotification extends Notification
|
final class RepostNotification extends Notification
|
||||||
{
|
{
|
||||||
protected $actionCode = 1;
|
protected $actionCode = 1;
|
||||||
|
protected $threshold = 120;
|
||||||
|
|
||||||
function __construct(User $recipient, Post $post, User $reposter)
|
function __construct(User $recipient, Post $post, User $reposter)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue