Fix behabiour of album-related methods

Generators f***** me in le a**
This commit is contained in:
Celestora 2022-03-23 22:44:54 +02:00
parent 13a750f296
commit 371f8d7110
3 changed files with 21 additions and 12 deletions

View file

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

View file

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

View file

@ -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 = [];