mirror of
https://github.com/openvk/openvk
synced 2024-12-22 16:42:32 +03:00
api(account): new method account.sendVotes
This commit is contained in:
parent
803831917c
commit
96d48efbaf
1 changed files with 38 additions and 0 deletions
|
@ -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()];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue