aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go37
-rw-r--r--playlist.go5
-rw-r--r--web/client.html27
-rw-r--r--wrms.go16
4 files changed, 82 insertions, 3 deletions
diff --git a/main.go b/main.go
index 2899eb1..55a789a 100644
--- a/main.go
+++ b/main.go
@@ -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}}
diff --git a/wrms.go b/wrms.go
index 430e5a9..bc5c732 100644
--- a/wrms.go
+++ b/wrms.go
@@ -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")