Fix: All scrobbles were applied to the Last.fm account associated with user ID 1 (#434)

This commit is contained in:
Tikhon Petrishchev
2026-01-18 09:34:57 +03:00
committed by GitHub
parent 7eb5c6bbc6
commit ea76cf5dc2
4 changed files with 16 additions and 8 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ def create_lastfm_session(body: LastFmSessionBody):
if not body.token:
return {"error": "Missing token"}, 400
lastfm = LastFmPlugin()
lastfm = LastFmPlugin(current_userid=get_current_userid())
session_key = lastfm.get_session_key(body.token)
if session_key:
+3 -1
View File
@@ -37,6 +37,8 @@ from swingmusic.utils.stats import (
get_artists_in_period,
get_tracks_in_period,
)
from swingmusic.utils.auth import get_current_userid
bp_tag = Tag(name="Logger", description="Log item plays")
api = APIBlueprint("logger", __name__, url_prefix="/logger", abp_tags=[bp_tag])
@@ -101,7 +103,7 @@ def log_track(body: LogTrackBody):
trackentry.increment_playcount(duration, timestamp)
track = trackentry.tracks[0]
lastfm = LastFmPlugin()
lastfm = LastFmPlugin(current_userid=get_current_userid())
if (
lastfm.enabled
+5 -5
View File
@@ -9,7 +9,6 @@ from urllib.parse import quote_plus
from swingmusic.config import UserConfig
from swingmusic.models.track import Track
from swingmusic.settings import Paths
from swingmusic.utils.auth import get_current_userid
from swingmusic.utils.threading import background
from swingmusic.plugins import Plugin, plugin_method
@@ -23,14 +22,15 @@ class LastFmPlugin(Plugin):
UPLOADING_DUMPS = False
def __init__(self):
def __init__(self, current_userid: int):
self.config = UserConfig()
self.current_userid = current_userid
super().__init__("lastfm", "Last.fm scrobbler")
self.set_active(
bool(
self.config.lastfmApiKey
and self.config.lastfmApiSecret
and self.config.lastfmSessionKeys.get(str(get_current_userid()))
and self.config.lastfmSessionKeys.get(str(self.current_userid))
)
)
@@ -46,7 +46,7 @@ class LastFmPlugin(Plugin):
url = "http://ws.audioscrobbler.com/2.0/?format=json"
data["api_key"] = self.config.lastfmApiKey
if useSessionKey:
data["sk"] = self.config.lastfmSessionKeys.get(str(get_current_userid()))
data["sk"] = self.config.lastfmSessionKeys.get(str(self.current_userid))
data["api_sig"] = self.get_api_signature(data)
@@ -113,7 +113,7 @@ class LastFmPlugin(Plugin):
log.error("LAST.FM: Invalid session key")
# Invalid session key
try:
self.config.lastfmSessionKeys.pop(str(get_current_userid()))
self.config.lastfmSessionKeys.pop(str(self.current_userid))
except KeyError:
pass
+7 -1
View File
@@ -4,6 +4,7 @@ import hashlib
from flask_jwt_extended import current_user
from swingmusic.config import UserConfig
from swingmusic.logger import log
def hash_password(password: str) -> str:
@@ -38,6 +39,11 @@ def get_current_userid() -> int:
"""
try:
return current_user["id"]
except RuntimeError:
except RuntimeError as e:
# Catch this error raised during migration execution
if log:
log.error("get_current_userid: Unable to get current user id")
log.error(e)
# TODO: possible change to other than real userid,
# because it is really hard to debug when no fault but data goes to wrong user
return 1