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 = []; $songs = [];
$list = $album->getAudios($offset, $count, $shuffleSeed); $list = $album->getAudios($offset, $count, $shuffleSeed);
foreach($list as $song) foreach($list as $song)
$songs[] = $this->toSafeAudioStruct($song, $hash, $need_user == 1); $songs[] = $this->toSafeAudioStruct($song, $hash, $need_user == 1);
@ -545,12 +546,12 @@ final class Audio extends VKAPIRequestHandler
return $this->getById($vid, $hash)->items[0]; 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(); $this->requireUser();
$owner_id ??= $this->getUser()->getId(); $owner_id = $owner_id == 0 ? $this->getUser()->getId() : $owner_id;
$playlists = []; $playlists = [];
foreach((new Audios)->getPlaylistsByEntityId($owner_id, $offset, $count) as $playlist) { foreach((new Audios)->getPlaylistsByEntityId($owner_id, $offset, $count) as $playlist) {
if(!$playlist->canBeViewedBy($this->getUser())) { if(!$playlist->canBeViewedBy($this->getUser())) {
if($drop_private == 1) if($drop_private == 1)
@ -560,7 +561,7 @@ final class Audio extends VKAPIRequestHandler
continue; continue;
} }
$playlists[] = $playlist->toVkApiStructure($this->getUser()); $playlists[] = $playlist->toVkApiStruct($this->getUser());
} }
return (object) [ return (object) [
@ -599,7 +600,7 @@ final class Audio extends VKAPIRequestHandler
$group = NULL; $group = NULL;
if($group_id != 0) { if($group_id != 0) {
$group = (new Clubs)->get($group_id); $group = (new Clubs)->get($group_id);
if(!$group_id) if(!$group)
$this->fail(0404, "Invalid group_id"); $this->fail(0404, "Invalid group_id");
else if(!$group->canBeModifiedBy($this->getUser())) else if(!$group->canBeModifiedBy($this->getUser()))
$this->fail(600, "Insufficient rights to this group"); $this->fail(600, "Insufficient rights to this group");
@ -616,6 +617,10 @@ final class Audio extends VKAPIRequestHandler
$album->setDescription($description); $album->setDescription($description);
$album->save(); $album->save();
if(!is_null($group))
$album->bookmark($group);
else
$album->bookmark($this->getUser());
return $album->getId(); return $album->getId();
} }
@ -673,7 +678,7 @@ final class Audio extends VKAPIRequestHandler
$audio = $this->audioFromAnyId($audio_id); $audio = $this->audioFromAnyId($audio_id);
if(!$audio) if(!$audio)
continue; continue;
else if($audio->canBeViewedBy($this->getUser())) else if(!$audio->canBeViewedBy($this->getUser()))
continue; continue;
$audios[] = $audio; $audios[] = $audio;
@ -685,7 +690,7 @@ final class Audio extends VKAPIRequestHandler
$res = 1; $res = 1;
try { try {
foreach ($audios as $audio) foreach ($audios as $audio)
$res = min($res, $album->add($audio)); $res = min($res, (int) $album->add($audio));
} catch(\OutOfBoundsException $ex) { } catch(\OutOfBoundsException $ex) {
return 0; return 0;
} }

View file

@ -41,8 +41,12 @@ class Playlist extends MediaCollection
function getAudios(int $offset = 0, ?int $limit = NULL, ?int $shuffleSeed = NULL): \Traversable function getAudios(int $offset = 0, ?int $limit = NULL, ?int $shuffleSeed = NULL): \Traversable
{ {
if(!$shuffleSeed) if(!$shuffleSeed) {
return $this->fetchClassic($offset, $limit); foreach ($this->fetchClassic($offset, $limit) as $e)
yield $e; # No, I can't return, it will break with []
return;
}
$ids = []; $ids = [];
foreach($this->relations->select("media AS i")->where("collection", $this->getId()) as $rel) 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) if($entity instanceof Club)
$id *= -1; $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"); throw new \OutOfBoundsException("Maximum amount of playlists");
$this->importTable->insert([ $this->importTable->insert([
@ -129,7 +133,7 @@ class Playlist extends MediaCollection
return (object) [ return (object) [
"id" => $this->getId(), "id" => $this->getId(),
"owner_id" => $oid, "owner_id" => $oid,
"title" => $this->getTitle(), "title" => $this->getName(),
"description" => $this->getDescription(), "description" => $this->getDescription(),
"size" => $this->size(), "size" => $this->size(),
"length" => $this->getLength(), "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 "..." 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); $data = is_array($arr) ? $arr : iterator_to_array($arr);
$retVal = []; $retVal = [];