VKAPI: Add reposted posts support (???)

This commit is contained in:
veselcraft 2022-07-23 03:25:17 +03:00
parent 25064b6e4f
commit 0c97ca1a4d
No known key found for this signature in database
GPG key ID: AED66BC1AC628A4E

View file

@ -23,22 +23,37 @@ final class Wall extends VKAPIRequestHandler
$from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId(); $from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
$attachments = []; $attachments = [];
$repost = [];
foreach($post->getChildren() as $attachment) { foreach($post->getChildren() as $attachment) {
if($attachment instanceof \openvk\Web\Models\Entities\Photo) { if($attachment instanceof \openvk\Web\Models\Entities\Photo) {
if($attachment->isDeleted()) if($attachment->isDeleted())
continue; continue;
$attachments[] = [ $attachments[] = $this->getApiPhoto($attachment);
"type" => "photo", } else if ($attachment instanceof \openvk\Web\Models\Entities\Post) {
"photo" => [ $repostAttachments = [];
"album_id" => $attachment->getAlbum() ? $attachment->getAlbum()->getId() : NULL,
"date" => $attachment->getPublicationTime()->timestamp(), foreach($attachment->getChildren() as $repostAttachment) {
"id" => $attachment->getVirtualId(), if($repostAttachment instanceof \openvk\Web\Models\Entities\Photo) {
"owner_id" => $attachment->getOwner()->getId(), if($attachment->isDeleted())
"sizes" => array_values($attachment->getVkApiSizes()), continue;
"text" => "",
"has_tags" => false $repostAttachments[] = $this->getApiPhoto($repostAttachment);
] /* Рекурсии, сука! Заказывали? */
}
}
$repost[] = [
"id" => $attachment->getVirtualId(),
"owner_id" => $attachment->getOwner()->getId(),
"from_id" => $attachment->getOwner()->getId(),
"date" => $attachment->getPublicationTime()->timestamp(),
"post_type" => "post",
"text" => $attachment->getText(false),
"attachments" => $repostAttachments,
"post_source" => [
"type" => "vk"
],
]; ];
} }
} }
@ -50,6 +65,7 @@ final class Wall extends VKAPIRequestHandler
"date" => $post->getPublicationTime()->timestamp(), "date" => $post->getPublicationTime()->timestamp(),
"post_type" => "post", "post_type" => "post",
"text" => $post->getText(false), "text" => $post->getText(false),
"copy_history" => $repost,
"can_edit" => 0, # TODO "can_edit" => 0, # TODO
"can_delete" => $post->canBeDeletedBy($this->getUser()), "can_delete" => $post->canBeDeletedBy($this->getUser()),
"can_pin" => $post->canBePinnedBy($this->getUser()), "can_pin" => $post->canBePinnedBy($this->getUser()),
@ -148,56 +164,35 @@ final class Wall extends VKAPIRequestHandler
$post = (new PostsRepo)->getPostById(intval($id[0]), intval($id[1])); $post = (new PostsRepo)->getPostById(intval($id[0]), intval($id[1]));
if($post) { if($post) {
$from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId(); $from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
$attachments; $attachments = [];
$repost = []; // чел высрал семь сигарет 😳 помянем 🕯
foreach($post->getChildren() as $attachment) { foreach($post->getChildren() as $attachment) {
if($attachment instanceof \openvk\Web\Models\Entities\Photo) { if($attachment instanceof \openvk\Web\Models\Entities\Photo) {
$attachments[] = [ $attachments[] = $this->getApiPhoto($attachment);
"type" => "photo", } else if ($attachment instanceof \openvk\Web\Models\Entities\Post) {
"photo" => [ $repostAttachments = [];
"album_id" => $attachment->getAlbum() ? $attachment->getAlbum()->getId() : NULL,
"date" => $attachment->getPublicationTime()->timestamp(), foreach($attachment->getChildren() as $repostAttachment) {
"id" => $attachment->getVirtualId(), if($repostAttachment instanceof \openvk\Web\Models\Entities\Photo) {
"owner_id" => $attachment->getOwner()->getId(), if($attachment->isDeleted())
"sizes" => array( continue;
[
"height" => 2560, $repostAttachments[] = $this->getApiPhoto($repostAttachment);
"url" => $attachment->getURLBySizeId("normal"), /* Рекурсии, сука! Заказывали? */
"type" => "m", }
"width" => 2560, }
],
[ $repost[] = [
"height" => 130, "id" => $attachment->getVirtualId(),
"url" => $attachment->getURLBySizeId("tiny"), "owner_id" => $attachment->getOwner()->getId(),
"type" => "o", "from_id" => $attachment->getOwner()->getId(),
"width" => 130, "date" => $attachment->getPublicationTime()->timestamp(),
], "post_type" => "post",
[ "text" => $attachment->getText(false),
"height" => 604, "attachments" => $repostAttachments,
"url" => $attachment->getURLBySizeId("normal"), "post_source" => [
"type" => "p", "type" => "vk"
"width" => 604, ],
],
[
"height" => 807,
"url" => $attachment->getURLBySizeId("large"),
"type" => "q",
"width" => 807,
],
[
"height" => 1280,
"url" => $attachment->getURLBySizeId("larger"),
"type" => "r",
"width" => 1280,
],
[
"height" => 75, # Для временного компросима оставляю статическое число. Если каждый раз обращаться к файлу за количеством пикселов, то наступает пuпuська полная с производительностью, так что пока так
"url" => $attachment->getURLBySizeId("miniscule"),
"type" => "s",
"width" => 75,
]),
"text" => "",
"has_tags" => false
]
]; ];
} }
} }
@ -209,6 +204,7 @@ final class Wall extends VKAPIRequestHandler
"date" => $post->getPublicationTime()->timestamp(), "date" => $post->getPublicationTime()->timestamp(),
"post_type" => "post", "post_type" => "post",
"text" => $post->getText(false), "text" => $post->getText(false),
"copy_history" => $repost,
"can_edit" => 0, # TODO "can_edit" => 0, # TODO
"can_delete" => $post->canBeDeletedBy($user), "can_delete" => $post->canBeDeletedBy($user),
"can_pin" => $post->canBePinnedBy($user), "can_pin" => $post->canBePinnedBy($user),
@ -238,7 +234,8 @@ final class Wall extends VKAPIRequestHandler
else else
$groups[] = $from_id * -1; $groups[] = $from_id * -1;
$attachments = NULL; # free attachments so it will not clone everythingg $attachments = NULL; # free attachments so it will not clone everything
$repost = NULL; # same
} }
} }
@ -374,7 +371,7 @@ final class Wall extends VKAPIRequestHandler
return (object)["post_id" => $post->getVirtualId()]; return (object)["post_id" => $post->getVirtualId()];
} }
function repost(string $object, string $message) { function repost(string $object, string $message = "") {
$this->requireUser(); $this->requireUser();
$postArray; $postArray;
@ -401,4 +398,19 @@ final class Wall extends VKAPIRequestHandler
"likes_count" => $post->getLikesCount() "likes_count" => $post->getLikesCount()
]; ];
} }
private function getApiPhoto($attachment) {
return [
"type" => "photo",
"photo" => [
"album_id" => $attachment->getAlbum() ? $attachment->getAlbum()->getId() : NULL,
"date" => $attachment->getPublicationTime()->timestamp(),
"id" => $attachment->getVirtualId(),
"owner_id" => $attachment->getOwner()->getId(),
"sizes" => array_values($attachment->getVkApiSizes()),
"text" => "",
"has_tags" => false
]
];
}
} }