2020-01-20 00:21:44 +01:00
|
|
|
package subsonic
|
2016-03-12 02:49:01 +01:00
|
|
|
|
|
|
|
import (
|
2020-01-22 05:01:43 +01:00
|
|
|
"context"
|
2022-10-01 00:54:25 +02:00
|
|
|
"errors"
|
2020-08-14 05:08:15 +02:00
|
|
|
"fmt"
|
2020-01-07 20:56:26 +01:00
|
|
|
"net/http"
|
2016-03-12 02:49:01 +01:00
|
|
|
"time"
|
|
|
|
|
2021-06-20 02:56:56 +02:00
|
|
|
"github.com/navidrome/navidrome/core/scrobbler"
|
2020-01-24 01:44:08 +01:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
"github.com/navidrome/navidrome/model"
|
2020-08-14 05:08:15 +02:00
|
|
|
"github.com/navidrome/navidrome/model/request"
|
2021-06-10 18:20:52 +02:00
|
|
|
"github.com/navidrome/navidrome/server/events"
|
2020-01-24 01:44:08 +01:00
|
|
|
"github.com/navidrome/navidrome/server/subsonic/responses"
|
2020-02-07 00:55:38 +01:00
|
|
|
"github.com/navidrome/navidrome/utils"
|
2016-03-12 02:49:01 +01:00
|
|
|
)
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
func (api *Router) SetRating(r *http.Request) (*responses.Subsonic, error) {
|
2020-10-27 20:23:29 +01:00
|
|
|
id, err := requiredParamString(r, "id")
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-27 20:23:29 +01:00
|
|
|
rating, err := requiredParamInt(r, "rating")
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-24 00:37:48 +01:00
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Debug(r, "Setting rating", "rating", rating, "id", id)
|
2022-11-21 18:57:56 +01:00
|
|
|
err = api.setRating(r.Context(), id, rating)
|
2016-03-24 00:37:48 +01:00
|
|
|
|
2022-10-01 00:54:25 +02:00
|
|
|
if errors.Is(err, model.ErrNotFound) {
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Error(r, err)
|
2020-08-14 04:11:18 +02:00
|
|
|
return nil, newError(responses.ErrorDataNotFound, "ID not found")
|
2022-10-01 00:54:25 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Error(r, err)
|
2020-10-27 20:23:29 +01:00
|
|
|
return nil, err
|
2016-03-24 00:37:48 +01:00
|
|
|
}
|
|
|
|
|
2020-08-14 04:11:18 +02:00
|
|
|
return newResponse(), nil
|
2016-03-24 00:37:48 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
func (api *Router) setRating(ctx context.Context, id string, rating int) error {
|
2021-06-10 21:03:30 +02:00
|
|
|
var repo model.AnnotatedRepository
|
|
|
|
var resource string
|
2021-04-07 05:06:18 +02:00
|
|
|
|
2022-12-28 18:32:46 +01:00
|
|
|
entity, err := model.GetEntityByID(ctx, api.ds, id)
|
2021-06-10 21:03:30 +02:00
|
|
|
if err != nil {
|
2021-06-10 18:20:52 +02:00
|
|
|
return err
|
2020-08-02 23:58:07 +02:00
|
|
|
}
|
2021-06-10 21:03:30 +02:00
|
|
|
switch entity.(type) {
|
|
|
|
case *model.Artist:
|
2022-11-21 18:57:56 +01:00
|
|
|
repo = api.ds.Artist(ctx)
|
2021-06-10 21:03:30 +02:00
|
|
|
resource = "artist"
|
|
|
|
case *model.Album:
|
2022-11-21 18:57:56 +01:00
|
|
|
repo = api.ds.Album(ctx)
|
2021-06-10 21:03:30 +02:00
|
|
|
resource = "album"
|
|
|
|
default:
|
2022-11-21 18:57:56 +01:00
|
|
|
repo = api.ds.MediaFile(ctx)
|
2021-06-10 21:03:30 +02:00
|
|
|
resource = "song"
|
|
|
|
}
|
|
|
|
err = repo.SetRating(rating, id)
|
|
|
|
if err != nil {
|
2021-06-10 18:20:52 +02:00
|
|
|
return err
|
2020-08-02 23:58:07 +02:00
|
|
|
}
|
2021-06-15 22:09:01 +02:00
|
|
|
event := &events.RefreshResource{}
|
2022-11-21 18:57:56 +01:00
|
|
|
api.broker.SendMessage(ctx, event.With(resource, id))
|
2021-06-10 21:03:30 +02:00
|
|
|
return nil
|
2020-08-02 23:58:07 +02:00
|
|
|
}
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
func (api *Router) Star(r *http.Request) (*responses.Subsonic, error) {
|
2020-02-07 00:55:38 +01:00
|
|
|
ids := utils.ParamStrings(r, "id")
|
|
|
|
albumIds := utils.ParamStrings(r, "albumId")
|
|
|
|
artistIds := utils.ParamStrings(r, "artistId")
|
2020-01-19 02:03:52 +01:00
|
|
|
if len(ids)+len(albumIds)+len(artistIds) == 0 {
|
2020-08-14 04:11:18 +02:00
|
|
|
return nil, newError(responses.ErrorMissingParameter, "Required id parameter is missing")
|
2016-03-28 17:38:45 +02:00
|
|
|
}
|
2020-01-19 02:03:52 +01:00
|
|
|
ids = append(ids, albumIds...)
|
|
|
|
ids = append(ids, artistIds...)
|
2016-03-28 17:38:45 +02:00
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
err := api.setStar(r.Context(), true, ids...)
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-22 05:01:43 +01:00
|
|
|
|
2020-08-14 04:11:18 +02:00
|
|
|
return newResponse(), nil
|
2020-01-22 05:01:43 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
func (api *Router) Unstar(r *http.Request) (*responses.Subsonic, error) {
|
2020-02-07 00:55:38 +01:00
|
|
|
ids := utils.ParamStrings(r, "id")
|
|
|
|
albumIds := utils.ParamStrings(r, "albumId")
|
|
|
|
artistIds := utils.ParamStrings(r, "artistId")
|
2020-01-22 05:01:43 +01:00
|
|
|
if len(ids)+len(albumIds)+len(artistIds) == 0 {
|
2020-08-14 04:11:18 +02:00
|
|
|
return nil, newError(responses.ErrorMissingParameter, "Required id parameter is missing")
|
2020-01-22 05:01:43 +01:00
|
|
|
}
|
|
|
|
ids = append(ids, albumIds...)
|
|
|
|
ids = append(ids, artistIds...)
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
err := api.setStar(r.Context(), false, ids...)
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 22:46:54 +01:00
|
|
|
|
2020-08-14 04:11:18 +02:00
|
|
|
return newResponse(), nil
|
2016-03-12 02:49:01 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
func (api *Router) setStar(ctx context.Context, star bool, ids ...string) error {
|
2021-06-26 19:52:29 +02:00
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Debug(ctx, "Changing starred", "ids", ids, "starred", star)
|
|
|
|
if len(ids) == 0 {
|
|
|
|
log.Warn(ctx, "Cannot star/unstar an empty list of ids")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
event := &events.RefreshResource{}
|
2022-11-21 18:57:56 +01:00
|
|
|
err := api.ds.WithTx(func(tx model.DataStore) error {
|
2021-06-26 19:52:29 +02:00
|
|
|
for _, id := range ids {
|
|
|
|
exist, err := tx.Album(ctx).Exists(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exist {
|
|
|
|
err = tx.Album(ctx).SetStar(star, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
event = event.With("album", id)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
exist, err = tx.Artist(ctx).Exists(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exist {
|
|
|
|
err = tx.Artist(ctx).SetStar(star, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
event = event.With("artist", id)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = tx.MediaFile(ctx).SetStar(star, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
event = event.With("song", id)
|
|
|
|
}
|
2022-11-21 18:57:56 +01:00
|
|
|
api.broker.SendMessage(ctx, event)
|
2021-06-26 19:52:29 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-10-01 00:54:25 +02:00
|
|
|
if errors.Is(err, model.ErrNotFound) {
|
2021-06-26 19:52:29 +02:00
|
|
|
log.Error(ctx, err)
|
|
|
|
return newError(responses.ErrorDataNotFound, "ID not found")
|
2022-10-01 00:54:25 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-06-26 19:52:29 +02:00
|
|
|
log.Error(ctx, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
func (api *Router) Scrobble(r *http.Request) (*responses.Subsonic, error) {
|
2020-10-27 20:23:29 +01:00
|
|
|
ids, err := requiredParamStrings(r, "id")
|
2020-01-07 20:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-07 00:55:38 +01:00
|
|
|
times := utils.ParamTimes(r, "time")
|
2016-03-22 00:35:40 +01:00
|
|
|
if len(times) > 0 && len(times) != len(ids) {
|
2020-08-14 04:11:18 +02:00
|
|
|
return nil, newError(responses.ErrorGeneric, "Wrong number of timestamps: %d, should be %d", len(times), len(ids))
|
2016-03-22 00:35:40 +01:00
|
|
|
}
|
2020-02-07 00:55:38 +01:00
|
|
|
submission := utils.ParamBool(r, "submission", true)
|
2021-06-16 16:23:34 +02:00
|
|
|
ctx := r.Context()
|
2016-03-17 14:34:32 +01:00
|
|
|
|
2021-06-23 05:56:29 +02:00
|
|
|
if submission {
|
2022-11-21 18:57:56 +01:00
|
|
|
err := api.scrobblerSubmit(ctx, ids, times)
|
2021-06-23 05:56:29 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "Error registering scrobbles", "ids", ids, "times", times, err)
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-21 18:57:56 +01:00
|
|
|
err := api.scrobblerNowPlaying(ctx, ids[0])
|
2021-06-23 05:56:29 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "Error setting NowPlaying", "id", ids[0], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newResponse(), nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
func (api *Router) scrobblerSubmit(ctx context.Context, ids []string, times []time.Time) error {
|
2021-06-23 05:56:29 +02:00
|
|
|
var submissions []scrobbler.Submission
|
|
|
|
log.Debug(ctx, "Scrobbling tracks", "ids", ids, "times", times)
|
2016-03-22 15:28:13 +01:00
|
|
|
for i, id := range ids {
|
2016-03-22 00:35:40 +01:00
|
|
|
var t time.Time
|
|
|
|
if len(times) > 0 {
|
|
|
|
t = times[i]
|
|
|
|
} else {
|
|
|
|
t = time.Now()
|
2016-03-12 02:49:01 +01:00
|
|
|
}
|
2021-06-23 05:56:29 +02:00
|
|
|
submissions = append(submissions, scrobbler.Submission{TrackID: id, Timestamp: t})
|
2021-06-16 16:23:34 +02:00
|
|
|
}
|
2020-08-14 05:08:15 +02:00
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
return api.scrobbler.Submit(ctx, submissions)
|
2020-08-14 05:08:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-21 18:57:56 +01:00
|
|
|
func (api *Router) scrobblerNowPlaying(ctx context.Context, trackId string) error {
|
|
|
|
mf, err := api.ds.MediaFile(ctx).Get(trackId)
|
2020-08-14 05:08:15 +02:00
|
|
|
if err != nil {
|
2021-06-20 02:56:56 +02:00
|
|
|
return err
|
2020-08-14 05:08:15 +02:00
|
|
|
}
|
|
|
|
if mf == nil {
|
2021-06-20 02:56:56 +02:00
|
|
|
return fmt.Errorf(`ID "%s" not found`, trackId)
|
2020-08-14 05:08:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 17:21:29 +02:00
|
|
|
player, _ := request.PlayerFrom(ctx)
|
|
|
|
username, _ := request.UsernameFrom(ctx)
|
|
|
|
client, _ := request.ClientFrom(ctx)
|
|
|
|
clientId, ok := request.ClientUniqueIdFrom(ctx)
|
|
|
|
if !ok {
|
|
|
|
clientId = player.ID
|
|
|
|
}
|
2020-08-14 05:08:15 +02:00
|
|
|
|
2022-11-04 16:29:58 +01:00
|
|
|
log.Info(ctx, "Now Playing", "title", mf.Title, "artist", mf.Artist, "user", username, "player", player.Name)
|
2022-11-21 18:57:56 +01:00
|
|
|
err = api.scrobbler.NowPlaying(ctx, clientId, client, trackId)
|
2021-06-20 02:56:56 +02:00
|
|
|
return err
|
2020-08-14 05:08:15 +02:00
|
|
|
}
|