mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
reportPlaybackStopped set IgnoreScrobble and force-submitted a play on every Stopped report, so a briefly-played track (e.g. an immediate skip) was marked played. Finamp sends Stopped on every track switch, so the threshold must be applied server-side. Let ReportPlayback's StateStopped logic decide (play + scrobble only past 50% of the track / 4-minute cap) instead. Subsonic differs because there the client gates submission.
122 lines
4.5 KiB
Go
122 lines
4.5 KiB
Go
package jellyfin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/navidrome/navidrome/core/scrobbler"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
"github.com/navidrome/navidrome/server/jellyfin/dto"
|
|
)
|
|
|
|
// playbackReport is the subset of Jellyfin's PlaybackStartInfo/PlaybackProgressInfo
|
|
// fields Navidrome needs to keep its playback/scrobbling state in sync.
|
|
type playbackReport struct {
|
|
ItemId string `json:"ItemId"`
|
|
PositionTicks int64 `json:"PositionTicks"`
|
|
IsPaused bool `json:"IsPaused"`
|
|
}
|
|
|
|
// decodeReport reads the playback report body. ItemId falls back to a query
|
|
// param, as some clients send it there instead of (or in addition to) the JSON body.
|
|
// ItemId is decoded here since it flows straight into scrobbler lookups by raw media file id.
|
|
func decodeReport(r *http.Request) playbackReport {
|
|
var body playbackReport
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
if body.ItemId == "" {
|
|
body.ItemId = r.URL.Query().Get("ItemId")
|
|
}
|
|
body.ItemId = dto.DecodeID(body.ItemId)
|
|
return body
|
|
}
|
|
|
|
// clientIdentity returns the scrobbler cache key/display name for the caller's
|
|
// player. Both are zero values if withPlayer could not resolve a player.
|
|
func clientIdentity(ctx context.Context) (id, name string) {
|
|
player, _ := request.PlayerFrom(ctx)
|
|
return player.ID, player.Client
|
|
}
|
|
|
|
// reportPlaybackStart handles POST /Sessions/Playing, sent once when a client
|
|
// starts playing an item.
|
|
//
|
|
// Access control: these Sessions endpoints report the caller's own playback
|
|
// history and are never used to look up or expose content, so unlike browse/stream
|
|
// endpoints they are intentionally not library-access-gated.
|
|
func (api *Router) reportPlaybackStart(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
body := decodeReport(r)
|
|
clientId, clientName := clientIdentity(ctx)
|
|
err := api.scrobbler.ReportPlayback(ctx, scrobbler.ReportPlaybackParams{
|
|
MediaId: body.ItemId,
|
|
PositionMs: body.PositionTicks / 10_000,
|
|
State: scrobbler.StatePlaying,
|
|
PlaybackRate: 1.0,
|
|
ClientId: clientId,
|
|
ClientName: clientName,
|
|
})
|
|
if err != nil {
|
|
log.Warn(ctx, "Jellyfin API: report playback start failed", "id", body.ItemId, err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// reportPlaybackProgress handles POST /Sessions/Playing/Progress, sent periodically
|
|
// (and on pause/resume/seek) while a client keeps playing an item.
|
|
func (api *Router) reportPlaybackProgress(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
body := decodeReport(r)
|
|
state := scrobbler.StatePlaying
|
|
if body.IsPaused {
|
|
state = scrobbler.StatePaused
|
|
}
|
|
clientId, clientName := clientIdentity(ctx)
|
|
err := api.scrobbler.ReportPlayback(ctx, scrobbler.ReportPlaybackParams{
|
|
MediaId: body.ItemId,
|
|
PositionMs: body.PositionTicks / 10_000,
|
|
State: state,
|
|
PlaybackRate: 1.0,
|
|
ClientId: clientId,
|
|
ClientName: clientName,
|
|
})
|
|
if err != nil {
|
|
log.Warn(ctx, "Jellyfin API: report playback progress failed", "id", body.ItemId, err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// reportPlaybackStopped handles POST /Sessions/Playing/Stopped, sent once when
|
|
// playback of an item ends.
|
|
//
|
|
// Unlike Subsonic — where the client decides a play "counts" and only then calls
|
|
// scrobble?submission=true — Jellyfin clients (Finamp) send a Stopped report on *every* stop,
|
|
// including an immediate track switch. So the play threshold has to be applied server-side: we let
|
|
// ReportPlayback's own StateStopped logic decide (play counts + external scrobble only when the
|
|
// reported position passes 50% of the track, capped at 4 minutes). Force-submitting here instead
|
|
// would mark even a one-second skip as played.
|
|
func (api *Router) reportPlaybackStopped(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
body := decodeReport(r)
|
|
clientId, clientName := clientIdentity(ctx)
|
|
|
|
err := api.scrobbler.ReportPlayback(ctx, scrobbler.ReportPlaybackParams{
|
|
MediaId: body.ItemId,
|
|
PositionMs: body.PositionTicks / 10_000,
|
|
State: scrobbler.StateStopped,
|
|
ClientId: clientId,
|
|
ClientName: clientName,
|
|
})
|
|
if err != nil {
|
|
log.Warn(ctx, "Jellyfin API: report playback stopped failed", "id", body.ItemId, err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// postCapabilities acknowledges Jellyfin session-capability negotiation.
|
|
// Navidrome doesn't track per-session client capabilities, so this is a no-op.
|
|
func (api *Router) postCapabilities(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|