navidrome/server/subsonic/media_annotation.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

187 lines
5.0 KiB
Go
Raw Normal View History

package subsonic
import (
2020-01-22 05:01:43 +01:00
"context"
"net/http"
"time"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/server/subsonic/engine"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/utils"
)
type MediaAnnotationController struct {
scrobbler engine.Scrobbler
2020-08-02 23:58:07 +02:00
ds model.DataStore
}
2020-08-02 23:58:07 +02:00
func NewMediaAnnotationController(scrobbler engine.Scrobbler, ds model.DataStore) *MediaAnnotationController {
return &MediaAnnotationController{scrobbler: scrobbler, ds: ds}
2016-03-23 22:46:54 +01:00
}
func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id, err := requiredParamString(r, "id", "Required id parameter is missing")
if err != nil {
return nil, err
}
rating, err := requiredParamInt(r, "rating", "Required rating parameter is missing")
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)
2020-08-02 23:58:07 +02:00
err = c.setRating(r.Context(), id, rating)
2016-03-24 00:37:48 +01:00
switch {
2020-01-15 04:22:34 +01:00
case err == model.ErrNotFound:
2020-01-09 02:45:07 +01:00
log.Error(r, err)
return nil, newError(responses.ErrorDataNotFound, "ID not found")
2016-03-24 00:37:48 +01:00
case err != nil:
2020-01-09 02:45:07 +01:00
log.Error(r, err)
return nil, newError(responses.ErrorGeneric, "Internal Error")
2016-03-24 00:37:48 +01:00
}
return newResponse(), nil
2016-03-24 00:37:48 +01:00
}
2020-08-02 23:58:07 +02:00
func (c *MediaAnnotationController) setRating(ctx context.Context, id string, rating int) error {
exist, err := c.ds.Album(ctx).Exists(id)
if err != nil {
return err
}
if exist {
return c.ds.Album(ctx).SetRating(rating, id)
}
return c.ds.MediaFile(ctx).SetRating(rating, id)
}
2020-01-22 05:01:43 +01:00
func (c *MediaAnnotationController) Star(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ids := utils.ParamStrings(r, "id")
albumIds := utils.ParamStrings(r, "albumId")
artistIds := utils.ParamStrings(r, "artistId")
if len(ids)+len(albumIds)+len(artistIds) == 0 {
return nil, newError(responses.ErrorMissingParameter, "Required id parameter is missing")
}
ids = append(ids, albumIds...)
ids = append(ids, artistIds...)
err := c.setStar(r.Context(), true, ids...)
if err != nil {
return nil, err
}
2020-01-22 05:01:43 +01:00
return newResponse(), nil
2020-01-22 05:01:43 +01:00
}
func (c *MediaAnnotationController) Unstar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
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 {
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...)
err := c.setStar(r.Context(), false, ids...)
if err != nil {
return nil, err
}
2016-03-23 22:46:54 +01:00
return newResponse(), nil
}
func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ids, err := requiredParamStrings(r, "id", "Required id parameter is missing")
if err != nil {
return nil, err
}
times := utils.ParamTimes(r, "time")
2016-03-22 00:35:40 +01:00
if len(times) > 0 && len(times) != len(ids) {
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
}
submission := utils.ParamBool(r, "submission", true)
2016-03-17 23:58:09 +01:00
playerId := 1 // TODO Multiple players, based on playerName/username/clientIP(?)
playerName := utils.ParamString(r, "c")
username := utils.ParamString(r, "u")
2020-01-09 02:45:07 +01:00
log.Debug(r, "Scrobbling tracks", "ids", ids, "times", times, "submission", submission)
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-22 00:35:40 +01:00
if submission {
2020-04-07 01:42:35 +02:00
_, err := c.scrobbler.Register(r.Context(), playerId, id, t)
2016-03-22 00:35:40 +01:00
if err != nil {
2020-01-09 02:45:07 +01:00
log.Error(r, "Error scrobbling track", "id", id, err)
continue
2016-03-22 00:35:40 +01:00
}
} else {
2020-04-07 01:42:35 +02:00
_, err := c.scrobbler.NowPlaying(r.Context(), playerId, playerName, id, username)
2016-03-22 00:35:40 +01:00
if err != nil {
2020-01-09 02:45:07 +01:00
log.Error(r, "Error setting current song", "id", id, err)
continue
2016-03-22 00:35:40 +01:00
}
2016-03-17 02:04:41 +01:00
}
}
return newResponse(), nil
}
2020-08-02 23:58:07 +02:00
func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids ...string) error {
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
}
err := c.ds.WithTx(func(tx model.DataStore) error {
for _, id := range ids {
2020-08-04 17:53:19 +02:00
exist, err := tx.Album(ctx).Exists(id)
2020-08-02 23:58:07 +02:00
if err != nil {
return err
}
if exist {
err = tx.Album(ctx).SetStar(star, ids...)
if err != nil {
return err
}
continue
}
2020-08-04 17:53:19 +02:00
exist, err = tx.Artist(ctx).Exists(id)
2020-08-02 23:58:07 +02:00
if err != nil {
return err
}
if exist {
err = tx.Artist(ctx).SetStar(star, ids...)
if err != nil {
return err
}
continue
}
err = tx.MediaFile(ctx).SetStar(star, ids...)
if err != nil {
return err
}
}
return nil
})
switch {
case err == model.ErrNotFound:
log.Error(ctx, err)
return newError(responses.ErrorDataNotFound, "ID not found")
2020-08-02 23:58:07 +02:00
case err != nil:
log.Error(ctx, err)
return newError(responses.ErrorGeneric, "Internal Error")
2020-08-02 23:58:07 +02:00
}
return nil
}