Simplify Subsonic API handler implementation
This commit is contained in:
@@ -17,17 +17,7 @@ import (
|
||||
"github.com/navidrome/navidrome/utils"
|
||||
)
|
||||
|
||||
type MediaAnnotationController struct {
|
||||
ds model.DataStore
|
||||
playTracker scrobbler.PlayTracker
|
||||
broker events.Broker
|
||||
}
|
||||
|
||||
func NewMediaAnnotationController(ds model.DataStore, playTracker scrobbler.PlayTracker, broker events.Broker) *MediaAnnotationController {
|
||||
return &MediaAnnotationController{ds: ds, playTracker: playTracker, broker: broker}
|
||||
}
|
||||
|
||||
func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||
func (api *Router) SetRating(r *http.Request) (*responses.Subsonic, error) {
|
||||
id, err := requiredParamString(r, "id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -38,7 +28,7 @@ func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
|
||||
log.Debug(r, "Setting rating", "rating", rating, "id", id)
|
||||
err = c.setRating(r.Context(), id, rating)
|
||||
err = api.setRating(r.Context(), id, rating)
|
||||
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(r, err)
|
||||
@@ -52,23 +42,23 @@ func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Req
|
||||
return newResponse(), nil
|
||||
}
|
||||
|
||||
func (c *MediaAnnotationController) setRating(ctx context.Context, id string, rating int) error {
|
||||
func (api *Router) setRating(ctx context.Context, id string, rating int) error {
|
||||
var repo model.AnnotatedRepository
|
||||
var resource string
|
||||
|
||||
entity, err := core.GetEntityByID(ctx, c.ds, id)
|
||||
entity, err := core.GetEntityByID(ctx, api.ds, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch entity.(type) {
|
||||
case *model.Artist:
|
||||
repo = c.ds.Artist(ctx)
|
||||
repo = api.ds.Artist(ctx)
|
||||
resource = "artist"
|
||||
case *model.Album:
|
||||
repo = c.ds.Album(ctx)
|
||||
repo = api.ds.Album(ctx)
|
||||
resource = "album"
|
||||
default:
|
||||
repo = c.ds.MediaFile(ctx)
|
||||
repo = api.ds.MediaFile(ctx)
|
||||
resource = "song"
|
||||
}
|
||||
err = repo.SetRating(rating, id)
|
||||
@@ -76,11 +66,11 @@ func (c *MediaAnnotationController) setRating(ctx context.Context, id string, ra
|
||||
return err
|
||||
}
|
||||
event := &events.RefreshResource{}
|
||||
c.broker.SendMessage(ctx, event.With(resource, id))
|
||||
api.broker.SendMessage(ctx, event.With(resource, id))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MediaAnnotationController) Star(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||
func (api *Router) Star(r *http.Request) (*responses.Subsonic, error) {
|
||||
ids := utils.ParamStrings(r, "id")
|
||||
albumIds := utils.ParamStrings(r, "albumId")
|
||||
artistIds := utils.ParamStrings(r, "artistId")
|
||||
@@ -90,7 +80,7 @@ func (c *MediaAnnotationController) Star(w http.ResponseWriter, r *http.Request)
|
||||
ids = append(ids, albumIds...)
|
||||
ids = append(ids, artistIds...)
|
||||
|
||||
err := c.setStar(r.Context(), true, ids...)
|
||||
err := api.setStar(r.Context(), true, ids...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -98,7 +88,7 @@ func (c *MediaAnnotationController) Star(w http.ResponseWriter, r *http.Request)
|
||||
return newResponse(), nil
|
||||
}
|
||||
|
||||
func (c *MediaAnnotationController) Unstar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||
func (api *Router) Unstar(r *http.Request) (*responses.Subsonic, error) {
|
||||
ids := utils.ParamStrings(r, "id")
|
||||
albumIds := utils.ParamStrings(r, "albumId")
|
||||
artistIds := utils.ParamStrings(r, "artistId")
|
||||
@@ -108,7 +98,7 @@ func (c *MediaAnnotationController) Unstar(w http.ResponseWriter, r *http.Reques
|
||||
ids = append(ids, albumIds...)
|
||||
ids = append(ids, artistIds...)
|
||||
|
||||
err := c.setStar(r.Context(), false, ids...)
|
||||
err := api.setStar(r.Context(), false, ids...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -116,7 +106,7 @@ func (c *MediaAnnotationController) Unstar(w http.ResponseWriter, r *http.Reques
|
||||
return newResponse(), nil
|
||||
}
|
||||
|
||||
func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids ...string) error {
|
||||
func (api *Router) setStar(ctx context.Context, star bool, ids ...string) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -126,7 +116,7 @@ func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids
|
||||
return nil
|
||||
}
|
||||
event := &events.RefreshResource{}
|
||||
err := c.ds.WithTx(func(tx model.DataStore) error {
|
||||
err := api.ds.WithTx(func(tx model.DataStore) error {
|
||||
for _, id := range ids {
|
||||
exist, err := tx.Album(ctx).Exists(id)
|
||||
if err != nil {
|
||||
@@ -158,7 +148,7 @@ func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids
|
||||
}
|
||||
event = event.With("song", id)
|
||||
}
|
||||
c.broker.SendMessage(ctx, event)
|
||||
api.broker.SendMessage(ctx, event)
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -173,7 +163,7 @@ func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||
func (api *Router) Scrobble(r *http.Request) (*responses.Subsonic, error) {
|
||||
ids, err := requiredParamStrings(r, "id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -186,12 +176,12 @@ func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Requ
|
||||
ctx := r.Context()
|
||||
|
||||
if submission {
|
||||
err := c.scrobblerSubmit(ctx, ids, times)
|
||||
err := api.scrobblerSubmit(ctx, ids, times)
|
||||
if err != nil {
|
||||
log.Error(ctx, "Error registering scrobbles", "ids", ids, "times", times, err)
|
||||
}
|
||||
} else {
|
||||
err := c.scrobblerNowPlaying(ctx, ids[0])
|
||||
err := api.scrobblerNowPlaying(ctx, ids[0])
|
||||
if err != nil {
|
||||
log.Error(ctx, "Error setting NowPlaying", "id", ids[0], err)
|
||||
}
|
||||
@@ -200,7 +190,7 @@ func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Requ
|
||||
return newResponse(), nil
|
||||
}
|
||||
|
||||
func (c *MediaAnnotationController) scrobblerSubmit(ctx context.Context, ids []string, times []time.Time) error {
|
||||
func (api *Router) scrobblerSubmit(ctx context.Context, ids []string, times []time.Time) error {
|
||||
var submissions []scrobbler.Submission
|
||||
log.Debug(ctx, "Scrobbling tracks", "ids", ids, "times", times)
|
||||
for i, id := range ids {
|
||||
@@ -213,11 +203,11 @@ func (c *MediaAnnotationController) scrobblerSubmit(ctx context.Context, ids []s
|
||||
submissions = append(submissions, scrobbler.Submission{TrackID: id, Timestamp: t})
|
||||
}
|
||||
|
||||
return c.playTracker.Submit(ctx, submissions)
|
||||
return api.scrobbler.Submit(ctx, submissions)
|
||||
}
|
||||
|
||||
func (c *MediaAnnotationController) scrobblerNowPlaying(ctx context.Context, trackId string) error {
|
||||
mf, err := c.ds.MediaFile(ctx).Get(trackId)
|
||||
func (api *Router) scrobblerNowPlaying(ctx context.Context, trackId string) error {
|
||||
mf, err := api.ds.MediaFile(ctx).Get(trackId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -234,6 +224,6 @@ func (c *MediaAnnotationController) scrobblerNowPlaying(ctx context.Context, tra
|
||||
}
|
||||
|
||||
log.Info(ctx, "Now Playing", "title", mf.Title, "artist", mf.Artist, "user", username, "player", player.Name)
|
||||
err = c.playTracker.NowPlaying(ctx, clientId, client, trackId)
|
||||
err = api.scrobbler.NowPlaying(ctx, clientId, client, trackId)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user