diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-05-31 18:14:25 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-05-31 18:14:25 +0200 |
| commit | 0a8e25ea698333c73c50fc84e3334e1079606a10 (patch) | |
| tree | cbb29a6c23de33214e67b951a75f1c554ba2fcef | |
| parent | 1f89f1d22ced0bb107514efb7e861247c2941a70 (diff) | |
| download | WRMS-0a8e25ea698333c73c50fc84e3334e1079606a10.tar.gz WRMS-0a8e25ea698333c73c50fc84e3334e1079606a10.zip | |
allow admins to skip or delete songs
| -rw-r--r-- | main.go | 37 | ||||
| -rw-r--r-- | playlist.go | 5 | ||||
| -rw-r--r-- | web/client.html | 27 | ||||
| -rw-r--r-- | wrms.go | 16 |
4 files changed, 82 insertions, 3 deletions
@@ -151,18 +151,47 @@ func addHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Added song %s", string(data)) } -func playPauseHandler(w http.ResponseWriter, r *http.Request) { +func deleteHandler(w http.ResponseWriter, r *http.Request) { connId, err := getConnId(w, r) if err != nil { return } if !wrms.Config.IsAdmin(connId) { - http.Error(w, "Only admin is allowed to control Playback", http.StatusUnauthorized) + http.Error(w, "Only admins are allowed to delete songs", http.StatusUnauthorized) + return + } + + songUri := r.URL.Query().Get("song") + llog.Info("Delete song %s via url %s", songUri, r.URL) + wrms.DeleteSong(songUri) +} + +func genericControlHandler(w http.ResponseWriter, r *http.Request, cmd string) { + connId, err := getConnId(w, r) + if err != nil { return } - wrms.PlayPause() + if !wrms.Config.IsAdmin(connId) { + http.Error(w, "Only admins are allowed to control playback", http.StatusUnauthorized) + return + } + + switch cmd { + case "playpause": + wrms.PlayPause() + case "next": + wrms.Next() + } +} + +func playPauseHandler(w http.ResponseWriter, r *http.Request) { + genericControlHandler(w, r, "playpause") +} + +func nextHandler(w http.ResponseWriter, r *http.Request) { + genericControlHandler(w, r, "next") } func wsEndpoint(w http.ResponseWriter, r *http.Request) { @@ -275,6 +304,8 @@ func setupRoutes() { http.HandleFunc("/down", downHandler) http.HandleFunc("/unvote", unvoteHandler) http.HandleFunc("/add", addHandler) + http.HandleFunc("/delete", deleteHandler) + http.HandleFunc("/next", nextHandler) http.HandleFunc("/playpause", playPauseHandler) http.HandleFunc("/ws", wsEndpoint) } diff --git a/playlist.go b/playlist.go index 249bad5..becc85e 100644 --- a/playlist.go +++ b/playlist.go @@ -56,3 +56,8 @@ func (pl *Playlist) Adjust(s *Song) { heap.Fix(pl, s.index) llog.DDebug("adjusting song %p in the playlist (%p) -> %v", s, pl, pl) } + +func (pl *Playlist) RemoveSong(s *Song) { + heap.Remove(pl, s.index) + llog.DDebug("removing song %p in the playlist (%p) -> %v", s, pl, pl) +} diff --git a/web/client.html b/web/client.html index 20aa7ad..91ce11d 100644 --- a/web/client.html +++ b/web/client.html @@ -147,6 +147,17 @@ listItem.appendChild(document.createTextNode(song.weight + ' ' + formatSong(song))); listItem.appendChild(newSourceLabel(song)); + {{if .IsAdmin}} + let delBtn = document.createElement("button"); + delBtn.style.marginLeft = 10 + "px"; + delBtn.appendChild(document.createTextNode("delete")); + delBtn.addEventListener("click", function() { + const params = new URLSearchParams(); + params.append("song", song.uri) + new HttpClient().get("/delete?" + params.toString(), console.log); + }); + listItem.appendChild(delBtn); + {{end}} playlist.appendChild(listItem); } } @@ -156,6 +167,14 @@ renderPlaylist(); } + function handleDelete(deleted) { + for (const deletedSong of deleted) { + const index = songs.findIndex(function(s) {return s.uri == deletedSong.uri}); + if (index > -1) { songs.splice(index, 1); } + } + renderPlaylist(); + } + function handleVotes(direction, votedSongs) { for (const song of votedSongs) { votes.set(song.uri, direction) @@ -219,6 +238,9 @@ case "add": handleAdd(cmd.songs) break; + case "delete": + handleDelete(cmd.songs) + break; case "update": handleUpdate(cmd.songs) break; @@ -327,6 +349,10 @@ document.getElementById("ppbutton").addEventListener("click", function() { new HttpClient().get("/playpause", console.log); }); + + document.getElementById("nextbutton").addEventListener("click", function() { + new HttpClient().get("/next", console.log); + }); {{end}} } </script> @@ -362,6 +388,7 @@ {{if .IsAdmin}} <div id='controls'> <button id="ppbutton">Play</button> + <button id="nextbutton">Next</button> </div> {{end}} @@ -126,6 +126,22 @@ func (wrms *Wrms) AddSong(song Song) { } } +func (wrms *Wrms) DeleteSong(songUri string) { + for i := 0; i < len(wrms.Songs); i++ { + s := &wrms.Songs[i] + if s.Uri != songUri { + continue + } + + wrms.Songs[i] = wrms.Songs[len(wrms.Songs)-1] + wrms.Songs = wrms.Songs[:len(wrms.Songs)-1] + + wrms.queue.RemoveSong(s) + wrms.Broadcast(newEvent("delete", []Song{*s})) + break + } +} + func (wrms *Wrms) Next() { llog.DDebug("Next Song") |
