Add editing methods

This commit is contained in:
lalka2016 2023-08-25 18:31:08 +03:00
parent a0ffd0a9d7
commit 1979b8f1fc
7 changed files with 201 additions and 10 deletions

View file

@ -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;
}
}

View file

@ -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;
}

View file

@ -28,7 +28,7 @@ abstract class VKAPIRequestHandler
protected function getPlatform(): ?string
{
return $this->platform;
return $this->platform ?? "";
}
protected function userAuthorized(): bool

View file

@ -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)

View file

@ -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",

View file

@ -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));
}
}

View file

@ -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";