mirror of
https://github.com/openvk/openvk
synced 2025-04-23 16:43:02 +03:00
Add apis for likes and polls
This commit is contained in:
parent
1979b8f1fc
commit
f6efbb4efc
4 changed files with 115 additions and 7 deletions
|
@ -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();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php declare(strict_types=1);
|
||||
namespace openvk\VKAPI\Handlers;
|
||||
use openvk\Web\Models\Repositories\Users as UsersRepo;
|
||||
use openvk\Web\Models\Repositories\Posts as PostsRepo;
|
||||
use openvk\Web\Models\Repositories\{Posts as PostsRepo, Comments as CommentsRepo, Photos as PhotosRepo, Videos as VideosRepo};
|
||||
|
||||
final class Likes extends VKAPIRequestHandler
|
||||
{
|
||||
|
@ -68,4 +68,51 @@ final class Likes extends VKAPIRequestHandler
|
|||
$this->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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue