VKAPI: Wall.get impovements

Added a "extended" value, added a "offset" to the Repositories/Posts->getPostsFromUsersWall function and little bit of refactoring
This commit is contained in:
veselcraft 2021-11-24 22:24:44 +03:00
parent 012bd072b4
commit a3d65a402e
No known key found for this signature in database
GPG key ID: AED66BC1AC628A4E
3 changed files with 74 additions and 25 deletions

View file

@ -16,20 +16,25 @@ final class Wall extends VKAPIRequestHandler
$posts = new PostsRepo; $posts = new PostsRepo;
$items = []; $items = [];
$profiles = [];
$groups = [];
$count = $posts->getPostCountOnUserWall((int) $owner_id);
foreach ($posts->getPostsFromUsersWall((int)$owner_id) as $post) { foreach ($posts->getPostsFromUsersWall((int)$owner_id, 1, $count, $offset) as $post) {
$from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
$items[] = (object)[ $items[] = (object)[
"id" => $post->getVirtualId(), "id" => $post->getVirtualId(),
"from_id" => $post->getOwner()->getId(), "from_id" => $from_id,
"owner_id" => $post->getTargetWall(), "owner_id" => $post->getTargetWall(),
"date" => $post->getPublicationTime()->timestamp(), "date" => $post->getPublicationTime()->timestamp(),
"post_type" => "post", "post_type" => "post",
"text" => $post->getText(), "text" => $post->getText(),
"can_edit" => 0, // TODO "can_edit" => 0, // TODO
"can_delete" => $post->canBeDeletedBy($this->getUser()), "can_delete" => $post->canBeDeletedBy($this->getUser()),
"can_pin" => 0, // TODO "can_pin" => $post->canBePinnedBy($this->getUser()),
"can_archive" => false, // TODO MAYBE "can_archive" => false, // TODO MAYBE
"is_archived" => false, "is_archived" => false,
"is_pinned" => $post->isPinned(),
"post_source" => (object)["type" => "vk"], "post_source" => (object)["type" => "vk"],
"comments" => (object)[ "comments" => (object)[
"count" => $post->getCommentsCount(), "count" => $post->getCommentsCount(),
@ -46,23 +51,63 @@ final class Wall extends VKAPIRequestHandler
"user_reposted" => 0 "user_reposted" => 0
] ]
]; ];
if ($from_id > 0)
$profiles[] = $from_id;
else
$groups[] = $from_id * -1;
} }
$profiles = []; if($extended == 1)
$groups = []; {
$profiles = array_unique($profiles);
$groups = array_unique($groups);
$groups[0] = 'lol'; $profilesFormatted = [];
$groups[2] = 'cec'; $groupsFormatted = [];
if($extended == 1) foreach ($profiles as $prof) {
return (object)[ $user = (new UsersRepo)->get($prof);
"items" => (array)$items, $profilesFormatted[] = (object)[
"cock" => (array)$groups "first_name" => $user->getFirstName(),
]; "id" => $user->getId(),
"last_name" => $user->getLastName(),
"can_access_closed" => false,
"is_closed" => false,
"sex" => $user->isFemale() ? 1 : 2,
"screen_name" => $user->getShortCode(),
"photo_50" => $user->getAvatarUrl(),
"photo_100" => $user->getAvatarUrl(),
"online" => $user->isOnline()
];
}
foreach($groups as $g) {
$group = (new ClubsRepo)->get($g);
$groupsFormatted[] = (object)[
"id" => $group->getId(),
"name" => $group->getName(),
"screen_name" => $group->getShortCode(),
"is_closed" => 0,
"type" => "group",
"photo_50" => $group->getAvatarUrl(),
"photo_100" => $group->getAvatarUrl(),
"photo_200" => $group->getAvatarUrl(),
];
}
return (object)[
"count" => $count,
"items" => (array)$items,
"profiles" => (array)$profilesFormatted,
"groups" => (array)$groupsFormatted
];
}
else else
return (object)[ return (object)[
"items" => (array)$items "count" => $count,
]; "items" => (array)$items
];
} }
function post(string $owner_id, string $message, int $from_group = 0, int $signed = 0): object function post(string $owner_id, string $message, int $from_group = 0, int $signed = 0): object

View file

@ -38,7 +38,7 @@ class Post extends Postable
*/ */
function getOwner(bool $honourFlags = true, bool $real = false): RowModel function getOwner(bool $honourFlags = true, bool $real = false): RowModel
{ {
if($honourFlags && ( ($this->getRecord()->flags & 0b10000000) > 0 )) { if($honourFlags && $this->isPostedOnBehalfOfGroup()) {
if($this->getRecord()->wall < 0) if($this->getRecord()->wall < 0)
return (new Clubs)->get(abs($this->getRecord()->wall)); return (new Clubs)->get(abs($this->getRecord()->wall));
} }

View file

@ -37,20 +37,24 @@ class Posts
return $this->toPost($post); return $this->toPost($post);
} }
function getPostsFromUsersWall(int $user, int $page = 1, ?int $perPage = NULL): \Traversable function getPostsFromUsersWall(int $user, int $page = 1, ?int $perPage = NULL, ?int $offset = NULL): \Traversable
{ {
$perPage ??= OPENVK_DEFAULT_PER_PAGE; $perPage ??= OPENVK_DEFAULT_PER_PAGE;
$offset = $perPage * ($page - 1); $offset ??= $perPage * ($page - 1);
$pinPost = $this->getPinnedPost($user); $pinPost = $this->getPinnedPost($user);
if(!is_null($pinPost)) { if(is_null($offset) || $offset == 0) {
if($page === 1) { if(!is_null($pinPost)) {
$perPage--; if($page === 1) {
$perPage--;
yield $pinPost;
} else { yield $pinPost;
$offset--; } else {
$offset--;
}
} }
} else if(!is_null($offset)) {
$offset--;
} }
$sel = $this->posts->where([ $sel = $this->posts->where([