diff --git a/src/swingmusic/api/plugins/__init__.py b/src/swingmusic/api/plugins/__init__.py index 714ddaeb..095cce94 100644 --- a/src/swingmusic/api/plugins/__init__.py +++ b/src/swingmusic/api/plugins/__init__.py @@ -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: diff --git a/src/swingmusic/api/scrobble/__init__.py b/src/swingmusic/api/scrobble/__init__.py index 154741f7..2bb8e84f 100644 --- a/src/swingmusic/api/scrobble/__init__.py +++ b/src/swingmusic/api/scrobble/__init__.py @@ -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 diff --git a/src/swingmusic/plugins/lastfm.py b/src/swingmusic/plugins/lastfm.py index 92ff43b3..1b127f55 100644 --- a/src/swingmusic/plugins/lastfm.py +++ b/src/swingmusic/plugins/lastfm.py @@ -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 diff --git a/src/swingmusic/utils/auth.py b/src/swingmusic/utils/auth.py index f93ef0f2..dcbfd199 100644 --- a/src/swingmusic/utils/auth.py +++ b/src/swingmusic/utils/auth.py @@ -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