From 96d48efbafafead869460304113d26f306b124ad Mon Sep 17 00:00:00 2001 From: mrilyew <99399973+mrilyew@users.noreply.github.com> Date: Mon, 18 Nov 2024 19:28:25 +0300 Subject: [PATCH] api(account): new method account.sendVotes --- VKAPI/Handlers/Account.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/VKAPI/Handlers/Account.php b/VKAPI/Handlers/Account.php index 8c0cb343..dcdf18c0 100644 --- a/VKAPI/Handlers/Account.php +++ b/VKAPI/Handlers/Account.php @@ -190,4 +190,42 @@ final class Account extends VKAPIRequestHandler return $settings_list; } + + function sendVotes(int $receiver, int $value, string $message = ""): object + { + $this->requireUser(); + $this->willExecuteWriteAction(); + + if(!OPENVK_ROOT_CONF["openvk"]["preferences"]["commerce"]) + $this->fail(-105, "Commerce is disabled on this instance"); + + if($receiver < 0) + $this->fail(-248, "Invalid receiver id"); + + if($value < 1) + $this->fail(-248, "Invalid value"); + + if(iconv_strlen($message) > 255) + $this->fail(-249, "Message is too long"); + + if($this->getUser()->getCoins() < $value) + $this->fail(-252, "Not enough votes"); + + $receiver_entity = (new \openvk\Web\Models\Repositories\Users)->get($receiver); + if(!$receiver_entity || $receiver_entity->isDeleted()) + $this->fail(-250, "Invalid receiver"); + + if($receiver_entity->getId() === $this->getUser()->getId()) + $this->fail(-251, "Can't transfer votes to yourself"); + + $this->getUser()->setCoins($this->getUser()->getCoins() - $value); + $this->getUser()->save(); + + $receiver_entity->setCoins($receiver_entity->getCoins() + $value); + $receiver_entity->save(); + + (new \openvk\Web\Models\Entities\Notifications\CoinsTransferNotification($receiver_entity, $this->getUser(), $value, $message))->emit(); + + return (object) ['votes' => $this->getUser()->getCoins()]; + } }