navidrome/server/subsonic/media_annotation.go

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

143 lines
4.1 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/engine"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/utils"
)
type MediaAnnotationController struct {
scrobbler engine.Scrobbler
2016-03-23 22:46:54 +01:00
ratings engine.Ratings
}
func NewMediaAnnotationController(scrobbler engine.Scrobbler, ratings engine.Ratings) *MediaAnnotationController {
return &MediaAnnotationController{
scrobbler: scrobbler,
ratings: ratings,
}
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)
err = c.ratings.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
}
2020-01-11 23:18:20 +01:00
return NewResponse(), nil
2016-03-24 00:37:48 +01:00
}
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
}
func (c *MediaAnnotationController) setStar(ctx context.Context, starred bool, ids ...string) error {
2020-01-22 05:01:43 +01:00
if len(ids) == 0 {
return nil
}
log.Debug(ctx, "Changing starred", "ids", ids, "starred", starred)
err := c.ratings.SetStar(ctx, starred, ids...)
2016-03-23 22:46:54 +01:00
switch {
2020-01-15 04:22:34 +01:00
case err == model.ErrNotFound:
2020-01-22 05:01:43 +01:00
log.Error(ctx, err)
return NewError(responses.ErrorDataNotFound, "ID not found")
2016-03-23 22:46:54 +01:00
case err != nil:
2020-01-22 05:01:43 +01:00
log.Error(ctx, err)
return NewError(responses.ErrorGeneric, "Internal Error")
2016-03-23 22:46:54 +01:00
}
2020-01-22 05:01:43 +01:00
return nil
2016-03-23 22:46:54 +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")
}
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
2020-01-11 23:18:20 +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
}
}
2020-01-11 23:18:20 +01:00
return NewResponse(), nil
}