From e91948b200a65d92588f35cf19d5d649258b8848 Mon Sep 17 00:00:00 2001 From: Chris Fulljames Date: Sat, 22 Feb 2025 13:28:09 -0500 Subject: [PATCH] Only update song list when user clicks play --- main.py | 8 +++----- static/player.js | 36 ++++++++++++++++++++---------------- templates/news.html | 6 ++++++ templates/song-list.html | 2 +- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index 0f68565..1d9dd77 100644 --- a/main.py +++ b/main.py @@ -1004,6 +1004,7 @@ class Song: created: str tags: list[str] collaborators: list[str] + user_has_pfp: bool def json(self): return json.dumps(vars(self)) @@ -1023,9 +1024,6 @@ class Song: return song_comments - def user_has_pfp(self): - return (get_user_images_path(self.userid)/"pfp.jpg").exists() - @classmethod def by_id(cls, songid): songs = cls._from_db("select * from songs inner join users on songs.userid = users.userid where songid = ?", [songid]) @@ -1073,8 +1071,8 @@ class Song: song_tags = [t["tag"] for t in tags[sd["songid"]]] song_collabs = [c["name"] for c in collabs[sd["songid"]]] created = datetime.fromisoformat(sd["created"]).astimezone().strftime("%Y-%m-%d") - songs.append(cls(sd["songid"], sd["userid"], sd["username"], sd["title"], sanitize_user_text(sd["description"]), created, song_tags, song_collabs)) - + user_has_pfp = (get_user_images_path(sd["userid"])/"pfp.jpg").exists() + songs.append(cls(sd["songid"], sd["userid"], sd["username"], sd["title"], sanitize_user_text(sd["description"]), created, song_tags, song_collabs, user_has_pfp)) return songs @classmethod diff --git a/static/player.js b/static/player.js index 0dc50b6..34f0939 100644 --- a/static/player.js +++ b/static/player.js @@ -3,11 +3,14 @@ var m_songIndex = 0; // Play a new song from the list in the player function play(event) { - var songElement = event.target; - while (!songElement.classList.contains("song")) - { - songElement = songElement.parentElement; + var songElement = event.target.closest(".song"); + + // Update song queue with songs on current page + m_allSongs = []; + for (const element of document.getElementsByClassName("song")) { + m_allSongs.push(element); } + m_songIndex = m_allSongs.indexOf(songElement); playCurrentSong(); } @@ -26,8 +29,16 @@ function playCurrentSong() { audio.play(); var pfp = document.getElementById("player-pfp") - pfp.style.display = "inline-block"; - pfp.src = `/pfp/${songData.userid}` + var albumImg; + if (songData.user_has_pfp) { + pfp.style.display = "inline-block"; + pfp.src = `/pfp/${songData.userid}`; + albumImg = `/pfp/${songData.userid}`; + } + else { + pfp.style.display = "none"; + albumImg = "/static/lsp_notes.png"; + } var title = document.getElementById("player-title"); title.textContent = songData.title; @@ -49,7 +60,7 @@ function playCurrentSong() { var collabname = collaborators[i].substr(1, collaborators[i].length - 1); var link = document.createElement("a"); link.href = `/users/${collabname}`; - link.classList.add("profile-link") + link.classList.add("profile-link"); link.textContent = collabname; collabs.appendChild(link); } @@ -60,16 +71,14 @@ function playCurrentSong() { } } - //collabs.textContent = songData.collaborators.join(", ") - - if ("mediaSession" in navigator) { navigator.mediaSession.metadata = new MediaMetadata({ title: songData.title, artist: songData.username, album: "Little Song Place", - artwork: [{src: "/static/lsp_notes.png"}], + artwork: [{src: albumImg}], }); + navigator.mediaSession.setActionHandler('nexttrack', () => { songNext(); }); @@ -199,10 +208,5 @@ document.addEventListener("DOMContentLoaded", (event) => { document.getElementById("volume-slider").oninput = function(event) { audio.volume = event.target.value; } - - // Song queue - for (const element of document.getElementsByClassName("song")) { - m_allSongs.push(element); - } }); diff --git a/templates/news.html b/templates/news.html index 27da4fe..5f6a196 100644 --- a/templates/news.html +++ b/templates/news.html @@ -6,6 +6,12 @@

Site News

+

2025-02-22 - Continuous Playback

+The song player now persists between pages as you navigate around the site, so +the music isn't interrupted when you go to someone's profile or comment on a +song. It will continue playing the songs from the original page until you +click play on a new song. +

2025-02-16 - Playlists

Do you need specific songs in a specific order? Have I got the feature for diff --git a/templates/song-list.html b/templates/song-list.html index 5581ca9..5efc46c 100644 --- a/templates/song-list.html +++ b/templates/song-list.html @@ -3,7 +3,7 @@

- {% if song.user_has_pfp() %} + {% if song.user_has_pfp %} {% endif %} -- 2.39.5