VKAPI: Add profiles and groups verification in Newsfeed.get and Wall.get

This commit is contained in:
Dmitry Tretyakov 2023-04-29 23:33:58 +07:00
parent 725e68d0e5
commit eb227db6a9

View file

@ -15,7 +15,7 @@ final class Wall extends VKAPIRequestHandler
function get(int $owner_id, string $domain = "", int $offset = 0, int $count = 30, int $extended = 0): object function get(int $owner_id, string $domain = "", int $offset = 0, int $count = 30, int $extended = 0): object
{ {
$this->requireUser(); $this->requireUser();
$posts = new PostsRepo; $posts = new PostsRepo;
$items = []; $items = [];
@ -44,7 +44,7 @@ final class Wall extends VKAPIRequestHandler
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[] = $this->getApiPhoto($attachment); $attachments[] = $this->getApiPhoto($attachment);
} else if($attachment instanceof \openvk\Web\Models\Entities\Poll) { } else if($attachment instanceof \openvk\Web\Models\Entities\Poll) {
$attachments[] = $this->getApiPoll($attachment, $this->getUser()); $attachments[] = $this->getApiPoll($attachment, $this->getUser());
@ -57,7 +57,7 @@ final class Wall extends VKAPIRequestHandler
if($repostAttachment instanceof \openvk\Web\Models\Entities\Photo) { if($repostAttachment instanceof \openvk\Web\Models\Entities\Photo) {
if($repostAttachment->isDeleted()) if($repostAttachment->isDeleted())
continue; continue;
$repostAttachments[] = $this->getApiPhoto($repostAttachment); $repostAttachments[] = $this->getApiPhoto($repostAttachment);
/* Рекурсии, сука! Заказывали? */ /* Рекурсии, сука! Заказывали? */
} }
@ -69,7 +69,7 @@ final class Wall extends VKAPIRequestHandler
$profiles[] = $attachment->getOwner()->getId(); $profiles[] = $attachment->getOwner()->getId();
$post_source = []; $post_source = [];
if($attachment->getPlatform(true) === NULL) { if($attachment->getPlatform(true) === NULL) {
$post_source = (object)["type" => "vk"]; $post_source = (object)["type" => "vk"];
} else { } else {
@ -93,7 +93,7 @@ final class Wall extends VKAPIRequestHandler
} }
$post_source = []; $post_source = [];
if($post->getPlatform(true) === NULL) { if($post->getPlatform(true) === NULL) {
$post_source = (object)["type" => "vk"]; $post_source = (object)["type" => "vk"];
} else { } else {
@ -134,7 +134,7 @@ final class Wall extends VKAPIRequestHandler
"user_reposted" => 0 "user_reposted" => 0
] ]
]; ];
if ($from_id > 0) if ($from_id > 0)
$profiles[] = $from_id; $profiles[] = $from_id;
else else
@ -162,7 +162,8 @@ final class Wall extends VKAPIRequestHandler
"screen_name" => $user->getShortCode(), "screen_name" => $user->getShortCode(),
"photo_50" => $user->getAvatarUrl(), "photo_50" => $user->getAvatarUrl(),
"photo_100" => $user->getAvatarUrl(), "photo_100" => $user->getAvatarUrl(),
"online" => $user->isOnline() "online" => $user->isOnline(),
"verified" => $user->isVerified()
]; ];
} }
@ -177,6 +178,7 @@ final class Wall extends VKAPIRequestHandler
"photo_50" => $group->getAvatarUrl(), "photo_50" => $group->getAvatarUrl(),
"photo_100" => $group->getAvatarUrl(), "photo_100" => $group->getAvatarUrl(),
"photo_200" => $group->getAvatarUrl(), "photo_200" => $group->getAvatarUrl(),
"verified" => $group->isVerified()
]; ];
} }
@ -227,19 +229,19 @@ final class Wall extends VKAPIRequestHandler
if($repostAttachment instanceof \openvk\Web\Models\Entities\Photo) { if($repostAttachment instanceof \openvk\Web\Models\Entities\Photo) {
if($attachment->isDeleted()) if($attachment->isDeleted())
continue; continue;
$repostAttachments[] = $this->getApiPhoto($repostAttachment); $repostAttachments[] = $this->getApiPhoto($repostAttachment);
/* Рекурсии, сука! Заказывали? */ /* Рекурсии, сука! Заказывали? */
} }
} }
if ($attachment->isPostedOnBehalfOfGroup()) if ($attachment->isPostedOnBehalfOfGroup())
$groups[] = $attachment->getOwner()->getId(); $groups[] = $attachment->getOwner()->getId();
else else
$profiles[] = $attachment->getOwner()->getId(); $profiles[] = $attachment->getOwner()->getId();
$post_source = []; $post_source = [];
if($attachment->getPlatform(true) === NULL) { if($attachment->getPlatform(true) === NULL) {
$post_source = (object)["type" => "vk"]; $post_source = (object)["type" => "vk"];
} else { } else {
@ -304,7 +306,7 @@ final class Wall extends VKAPIRequestHandler
"user_reposted" => 0 "user_reposted" => 0
] ]
]; ];
if ($from_id > 0) if ($from_id > 0)
$profiles[] = $from_id; $profiles[] = $from_id;
else else
@ -334,7 +336,8 @@ final class Wall extends VKAPIRequestHandler
"screen_name" => $user->getShortCode(), "screen_name" => $user->getShortCode(),
"photo_50" => $user->getAvatarUrl(), "photo_50" => $user->getAvatarUrl(),
"photo_100" => $user->getAvatarUrl(), "photo_100" => $user->getAvatarUrl(),
"online" => $user->isOnline() "online" => $user->isOnline(),
"verified" => $user->isVerified()
]; ];
} }
@ -349,6 +352,7 @@ final class Wall extends VKAPIRequestHandler
"photo_50" => $group->getAvatarUrl(), "photo_50" => $group->getAvatarUrl(),
"photo_100" => $group->getAvatarUrl(), "photo_100" => $group->getAvatarUrl(),
"photo_200" => $group->getAvatarUrl(), "photo_200" => $group->getAvatarUrl(),
"verified" => $group->isVerified()
]; ];
} }
@ -369,7 +373,7 @@ final class Wall extends VKAPIRequestHandler
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$owner_id = intval($owner_id); $owner_id = intval($owner_id);
$wallOwner = ($owner_id > 0 ? (new UsersRepo)->get($owner_id) : (new ClubsRepo)->get($owner_id * -1)) $wallOwner = ($owner_id > 0 ? (new UsersRepo)->get($owner_id) : (new ClubsRepo)->get($owner_id * -1))
?? $this->fail(18, "User was deleted or banned"); ?? $this->fail(18, "User was deleted or banned");
if($owner_id > 0) if($owner_id > 0)
@ -380,7 +384,7 @@ final class Wall extends VKAPIRequestHandler
else else
$canPost = $wallOwner->canPost(); $canPost = $wallOwner->canPost();
else else
$canPost = false; $canPost = false;
if($canPost == false) $this->fail(15, "Access denied"); if($canPost == false) $this->fail(15, "Access denied");
@ -456,10 +460,10 @@ final class Wall extends VKAPIRequestHandler
$postArray; $postArray;
if(preg_match('/wall((?:-?)[0-9]+)_([0-9]+)/', $object, $postArray) == 0) if(preg_match('/wall((?:-?)[0-9]+)_([0-9]+)/', $object, $postArray) == 0)
$this->fail(100, "One of the parameters specified was missing or invalid: object is incorrect"); $this->fail(100, "One of the parameters specified was missing or invalid: object is incorrect");
$post = (new PostsRepo)->getPostById((int) $postArray[1], (int) $postArray[2]); $post = (new PostsRepo)->getPostById((int) $postArray[1], (int) $postArray[2]);
if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid"); if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid");
$nPost = new Post; $nPost = new Post;
$nPost->setOwner($this->user->getId()); $nPost->setOwner($this->user->getId());
$nPost->setWall($this->user->getId()); $nPost->setWall($this->user->getId());
@ -467,7 +471,7 @@ final class Wall extends VKAPIRequestHandler
$nPost->setApi_Source_Name($this->getPlatform()); $nPost->setApi_Source_Name($this->getPlatform());
$nPost->save(); $nPost->save();
$nPost->attach($post); $nPost->attach($post);
if($post->getOwner(false)->getId() !== $this->user->getId() && !($post->getOwner() instanceof Club)) if($post->getOwner(false)->getId() !== $this->user->getId() && !($post->getOwner() instanceof Club))
(new RepostNotification($post->getOwner(false), $post, $this->user->identity))->emit(); (new RepostNotification($post->getOwner(false), $post, $this->user->identity))->emit();
@ -486,7 +490,7 @@ final class Wall extends VKAPIRequestHandler
if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid"); if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid");
$comments = (new CommentsRepo)->getCommentsByTarget($post, $offset+1, $count, $sort == "desc" ? "DESC" : "ASC"); $comments = (new CommentsRepo)->getCommentsByTarget($post, $offset+1, $count, $sort == "desc" ? "DESC" : "ASC");
$items = []; $items = [];
$profiles = []; $profiles = [];
@ -503,7 +507,7 @@ final class Wall extends VKAPIRequestHandler
$attachments[] = $this->getApiPhoto($attachment); $attachments[] = $this->getApiPhoto($attachment);
} }
} }
$item = [ $item = [
"id" => $comment->getId(), "id" => $comment->getId(),
"from_id" => $oid, "from_id" => $oid,
@ -529,7 +533,7 @@ final class Wall extends VKAPIRequestHandler
"user_likes" => (int) $comment->hasLikeFrom($this->getUser()), "user_likes" => (int) $comment->hasLikeFrom($this->getUser()),
"can_publish" => 1 "can_publish" => 1
]; ];
$items[] = $item; $items[] = $item;
if($extended == true) if($extended == true)
$profiles[] = $comment->getOwner()->getId(); $profiles[] = $comment->getOwner()->getId();
@ -563,7 +567,7 @@ final class Wall extends VKAPIRequestHandler
$profiles = []; $profiles = [];
$attachments = []; $attachments = [];
foreach($comment->getChildren() as $attachment) { foreach($comment->getChildren() as $attachment) {
if($attachment instanceof \openvk\Web\Models\Entities\Photo) { if($attachment instanceof \openvk\Web\Models\Entities\Photo) {
$attachments[] = $this->getApiPhoto($attachment); $attachments[] = $this->getApiPhoto($attachment);
@ -593,7 +597,7 @@ final class Wall extends VKAPIRequestHandler
"groups_can_post" => false, "groups_can_post" => false,
] ]
]; ];
if($extended == true) if($extended == true)
$profiles[] = $comment->getOwner()->getId(); $profiles[] = $comment->getOwner()->getId();
@ -609,7 +613,7 @@ final class Wall extends VKAPIRequestHandler
$response['profiles'] = (!empty($profiles) ? (new Users)->get(implode(',', $profiles), $fields) : []); $response['profiles'] = (!empty($profiles) ? (new Users)->get(implode(',', $profiles), $fields) : []);
} }
return $response; return $response;
} }
@ -617,17 +621,17 @@ final class Wall extends VKAPIRequestHandler
function createComment(int $owner_id, int $post_id, string $message, int $from_group = 0) { function createComment(int $owner_id, int $post_id, string $message, int $from_group = 0) {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$post = (new PostsRepo)->getPostById($owner_id, $post_id); $post = (new PostsRepo)->getPostById($owner_id, $post_id);
if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid"); if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid");
if($post->getTargetWall() < 0) if($post->getTargetWall() < 0)
$club = (new ClubsRepo)->get(abs($post->getTargetWall())); $club = (new ClubsRepo)->get(abs($post->getTargetWall()));
$flags = 0; $flags = 0;
if($from_group != 0 && !is_null($club) && $club->canBeModifiedBy($this->user)) if($from_group != 0 && !is_null($club) && $club->canBeModifiedBy($this->user))
$flags |= 0b10000000; $flags |= 0b10000000;
try { try {
$comment = new Comment; $comment = new Comment;
$comment->setOwner($this->user->getId()); $comment->setOwner($this->user->getId());
@ -640,11 +644,11 @@ final class Wall extends VKAPIRequestHandler
} catch (\LengthException $ex) { } catch (\LengthException $ex) {
$this->fail(1, "ошибка про то что коммент большой слишком"); $this->fail(1, "ошибка про то что коммент большой слишком");
} }
if($post->getOwner()->getId() !== $this->user->getId()) if($post->getOwner()->getId() !== $this->user->getId())
if(($owner = $post->getOwner()) instanceof User) if(($owner = $post->getOwner()) instanceof User)
(new CommentNotification($owner, $comment, $post, $this->user))->emit(); (new CommentNotification($owner, $comment, $post, $this->user))->emit();
return (object) [ return (object) [
"comment_id" => $comment->getId(), "comment_id" => $comment->getId(),
"parents_stack" => [] "parents_stack" => []
@ -659,12 +663,12 @@ final class Wall extends VKAPIRequestHandler
if(!$comment) $this->fail(100, "One of the parameters specified was missing or invalid");; if(!$comment) $this->fail(100, "One of the parameters specified was missing or invalid");;
if(!$comment->canBeDeletedBy($this->user)) if(!$comment->canBeDeletedBy($this->user))
$this->fail(7, "Access denied"); $this->fail(7, "Access denied");
$comment->delete(); $comment->delete();
return 1; return 1;
} }
private function getApiPhoto($attachment) { private function getApiPhoto($attachment) {
return [ return [
"type" => "photo", "type" => "photo",
@ -690,7 +694,7 @@ final class Wall extends VKAPIRequestHandler
"votes" => $answer->votes "votes" => $answer->votes
]; ];
} }
$userVote = array(); $userVote = array();
foreach($attachment->getUserVote($user) as $vote) foreach($attachment->getUserVote($user) as $vote)
$userVote[] = $vote[0]; $userVote[] = $vote[0];