Add playlist model

This commit is contained in:
Celestora 2022-03-23 20:27:12 +02:00
parent 7aef23b3cd
commit e1de07284e
6 changed files with 257 additions and 16 deletions

View file

@ -320,7 +320,7 @@ final class Audio extends VKAPIRequestHandler
$this->requireUser(); $this->requireUser();
[$owner, $aid] = explode("_", $audio); [$owner, $aid] = explode("_", $audio);
$song = (new Audios)->getByOwnerAndVID($owner, $aid); $song = (new Audios)->getByOwnerAndVID((int) $owner, (int) $aid);
$ids = []; $ids = [];
foreach(explode(",", $target_ids) as $id) { foreach(explode(",", $target_ids) as $id) {
$id = (int) $id; $id = (int) $id;

View file

@ -444,6 +444,8 @@ class Audio extends Media
->delete(); ->delete();
$ctx->table("audio_listens")->where("audio", $this->getId()) $ctx->table("audio_listens")->where("audio", $this->getId())
->delete(); ->delete();
$ctx->table("playlist_relations")->where("media", $this->getId())
->delete();
parent::delete($softly); parent::delete($softly);
} }

View file

@ -17,7 +17,17 @@ abstract class MediaCollection extends RowModel
protected $specialNames = []; protected $specialNames = [];
private $relations; protected $relations;
/**
* Maximum amount of items Collection can have
*/
const MAX_ITEMS = INF;
/**
* Maximum amount of Collections with same "owner" allowed
*/
const MAX_COUNT = INF;
function __construct(?ActiveRow $ar = NULL) function __construct(?ActiveRow $ar = NULL)
{ {
@ -71,9 +81,12 @@ abstract class MediaCollection extends RowModel
abstract function getCoverURL(): ?string; abstract function getCoverURL(): ?string;
function fetch(int $page = 1, ?int $perPage = NULL): \Traversable function fetchClassic(int $offset = 0, ?int $limit = NULL): \Traversable
{ {
$related = $this->getRecord()->related("$this->relTableName.collection")->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE)->order("media ASC"); $related = $this->getRecord()->related("$this->relTableName.collection")
->limit($limit ?? OPENVK_DEFAULT_PER_PAGE, $offset)
->order("media ASC");
foreach($related as $rel) { foreach($related as $rel) {
$media = $rel->ref($this->entityTableName, "media"); $media = $rel->ref($this->entityTableName, "media");
if(!$media) if(!$media)
@ -83,6 +96,14 @@ abstract class MediaCollection extends RowModel
} }
} }
function fetch(int $page = 1, ?int $perPage = NULL): \Traversable
{
$page = max(1, $page);
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
return $this->fetchClassic($perPage * ($page - 1), $perPage);
}
function size(): int function size(): int
{ {
return sizeof($this->getRecord()->related("$this->relTableName.collection")); return sizeof($this->getRecord()->related("$this->relTableName.collection"));
@ -119,6 +140,10 @@ abstract class MediaCollection extends RowModel
if($this->has($entity)) if($this->has($entity))
return false; return false;
if(self::MAX_ITEMS != INF)
if(sizeof($this->relations->where("collection", $this->getId())) > self::MAX_ITEMS)
throw new \OutOfBoundsException("Collection is full");
$this->relations->insert([ $this->relations->insert([
"collection" => $this->getId(), "collection" => $this->getId(),
"media" => $entity->getId(), "media" => $entity->getId(),
@ -127,14 +152,14 @@ abstract class MediaCollection extends RowModel
return true; return true;
} }
function remove(RowModel $entity): void function remove(RowModel $entity): bool
{ {
$this->entitySuitable($entity); $this->entitySuitable($entity);
$this->relations->where([ return $this->relations->where([
"collection" => $this->getId(), "collection" => $this->getId(),
"media" => $entity->getId(), "media" => $entity->getId(),
])->delete(); ])->delete() > 0;
} }
function has(RowModel $entity): bool function has(RowModel $entity): bool
@ -149,5 +174,32 @@ abstract class MediaCollection extends RowModel
return !is_null($rel); return !is_null($rel);
} }
function save(): void
{
$thisTable = DatabaseConnection::i()->getContext()->table($this->tableName);
if(self::MAX_COUNT != INF)
if(isset($this->changes["owner"]))
if(sizeof($thisTable->where("owner", $this->changes["owner"])) > self::MAX_COUNT)
throw new \OutOfBoundsException("Maximum amount of collections");
if(is_null($this->getRecord()))
if(!isset($this->changes["created"]))
$this->stateChanges("created", time());
else
$this->stateChanges("edited", time());
parent::save();
}
function delete(bool $softly = true): void
{
if(!$softly) {
$this->relations->where("collection", $this->getId())
->delete();
}
parent::delete($softly);
}
use Traits\TOwnable; use Traits\TOwnable;
} }

View file

@ -0,0 +1,130 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection;
use Nette\Database\Table\ActiveRow;
use openvk\Web\Models\Repositories\Audios;
use openvk\Web\Models\RowModel;
/**
* @method setName(string $name)
* @method setDescription(?string $desc)
*/
class Playlist extends MediaCollection
{
protected $tableName = "playlists";
protected $relTableName = "playlist_relations";
protected $entityTableName = "audios";
protected $entityClassName = 'openvk\Web\Models\Entities\Audio';
private $importTable;
const MAX_COUNT = 1000;
const MAX_ITEMS = 10000;
function __construct(?ActiveRow $ar = NULL)
{
parent::__construct($ar);
$this->importTable = DatabaseConnection::i()->getContext()->table("playlist_imports");
}
function getCoverURL(): ?string
{
return NULL;
}
function getAudios(int $offset = 0, ?int $limit = NULL, ?int $shuffleSeed = NULL): \Traversable
{
if(!$shuffleSeed)
return $this->fetchClassic($offset, $limit);
$ids = [];
foreach($this->relations->select("media AS i")->where("collection", $this->getId()) as $rel)
$ids[] = $rel->i;
$ids = knuth_shuffle($ids, $shuffleSeed);
$ids = array_slice($ids, $offset, $limit ?? OPENVK_DEFAULT_PER_PAGE);
foreach($ids as $id)
yield (new Audios)->get($id);
}
function add(RowModel $audio): bool
{
if($res = parent::add($audio)) {
$this->stateChanges("length", $this->getRecord()->length + $audio->getLength());
$this->save();
}
return $res;
}
function remove(RowModel $audio): bool
{
if($res = parent::remove($audio)) {
$this->stateChanges("length", $this->getRecord()->length - $audio->getLength());
$this->save();
}
return $res;
}
function isBookmarkedBy(RowModel $entity): bool
{
$id = $entity->getId();
if($entity instanceof Club)
$id *= -1;
return !is_null($this->importTable->where([
"entity" => $id,
"playlist" => $this->getId(),
])->fetch());
}
function bookmark(RowModel $entity): bool
{
if($this->isBookmarkedBy($entity))
return false;
$id = $entity->getId();
if($entity instanceof Club)
$id *= -1;
if($this->importTable->where("entity", $id)->count > self::MAX_COUNT)
throw new \OutOfBoundsException("Maximum amount of playlists");
$this->importTable->insert([
"entity" => $id,
"playlist" => $this->getId(),
]);
return true;
}
function unbookmark(RowModel $entity): bool
{
$id = $entity->getId();
if($entity instanceof Club)
$id *= -1;
$count = $this->importTable->where([
"entity" => $id,
"playlist" => $this->getId(),
])->delete();
return $count > 0;
}
function setLength(): void
{
throw new \LogicException("Can't set length of playlist manually");
}
function delete(bool $softly = true): void
{
$ctx = DatabaseConnection::i()->getContext();
$ctx->table("playlist_imports")->where("playlist", $this->getId())
->delete();
parent::delete($softly);
}
}

View file

@ -3,6 +3,7 @@ namespace openvk\Web\Models\Repositories;
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Entities\Audio; use openvk\Web\Models\Entities\Audio;
use openvk\Web\Models\Entities\Club; use openvk\Web\Models\Entities\Club;
use openvk\Web\Models\Entities\Playlist;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Util\EntityStream; use openvk\Web\Models\Repositories\Util\EntityStream;
@ -11,6 +12,8 @@ class Audios
private $context; private $context;
private $audios; private $audios;
private $rels; private $rels;
private $playlists;
private $playlistImports;
const ORDER_NEW = 0; const ORDER_NEW = 0;
const ORDER_POPULAR = 1; const ORDER_POPULAR = 1;
@ -24,6 +27,9 @@ class Audios
$this->context = DatabaseConnection::i()->getContext(); $this->context = DatabaseConnection::i()->getContext();
$this->audios = $this->context->table("audios"); $this->audios = $this->context->table("audios");
$this->rels = $this->context->table("audio_relations"); $this->rels = $this->context->table("audio_relations");
$this->playlists = $this->context->table("playlists");
$this->playlistImports = $this->context->table("playlist_imports");
} }
function get(int $id): ?Audio function get(int $id): ?Audio
@ -35,6 +41,15 @@ class Audios
return new Audio($audio); return new Audio($audio);
} }
private function getPlaylist(int $id): ?Playlist
{
$playlist = $this->playlists->get($id);
if(!$playlist)
return NULL;
return new Playlist($playlist);
}
function getByOwnerAndVID(int $owner, int $vId): ?Audio function getByOwnerAndVID(int $owner, int $vId): ?Audio
{ {
$audio = $this->audios->where([ $audio = $this->audios->where([
@ -48,8 +63,8 @@ class Audios
function getByEntityID(int $entity, int $offset = 0, ?int $limit = NULL, ?int& $deleted = nullptr): \Traversable function getByEntityID(int $entity, int $offset = 0, ?int $limit = NULL, ?int& $deleted = nullptr): \Traversable
{ {
$perPage ??= OPENVK_DEFAULT_PER_PAGE; $limit ??= OPENVK_DEFAULT_PER_PAGE;
$iter = $this->rels->where("entity", $entity)->limit($limit ?? OPENVK_DEFAULT_PER_PAGE, $offset); $iter = $this->rels->where("entity", $entity)->limit($limit, $offset);
foreach($iter as $rel) { foreach($iter as $rel) {
$audio = $this->get($rel->audio); $audio = $this->get($rel->audio);
if(!$audio || $audio->isDeleted()) { if(!$audio || $audio->isDeleted()) {
@ -61,6 +76,21 @@ class Audios
} }
} }
function getPlaylistsByEntityId(int $entity, int $offset = 0, ?int $limit = NULL, ?int& $deleted = nullptr): \Traversable
{
$limit ??= OPENVK_DEFAULT_PER_PAGE;
$iter = $this->playlistImports->where("entity", $entity)->limit($limit, $offset);
foreach($iter as $rel) {
$playlist = $this->getPlaylist($rel->playlist);
if(!$playlist || $playlist->isDeleted()) {
$deleted++;
continue;
}
yield $playlist;
}
}
function getByUser(User $user, int $page = 1, ?int $perPage = NULL, ?int& $deleted = nullptr): \Traversable function getByUser(User $user, int $page = 1, ?int $perPage = NULL, ?int& $deleted = nullptr): \Traversable
{ {
return $this->getByEntityID($user->getId(), ($perPage * ($page - 1)), $perPage, $deleted); return $this->getByEntityID($user->getId(), ($perPage * ($page - 1)), $perPage, $deleted);
@ -71,6 +101,16 @@ class Audios
return $this->getByEntityID($club->getId() * -1, ($perPage * ($page - 1)), $perPage, $deleted); return $this->getByEntityID($club->getId() * -1, ($perPage * ($page - 1)), $perPage, $deleted);
} }
function getPlaylistsByUser(User $user, int $page = 1, ?int $perPage = NULL, ?int& $deleted = nullptr): \Traversable
{
return $this->getPlaylistsByEntityId($user->getId(), ($perPage * ($page - 1)), $perPage, $deleted);
}
function getPlaylistsByClub(Club $club, int $page = 1, ?int $perPage = NULL, ?int& $deleted = nullptr): \Traversable
{
return $this->getPlaylistsByEntityId($club->getId() * -1, ($perPage * ($page - 1)), $perPage, $deleted);
}
function getUserCollectionSize(User $user): int function getUserCollectionSize(User $user): int
{ {
return sizeof($this->rels->where("entity", $user->getId())); return sizeof($this->rels->where("entity", $user->getId()));
@ -81,6 +121,16 @@ class Audios
return sizeof($this->rels->where("entity", $club->getId() * -1)); return sizeof($this->rels->where("entity", $club->getId() * -1));
} }
function getUserPlaylistsCount(User $user): int
{
return sizeof($this->playlistImports->where("entity", $user->getId()));
}
function getClubPlaylistsCount(Club $club): int
{
return sizeof($this->playlistImports->where("entity", $club->getId() * -1));
}
function getByUploader(User $user): EntityStream function getByUploader(User $user): EntityStream
{ {
$search = $this->audios->where([ $search = $this->audios->where([
@ -120,4 +170,13 @@ class Audios
return new EntityStream("Audio", $search); return new EntityStream("Audio", $search);
} }
function searchPlaylists(string $query): EntityStream
{
$search = $this->audios->where([
"deleted" => 0,
])->where("MATCH (title, description) AGAINST (? IN BOOLEAN MODE)", $query);
return new EntityStream("Playlist", $search);
}
} }

View file

@ -1,8 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace openvk\Web\Presenters; namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\Audio; use openvk\Web\Models\Entities\Audio;
use openvk\Web\Models\Entities\Club;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Audios; use openvk\Web\Models\Repositories\Audios;
use openvk\Web\Models\Repositories\Clubs; use openvk\Web\Models\Repositories\Clubs;
use openvk\Web\Models\Repositories\Users; use openvk\Web\Models\Repositories\Users;