diff --git a/VKAPI/Handlers/Gifts.php b/VKAPI/Handlers/Gifts.php index f4c3d301..b3ae65c3 100644 --- a/VKAPI/Handlers/Gifts.php +++ b/VKAPI/Handlers/Gifts.php @@ -20,7 +20,7 @@ final class Gifts extends VKAPIRequestHandler $gift_item = []; - $userGifts = $user->getGifts(1, $count, false); + $userGifts = array_slice(iterator_to_array($user->getGifts(1, $count, false)), $offset); if(sizeof($userGifts) < 0) { return NULL; diff --git a/VKAPI/Handlers/Notes.php b/VKAPI/Handlers/Notes.php index 1267c9c9..266ec99b 100644 --- a/VKAPI/Handlers/Notes.php +++ b/VKAPI/Handlers/Notes.php @@ -4,7 +4,6 @@ use openvk\Web\Models\Repositories\Notes as NotesRepo; use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Comments as CommentsRepo; use openvk\Web\Models\Entities\{Note, Comment}; -use openvk\VKAPI\Structures\{Note as APINote}; use openvk\VKAPI\Structures\{Comment as APIComment}; final class Notes extends VKAPIRequestHandler @@ -72,7 +71,7 @@ final class Notes extends VKAPIRequestHandler $comment = (new CommentsRepo)->get($comment_id); - if(!$comment) + if(!$comment || !$comment->canBeDeletedBy($this->getUser())) $this->fail(403, "Access to comment denied"); $comment->delete(); @@ -94,7 +93,7 @@ final class Notes extends VKAPIRequestHandler $this->fail(189, "Note is deleted"); !empty($title) ? $note->setName($title) : NULL; - !empty($text) ? $note->setSource($text) : NULL; + !empty($text) ? $note->setSource($text) : NULL; $note->setCached_Content(NULL); $note->setEdited(time()); @@ -128,29 +127,32 @@ final class Notes extends VKAPIRequestHandler if(!$user) $this->fail(15, "Invalid user"); - $notes = iterator_to_array((new NotesRepo)->getUserNotes($user, 1, $count, $sort == 0 ? "ASC" : "DESC")); - $nodez = (object) [ - "count" => (new NotesRepo)->getUserNotesCount((new UsersRepo)->get($user_id)), - "notes" => [] - ]; + if(empty($note_ids)) { + $notes = array_slice(iterator_to_array((new NotesRepo)->getUserNotes($user, 1, $count, $sort == 0 ? "ASC" : "DESC")), $offset); + $nodez = (object) [ + "count" => (new NotesRepo)->getUserNotesCount((new UsersRepo)->get($user_id)), + "notes" => [] + ]; + + foreach($notes as $note) { + if($note->isDeleted()) continue; + + $nodez->notes[] = $note->toVkApiStruct(); + } + } else { + $notes = explode(',', $note_ids); - foreach($notes as $note) { - if($note->isDeleted()) - continue; - - $apiNote = new APINote; - $apiNote->id = $note->getId(); - $apiNote->owner_id = $note->getOwner()->getId(); - $apiNote->title = $note->getName(); - $apiNote->text = $note->getText(); - $apiNote->date = $note->getPublicationTime()->timestamp(); - $apiNote->comments = $note->getCommentsCount(); - $apiNote->read_comments = $note->getCommentsCount(); - $apiNote->view_url = "/note".$note->getOwner()->getId()."_".$note->getId(); - $apiNote->privacy_view = 1; - $apiNote->can_comment = 1; - $apiNote->text_wiki = "r"; - $nodez->notes[] = $apiNote; + foreach($notes as $note) + { + $id = explode("_", $note); + + $items = []; + + $note = (new NotesRepo)->getNoteById((int)$id[0], (int)$id[1]); + if($note) { + $nodez->notes[] = $note->toVkApiStruct(); + } + } } return $nodez; @@ -168,26 +170,10 @@ final class Notes extends VKAPIRequestHandler if($note->isDeleted()) $this->fail(189, "Note is deleted"); - if(!$user || $note->getOwner()->isDeleted()) + if(!$note->getOwner() || $note->getOwner()->isDeleted()) $this->fail(177, "Owner does not exists"); - - $apiNote = new APINote; - $apiNote->id = $note->getId(); - $apiNote->owner_id = $note->getOwner()->getId(); - $apiNote->title = $note->getName(); - $apiNote->text = $note->getText(); - $apiNote->date = $note->getPublicationTime()->timestamp(); - $apiNote->comments = $note->getCommentsCount(); - $apiNote->read_comments = $note->getCommentsCount(); - $apiNote->view_url = "/note".$note->getOwner()->getId()."_".$note->getId(); - $apiNote->privacy_view = 1; - $apiNote->can_comment = 1; - $apiNote->text_wiki = "r"; - - $nodez->notes[] = $apiNote; - - return $apiNote; + return $note->toVkApiStruct(); } function getComments(int $note_id, int $owner_id, int $sort = 1, int $offset = 0, int $count = 100) @@ -200,14 +186,14 @@ final class Notes extends VKAPIRequestHandler if($note->isDeleted()) $this->fail(189, "Note is deleted"); - if($note->getOwner()->isDeleted()) + if(!$note->getOwner()) $this->fail(177, "Owner does not exists"); $arr = (object) [ "count" => $note->getCommentsCount(), "comments" => []]; - $comments = $note->getComments(1, $count); - + $comments = array_slice(iterator_to_array($note->getComments(1, $count)), $offset); + foreach($comments as $comment) { $comm = new APIComment; $comm->id = $comment->getId(); @@ -223,13 +209,13 @@ final class Notes extends VKAPIRequestHandler return $arr; } - function getFriendsNotes(int $offset, int $count) + function getFriendsNotes(int $offset = 0, int $count = 0) { - return 1; + $this->fail(4, "Not implemented"); } - function restoreComment(int $comment_id, int $owner_id) + function restoreComment(int $comment_id = 0, int $owner_id = 0) { - $this->fail(4, " "); + $this->fail(4, "Not implemented"); } } diff --git a/VKAPI/Handlers/Wall.php b/VKAPI/Handlers/Wall.php index ef40fe00..85cccdbf 100644 --- a/VKAPI/Handlers/Wall.php +++ b/VKAPI/Handlers/Wall.php @@ -430,7 +430,7 @@ final class Wall extends VKAPIRequestHandler # Аттачи такого вида: [тип][id владельца]_[id вложения] # Пример: photo1_1 - if(count($attachmentsArr) > 10) + if(sizeof($attachmentsArr) > 10) $this->fail(50, "Error: too many attachments"); foreach($attachmentsArr as $attac) { @@ -452,7 +452,7 @@ final class Wall extends VKAPIRequestHandler if($attachmentType == "photo") { $attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); - if(is_null($attacc)) + if(!$attacc || $attacc->isDeleted()) $this->fail(100, "Photo does not exists"); if($attacc->getOwner()->getId() != $this->getUser()->getId()) $this->fail(43, "You do not have access to this photo"); @@ -460,7 +460,7 @@ final class Wall extends VKAPIRequestHandler $post->attach($attacc); } elseif($attachmentType == "video") { $attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); - if(!$attacc) + if(!$attacc || $attacc->isDeleted()) $this->fail(100, "Video does not exists"); if($attacc->getOwner()->getId() != $this->getUser()->getId()) $this->fail(43, "You do not have access to this video"); @@ -641,16 +641,20 @@ final class Wall extends VKAPIRequestHandler return $response; } - 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, string $attachments = "") { $this->requireUser(); $this->willExecuteWriteAction(); $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, "Invalid post"); if($post->getTargetWall() < 0) $club = (new ClubsRepo)->get(abs($post->getTargetWall())); + if(empty($message) && empty($attachments)) { + $this->fail(100, "Required parameter 'message' missing."); + } + $flags = 0; if($from_group != 0 && !is_null($club) && $club->canBeModifiedBy($this->user)) $flags |= 0b10000000; @@ -668,6 +672,49 @@ final class Wall extends VKAPIRequestHandler $this->fail(1, "ошибка про то что коммент большой слишком"); } + if(!empty($attachments)) { + $attachmentsArr = explode(",", $attachments); + + if(sizeof($attachmentsArr) > 10) + $this->fail(50, "Error: too many attachments"); + + foreach($attachmentsArr as $attac) { + $attachmentType = NULL; + + if(str_contains($attac, "photo")) + $attachmentType = "photo"; + elseif(str_contains($attac, "video")) + $attachmentType = "video"; + else + $this->fail(205, "Unknown attachment type"); + + $attachment = str_replace($attachmentType, "", $attac); + + $attachmentOwner = (int)explode("_", $attachment)[0]; + $attachmentId = (int)end(explode("_", $attachment)); + + $attacc = NULL; + + if($attachmentType == "photo") { + $attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); + if(!$attacc || $attacc->isDeleted()) + $this->fail(100, "Photo does not exists"); + if($attacc->getOwner()->getId() != $this->getUser()->getId()) + $this->fail(43, "You do not have access to this photo"); + + $comment->attach($attacc); + } elseif($attachmentType == "video") { + $attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); + if(!$attacc || $attacc->isDeleted()) + $this->fail(100, "Video does not exists"); + if($attacc->getOwner()->getId() != $this->getUser()->getId()) + $this->fail(43, "You do not have access to this video"); + + $comment->attach($attacc); + } + } + } + if($post->getOwner()->getId() !== $this->user->getId()) if(($owner = $post->getOwner()) instanceof User) (new CommentNotification($owner, $comment, $post, $this->user))->emit(); diff --git a/VKAPI/Structures/Note.php b/VKAPI/Structures/Note.php deleted file mode 100644 index e32b0523..00000000 --- a/VKAPI/Structures/Note.php +++ /dev/null @@ -1,17 +0,0 @@ -getRecord()->source; } + + function toVkApiStruct(): object + { + $res = (object) []; + + $res->id = $this->getId(); + $res->owner_id = $this->getOwner()->getId(); + $res->title = $this->getName(); + $res->text = $this->getText(); + $res->date = $this->getPublicationTime()->timestamp(); + $res->comments = $this->getCommentsCount(); + $res->read_comments = $this->getCommentsCount(); + $res->view_url = "/note".$this->getOwner()->getId()."_".$this->getId(); + $res->privacy_view = 1; + $res->can_comment = 1; + $res->text_wiki = "r"; + + return $res; + } } diff --git a/Web/Models/Repositories/Notes.php b/Web/Models/Repositories/Notes.php index 7d95f975..0473070a 100644 --- a/Web/Models/Repositories/Notes.php +++ b/Web/Models/Repositories/Notes.php @@ -26,10 +26,10 @@ class Notes return $this->toNote($this->notes->get($id)); } - function getUserNotes(User $user, int $page = 1, ?int $perPage = NULL): \Traversable + function getUserNotes(User $user, int $page = 1, ?int $perPage = NULL, string $sort = "DESC"): \Traversable { $perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; - foreach($this->notes->where("owner", $user->getId())->where("deleted", 0)->order("created DESC")->page($page, $perPage) as $album) + foreach($this->notes->where("owner", $user->getId())->where("deleted", 0)->order("created $sort")->page($page, $perPage) as $album) yield new Note($album); }