mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-12 08:50:40 -07:00
feat: add ability to manually scrobble track (#91)
* feat: add button to manually scrobble from ui * fix: ensure timestamp is in the past, log fix * test: add integration test
This commit is contained in:
parent
1be573e720
commit
300bac0e19
6 changed files with 185 additions and 1 deletions
77
engine/handlers/manual_scrobble.go
Normal file
77
engine/handlers/manual_scrobble.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gabehf/koito/engine/middleware"
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/gabehf/koito/internal/utils"
|
||||
)
|
||||
|
||||
func SubmitListenWithIDHandler(store db.DB) http.HandlerFunc {
|
||||
|
||||
var defaultClientStr = "Koito Web UI"
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
l := logger.FromContext(ctx)
|
||||
|
||||
l.Debug().Msg("SubmitListenWithIDHandler: Got request")
|
||||
|
||||
u := middleware.GetUserFromContext(ctx)
|
||||
if u == nil {
|
||||
l.Debug().Msg("SubmitListenWithIDHandler: Unauthorized request (user context is nil)")
|
||||
utils.WriteError(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
l.Debug().Msg("SubmitListenWithIDHandler: Failed to parse form")
|
||||
utils.WriteError(w, "form is invalid", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
trackIDStr := r.FormValue("track_id")
|
||||
timestampStr := r.FormValue("unix")
|
||||
client := r.FormValue("client")
|
||||
if client == "" {
|
||||
client = defaultClientStr
|
||||
}
|
||||
|
||||
if trackIDStr == "" || timestampStr == "" {
|
||||
l.Debug().Msg("SubmitListenWithIDHandler: Request is missing required parameters")
|
||||
utils.WriteError(w, "track_id and unix (timestamp) must be provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
trackID, err := strconv.Atoi(trackIDStr)
|
||||
if err != nil {
|
||||
l.Debug().AnErr("error", err).Msg("SubmitListenWithIDHandler: Invalid track id")
|
||||
utils.WriteError(w, "invalid track_id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
unix, err := strconv.ParseInt(timestampStr, 10, 64)
|
||||
if err != nil || time.Now().Unix() < unix {
|
||||
l.Debug().AnErr("error", err).Msg("SubmitListenWithIDHandler: Invalid unix timestamp")
|
||||
utils.WriteError(w, "invalid timestamp", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
ts := time.Unix(unix, 0)
|
||||
err = store.SaveListen(ctx, db.SaveListenOpts{
|
||||
TrackID: int32(trackID),
|
||||
Time: ts,
|
||||
UserID: u.ID,
|
||||
Client: client,
|
||||
})
|
||||
if err != nil {
|
||||
l.Err(err).Msg("SubmitListenWithIDHandler: Failed to submit listen")
|
||||
utils.WriteError(w, "failed to submit listen", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
|
@ -890,3 +891,29 @@ func TestSetPrimaryArtist(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 1, count, "expected only one primary artist for track")
|
||||
}
|
||||
|
||||
func TestManualListen(t *testing.T) {
|
||||
|
||||
t.Run("Submit Listens", doSubmitListens)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// happy
|
||||
formdata := url.Values{}
|
||||
formdata.Set("track_id", "1")
|
||||
formdata.Set("unix", strconv.FormatInt(time.Now().Unix()-60, 10))
|
||||
body := formdata.Encode()
|
||||
resp, err := makeAuthRequest(t, session, "POST", "/apis/web/v1/listen", strings.NewReader(body))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusCreated, resp.StatusCode)
|
||||
count, _ := store.Count(ctx, `SELECT COUNT(*) FROM listens WHERE track_id = $1`, 1)
|
||||
assert.Equal(t, 2, count)
|
||||
|
||||
// 400
|
||||
formdata.Set("track_id", "1")
|
||||
formdata.Set("unix", strconv.FormatInt(time.Now().Unix()+60, 10))
|
||||
body = formdata.Encode()
|
||||
resp, err = makeAuthRequest(t, session, "POST", "/apis/web/v1/listen", strings.NewReader(body))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,6 +81,7 @@ func bindRoutes(
|
|||
r.Post("/artists/primary", handlers.SetPrimaryArtistHandler(db))
|
||||
r.Delete("/album", handlers.DeleteAlbumHandler(db))
|
||||
r.Delete("/track", handlers.DeleteTrackHandler(db))
|
||||
r.Post("/listen", handlers.SubmitListenWithIDHandler(db))
|
||||
r.Delete("/listen", handlers.DeleteListenHandler(db))
|
||||
r.Post("/aliases", handlers.CreateAliasHandler(db))
|
||||
r.Post("/aliases/delete", handlers.DeleteAliasHandler(db))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue