mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-13 09:30:27 -07:00
feat: add now playing endpoint and ui (#93)
* wip * feat: add now playing
This commit is contained in:
parent
0b7ecb0b96
commit
a4689bed27
8 changed files with 265 additions and 9 deletions
|
|
@ -211,10 +211,8 @@ func LbzSubmitListenHandler(store db.DB, mbzc mbz.MusicBrainzCaller) func(w http
|
|||
Time: listenedAt,
|
||||
UserID: u.ID,
|
||||
Client: client,
|
||||
}
|
||||
|
||||
if req.ListenType == ListenTypePlayingNow {
|
||||
opts.SkipSaveListen = true
|
||||
IsNowPlaying: req.ListenType == ListenTypePlayingNow,
|
||||
SkipSaveListen: req.ListenType == ListenTypePlayingNow,
|
||||
}
|
||||
|
||||
_, err, shared := sfGroup.Do(buildCaolescingKey(payload), func() (interface{}, error) {
|
||||
|
|
|
|||
41
engine/handlers/now_playing.go
Normal file
41
engine/handlers/now_playing.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/gabehf/koito/internal/memkv"
|
||||
"github.com/gabehf/koito/internal/models"
|
||||
"github.com/gabehf/koito/internal/utils"
|
||||
)
|
||||
|
||||
type NowPlayingResponse struct {
|
||||
CurrentlyPlaying bool `json:"currently_playing"`
|
||||
Track models.Track `json:"track"`
|
||||
}
|
||||
|
||||
func NowPlayingHandler(store db.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
l := logger.FromContext(ctx)
|
||||
|
||||
l.Debug().Msg("NowPlayingHandler: Got request")
|
||||
|
||||
// Hardcoded user id as 1. Not great but it works until (if) multi-user is supported.
|
||||
if trackIdI, ok := memkv.Store.Get("1"); !ok {
|
||||
utils.WriteJSON(w, http.StatusOK, NowPlayingResponse{CurrentlyPlaying: false})
|
||||
} else if trackId, ok := trackIdI.(int32); !ok {
|
||||
l.Debug().Msg("NowPlayingHandler: Failed type assertion for trackIdI")
|
||||
utils.WriteError(w, "internal server error", http.StatusInternalServerError)
|
||||
} else {
|
||||
track, err := store.GetTrack(ctx, db.GetTrackOpts{ID: trackId})
|
||||
if err != nil {
|
||||
l.Error().Err(err).Msg("NowPlayingHandler: Failed to get track from database")
|
||||
utils.WriteError(w, "failed to fetch currently playing track from database", http.StatusInternalServerError)
|
||||
} else {
|
||||
utils.WriteJSON(w, http.StatusOK, NowPlayingResponse{CurrentlyPlaying: true, Track: *track})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue