From b3e57147b7fd978b22b329cf9adb928bf0c85995 Mon Sep 17 00:00:00 2001 From: mrilyew <99399973+mrilyew@users.noreply.github.com> Date: Tue, 15 Oct 2024 20:42:59 +0300 Subject: [PATCH] =?UTF-8?q?api:=20add=20some=20fields=20and=202=20new=20me?= =?UTF-8?q?thods=20=D0=B8=20jsonp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Изменения коснулись методов account.getProfileInfo, account.getBalance, account.getOvkSettings, gifts.get, gifts.getCategories, groups.get, groups.getById, users.get, wall.get, wall.getById, wall.getComments, wall.getComment. - Добавлена поддержка JSONP --- VKAPI/Handlers/Account.php | 62 ++++++++++++---- VKAPI/Handlers/Gifts.php | 24 ++++--- VKAPI/Handlers/Groups.php | 8 +++ VKAPI/Handlers/Users.php | 36 +++++++--- VKAPI/Handlers/Wall.php | 115 +++++++++++------------------- Web/Models/Entities/Comment.php | 16 +++++ Web/Models/Entities/Post.php | 25 +++++++ Web/Models/Entities/User.php | 12 +++- Web/Presenters/VKAPIPresenter.php | 12 +++- 9 files changed, 198 insertions(+), 112 deletions(-) diff --git a/VKAPI/Handlers/Account.php b/VKAPI/Handlers/Account.php index 16308b97..8c0cb343 100644 --- a/VKAPI/Handlers/Account.php +++ b/VKAPI/Handlers/Account.php @@ -7,20 +7,32 @@ final class Account extends VKAPIRequestHandler function getProfileInfo(): object { $this->requireUser(); - - return (object) [ - "first_name" => $this->getUser()->getFirstName(), - "id" => $this->getUser()->getId(), - "last_name" => $this->getUser()->getLastName(), - "home_town" => $this->getUser()->getHometown(), - "status" => $this->getUser()->getStatus(), - "audio_status" => is_null($this->getUser()->getCurrentAudioStatus()) ? NULL : $this->getUser()->getCurrentAudioStatus()->toVkApiStruct($this->getUser()), - "bdate" => is_null($this->getUser()->getBirthday()) ? '01.01.1970' : $this->getUser()->getBirthday()->format('%e.%m.%Y'), - "bdate_visibility" => $this->getUser()->getBirthdayPrivacy(), + $user = $this->getUser(); + $return_object = (object) [ + "first_name" => $user->getFirstName(), + "photo_200" => $user->getAvatarURL("normal"), + "nickname" => $user->getPseudo(), + "is_service_account" => false, + "id" => $user->getId(), + "is_verified" => $user->isVerified(), + "verification_status" => $user->isVerified() ? 'verified' : 'unverified', + "last_name" => $user->getLastName(), + "home_town" => $user->getHometown(), + "status" => $user->getStatus(), + "bdate" => is_null($user->getBirthday()) ? '01.01.1970' : $user->getBirthday()->format('%e.%m.%Y'), + "bdate_visibility" => $user->getBirthdayPrivacy(), "phone" => "+420 ** *** 228", # TODO - "relation" => $this->getUser()->getMaritalStatus(), - "sex" => $this->getUser()->isFemale() ? 1 : 2 + "relation" => $user->getMaritalStatus(), + "screen_name" => $user->getShortCode(), + "sex" => $user->isFemale() ? 1 : 2, + #"email" => $user->getEmail(), ]; + + $audio_status = $user->getCurrentAudioStatus(); + if(!is_null($audio_status)) + $return_object->audio_status = $audio_status->toVkApiStruct($user); + + return $return_object; } function getInfo(): object @@ -152,4 +164,30 @@ final class Account extends VKAPIRequestHandler return (object) $output; } + + function getBalance(): object + { + $this->requireUser(); + if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) + $this->fail(105, "Commerce is disabled on this instance"); + + return (object) ['votes' => $this->getUser()->getCoins()]; + } + + function getOvkSettings(): object + { + $this->requireUser(); + $user = $this->getUser(); + + $settings_list = (object)[ + 'avatar_style' => $user->getStyleAvatar(), + 'style' => $user->getStyle(), + 'show_rating' => !$user->prefersNotToSeeRating(), + 'nsfw_tolerance' => $user->getNsfwTolerance(), + 'post_view' => $user->hasMicroblogEnabled() ? 'microblog' : 'old', + 'main_page' => $user->getMainPage() == 0 ? 'my_page' : 'news', + ]; + + return $settings_list; + } } diff --git a/VKAPI/Handlers/Gifts.php b/VKAPI/Handlers/Gifts.php index dd6fa07a..0d1fc598 100644 --- a/VKAPI/Handlers/Gifts.php +++ b/VKAPI/Handlers/Gifts.php @@ -6,15 +6,18 @@ use openvk\Web\Models\Entities\Notifications\GiftNotification; final class Gifts extends VKAPIRequestHandler { - function get(int $user_id, int $count = 10, int $offset = 0) + function get(int $user_id = NULL, int $count = 10, int $offset = 0) { $this->requireUser(); $i = 0; - $i += $offset; + $server_url = ovk_scheme(true) . $_SERVER["HTTP_HOST"]; - $user = (new UsersRepo)->get($user_id); + if($user_id) + $user = (new UsersRepo)->get($user_id); + else + $user = $this->getUser(); if(!$user || $user->isDeleted()) $this->fail(177, "Invalid user"); @@ -47,9 +50,9 @@ final class Gifts extends VKAPIRequestHandler "date" => $gift->sent->timestamp(), "gift" => [ "id" => $gift->gift->getId(), - "thumb_256" => $gift->gift->getImage(2), - "thumb_96" => $gift->gift->getImage(2), - "thumb_48" => $gift->gift->getImage(2) + "thumb_256" => $server_url. $gift->gift->getImage(2), + "thumb_96" => $server_url . $gift->gift->getImage(2), + "thumb_48" => $server_url . $gift->gift->getImage(2) ], "privacy" => 0 ]; @@ -125,12 +128,13 @@ final class Gifts extends VKAPIRequestHandler $this->fail(501, "Not implemented"); } - # этих методов не было в ВК, но я их добавил чтобы можно было отобразить список подарков + # в vk кстати называется gifts.getCatalog function getCategories(bool $extended = false, int $page = 1) { $cats = (new GiftsRepo)->getCategories($page); $categ = []; $i = 0; + $server_url = ovk_scheme(true) . $_SERVER["HTTP_HOST"]; if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) $this->fail(105, "Commerce is disabled on this instance"); @@ -140,8 +144,8 @@ final class Gifts extends VKAPIRequestHandler "name" => $cat->getName(), "description" => $cat->getDescription(), "id" => $cat->getId(), - "thumbnail" => $cat->getThumbnailURL(), - ]; + "thumbnail" => $server_url . $cat->getThumbnailURL(), + ]; if($extended == true) { $categ[$i]["localizations"] = []; @@ -178,7 +182,7 @@ final class Gifts extends VKAPIRequestHandler "name" => $gift->getName(), "image" => $gift->getImage(2), "usages_left" => (int)$gift->getUsagesLeft($this->getUser()), - "price" => $gift->getPrice(), # голосов + "price" => $gift->getPrice(), "is_free" => $gift->isFree() ]; } diff --git a/VKAPI/Handlers/Groups.php b/VKAPI/Handlers/Groups.php index f83079ec..ca67e7b7 100644 --- a/VKAPI/Handlers/Groups.php +++ b/VKAPI/Handlers/Groups.php @@ -88,6 +88,10 @@ final class Groups extends VKAPIRequestHandler case "can_suggest": $rClubs[$i]->can_suggest = !$usr->canBeModifiedBy($this->getUser()) && $usr->getWallType() == 2; break; + case "background": + $backgrounds = $usr->getBackDropPictureURLs(); + $rClubs[$i]->background = $backgrounds; + break; # unstandard feild case "suggested_count": if($usr->getWallType() != 2) { @@ -208,6 +212,10 @@ final class Groups extends VKAPIRequestHandler case "can_suggest": $response[$i]->can_suggest = !$clb->canBeModifiedBy($this->getUser()) && $clb->getWallType() == 2; break; + case "background": + $backgrounds = $clb->getBackDropPictureURLs(); + $response[$i]->background = $backgrounds; + break; # unstandard feild case "suggested_count": if($clb->getWallType() != 2) { diff --git a/VKAPI/Handlers/Users.php b/VKAPI/Handlers/Users.php index f6e5823f..132b6204 100644 --- a/VKAPI/Handlers/Users.php +++ b/VKAPI/Handlers/Users.php @@ -151,7 +151,6 @@ final class Users extends VKAPIRequestHandler } case "music": if(!$canView) { - $response[$i]->music = "secret"; break; } @@ -159,7 +158,6 @@ final class Users extends VKAPIRequestHandler break; case "movies": if(!$canView) { - $response[$i]->movies = "secret"; break; } @@ -167,7 +165,6 @@ final class Users extends VKAPIRequestHandler break; case "tv": if(!$canView) { - $response[$i]->tv = "secret"; break; } @@ -175,7 +172,6 @@ final class Users extends VKAPIRequestHandler break; case "books": if(!$canView) { - $response[$i]->books = "secret"; break; } @@ -183,7 +179,6 @@ final class Users extends VKAPIRequestHandler break; case "city": if(!$canView) { - $response[$i]->city = "Воскресенск"; break; } @@ -191,7 +186,6 @@ final class Users extends VKAPIRequestHandler break; case "interests": if(!$canView) { - $response[$i]->interests = "secret"; break; } @@ -199,7 +193,6 @@ final class Users extends VKAPIRequestHandler break; case "quotes": if(!$canView) { - $response[$i]->quotes = "secret"; break; } @@ -207,7 +200,6 @@ final class Users extends VKAPIRequestHandler break; case "email": if(!$canView) { - $response[$i]->email = "secret@gmail.com"; break; } @@ -215,7 +207,6 @@ final class Users extends VKAPIRequestHandler break; case "telegram": if(!$canView) { - $response[$i]->telegram = "@secret"; break; } @@ -223,7 +214,6 @@ final class Users extends VKAPIRequestHandler break; case "about": if(!$canView) { - $response[$i]->about = "secret"; break; } @@ -231,7 +221,6 @@ final class Users extends VKAPIRequestHandler break; case "rating": if(!$canView) { - $response[$i]->rating = 22; break; } @@ -246,9 +235,34 @@ final class Users extends VKAPIRequestHandler "notes_count" => (new Notes)->getUserNotesCount($usr) ]; break; + case "correct_counters": + $response[$i]->counters = (object) [ + "friends" => $usr->getFriendsCount(), + "photos" => (new Photos)->getUserPhotosCount($usr), + "videos" => (new Videos)->getUserVideosCount($usr), + "audios" => (new Audios)->getUserCollectionSize($usr), + "notes" => (new Notes)->getUserNotesCount($usr), + "groups" => $usr->getClubCount(), + "online_friends" => $usr->getFriendsOnlineCount(), + ]; + break; case "guid": $response[$i]->guid = $usr->getChandlerGUID(); break; + case 'background': + $backgrounds = $usr->getBackDropPictureURLs(); + $response[$i]->background = $backgrounds; + break; + case 'reg_date': + if(!$canView) { + break; + } + + $response[$i]->reg_date = $usr->getRegistrationTime()->timestamp(); + break; + case 'is_dead': + $response[$i]->is_dead = $usr->isDead(); + break; } } diff --git a/VKAPI/Handlers/Wall.php b/VKAPI/Handlers/Wall.php index 3cbc7648..a305fde6 100644 --- a/VKAPI/Handlers/Wall.php +++ b/VKAPI/Handlers/Wall.php @@ -126,65 +126,41 @@ final class Wall extends VKAPIRequestHandler else $profiles[] = $attachment->getOwner()->getId(); - $post_source = []; - - if($attachment->getPlatform(true) === NULL) { - $post_source = (object)["type" => "vk"]; - } else { - $post_source = (object)[ - "type" => "api", - "platform" => $attachment->getPlatform(true) - ]; - } - $repost[] = [ "id" => $attachment->getVirtualId(), "owner_id" => $attachment->isPostedOnBehalfOfGroup() ? $attachment->getOwner()->getId() * -1 : $attachment->getOwner()->getId(), "from_id" => $attachment->isPostedOnBehalfOfGroup() ? $attachment->getOwner()->getId() * -1 : $attachment->getOwner()->getId(), "date" => $attachment->getPublicationTime()->timestamp(), - "post_type" => "post", + "post_type" => $attachment->getVkApiType(), "text" => $attachment->getText(false), "attachments" => $repostAttachments, - "post_source" => $post_source, + "post_source" => $attachment->getPostSourceInfo(), ]; - if ($attachment->getVirtualId() > 0) - $profiles[] = $attachment->getVirtualId(); + if ($attachment->getTargetWall() > 0) + $profiles[] = $attachment->getTargetWall(); else - $groups[] = $attachment->getVirtualId(); + $groups[] = abs($attachment->getTargetWall()); if($post->isSigned()) $profiles[] = $attachment->getOwner()->getId(); } } - $post_source = []; - - if($post->getPlatform(true) === NULL) { - $post_source = (object)["type" => "vk"]; - } else { - $post_source = (object)[ - "type" => "api", - "platform" => $post->getPlatform(true) - ]; - } - - $postType = "post"; $signerId = NULL; - if($post->getSuggestionType() != 0) - $postType = "suggest"; - - if($post->isSigned()) { $actualAuthor = $post->getOwner(false); $signerId = $actualAuthor->getId(); } - $items[] = (object)[ + # TODO "can_pin", "copy_history" и прочее не должны возвращаться, если равны null или false + # Ну и ещё всё надо перенести в toVkApiStruct, а то слишком много дублированного кода + + $post_temp_obj = (object)[ "id" => $post->getVirtualId(), "from_id" => $from_id, "owner_id" => $post->getTargetWall(), "date" => $post->getPublicationTime()->timestamp(), - "post_type" => $postType, + "post_type" => $post->getVkApiType(), "text" => $post->getText(false), "copy_history" => $repost, "can_edit" => $post->canBeEditedBy($this->getUser()), @@ -195,8 +171,7 @@ final class Wall extends VKAPIRequestHandler "is_pinned" => $post->isPinned(), "is_explicit" => $post->isExplicit(), "attachments" => $attachments, - "post_source" => $post_source, - "signer_id" => $signerId, + "post_source" => $post->getPostSourceInfo(), "comments" => (object)[ "count" => $post->getCommentsCount(), "can_post" => 1 @@ -213,6 +188,14 @@ final class Wall extends VKAPIRequestHandler ] ]; + if($signerId) + $post_temp_obj->signer_id = $signerId; + + if($post->isDeactivationMessage()) + $post_temp_obj->final_post = 1; + + $items[] = $post_temp_obj; + if ($from_id > 0) $profiles[] = $from_id; else @@ -332,17 +315,6 @@ final class Wall extends VKAPIRequestHandler else $profiles[] = $attachment->getOwner()->getId(); - $post_source = []; - - if($attachment->getPlatform(true) === NULL) { - $post_source = (object)["type" => "vk"]; - } else { - $post_source = (object)[ - "type" => "api", - "platform" => $attachment->getPlatform(true) - ]; - } - $repost[] = [ "id" => $attachment->getVirtualId(), "owner_id" => $attachment->isPostedOnBehalfOfGroup() ? $attachment->getOwner()->getId() * -1 : $attachment->getOwner()->getId(), @@ -351,47 +323,29 @@ final class Wall extends VKAPIRequestHandler "post_type" => "post", "text" => $attachment->getText(false), "attachments" => $repostAttachments, - "post_source" => $post_source, + "post_source" => $attachment->getPostSourceInfo(), ]; - if ($attachment->getVirtualId() > 0) - $profiles[] = $attachment->getVirtualId(); + if ($attachment->getTargetWall() > 0) + $profiles[] = $attachment->getTargetWall(); else - $groups[] = $attachment->getVirtualId(); + $groups[] = abs($attachment->getTargetWall()); if($post->isSigned()) $profiles[] = $attachment->getOwner()->getId(); } } - $post_source = []; - - if($post->getPlatform(true) === NULL) { - $post_source = (object)["type" => "vk"]; - } else { - $post_source = (object)[ - "type" => "api", - "platform" => $post->getPlatform(true) - ]; - } - - # TODO: $post->getVkApiType() - $postType = "post"; - $signerId = NULL; - if($post->getSuggestionType() != 0) - $postType = "suggest"; - - if($post->isSigned()) { $actualAuthor = $post->getOwner(false); $signerId = $actualAuthor->getId(); } - $items[] = (object)[ + $post_temp_obj = (object)[ "id" => $post->getVirtualId(), "from_id" => $from_id, "owner_id" => $post->getTargetWall(), "date" => $post->getPublicationTime()->timestamp(), - "post_type" => $postType, + "post_type" => $post->getVkApiType(), "text" => $post->getText(false), "copy_history" => $repost, "can_edit" => $post->canBeEditedBy($this->getUser()), @@ -401,8 +355,7 @@ final class Wall extends VKAPIRequestHandler "is_archived" => false, "is_pinned" => $post->isPinned(), "is_explicit" => $post->isExplicit(), - "post_source" => $post_source, - "signer_id" => $signerId, + "post_source" => $post->getPostSourceInfo(), "attachments" => $attachments, "comments" => (object)[ "count" => $post->getCommentsCount(), @@ -420,6 +373,14 @@ final class Wall extends VKAPIRequestHandler ] ]; + if($signerId) + $post_temp_obj->signer_id = $signerId; + + if($post->isDeactivationMessage()) + $post_temp_obj->final_post = 1; + + $items[] = $post_temp_obj; + if ($from_id > 0) $profiles[] = $from_id; else @@ -792,6 +753,9 @@ final class Wall extends VKAPIRequestHandler ] ]; + if($comment->isFromPostAuthor($post)) + $item['is_from_post_author'] = true; + if($need_likes == true) $item['likes'] = [ "can_like" => 1, @@ -875,6 +839,9 @@ final class Wall extends VKAPIRequestHandler ] ]; + if($comment->isFromPostAuthor()) + $item['is_from_post_author'] = true; + if($extended == true) $profiles[] = $comment->getOwner()->getId(); @@ -890,8 +857,6 @@ final class Wall extends VKAPIRequestHandler $response['profiles'] = (!empty($profiles) ? (new Users)->get(implode(',', $profiles), $fields) : []); } - - return $response; } diff --git a/Web/Models/Entities/Comment.php b/Web/Models/Entities/Comment.php index 24fb2c1b..200d7a16 100644 --- a/Web/Models/Entities/Comment.php +++ b/Web/Models/Entities/Comment.php @@ -103,6 +103,22 @@ class Comment extends Post return $this->getTarget()->canBeViewedBy($user); } + + function isFromPostAuthor($target = NULL) + { + if(!$target) + $target = $this->getTarget(); + + $target_owner = $target->getOwner(); + $comment_owner = $this->getOwner(); + + if($target_owner->getRealId() === $comment_owner->getRealId()) + return true; + + # TODO: make it work with signer_id + + return false; + } function toNotifApiStruct() { diff --git a/Web/Models/Entities/Post.php b/Web/Models/Entities/Post.php index 4e0386a7..5a75d99b 100644 --- a/Web/Models/Entities/Post.php +++ b/Web/Models/Entities/Post.php @@ -179,6 +179,31 @@ class Post extends Postable "img" => NULL ]; } + + function getPostSourceInfo(): array + { + $post_source = ["type" => "vk"]; + if($this->getPlatform(true) !== NULL) { + $post_source = [ + "type" => "api", + "platform" => $this->getPlatform(true) + ]; + } + + if($this->isUpdateAvatarMessage()) + $post_source['data'] = 'profile_photo'; + + return $post_source; + } + + function getVkApiType(): string + { + $type = 'post'; + if($this->getSuggestionType() != 0) + $type = 'suggest'; + + return $type; + } function pin(): void { diff --git a/Web/Models/Entities/User.php b/Web/Models/Entities/User.php index 933ab486..e8756550 100644 --- a/Web/Models/Entities/User.php +++ b/Web/Models/Entities/User.php @@ -1224,6 +1224,11 @@ class User extends RowModel return (bool) $this->getRecord()->activated; } + function isDead(): bool + { + return $this->onlineStatus() == 2; + } + function getUnbanTime(): ?string { $ban = (new Bans)->get((int) $this->getRecord()->block_reason); @@ -1338,13 +1343,14 @@ class User extends RowModel $res->photo_100 = $this->getAvatarURL("tiny"); $res->photo_200 = $this->getAvatarURL("normal"); $res->photo_id = !is_null($this->getAvatarPhoto()) ? $this->getAvatarPhoto()->getPrettyId() : NULL; - # TODO: Perenesti syuda vsyo ostalnoyie $res->is_closed = $this->isClosed(); - if(!is_null($user)) { + if(!is_null($user)) $res->can_access_closed = (bool)$this->canBeViewedBy($user); - } + + if($user->isDead()) + $res->is_dead = 1; return $res; } diff --git a/Web/Presenters/VKAPIPresenter.php b/Web/Presenters/VKAPIPresenter.php index d82194cf..9eb4565a 100644 --- a/Web/Presenters/VKAPIPresenter.php +++ b/Web/Presenters/VKAPIPresenter.php @@ -186,8 +186,12 @@ final class VKAPIPresenter extends OpenVKPresenter function renderRoute(string $object, string $method): void { + $callback = $this->queryParam("callback"); $authMechanism = $this->queryParam("auth_mechanism") ?? "token"; if($authMechanism === "roaming") { + if($callback) + $this->fail(-1, "User authorization failed: roaming mechanism is unavailable with jsonp.", $object, $method); + if(!$this->user->identity) $this->fail(5, "User authorization failed: roaming mechanism is selected, but user is not logged in.", $object, $method); else @@ -259,10 +263,16 @@ final class VKAPIPresenter extends OpenVKPresenter $result = json_encode([ "response" => $res, ]); + + if($callback) { + $result = $callback . '(' . $result . ')'; + header('Content-Type: application/javascript'); + } else + header("Content-Type: application/json"); $size = strlen($result); - header("Content-Type: application/json"); header("Content-Length: $size"); + exit($result); }