From 1979b8f1fcc20cd1a0857837599655cb4ee7c865 Mon Sep 17 00:00:00 2001 From: lalka2016 <99399973+lalka2016@users.noreply.github.com> Date: Fri, 25 Aug 2023 18:31:08 +0300 Subject: [PATCH] Add editing methods --- VKAPI/Handlers/Messages.php | 66 ++++++++++++++++++- VKAPI/Handlers/Notes.php | 6 +- VKAPI/Handlers/VKAPIRequestHandler.php | 2 +- VKAPI/Handlers/Video.php | 4 +- VKAPI/Handlers/Wall.php | 89 ++++++++++++++++++++++++++ Web/Models/Entities/Message.php | 6 +- bootstrap.php | 38 +++++++++++ 7 files changed, 201 insertions(+), 10 deletions(-) diff --git a/VKAPI/Handlers/Messages.php b/VKAPI/Handlers/Messages.php index bc9035f5..6fc057e0 100644 --- a/VKAPI/Handlers/Messages.php +++ b/VKAPI/Handlers/Messages.php @@ -65,7 +65,8 @@ final class Messages extends VKAPIRequestHandler ]; } - function send(int $user_id = -1, int $peer_id = -1, string $domain = "", int $chat_id = -1, string $user_ids = "", string $message = "", int $sticker_id = -1, int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0) + function send(int $user_id = -1, int $peer_id = -1, string $domain = "", int $chat_id = -1, string $user_ids = "", string $message = "", int $sticker_id = -1, int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0, + string $attachment = "") # интересно почему не attachments { $this->requireUser(); $this->willExecuteWriteAction(); @@ -79,7 +80,8 @@ final class Messages extends VKAPIRequestHandler $this->fail(946, "Chats are not implemented"); else if($sticker_id !== -1) $this->fail(-151, "Stickers are not implemented"); - else if(empty($message)) + + if(empty($message) && empty($attachment)) $this->fail(100, "Message text is empty or invalid"); # lol recursion @@ -117,6 +119,21 @@ final class Messages extends VKAPIRequestHandler if(!$msg) $this->fail(950, "Internal error"); else + if(!empty($attachment)) { + $attachs = parseAttachments($attachment); + + # Работают только фотки, остальное просто не будет отображаться. + if(sizeof($attachs) >= 10) + $this->fail(15, "Too many attachments"); + + foreach($attachs as $attach) { + if($attach && !$attach->isDeleted() && $attach->getOwner()->getId() == $this->getUser()->getId()) + $msg->attach($attach); + else + $this->fail(52, "One of the attachments is invalid"); + } + } + return $msg->getId(); } @@ -393,4 +410,49 @@ final class Messages extends VKAPIRequestHandler return $res; } + + function edit(int $message_id, string $message = "", string $attachment = "", int $peer_id = 0) + { + $this->requireUser(); + $this->willExecuteWriteAction(); + + $msg = (new MSGRepo)->get($message_id); + + if(empty($message) && empty($attachment)) + $this->fail(100, "Required parameter 'message' missing."); + + if(!$msg || $msg->isDeleted()) + $this->fail(102, "Invalid message"); + + if($msg->getSender()->getId() != $this->getUser()->getId()) + $this->fail(15, "Access to message denied"); + + if(!empty($message)) + $msg->setContent($message); + + $msg->setEdited(time()); + $msg->save(true); + + if(!empty($attachment)) { + $attachs = parseAttachments($attachment); + $newAttachmentsCount = sizeof($attachs); + + $postsAttachments = iterator_to_array($msg->getChildren()); + + if(sizeof($postsAttachments) >= 10) + $this->fail(15, "Message have too many attachments"); + + if(($newAttachmentsCount + sizeof($postsAttachments)) > 10) + $this->fail(158, "Message will have too many attachments"); + + foreach($attachs as $attach) { + if($attach && !$attach->isDeleted() && $attach->getOwner()->getId() == $this->getUser()->getId()) + $msg->attach($attach); + else + $this->fail(52, "One of the attachments is invalid"); + } + } + + return 1; + } } diff --git a/VKAPI/Handlers/Notes.php b/VKAPI/Handlers/Notes.php index 7c9c9fec..ef8cd071 100644 --- a/VKAPI/Handlers/Notes.php +++ b/VKAPI/Handlers/Notes.php @@ -161,19 +161,17 @@ final class Notes extends VKAPIRequestHandler function editComment(int $comment_id, string $message, int $owner_id = NULL) { - /* $this->requireUser(); $this->willExecuteWriteAction(); $comment = (new CommentsRepo)->get($comment_id); - if($comment->getOwner() != $this->getUser()->getId()) + if($comment->getOwner()->getId() != $this->getUser()->getId()) $this->fail(15, "Access to comment denied"); $comment->setContent($message); $comment->setEdited(time()); - $comment->save(); - */ + $comment->save(true); return 1; } diff --git a/VKAPI/Handlers/VKAPIRequestHandler.php b/VKAPI/Handlers/VKAPIRequestHandler.php index d2fcfc74..4953dbe1 100644 --- a/VKAPI/Handlers/VKAPIRequestHandler.php +++ b/VKAPI/Handlers/VKAPIRequestHandler.php @@ -28,7 +28,7 @@ abstract class VKAPIRequestHandler protected function getPlatform(): ?string { - return $this->platform; + return $this->platform ?? ""; } protected function userAuthorized(): bool diff --git a/VKAPI/Handlers/Video.php b/VKAPI/Handlers/Video.php index 740ccd54..c51fff3f 100755 --- a/VKAPI/Handlers/Video.php +++ b/VKAPI/Handlers/Video.php @@ -11,11 +11,11 @@ use openvk\Web\Models\Repositories\Comments as CommentsRepo; final class Video extends VKAPIRequestHandler { - function get(int $owner_id, string $videos, int $offset = 0, int $count = 30, int $extended = 0): object + function get(int $owner_id, string $videos = "", int $offset = 0, int $count = 30, int $extended = 0): object { $this->requireUser(); - if ($videos) { + if(!empty($videos)) { $vids = explode(',', $videos); foreach($vids as $vid) diff --git a/VKAPI/Handlers/Wall.php b/VKAPI/Handlers/Wall.php index d52dfce1..473c3cf4 100644 --- a/VKAPI/Handlers/Wall.php +++ b/VKAPI/Handlers/Wall.php @@ -433,6 +433,7 @@ final class Wall extends VKAPIRequestHandler $this->fail(100, "One of the parameters specified was missing or invalid"); } + # TODO use parseAttachments if(!empty($attachments)) { $attachmentsArr = explode(",", $attachments); # Аттачи такого вида: [тип][id владельца]_[id вложения] @@ -776,6 +777,94 @@ final class Wall extends VKAPIRequestHandler return 1; } + function edit(int $owner_id, int $post_id, string $message = "", string $attachments = "") { + $this->requireUser(); + $this->willExecuteWriteAction(); + + $post = (new PostsRepo)->getPostById($owner_id, $post_id); + + if(!$post || $post->isDeleted()) + $this->fail(102, "Invalid post"); + + if(empty($message) && empty($attachments)) + $this->fail(100, "Required parameter 'message' missing."); + + if($post->getOwner(false)->getId() != $this->getUser()->getId()) + $this->fail(7, "Access to editing denied"); + + if(!empty($message)) + $post->setContent($message); + + $post->setEdited(time()); + $post->save(true); + + if(!empty($attachments)) { + $attachs = parseAttachments($attachments); + $newAttachmentsCount = sizeof($attachs); + + $postsAttachments = iterator_to_array($post->getChildren()); + + if(sizeof($postsAttachments) >= 10) + $this->fail(15, "Post have too many attachments"); + + if(($newAttachmentsCount + sizeof($postsAttachments)) > 10) + $this->fail(158, "Post will have too many attachments"); + + foreach($attachs as $attach) { + if($attach && !$attach->isDeleted() && $attach->getOwner()->getId() == $this->getUser()->getId()) + $post->attach($attach); + else + $this->fail(52, "One of the attachments is invalid"); + } + } + + return ["post_id" => $post->getVirtualId()]; + } + + function editComment(int $comment_id, int $owner_id = 0, string $message = "", string $attachments = "") { + $this->requireUser(); + $this->willExecuteWriteAction(); + + $comment = (new CommentsRepo)->get($comment_id); + + if(empty($message) && empty($attachments)) + $this->fail(100, "Required parameter 'message' missing."); + + if(!$comment || $comment->isDeleted()) + $this->fail(102, "Invalid comment"); + + if($comment->getOwner()->getId() != $this->getUser()->getId()) + $this->fail(15, "Access to comment denied"); + + if(!empty($message)) + $comment->setContent($message); + + $comment->setEdited(time()); + $comment->save(true); + + if(!empty($attachments)) { + $attachs = parseAttachments($attachments); + $newAttachmentsCount = sizeof($attachs); + + $postsAttachments = iterator_to_array($comment->getChildren()); + + if(sizeof($postsAttachments) >= 10) + $this->fail(15, "Post have too many attachments"); + + if(($newAttachmentsCount + sizeof($postsAttachments)) > 10) + $this->fail(158, "Post will have too many attachments"); + + foreach($attachs as $attach) { + if($attach && !$attach->isDeleted() && $attach->getOwner()->getId() == $this->getUser()->getId()) + $comment->attach($attach); + else + $this->fail(52, "One of the attachments is invalid"); + } + } + + return 1; + } + private function getApiPhoto($attachment) { return [ "type" => "photo", diff --git a/Web/Models/Entities/Message.php b/Web/Models/Entities/Message.php index 97f3cf69..00de6e98 100644 --- a/Web/Models/Entities/Message.php +++ b/Web/Models/Entities/Message.php @@ -123,7 +123,11 @@ class Message extends RowModel ], ]; } else { - throw new \Exception("Unknown attachment type: " . get_class($attachment)); + $attachments[] = [ + "type" => "unknown" + ]; + + # throw new \Exception("Unknown attachment type: " . get_class($attachment)); } } diff --git a/bootstrap.php b/bootstrap.php index faa798f6..c4e5319b 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -203,6 +203,44 @@ function ovk_is_ssl(): bool return $GLOBALS["requestIsSSL"]; } +function parseAttachments(string $attachments) +{ + $attachmentsArr = explode(",", $attachments); + $returnArr = []; + + foreach($attachmentsArr as $attachment) { + $attachmentType = NULL; + + if(str_contains($attachment, "photo")) + $attachmentType = "photo"; + elseif(str_contains($attachment, "video")) + $attachmentType = "video"; + elseif(str_contains($attachment, "note")) + $attachmentType = "note"; + + $attachmentIds = str_replace($attachmentType, "", $attachment); + $attachmentOwner = (int)explode("_", $attachmentIds)[0]; + $attachmentId = (int)end(explode("_", $attachmentIds)); + + switch($attachmentType) { + case "photo": + $attachmentObj = (new openvk\Web\Models\Repositories\Photos)->getByOwnerAndVID($attachmentOwner, $attachmentId); + $returnArr[] = $attachmentObj; + break; + case "video": + $attachmentObj = (new openvk\Web\Models\Repositories\Videos)->getByOwnerAndVID($attachmentOwner, $attachmentId); + $returnArr[] = $attachmentObj; + break; + case "note": + $attachmentObj = (new openvk\Web\Models\Repositories\Notes)->getNoteById($attachmentOwner, $attachmentId); + $returnArr[] = $attachmentObj; + break; + } + } + + return $returnArr; +} + function ovk_scheme(bool $with_slashes = false): string { $scheme = ovk_is_ssl() ? "https" : "http";