Skip to content

Commit

Permalink
feat: ajax, audio refactor (#1164)
Browse files Browse the repository at this point in the history
* Make audio player pizdatey

* Simple ajax routing (scripts are broken)

* Fix most common script problems pt1

* Add ajax player

Осталось пофиксить скрипты и создание плейлистов! Ну и ещё некоторые хуйни по аудиозаписям которые я задумал.

* Add context menu for audios

* Refactor audio upload page

* Repair playlists

* Fix main problems

* Midnight teme adaptation

* Stupid bug fix

* Save audios list in da localstorage and fix msgbox

* Fix time setting

* add beforeUnload event

* Stupid bugs fix

* update page footer on transition

* fix wall publihing

* fix 500 on non existent page
  • Loading branch information
mrilyew authored Dec 7, 2024
1 parent c44a4ce commit 7d450c1
Show file tree
Hide file tree
Showing 53 changed files with 3,107 additions and 1,993 deletions.
5 changes: 5 additions & 0 deletions Web/Models/Entities/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ function getPerformer(): string
return $this->getRecord()->performer;
}

function getPerformers(): array
{
return explode(", ", $this->getRecord()->performer);
}

function getName(): string
{
return $this->getPerformer() . "" . $this->getTitle();
Expand Down
8 changes: 5 additions & 3 deletions Web/Models/Repositories/Audios.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,17 @@ function find(string $query, array $params = [], array $order = ['type' => 'id',
{
$query = "%$query%";
$result = $this->audios->where([
"unlisted" => 0,
"deleted" => 0,
"unlisted" => 0,
"deleted" => 0,
/*"withdrawn" => 0,
"processed" => 1,*/
]);
$order_str = (in_array($order['type'], ['id', 'length', 'listens']) ? $order['type'] : 'id') . ' ' . ($order['invert'] ? 'ASC' : 'DESC');;

if($params["only_performers"] == "1") {
$result->where("performer LIKE ?", $query);
} else {
$result->where("name LIKE ? OR performer LIKE ?", $query, $query);
$result->where("CONCAT_WS(' ', performer, name) LIKE ?", $query);
}

foreach($params as $paramName => $paramValue) {
Expand Down
112 changes: 67 additions & 45 deletions Web/Presenters/AudioPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ function renderList(?int $owner = NULL, ?string $mode = "list"): void
$this->template->club = $owner < 0 ? $entity : NULL;
$this->template->isMy = ($owner > 0 && ($entity->getId() === $this->user->id));
$this->template->isMyClub = ($owner < 0 && $entity->canBeModifiedBy($this->user->identity));
} else {
$audios = $this->audios->getPopular();
$audiosCount = $audios->size();
} else if ($mode === 'alone_audio') {
$audios = [$this->template->alone_audio];
$audiosCount = 1;

$this->template->owner = $this->user->identity;
$this->template->ownerId = $this->user->id;
}

// $this->renderApp("owner=$owner");
Expand Down Expand Up @@ -271,6 +274,19 @@ function renderUpload(): void
}
}

function renderAloneAudio(int $owner_id, int $audio_id): void
{
$this->assertUserLoggedIn();

$found_audio = $this->audios->get($audio_id);
if(!$found_audio || $found_audio->isDeleted() || !$found_audio->canBeViewedBy($this->user->identity)) {
$this->notFound();
}

$this->template->alone_audio = $found_audio;
$this->renderList(NULL, 'alone_audio');
}

function renderListen(int $id): void
{
if ($_SERVER["REQUEST_METHOD"] === "POST") {
Expand Down Expand Up @@ -327,17 +343,15 @@ function renderNewPlaylist(): void
$this->template->club = $club;
}

$this->template->owner = $owner;

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) : [];
$is_ajax = (int)$this->postParam('ajax') == 1;
$audios = array_slice(explode(",", $this->postParam("audios")), 0, 1000);

if(empty($title) || iconv_strlen($title) < 1)
$this->flashFail("err", tr("error"), tr("set_playlist_name"));
$this->flashFail("err", tr("error"), tr("set_playlist_name"), NULL, $is_ajax);

$playlist = new Playlist;
$playlist->setOwner($owner);
Expand All @@ -348,38 +362,35 @@ function renderNewPlaylist(): void

if($_FILES["cover"]["error"] === UPLOAD_ERR_OK) {
if(!str_starts_with($_FILES["cover"]["type"], "image"))
$this->flashFail("err", tr("error"), tr("not_a_photo"));
$this->flashFail("err", tr("error"), tr("not_a_photo"), NULL, $is_ajax);

try {
$playlist->fastMakeCover($this->user->id, $_FILES["cover"]);
} catch(\Throwable $e) {
$this->flashFail("err", tr("error"), tr("invalid_cover_photo"));
$this->flashFail("err", tr("error"), tr("invalid_cover_photo"), NULL, $is_ajax);
}
}

$playlist->save();

foreach($audios as $audio) {
$audio = $this->audios->get((int)$audio);

if(!$audio || $audio->isDeleted() || !$audio->canBeViewedBy($this->user->identity))
if(!$audio || $audio->isDeleted())
continue;

$playlist->add($audio);
}

$playlist->bookmark(isset($club) ? $club : $this->user->identity);
if($is_ajax) {
$this->returnJson([
'success' => true,
'redirect' => '/playlist' . $owner . "_" . $playlist->getId()
]);
}
$this->redirect("/playlist" . $owner . "_" . $playlist->getId());
} else {
if(isset($club)) {
$this->template->audios = iterator_to_array($this->audios->getByClub($club, 1, 10));
$count = (new Audios)->getClubCollectionSize($club);
} else {
$this->template->audios = iterator_to_array($this->audios->getByUser($this->user->identity, 1, 10));
$count = (new Audios)->getUserCollectionSize($this->user->identity);
}

$this->template->pagesCount = ceil($count / 10);
$this->template->owner = $owner;
}
}

Expand Down Expand Up @@ -435,28 +446,20 @@ function renderEditPlaylist(int $owner_id, int $virtual_id)
$this->willExecuteWriteAction();

$playlist = $this->audios->getPlaylistByOwnerAndVID($owner_id, $virtual_id);
$page = (int)($this->queryParam("p") ?? 1);
if (!$playlist || $playlist->isDeleted() || !$playlist->canBeModifiedBy($this->user->identity))
$this->notFound();

$this->template->playlist = $playlist;
$this->template->page = $page;

$audios = iterator_to_array($playlist->fetch(1, $playlist->size()));
$this->template->audios = array_slice($audios, 0, 10);
$audiosIds = [];

foreach($audios as $aud)
$audiosIds[] = $aud->getId();

$this->template->audiosIds = implode(",", array_unique($audiosIds)) . ",";
$this->template->audios = array_slice($audios, 0, 1000);
$this->template->ownerId = $owner_id;
$this->template->owner = $playlist->getOwner();
$this->template->pagesCount = $pagesCount = ceil($playlist->size() / 10);

if($_SERVER["REQUEST_METHOD"] !== "POST")
return;

$is_ajax = (int)$this->postParam('ajax') == 1;
$title = $this->postParam("title");
$description = $this->postParam("description");
$is_unlisted = (int)$this->postParam('is_unlisted');
Expand All @@ -471,12 +474,12 @@ function renderEditPlaylist(int $owner_id, int $virtual_id)
$playlist->resetLength();
$playlist->setUnlisted((bool)$is_unlisted);

if($_FILES["new_cover"]["error"] === UPLOAD_ERR_OK) {
if(!str_starts_with($_FILES["new_cover"]["type"], "image"))
if($_FILES["cover"]["error"] === UPLOAD_ERR_OK) {
if(!str_starts_with($_FILES["cover"]["type"], "image"))
$this->flashFail("err", tr("error"), tr("not_a_photo"));

try {
$playlist->fastMakeCover($this->user->id, $_FILES["new_cover"]);
$playlist->fastMakeCover($this->user->id, $_FILES["cover"]);
} catch(\Throwable $e) {
$this->flashFail("err", tr("error"), tr("invalid_cover_photo"));
}
Expand All @@ -490,13 +493,18 @@ function renderEditPlaylist(int $owner_id, int $virtual_id)

foreach ($new_audios as $new_audio) {
$audio = (new Audios)->get((int)$new_audio);

if(!$audio || $audio->isDeleted())
continue;

$playlist->add($audio);
}

if($is_ajax) {
$this->returnJson([
'success' => true,
'redirect' => '/playlist' . $playlist->getPrettyId()
]);
}
$this->redirect("/playlist".$playlist->getPrettyId());
}

Expand Down Expand Up @@ -762,6 +770,15 @@ function renderApiGetContext()
$audios = $stream->page($page, 10);
$audiosCount = $stream->size();
break;
case 'alone_audio':
$found_audio = $this->audios->get($ctx_id);
if(!$found_audio || $found_audio->isDeleted() || !$found_audio->canBeViewedBy($this->user->identity)) {
$this->flashFail("err", "Error", "Not found", 89, true);
}

$audios = [$found_audio];
$audiosCount = 1;
break;
}

$pagesCount = ceil($audiosCount / $perPage);
Expand All @@ -779,16 +796,21 @@ function renderApiGetContext()
$audiosArr = [];

foreach($audios as $audio) {
$audiosArr[] = [
"id" => $audio->getId(),
"name" => $audio->getTitle(),
"performer" => $audio->getPerformer(),
"keys" => $audio->getKeys(),
"url" => $audio->getUrl(),
"length" => $audio->getLength(),
"available" => $audio->isAvailable(),
"withdrawn" => $audio->isWithdrawn(),
];
$output_array = [];
$output_array['id'] = $audio->getId();
$output_array['name'] = $audio->getTitle();
$output_array['performer'] = $audio->getPerformer();

if(!$audio->isWithdrawn()) {
$output_array['keys'] = $audio->getKeys();
$output_array['url'] = $audio->getUrl();
}

$output_array['length'] = $audio->getLength();
$output_array['available'] = $audio->isAvailable();
$output_array['withdrawn'] = $audio->isWithdrawn();

$audiosArr[] = $output_array;
}

$resultArr = [
Expand Down
1 change: 1 addition & 0 deletions Web/Presenters/AuthPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ function renderFinishRestoringPassword(): void
return;
}

$this->template->disable_ajax = 1;
$this->template->is2faEnabled = $request->getUser()->is2faEnabled();

if($_SERVER["REQUEST_METHOD"] === "POST") {
Expand Down
1 change: 1 addition & 0 deletions Web/Presenters/MessengerPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function renderApp(int $sel): void
$this->flash("err", tr("warning"), tr("user_may_not_reply"));
}

$this->template->disable_ajax = 1;
$this->template->selId = $sel;
$this->template->correspondent = $correspondent;
}
Expand Down
2 changes: 2 additions & 0 deletions Web/Presenters/NoSpamPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function renderIndex(): void

if ($mode === "form") {
$this->template->_template = "NoSpam/Index";
$this->template->disable_ajax = 1;
$foundClasses = [];
foreach (Finder::findFiles('*.php')->from($targetDir) as $file) {
$content = file_get_contents($file->getPathname());
Expand Down Expand Up @@ -67,6 +68,7 @@ function renderIndex(): void
$this->template->models = $models;
} else if ($mode === "templates") {
$this->template->_template = "NoSpam/Templates.xml";
$this->template->disable_ajax = 1;
$filter = [];
if ($this->queryParam("id")) {
$filter["id"] = (int)$this->queryParam("id");
Expand Down
8 changes: 4 additions & 4 deletions Web/Presenters/OpenVKPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@ function onStartup(): void
$this->redirect("/maintenances/");
}
}

/*if($this->queryParam('al') == '1') {
$this->assertNoCSRF();
if($_SERVER['HTTP_X_OPENVK_AJAX_QUERY'] == '1' && $this->user->identity) {
error_reporting(0);
header('Content-Type: text/plain; charset=UTF-8');
}*/
}

parent::onStartup();
}
Expand Down
2 changes: 2 additions & 0 deletions Web/Presenters/ReportPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function renderList(): void
"perPage" => 15,
];
$this->template->mode = $act ?? "all";
$this->template->disable_ajax = 1;

if ($_SERVER["REQUEST_METHOD"] === "POST") {
$reports = [];
Expand Down Expand Up @@ -78,6 +79,7 @@ function renderView(int $id): void
$this->notFound();

$this->template->report = $report;
$this->template->disable_ajax = 1;
}

function renderCreate(int $id): void
Expand Down
2 changes: 1 addition & 1 deletion Web/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function renderView(int $id): void
$this->template->_template = "User/deactivated.xml";

$this->template->user = $user;
} else if(!$user->canBeViewedBy($this->user->identity)) {
} else if(!is_null($user) && !$user->canBeViewedBy($this->user->identity)) {
$this->template->_template = "User/private.xml";

$this->template->user = $user;
Expand Down
Loading

0 comments on commit 7d450c1

Please sign in to comment.