set up common events

This commit is contained in:
mrilyew 2025-06-09 16:15:21 +03:00
parent 969168e53a
commit b180a28b46
7 changed files with 47 additions and 9 deletions

View file

@ -21,7 +21,6 @@ use openvk\Web\Models\Entities\Note;
use openvk\Web\Models\Repositories\Notes as NotesRepo;
use openvk\Web\Models\Repositories\Polls as PollsRepo;
use openvk\Web\Models\Repositories\Audios as AudiosRepo;
use openvk\Web\Util\EventRateLimiter;
final class Wall extends VKAPIRequestHandler
{
@ -727,7 +726,7 @@ final class Wall extends VKAPIRequestHandler
(new WallPostNotification($wallOwner, $post, $this->getUser()->getId()))->emit();
}
EventRateLimiter::i()->writeEvent("wall.post", $this->getUser(), $wallOwner);
\openvk\Web\Util\EventRateLimiter::i()->writeEvent("wall.post", $this->getUser(), $wallOwner);
return (object) ["post_id" => $post->getVirtualId()];
}

View file

@ -37,6 +37,15 @@ trait TSubscribable
if (!($sub->fetch())) {
$ctx->table("subscriptions")->insert($data);
# todo change
if (str_contains(static::class, "Club")) {
\openvk\Web\Util\EventRateLimiter::i()->writeEvent("groups.sub", $user, $this);
} else {
\openvk\Web\Util\EventRateLimiter::i()->writeEvent("friends.outgoing_sub", $user, $this);
}
return true;
}

View file

@ -114,6 +114,7 @@ class User extends RowModel
public function getChandlerUser(): ChandlerUser
{
# TODO cache this function
return new ChandlerUser($this->getRecord()->ref("ChandlerUsers", "user"));
}
@ -1043,6 +1044,8 @@ class User extends RowModel
"anonymous" => $anonymous,
"sent" => time(),
]);
\openvk\Web\Util\EventRateLimiter::i()->writeEvent("gifts.send", $sender, $this);
}
public function ban(string $reason, bool $deleteSubscriptions = true, $unban_time = null, ?int $initiator = null): void

View file

@ -79,6 +79,9 @@ final class GroupPresenter extends OpenVKPresenter
}
$club->toggleSubscription($this->user->identity);
\openvk\Web\Util\EventRateLimiter::i()->writeEvent("groups.create", $this->user->identity, $club);
$this->redirect("/club" . $club->getId());
} else {
$this->flashFail("err", tr("error"), tr("error_no_group_name"));

View file

@ -432,6 +432,8 @@ final class WallPresenter extends OpenVKPresenter
}
}
\openvk\Web\Util\EventRateLimiter::i()->writeEvent("wall.post", $this->user->identity, $wallOwner);
if ($should_be_suggested) {
$this->redirect("/club" . $wallOwner->getId() . "/suggested");
} else {

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace openvk\Web\Util;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\RowModel;
use Chandler\Patterns\TSimpleSingleton;
class UserEvent
@ -28,10 +29,31 @@ class EventRateLimiter
{
use TSimpleSingleton;
public function writeEvent(string $event_name, User $initiator, ?User $reciever = null): bool
private $config;
private $availableFields;
public function __construct()
{
$eventsConfig = OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"]["eventsLimit"];
if (!$eventsConfig['enable']) {
$this->config = OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"]["eventsLimit"];
$this->availableFields = array_keys($this->config['list']);
}
public function tryToLimit(?User $user): bool
{
if (!$this->config['enable']) {
return false;
}
if ($user->isAdmin()) {
return false;
}
return true;
}
public function writeEvent(string $event_name, User $initiator, ?RowModel $reciever = null): bool
{
if (!$this->config['enable']) {
return false;
}
@ -48,7 +70,7 @@ class EventRateLimiter
];
if ($reciever) {
$data['receiverId'] = $reciever->getId();
$data['receiverId'] = $reciever->getRealId();
}
$newEvent = new UserEvent($data);

View file

@ -1,8 +1,8 @@
CREATE TABLE `user-events`
(
`initiatorId` BIGINT(20) NOT NULL,
`initiatorId` BIGINT(20) UNSIGNED NOT NULL,
`initiatorIp` VARBINARY(16) NULL DEFAULT NULL,
`receiverId` BIGINT(20) NOT NULL,
`receiverId` BIGINT(20) NULL DEFAULT NULL,
`eventType` CHAR(25) NOT NULL,
`eventTime` BIGINT(20) NOT NULL
`eventTime` BIGINT(20) UNSIGNED NOT NULL
) ENGINE = InnoDB;