From 371f8d71105390a81d16322a90118d47c3012c45 Mon Sep 17 00:00:00 2001 From: Celestora Date: Wed, 23 Mar 2022 22:44:54 +0200 Subject: [PATCH] Fix behabiour of album-related methods Generators f***** me in le a** --- VKAPI/Handlers/Audio.php | 19 ++++++++++++------- Web/Models/Entities/Playlist.php | 12 ++++++++---- bootstrap.php | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/VKAPI/Handlers/Audio.php b/VKAPI/Handlers/Audio.php index a596a466..59b0e7e0 100644 --- a/VKAPI/Handlers/Audio.php +++ b/VKAPI/Handlers/Audio.php @@ -228,6 +228,7 @@ final class Audio extends VKAPIRequestHandler $songs = []; $list = $album->getAudios($offset, $count, $shuffleSeed); + foreach($list as $song) $songs[] = $this->toSafeAudioStruct($song, $hash, $need_user == 1); @@ -545,12 +546,12 @@ final class Audio extends VKAPIRequestHandler return $this->getById($vid, $hash)->items[0]; } - function getAlbums(?int $owner_id = NULL, int $offset = 0, int $count = 50, int $drop_private = 1): object + function getAlbums(int $owner_id = 0, int $offset = 0, int $count = 50, int $drop_private = 1): object { $this->requireUser(); - $owner_id ??= $this->getUser()->getId(); - $playlists = []; + $owner_id = $owner_id == 0 ? $this->getUser()->getId() : $owner_id; + $playlists = []; foreach((new Audios)->getPlaylistsByEntityId($owner_id, $offset, $count) as $playlist) { if(!$playlist->canBeViewedBy($this->getUser())) { if($drop_private == 1) @@ -560,7 +561,7 @@ final class Audio extends VKAPIRequestHandler continue; } - $playlists[] = $playlist->toVkApiStructure($this->getUser()); + $playlists[] = $playlist->toVkApiStruct($this->getUser()); } return (object) [ @@ -599,7 +600,7 @@ final class Audio extends VKAPIRequestHandler $group = NULL; if($group_id != 0) { $group = (new Clubs)->get($group_id); - if(!$group_id) + if(!$group) $this->fail(0404, "Invalid group_id"); else if(!$group->canBeModifiedBy($this->getUser())) $this->fail(600, "Insufficient rights to this group"); @@ -616,6 +617,10 @@ final class Audio extends VKAPIRequestHandler $album->setDescription($description); $album->save(); + if(!is_null($group)) + $album->bookmark($group); + else + $album->bookmark($this->getUser()); return $album->getId(); } @@ -673,7 +678,7 @@ final class Audio extends VKAPIRequestHandler $audio = $this->audioFromAnyId($audio_id); if(!$audio) continue; - else if($audio->canBeViewedBy($this->getUser())) + else if(!$audio->canBeViewedBy($this->getUser())) continue; $audios[] = $audio; @@ -685,7 +690,7 @@ final class Audio extends VKAPIRequestHandler $res = 1; try { foreach ($audios as $audio) - $res = min($res, $album->add($audio)); + $res = min($res, (int) $album->add($audio)); } catch(\OutOfBoundsException $ex) { return 0; } diff --git a/Web/Models/Entities/Playlist.php b/Web/Models/Entities/Playlist.php index 95025fef..e79f8ab6 100644 --- a/Web/Models/Entities/Playlist.php +++ b/Web/Models/Entities/Playlist.php @@ -41,8 +41,12 @@ class Playlist extends MediaCollection function getAudios(int $offset = 0, ?int $limit = NULL, ?int $shuffleSeed = NULL): \Traversable { - if(!$shuffleSeed) - return $this->fetchClassic($offset, $limit); + if(!$shuffleSeed) { + foreach ($this->fetchClassic($offset, $limit) as $e) + yield $e; # No, I can't return, it will break with [] + + return; + } $ids = []; foreach($this->relations->select("media AS i")->where("collection", $this->getId()) as $rel) @@ -95,7 +99,7 @@ class Playlist extends MediaCollection if($entity instanceof Club) $id *= -1; - if($this->importTable->where("entity", $id)->count > self::MAX_COUNT) + if($this->importTable->where("entity", $id)->count() > self::MAX_COUNT) throw new \OutOfBoundsException("Maximum amount of playlists"); $this->importTable->insert([ @@ -129,7 +133,7 @@ class Playlist extends MediaCollection return (object) [ "id" => $this->getId(), "owner_id" => $oid, - "title" => $this->getTitle(), + "title" => $this->getName(), "description" => $this->getDescription(), "size" => $this->size(), "length" => $this->getLength(), diff --git a/bootstrap.php b/bootstrap.php index 8adcbb94..eec8aca3 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -62,7 +62,7 @@ function ovk_proc_strtr(string $string, int $length = 0): string return $newString . ($string !== $newString ? "…" : ""); #if cut hasn't happened, don't append "..." } -function knuth_shuffle(Traversable $arr, int $seed): array +function knuth_shuffle(iterable $arr, int $seed): array { $data = is_array($arr) ? $arr : iterator_to_array($arr); $retVal = [];