diff --git a/VKAPI/Handlers/Video.php b/VKAPI/Handlers/Video.php index 975ce1c3..bc962b70 100755 --- a/VKAPI/Handlers/Video.php +++ b/VKAPI/Handlers/Video.php @@ -11,7 +11,7 @@ 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 = "", string $fields = "", int $offset = 0, int $count = 30, int $extended = 0): object { $this->requireUser(); @@ -46,12 +46,46 @@ final class Video extends VKAPIRequestHandler if(!$user->getPrivacyPermission('videos.read', $this->getUser())) $this->fail(21, "This user chose to hide his videos."); - $videos = (new VideosRepo)->getByUser($user, $offset + 1, $count); + $videos = (new VideosRepo)->getByUserLimit($user, $offset, $count); $videosCount = (new VideosRepo)->getUserVideosCount($user); $items = []; - foreach ($videos as $video) { - $items[] = $video->getApiStructure($this->getUser()); + $profiles = []; + $groups = []; + foreach($videos as $video) { + $video = $video->getApiStructure($this->getUser())->video; + $items[] = $video; + if($video['owner_id']) { + if($video['owner_id'] > 0) + $profiles[] = $video['owner_id']; + else + $groups[] = abs($video['owner_id']); + } + } + + if($extended == 1) { + $profiles = array_unique($profiles); + $groups = array_unique($groups); + + $profilesFormatted = []; + $groupsFormatted = []; + + foreach($profiles as $prof) { + $profile = (new UsersRepo)->get($prof); + $profilesFormatted[] = $profile->toVkApiStruct($this->getUser(), $fields); + } + + foreach($groups as $gr) { + $group = (new ClubsRepo)->get($gr); + $groupsFormatted[] = $group->toVkApiStruct($this->getUser(), $fields); + } + + return (object) [ + "count" => $videosCount, + "items" => $items, + "profiles" => $profilesFormatted, + "groups" => $groupsFormatted, + ]; } return (object) [ diff --git a/VKAPI/Handlers/Wall.php b/VKAPI/Handlers/Wall.php index f4a83d8c..c2c73293 100644 --- a/VKAPI/Handlers/Wall.php +++ b/VKAPI/Handlers/Wall.php @@ -473,7 +473,7 @@ final class Wall extends VKAPIRequestHandler ]; } - function post(string $owner_id, string $message = "", int $from_group = 0, int $signed = 0, string $attachments = "", int $post_id = 0): object + function post(string $owner_id, string $message = "", string $copyright = "", int $from_group = 0, int $signed = 0, string $attachments = "", int $post_id = 0): object { $this->requireUser(); $this->willExecuteWriteAction(); @@ -551,7 +551,17 @@ final class Wall extends VKAPIRequestHandler if($signed == 1) $flags |= 0b01000000; - if(empty($message) && empty($attachments)) + $parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'poll', 'audio']); + $final_attachments = []; + $should_be_suggested = $owner_id < 0 && !$wallOwner->canBeModifiedBy($this->getUser()) && $wallOwner->getWallType() == 2; + foreach($parsed_attachments as $attachment) { + if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) && + !(method_exists($attachment, 'getVoters') && $attachment->getOwner()->getId() != $this->getUser()->getId())) { + $final_attachments[] = $attachment; + } + } + + if((empty($message) && (empty($attachments) || sizeof($final_attachments) < 1))) $this->fail(100, "Required parameter 'message' missing."); try { @@ -569,7 +579,7 @@ final class Wall extends VKAPIRequestHandler } catch(\Throwable) {} } - if($owner_id < 0 && !$wallOwner->canBeModifiedBy($this->getUser()) && $wallOwner->getWallType() == 2) + if($should_be_suggested) $post->setSuggested(1); $post->save(); @@ -577,90 +587,14 @@ 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 вложения] - # Пример: photo1_1 - - if(sizeof($attachmentsArr) > 10) - $this->fail(50, "Too many attachments"); - - preg_match_all("/poll/m", $attachments, $matches, PREG_SET_ORDER, 0); - if(sizeof($matches) > 1) - $this->fail(85, "Too many polls"); - - foreach($attachmentsArr as $attac) { - $attachmentType = NULL; - - if(str_contains($attac, "photo")) - $attachmentType = "photo"; - elseif(str_contains($attac, "video")) - $attachmentType = "video"; - elseif(str_contains($attac, "note")) - $attachmentType = "note"; - elseif(str_contains($attac, "poll")) - $attachmentType = "poll"; - elseif(str_contains($attac, "audio")) - $attachmentType = "audio"; - - 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, "Invalid photo"); - if(!$attacc->getOwner()->getPrivacyPermission('photos.read', $this->getUser())) - $this->fail(43, "Access to photo denied"); - - $post->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()->getPrivacyPermission('videos.read', $this->getUser())) - $this->fail(43, "Access to video denied"); - - $post->attach($attacc); - } elseif($attachmentType == "note") { - $attacc = (new NotesRepo)->getNoteById($attachmentOwner, $attachmentId); - if(!$attacc || $attacc->isDeleted()) - $this->fail(100, "Note does not exist"); - if(!$attacc->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) - $this->fail(11, "Access to note denied"); - - $post->attach($attacc); - } elseif($attachmentType == "poll") { - $attacc = (new PollsRepo)->get($attachmentId); - - if(!$attacc || $attacc->isDeleted()) - $this->fail(100, "Poll does not exist"); - if($attacc->getOwner()->getId() != $this->getUser()->getId()) - $this->fail(43, "You do not have access to this poll"); - - $post->attach($attacc); - } elseif($attachmentType == "audio") { - $attacc = (new AudiosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); - if(!$attacc || $attacc->isDeleted()) - $this->fail(100, "Audio does not exist"); - - $post->attach($attacc); - } - } + foreach($final_attachments as $attachment) { + $post->attach($attachment); } if($wall > 0 && $wall !== $this->user->identity->getId()) (new WallPostNotification($wallOwner, $post, $this->user->identity))->emit(); - if($owner_id < 0 && !$wallOwner->canBeModifiedBy($this->getUser()) && $wallOwner->getWallType() == 2) { + if($should_be_suggested) { return (object)["post_id" => "on_view"]; } @@ -675,6 +609,15 @@ final class Wall extends VKAPIRequestHandler 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"); + $parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio']); + $final_attachments = []; + foreach($parsed_attachments as $attachment) { + if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) && + !(method_exists($attachment, 'getVoters') && $attachment->getOwner()->getId() != $this->getUser()->getId())) { + $final_attachments[] = $attachment; + } + } + $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"); @@ -710,12 +653,8 @@ final class Wall extends VKAPIRequestHandler $nPost->save(); $nPost->attach($post); - $attachments_arr = parseAttachments($attachments, ['photo', 'video', 'audio']); - foreach($attachments_arr as $attachment) { - if(!$attachment || $attachment->isDeleted() || !$attachment->canBeViewedBy($this->getUser())) { - continue; - } + foreach($parsed_attachments as $attachment) { $nPost->attach($attachment); } @@ -905,7 +844,16 @@ final class Wall extends VKAPIRequestHandler if($post->getTargetWall() < 0) $club = (new ClubsRepo)->get(abs($post->getTargetWall())); - if(empty($message) && empty($attachments)) { + $parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio']); + $final_attachments = []; + foreach($parsed_attachments as $attachment) { + if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) && + !(method_exists($attachment, 'getVoters') && $attachment->getOwner()->getId() != $this->getUser()->getId())) { + $final_attachments[] = $attachment; + } + } + + if((empty($message) && (empty($attachments) || sizeof($final_attachments) < 1))) { $this->fail(100, "Required parameter 'message' missing."); } @@ -926,55 +874,8 @@ 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"; - elseif(str_contains($attac, "audio")) - $attachmentType = "audio"; - 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()->getPrivacyPermission('photos.read', $this->getUser())) - $this->fail(11, "Access to photo denied"); - - $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()->getPrivacyPermission('videos.read', $this->getUser())) - $this->fail(11, "Access to video denied"); - - $comment->attach($attacc); - } elseif($attachmentType == "audio") { - $attacc = (new AudiosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); - if(!$attacc || $attacc->isDeleted()) - $this->fail(100, "Audio does not exist"); - - $comment->attach($attacc); - } - } + foreach($final_attachments as $attachment) { + $comment->attach($attachment); } if($post->getOwner()->getId() !== $this->user->getId()) @@ -1048,25 +949,20 @@ final class Wall extends VKAPIRequestHandler } $post->save(true); + + $parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio']); + $final_attachments = []; + foreach($parsed_attachments as $attachment) { + if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) && + !(method_exists($attachment, 'getVoters') && $attachment->getOwner()->getId() != $this->getUser()->getId())) { + $final_attachments[] = $attachment; + } + } - # todo добавить такое в веб версию - 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()) - $post->attach($attach); - else - $this->fail(52, "One of the attachments is invalid"); + if(sizeof($final_attachments) > 0) { + $post->unwire(); + foreach($final_attachments as $attachment) { + $post->attach($attachment); } } @@ -1093,24 +989,20 @@ final class Wall extends VKAPIRequestHandler $comment->setEdited(time()); $comment->save(true); - - if(!empty($attachments)) { - $attachs = parseAttachments($attachments); - $newAttachmentsCount = sizeof($attachs); - $postsAttachments = iterator_to_array($comment->getChildren()); + $parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio']); + $final_attachments = []; + foreach($parsed_attachments as $attachment) { + if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) && + !(method_exists($attachment, 'getVoters') && $attachment->getOwner()->getId() != $this->getUser()->getId())) { + $final_attachments[] = $attachment; + } + } - 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()) - $comment->attach($attach); - else - $this->fail(52, "One of the attachments is invalid"); + if(sizeof($final_attachments) > 0) { + $comment->unwire(); + foreach($final_attachments as $attachment) { + $comment->attach($attachment); } } diff --git a/Web/Models/Repositories/Videos.php b/Web/Models/Repositories/Videos.php index e91c226d..e78a1b0f 100644 --- a/Web/Models/Repositories/Videos.php +++ b/Web/Models/Repositories/Videos.php @@ -39,6 +39,13 @@ class Videos $perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; foreach($this->videos->where("owner", $user->getId())->where(["deleted" => 0, "unlisted" => 0])->page($page, $perPage)->order("created DESC") as $video) yield new Video($video); + } + + function getByUserLimit(User $user, int $offset = 0, int $limit = 10): \Traversable + { + $perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; + foreach($this->videos->where("owner", $user->getId())->where(["deleted" => 0, "unlisted" => 0])->limit($limit, $offset)->order("created DESC") as $video) + yield new Video($video); } function getUserVideosCount(User $user): int diff --git a/Web/Presenters/CommentPresenter.php b/Web/Presenters/CommentPresenter.php index 741a03e6..7ab74ac0 100644 --- a/Web/Presenters/CommentPresenter.php +++ b/Web/Presenters/CommentPresenter.php @@ -79,15 +79,15 @@ final class CommentPresenter extends OpenVKPresenter $horizontal_attachments = []; $vertical_attachments = []; if(!empty($this->postParam("horizontal_attachments"))) { - $horizontal_attachments_array = explode(",", $this->postParam("horizontal_attachments")); - if(sizeof($horizontal_attachments_array) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]) { + $horizontal_attachments_array = array_slice(explode(",", $this->postParam("horizontal_attachments")), 0, OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]); + if(sizeof($horizontal_attachments_array) > 0) { $horizontal_attachments = parseAttachments($horizontal_attachments_array, ['photo', 'video']); } } if(!empty($this->postParam("vertical_attachments"))) { - $vertical_attachments_array = explode(",", $this->postParam("vertical_attachments")); - if(sizeof($vertical_attachments_array) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]) { + $vertical_attachments_array = array_slice(explode(",", $this->postParam("vertical_attachments")), 0, OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]); + if(sizeof($vertical_attachments_array) > 0) { $vertical_attachments = parseAttachments($vertical_attachments_array, ['audio', 'note']); } } diff --git a/Web/Presenters/WallPresenter.php b/Web/Presenters/WallPresenter.php index 8a68d427..13df558d 100644 --- a/Web/Presenters/WallPresenter.php +++ b/Web/Presenters/WallPresenter.php @@ -275,15 +275,15 @@ final class WallPresenter extends OpenVKPresenter $horizontal_attachments = []; $vertical_attachments = []; if(!empty($this->postParam("horizontal_attachments"))) { - $horizontal_attachments_array = explode(",", $this->postParam("horizontal_attachments")); - if(sizeof($horizontal_attachments_array) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]) { + $horizontal_attachments_array = array_slice(explode(",", $this->postParam("horizontal_attachments")), 0, OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]); + if(sizeof($horizontal_attachments_array) > 0) { $horizontal_attachments = parseAttachments($horizontal_attachments_array, ['photo', 'video']); } } if(!empty($this->postParam("vertical_attachments"))) { - $vertical_attachments_array = explode(",", $this->postParam("vertical_attachments")); - if(sizeof($vertical_attachments_array) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]) { + $vertical_attachments_array = array_slice(explode(",", $this->postParam("vertical_attachments")), 0, OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxAttachments"]); + if(sizeof($vertical_attachments_array) > 0) { $vertical_attachments = parseAttachments($vertical_attachments_array, ['audio', 'note']); } } diff --git a/Web/Presenters/templates/Audio/player.xml b/Web/Presenters/templates/Audio/player.xml index de8f03d7..d94341d2 100644 --- a/Web/Presenters/templates/Audio/player.xml +++ b/Web/Presenters/templates/Audio/player.xml @@ -5,17 +5,17 @@