From b5ed1745ff98905daee65fd9848b569ad5888e34 Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Sun, 17 May 2026 23:38:41 +0200 Subject: [PATCH] feat(soundtouch-web): add recents panel + generic content-item player MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ports app's commit 3122c4e to the package layout. Two new handlers and route registrations; the frontend was already shipped in the Preact swap. HandleDeviceRecents GET /api/device-recents/{id} Returns the speaker's /recents list as APIResponse{Success,Data}. Backs the Recents.js component (lazy-loaded list under the device-detail view; hides itself when the device returns no recents). HandleDevicePlay POST /api/device-play/{id} Generic content-item player. Decodes a {source,type,location, sourceAccount,itemName,containerArt,isPresetable} JSON body into a *models.ContentItem and runs Client.SelectContentItem. Used by Recents.js to replay items the speaker reports, regardless of source — TuneIn, Spotify, AUX, etc. Different from HandlePlayTuneIn which is TuneIn-specific. Translation note: app's bodies used app.Devices[id] directly; main's registry is encapsulated behind GetDevice/AddDevice/TouchDevice (see the post-base refactor that introduced registry_test.go), so this commit uses app.GetDevice(id) instead. Same lookup, just through the maintained API. go build ./... clean. go test ./... clean (only pre-existing TestDocsConsistency fails, untracked-file issue, unrelated). golangci-lint run ./pkg/service/soundtouchweb/... 0 issues. Co-Authored-By: Claude Opus 4.7 (1M context) --- pkg/service/soundtouchweb/handler.go | 90 ++++++++++++++++++++++++++++ pkg/service/soundtouchweb/mount.go | 2 + 2 files changed, 92 insertions(+) diff --git a/pkg/service/soundtouchweb/handler.go b/pkg/service/soundtouchweb/handler.go index 0b831e4..405b90f 100644 --- a/pkg/service/soundtouchweb/handler.go +++ b/pkg/service/soundtouchweb/handler.go @@ -681,6 +681,96 @@ func (app *WebApp) HandleTuneInNavigate(w http.ResponseWriter, r *http.Request) } } +// HandleDeviceRecents returns recently played items for a device. +func (app *WebApp) HandleDeviceRecents(w http.ResponseWriter, r *http.Request) { + deviceID := chi.URLParam(r, "id") + + device, exists := app.GetDevice(deviceID) + if !exists { + app.sendError(w, "Device not found", http.StatusNotFound) + return + } + + if device.Client == nil { + app.sendError(w, "Device client not available", http.StatusInternalServerError) + return + } + + recents, err := device.Client.GetRecents() + if err != nil { + app.sendError(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + + if encErr := json.NewEncoder(w).Encode(webtypes.APIResponse{Success: true, Data: recents}); encErr != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) + } +} + +// HandleDevicePlay plays an arbitrary content item on a device. Generic +// counterpart to HandlePlayTuneIn — used by the Recents panel to replay +// items the speaker reports under /recents, regardless of their source. +func (app *WebApp) HandleDevicePlay(w http.ResponseWriter, r *http.Request) { + deviceID := chi.URLParam(r, "id") + + device, exists := app.GetDevice(deviceID) + if !exists { + app.sendError(w, "Device not found", http.StatusNotFound) + return + } + + if device.Client == nil { + app.sendError(w, "Device client not available", http.StatusInternalServerError) + return + } + + var req struct { + Source string `json:"source"` + Type string `json:"type"` + Location string `json:"location"` + SourceAccount string `json:"sourceAccount"` + ItemName string `json:"itemName"` + ContainerArt string `json:"containerArt"` + IsPresetable bool `json:"isPresetable"` + } + + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + app.sendError(w, "Invalid request body", http.StatusBadRequest) + return + } + + if req.Location == "" { + app.sendError(w, "location is required", http.StatusBadRequest) + return + } + + contentItem := &models.ContentItem{ + Source: req.Source, + Type: req.Type, + Location: req.Location, + SourceAccount: req.SourceAccount, + ItemName: req.ItemName, + ContainerArt: req.ContainerArt, + IsPresetable: req.IsPresetable, + } + + if err := device.Client.SelectContentItem(contentItem); err != nil { + app.sendError(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + + if encErr := json.NewEncoder(w).Encode(webtypes.APIResponse{ + Success: true, + Data: map[string]string{"message": "Playing " + req.ItemName}, + }); encErr != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) + } +} + // HandlePlayTuneIn plays a TuneIn content item on a specific device via POST /select. func (app *WebApp) HandlePlayTuneIn(w http.ResponseWriter, r *http.Request) { deviceID := chi.URLParam(r, "id") diff --git a/pkg/service/soundtouchweb/mount.go b/pkg/service/soundtouchweb/mount.go index b650bde..7b766fa 100644 --- a/pkg/service/soundtouchweb/mount.go +++ b/pkg/service/soundtouchweb/mount.go @@ -59,6 +59,8 @@ func (app *WebApp) Mount(r chi.Router, discoveryService *discovery.UnifiedDiscov r.Post("/api/device-volume/{id}/{volume}", app.HandleDirectVolumeControl) r.Post("/api/device-power/{id}", app.HandleDevicePower) r.Get("/api/device-power-status/{id}", app.HandleDevicePowerStatus) + r.Get("/api/device-recents/{id}", app.HandleDeviceRecents) + r.Post("/api/device-play/{id}", app.HandleDevicePlay) r.Get("/api/device-ws/{id}", app.HandleDeviceWebSocket) // SPA routes — serve index.html for client-side routing