From 4e5bd6433f1f964eab4a79bbfc4915ef13489f56 Mon Sep 17 00:00:00 2001 From: mrilyew <99399973+mrilyew@users.noreply.github.com> Date: Sat, 19 Oct 2024 20:58:27 +0300 Subject: [PATCH] search: a bit refactor 2 --- Web/Models/Entities/Playlist.php | 6 ++ Web/Models/Entities/Traits/TRichText.php | 2 +- Web/Models/Repositories/Audios.php | 24 ++++++- Web/Presenters/AudioPresenter.php | 14 +++- Web/Presenters/SearchPresenter.php | 14 +++- Web/Presenters/templates/@layout.xml | 2 +- .../templates/Audio/EditPlaylist.xml | 11 ++- Web/Presenters/templates/Audio/List.xml | 23 ++----- .../templates/Audio/NewPlaylist.xml | 10 ++- Web/Presenters/templates/Audio/Playlist.xml | 31 +++++---- .../templates/Audio/playlistListView.xml | 20 ++++++ Web/Presenters/templates/Audio/tabs.xml | 2 +- Web/Presenters/templates/Search/Index.xml | 8 ++- Web/static/css/audios.css | 67 ++++++++++++------- Web/static/css/main.css | 1 + Web/static/js/al_music.js | 17 ++++- install/sqls/00047-unlisted-playlists.sql | 1 + locales/en.strings | 1 + locales/ru.strings | 2 + 19 files changed, 182 insertions(+), 74 deletions(-) create mode 100644 Web/Presenters/templates/Audio/playlistListView.xml create mode 100644 install/sqls/00047-unlisted-playlists.sql diff --git a/Web/Models/Entities/Playlist.php b/Web/Models/Entities/Playlist.php index 1e9ea6c3..29a6aa75 100644 --- a/Web/Models/Entities/Playlist.php +++ b/Web/Models/Entities/Playlist.php @@ -162,6 +162,7 @@ class Playlist extends MediaCollection "bookmarked" => $this->isBookmarkedBy($user), "listens" => $this->getListens(), "cover_url" => $this->getCoverURL(), + "searchable" => !$this->isUnlisted(), ]; } @@ -258,4 +259,9 @@ class Playlist extends MediaCollection return implode(" • ", $props); } + + function isUnlisted(): bool + { + return (bool)$this->getRecord()->unlisted; + } } diff --git a/Web/Models/Entities/Traits/TRichText.php b/Web/Models/Entities/Traits/TRichText.php index dc78a034..0f02a8ab 100644 --- a/Web/Models/Entities/Traits/TRichText.php +++ b/Web/Models/Entities/Traits/TRichText.php @@ -123,7 +123,7 @@ trait TRichText $text = preg_replace_callback("%([\n\r\s]|^)(\#([\p{L}_0-9][\p{L}_0-9\(\)\-\']+[\p{L}_0-9\(\)]|[\p{L}_0-9]{1,2}))%Xu", function($m) { $slug = rawurlencode($m[3]); - return "$m[1]$m[2]"; + return "$m[1]$m[2]"; }, $text); $text = $this->formatEmojis($text); diff --git a/Web/Models/Repositories/Audios.php b/Web/Models/Repositories/Audios.php index 5843b2b2..22e7ac1e 100644 --- a/Web/Models/Repositories/Audios.php +++ b/Web/Models/Repositories/Audios.php @@ -219,6 +219,7 @@ class Audios function searchPlaylists(string $query): EntityStream { $search = $this->playlists->where([ + "unlisted" => 0, "deleted" => 0, ])->where("MATCH (`name`, `description`) AGAINST (? IN BOOLEAN MODE)", $query); @@ -296,9 +297,28 @@ class Audios return new Util\EntityStream("Audio", $result); } - function findPlaylists(string $query, int $page = 1, ?int $perPage = NULL): \Traversable + function findPlaylists(string $query, array $params = [], array $order = ['type' => 'id', 'invert' => false]): \Traversable { - $result = $this->playlists->where("name LIKE ?", "%$query%"); + $result = $this->playlists->where([ + "unlisted" => 0, + "deleted" => 0, + ])->where("CONCAT_WS(' ', name, description) LIKE ?", "%$query%"); + $order_str = 'id'; + + switch($order['type']) { + case 'id': + $order_str = 'id ' . ($order['invert'] ? 'ASC' : 'DESC'); + break; + case 'length': + $order_str = 'length ' . ($order['invert'] ? 'ASC' : 'DESC'); + break; + case 'listens': + $order_str = 'listens ' . ($order['invert'] ? 'ASC' : 'DESC'); + break; + } + + if($order_str) + $result->order($order_str); return new Util\EntityStream("Playlist", $result); } diff --git a/Web/Presenters/AudioPresenter.php b/Web/Presenters/AudioPresenter.php index 78d3ab52..83a504bd 100644 --- a/Web/Presenters/AudioPresenter.php +++ b/Web/Presenters/AudioPresenter.php @@ -75,7 +75,7 @@ final class AudioPresenter extends OpenVKPresenter if (!$entity || $entity->isBanned()) $this->redirect("/playlists" . $this->user->id); - $playlists = $this->audios->getPlaylistsByClub($entity, $page, 10); + $playlists = $this->audios->getPlaylistsByClub($entity, $page, OPENVK_DEFAULT_PER_PAGE); $playlistsCount = $this->audios->getClubPlaylistsCount($entity); } else { $entity = (new Users)->get($owner); @@ -85,7 +85,7 @@ final class AudioPresenter extends OpenVKPresenter if(!$entity->getPrivacyPermission("audios.read", $this->user->identity)) $this->flashFail("err", tr("forbidden"), tr("forbidden_comment")); - $playlists = $this->audios->getPlaylistsByUser($entity, $page, 9); + $playlists = $this->audios->getPlaylistsByUser($entity, $page, OPENVK_DEFAULT_PER_PAGE); $playlistsCount = $this->audios->getUserPlaylistsCount($entity); } @@ -304,6 +304,8 @@ final class AudioPresenter extends OpenVKPresenter if ($_SERVER["REQUEST_METHOD"] === "POST") { $title = $this->postParam("title"); $description = $this->postParam("description"); + $is_unlisted = (int)$this->postParam('is_unlisted'); + $audios = !empty($this->postParam("audios")) ? array_slice(explode(",", $this->postParam("audios")), 0, 1000) : []; if(empty($title) || iconv_strlen($title) < 1) @@ -313,7 +315,9 @@ final class AudioPresenter extends OpenVKPresenter $playlist->setOwner($owner); $playlist->setName(substr($title, 0, 125)); $playlist->setDescription(substr($description, 0, 2045)); - + if($is_unlisted == 1) + $playlist->setUnlisted(true); + if($_FILES["cover"]["error"] === UPLOAD_ERR_OK) { if(!str_starts_with($_FILES["cover"]["type"], "image")) $this->flashFail("err", tr("error"), tr("not_a_photo")); @@ -427,6 +431,7 @@ final class AudioPresenter extends OpenVKPresenter $title = $this->postParam("title"); $description = $this->postParam("description"); + $is_unlisted = (int)$this->postParam('is_unlisted'); $new_audios = !empty($this->postParam("audios")) ? explode(",", rtrim($this->postParam("audios"), ",")) : []; if(empty($title) || iconv_strlen($title) < 1) @@ -436,6 +441,7 @@ final class AudioPresenter extends OpenVKPresenter $playlist->setDescription(ovk_proc_strtr($description, 2045)); $playlist->setEdited(time()); $playlist->resetLength(); + $playlist->setUnlisted((bool)$is_unlisted); if($_FILES["new_cover"]["error"] === UPLOAD_ERR_OK) { if(!str_starts_with($_FILES["new_cover"]["type"], "image")) @@ -476,12 +482,14 @@ final class AudioPresenter extends OpenVKPresenter $this->template->playlist = $playlist; $this->template->page = $page; $this->template->cover = $playlist->getCoverPhoto(); + $this->template->cover_url = $this->template->cover ? $this->template->cover->getURL() : "/assets/packages/static/openvk/img/song.jpg"; $this->template->audios = iterator_to_array($playlist->fetch($page, 10)); $this->template->ownerId = $owner_id; $this->template->owner = $playlist->getOwner(); $this->template->isBookmarked = $this->user->identity && $playlist->isBookmarkedBy($this->user->identity); $this->template->isMy = $this->user->identity && $playlist->getOwner()->getId() === $this->user->id; $this->template->canEdit = $this->user->identity && $playlist->canBeModifiedBy($this->user->identity); + $this->template->count = $playlist->size(); } function renderAction(int $audio_id): void diff --git a/Web/Presenters/SearchPresenter.php b/Web/Presenters/SearchPresenter.php index 0e23ac57..d7104f49 100644 --- a/Web/Presenters/SearchPresenter.php +++ b/Web/Presenters/SearchPresenter.php @@ -48,7 +48,8 @@ final class SearchPresenter extends OpenVKPresenter "comments" => "comments", "videos" => "videos", "audios" => "audios", - "apps" => "apps" + "apps" => "apps", + "audios_playlists" => "audios" ]; $parameters = [ "ignore_private" => true, @@ -92,7 +93,16 @@ final class SearchPresenter extends OpenVKPresenter $repo = $repos[$section] or $this->throwError(400, "Bad Request", "Invalid search entity $section."); - $results = $this->{$repo}->find($query, $parameters, ['type' => $order, 'invert' => $invert]); + $results = NULL; + switch($section) { + default: + $results = $this->{$repo}->find($query, $parameters, ['type' => $order, 'invert' => $invert]); + break; + case 'audios_playlists': + $results = $this->{$repo}->findPlaylists($query, $parameters, ['type' => $order, 'invert' => $invert]); + break; + } + $iterator = $results->page($page, OPENVK_DEFAULT_PER_PAGE); $count = $results->size(); diff --git a/Web/Presenters/templates/@layout.xml b/Web/Presenters/templates/@layout.xml index 09270ea9..c62a6e31 100644 --- a/Web/Presenters/templates/@layout.xml +++ b/Web/Presenters/templates/@layout.xml @@ -127,7 +127,7 @@ - +