VKAPI: Make offset actually work in groups.get method

Users: add count and offset (acting like switch for literal offset) param to getClubs
This commit is contained in:
veselcraft 2022-10-11 03:46:49 +03:00
parent 7b1182f4e8
commit 699df76619
No known key found for this signature in database
GPG key ID: AED66BC1AC628A4E
2 changed files with 8 additions and 7 deletions

View file

@ -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)) {

View file

@ -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;