From f6efbb4efc6db87a0fc18093af93fe6d9a4ef8d1 Mon Sep 17 00:00:00 2001 From: lalka2016 <99399973+lalka2016@users.noreply.github.com> Date: Fri, 25 Aug 2023 20:28:47 +0300 Subject: [PATCH] Add apis for likes and polls --- VKAPI/Handlers/Groups.php | 6 ++-- VKAPI/Handlers/Likes.php | 49 +++++++++++++++++++++++++- VKAPI/Handlers/Polls.php | 60 ++++++++++++++++++++++++++++++++ Web/Models/Entities/Postable.php | 7 ++-- 4 files changed, 115 insertions(+), 7 deletions(-) diff --git a/VKAPI/Handlers/Groups.php b/VKAPI/Handlers/Groups.php index 3123a43f..9cc6a08c 100644 --- a/VKAPI/Handlers/Groups.php +++ b/VKAPI/Handlers/Groups.php @@ -6,12 +6,12 @@ use openvk\Web\Models\Entities\Club; final class Groups extends VKAPIRequestHandler { - function get(int $user_id = 0, string $fields = "", int $offset = 0, int $count = 6, bool $online = false): object + function get(int $user_id = 0, string $fields = "", int $offset = 0, int $count = 6, bool $online = false, string $filter = "groups"): object { $this->requireUser(); if($user_id == 0) { - foreach($this->getUser()->getClubs($offset, false, $count, true) as $club) + foreach($this->getUser()->getClubs($offset, $filter == "admin", $count, true) as $club) $clbs[] = $club; $clbsCount = $this->getUser()->getClubCount(); } else { @@ -21,7 +21,7 @@ final class Groups extends VKAPIRequestHandler if(is_null($user)) $this->fail(15, "Access denied"); - foreach($user->getClubs($offset, false, $count, true) as $club) + foreach($user->getClubs($offset, $filter == "admin", $count, true) as $club) $clbs[] = $club; $clbsCount = $user->getClubCount(); diff --git a/VKAPI/Handlers/Likes.php b/VKAPI/Handlers/Likes.php index 9501b433..08c4e6e4 100644 --- a/VKAPI/Handlers/Likes.php +++ b/VKAPI/Handlers/Likes.php @@ -1,7 +1,7 @@ fail(100, "One of the parameters specified was missing or invalid: incorrect type"); } } + + function getList(string $type, int $owner_id, int $item_id, bool $extended = false, int $offset = 0, int $count = 10, bool $skip_own = false) + { + $this->requireUser(); + + $object = NULL; + + switch($type) { + case "post": + $object = (new PostsRepo)->getPostById($owner_id, $item_id); + break; + case "comment": + $object = (new CommentsRepo)->get($item_id); + break; + case "photo": + $object = (new PhotosRepo)->getByOwnerAndVID($owner_id, $item_id); + break; + case "video": + $object = (new VideosRepo)->getByOwnerAndVID($owner_id, $item_id); + break; + default: + $this->fail(58, "Invalid type"); + break; + } + + if(!$object || $object->isDeleted()) + $this->fail(56, "Invalid postable"); + + $res = (object)[ + "count" => $object->getLikesCount(), + "items" => [] + ]; + + $likers = array_slice(iterator_to_array($object->getLikers(1, $offset + $count)), $offset); + + foreach($likers as $liker) { + if($skip_own && $liker->getId() == $this->getUser()->getId()) + continue; + + if(!$extended) + $res->items[] = $liker->getId(); + else + $res->items[] = $liker->toVkApiStruct(); + } + + return $res; + } } diff --git a/VKAPI/Handlers/Polls.php b/VKAPI/Handlers/Polls.php index be947a44..299d35e8 100755 --- a/VKAPI/Handlers/Polls.php +++ b/VKAPI/Handlers/Polls.php @@ -104,4 +104,64 @@ final class Polls extends VKAPIRequestHandler $this->fail(8, "how.to. ook.bacon.in.microwova."); } } + + function getVoters(int $poll_id, int $answer_ids, int $offset = 0, int $count = 6) + { + $this->requireUser(); + + $poll = (new PollsRepo)->get($poll_id); + + if(!$poll) + $this->fail(251, "Invalid poll"); + + $voters = array_slice($poll->getVoters($answer_ids, 1, $offset + $count), $offset); + $res = (object)[ + "answer_id" => $answer_ids, + "users" => [] + ]; + + foreach($voters as $voter) + $res->users[] = $voter->toVkApiStruct(); + + return $res; + } + + function create(string $question, string $add_answers, bool $disable_unvote = false, bool $is_anonymous = false, bool $is_multiple = false, int $end_date = 0) + { + $this->requireUser(); + $this->willExecuteWriteAction(); + + $options = json_decode($add_answers); + + if(!$options || empty($options)) + $this->fail(62, "Invalid options"); + + if(sizeof($options) > ovkGetQuirk("polls.max-opts")) + $this->fail(51, "Too many options"); + + $poll = new Poll; + $poll->setOwner($this->getUser()); + $poll->setTitle($question); + $poll->setMultipleChoice($is_multiple); + $poll->setAnonymity($is_anonymous); + $poll->setRevotability(!$disable_unvote); + $poll->setOptions($options); + + if($end_date > time()) { + if($end_date > time() + (DAY * 365)) + $this->fail(89, "End date is too big"); + + $poll->setEndDate($end_date); + } + + $poll->save(); + + return $this->getById($poll->getId()); + } + + function edit() + { + #todo + return 1; + } } diff --git a/Web/Models/Entities/Postable.php b/Web/Models/Entities/Postable.php index 23981cc1..c86aec96 100644 --- a/Web/Models/Entities/Postable.php +++ b/Web/Models/Entities/Postable.php @@ -88,13 +88,14 @@ abstract class Postable extends Attachable ])->group("origin")); } - # TODO add pagination - function getLikers(): \Traversable + function getLikers(int $page = 1, ?int $perPage = NULL): \Traversable { + $perPage ??= OPENVK_DEFAULT_PER_PAGE; + $sel = DB::i()->getContext()->table("likes")->where([ "model" => static::class, "target" => $this->getRecord()->id, - ]); + ])->page($page, $perPage); foreach($sel as $like) yield (new Users)->get($like->origin);