From 699df766192637208b2abb147fb75e47a6cb29e6 Mon Sep 17 00:00:00 2001 From: veselcraft Date: Tue, 11 Oct 2022 03:46:49 +0300 Subject: [PATCH] VKAPI: Make offset actually work in groups.get method Users: add count and offset (acting like switch for literal offset) param to getClubs --- VKAPI/Handlers/Groups.php | 6 ++---- Web/Models/Entities/User.php | 9 ++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/VKAPI/Handlers/Groups.php b/VKAPI/Handlers/Groups.php index b73404a5..e2d1e779 100644 --- a/VKAPI/Handlers/Groups.php +++ b/VKAPI/Handlers/Groups.php @@ -10,7 +10,7 @@ final class Groups extends VKAPIRequestHandler $this->requireUser(); if($user_id == 0) { - foreach($this->getUser()->getClubs((int) floor($offset/$count)+1) as $club) + foreach($this->getUser()->getClubs($offset, false, $count, true) as $club) $clbs[] = $club; $clbsCount = $this->getUser()->getClubCount(); } else { @@ -20,7 +20,7 @@ final class Groups extends VKAPIRequestHandler if(is_null($user)) $this->fail(15, "Access denied"); - foreach($user->getClubs($offset+1) as $club) + foreach($user->getClubs($offset, false, $count, true) as $club) $clbs[] = $club; $clbsCount = $user->getClubCount(); @@ -33,8 +33,6 @@ final class Groups extends VKAPIRequestHandler $ic = $count; if(!empty($clbs)) { - $clbs = array_slice($clbs, $offset * $count); - for($i=0; $i < $ic; $i++) { $usr = $clbs[$i]; if(is_null($usr)) { diff --git a/Web/Models/Entities/User.php b/Web/Models/Entities/User.php index f1045f62..b7516c90 100644 --- a/Web/Models/Entities/User.php +++ b/Web/Models/Entities/User.php @@ -535,12 +535,15 @@ class User extends RowModel return sizeof(DatabaseConnection::i()->getContext()->table("messages")->where(["recipient_id" => $this->getId(), "unread" => 1])); } - function getClubs(int $page = 1, bool $admin = false): \Traversable + function getClubs(int $page = 1, bool $admin = false, int $count = OPENVK_DEFAULT_PER_PAGE, bool $offset = false): \Traversable { + if(!$offset) + $page = ($page - 1) * $count; + if($admin) { $id = $this->getId(); $query = "SELECT `id` FROM `groups` WHERE `owner` = ? UNION SELECT `club` as `id` FROM `group_coadmins` WHERE `user` = ?"; - $query .= " LIMIT " . OPENVK_DEFAULT_PER_PAGE . " OFFSET " . ($page - 1) * OPENVK_DEFAULT_PER_PAGE; + $query .= " LIMIT " . $count . " OFFSET " . $page; $sel = DatabaseConnection::i()->getConnection()->query($query, $id, $id); foreach($sel as $target) { @@ -550,7 +553,7 @@ class User extends RowModel yield $target; } } else { - $sel = $this->getRecord()->related("subscriptions.follower")->page($page, OPENVK_DEFAULT_PER_PAGE); + $sel = $this->getRecord()->related("subscriptions.follower")->limit($count, $page); foreach($sel->where("model", "openvk\\Web\\Models\\Entities\\Club") as $target) { $target = (new Clubs)->get($target->target); if(!$target) continue;