mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
hotfix(better)
This commit is contained in:
1 parent
0f3bc0f54d
commit
e74efdd3bf
3 files changed
+114
-6
No files matched your search
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
import { logger } from "../../core/Logger.mjs";
|
||||
import { apiCall } from "./constants.mjs";
|
||||
import { apiCall, isLastFmUserNotFound, noteStaleUser } from "./constants.mjs";
|
||||
|
||||
/**
|
||||
* @type {object}
|
||||
@@ -36,6 +36,12 @@ const ScrobblingMixin = {
|
||||
true
|
||||
);
|
||||
} catch (err) {
|
||||
if (isLastFmUserNotFound(err)) {
|
||||
if (noteStaleUser(this, userId)) {
|
||||
logger.warn(`[LastFm] Now-playing updates paused for ${userId}: Last.fm user not found (account renamed or deleted? re-link required)`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
logger.warn(`[LastFm] updateNowPlaying failed for ${userId}: ${err.message}`);
|
||||
}
|
||||
},
|
||||
@@ -65,6 +71,12 @@ const ScrobblingMixin = {
|
||||
|
||||
this._incrementScrobbleCount(userId);
|
||||
} catch (err) {
|
||||
if (isLastFmUserNotFound(err)) {
|
||||
if (noteStaleUser(this, userId)) {
|
||||
logger.warn(`[LastFm] Scrobbling paused for ${userId}: Last.fm user not found (account renamed or deleted? re-link required)`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
logger.warn(`[LastFm] Scrobble failed for ${userId}: ${err.message}`);
|
||||
}
|
||||
},
|
||||
@@ -88,8 +100,14 @@ const ScrobblingMixin = {
|
||||
);
|
||||
return playcount;
|
||||
} catch (e) {
|
||||
logger.warn("[LastFm] Error:", e?.message);
|
||||
if (isLastFmUserNotFound(e)) {
|
||||
if (noteStaleUser(this, userId)) {
|
||||
logger.warn(`[LastFm] Scrobble count sync skipped for ${userId}: Last.fm user not found (account renamed or deleted? re-link required)`);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
logger.warn("[LastFm] Error:", e?.message);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import { logger } from "../../core/Logger.mjs";
|
||||
import { isLastFmUserNotFound, noteStaleUser } from "./constants.mjs";
|
||||
|
||||
/**
|
||||
* @type {object}
|
||||
@@ -44,6 +45,12 @@ const ServerStatsMixin = {
|
||||
playcount,
|
||||
};
|
||||
} catch (e) {
|
||||
if (isLastFmUserNotFound(e)) {
|
||||
if (noteStaleUser(this, uid)) {
|
||||
logger.warn(`[LastFm] Skipping ${uid}: Last.fm user not found (account renamed or deleted? re-link required)`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
logger.warn("[LastFm] Error:", e?.message);
|
||||
return {
|
||||
userId: uid,
|
||||
@@ -97,6 +104,12 @@ const ServerStatsMixin = {
|
||||
playcount,
|
||||
};
|
||||
} catch (e) {
|
||||
if (isLastFmUserNotFound(e)) {
|
||||
if (noteStaleUser(this, uid)) {
|
||||
logger.warn(`[LastFm] Skipping ${uid}: Last.fm user not found (account renamed or deleted? re-link required)`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
logger.warn("[LastFm] Error:", e?.message);
|
||||
return {
|
||||
userId: uid,
|
||||
@@ -150,6 +163,12 @@ const ServerStatsMixin = {
|
||||
playcount,
|
||||
};
|
||||
} catch (e) {
|
||||
if (isLastFmUserNotFound(e)) {
|
||||
if (noteStaleUser(this, uid)) {
|
||||
logger.warn(`[LastFm] Skipping ${uid}: Last.fm user not found (account renamed or deleted? re-link required)`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
logger.warn("[LastFm] Error:", e?.message);
|
||||
return {
|
||||
userId: uid,
|
||||
@@ -466,6 +485,12 @@ const ServerStatsMixin = {
|
||||
const artists = await this.getTopArtists(uid, "overall", 50);
|
||||
return { uid, username: user.username, artists };
|
||||
} catch (e) {
|
||||
if (isLastFmUserNotFound(e)) {
|
||||
if (noteStaleUser(this, uid)) {
|
||||
logger.warn(`[LastFm] Skipping ${uid}: Last.fm user not found (account renamed or deleted? re-link required)`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
logger.warn("[LastFm] Error:", e?.message);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,61 @@ function buildSignature(params, apiSecret) {
|
||||
/** @private @type {number} @description AbortSignal timeout (ms) for Last.fm API calls — prevents stalled TCP connections from hanging for the OS-level timeout (~15 min). */
|
||||
const LASTFM_FETCH_TIMEOUT_MS = 15_000;
|
||||
|
||||
/** @private Make an authenticated Last.fm API call. @async @param {object} params @param {string} apiSecret @param {boolean} [post=false] @returns {Promise<object>} @throws {Error} On HTTP or Last.fm API error. */
|
||||
/**
|
||||
* @class LastFmApiError
|
||||
* @description Typed error for Last.fm API failures. All diagnostic detail
|
||||
* lives in the message itself ("Last.fm user not found", "Last.fm HTTP 404:")
|
||||
* so callers classify outcomes via isLastFmUserNotFound() without needing
|
||||
* extra structured fields on the error object.
|
||||
*/
|
||||
class LastFmApiError extends Error {
|
||||
/**
|
||||
* @param {string} apiMessage - Message from the Last.fm API body (may be empty).
|
||||
* @param {object} [meta]
|
||||
* @param {number} [meta.status] - HTTP status code (0 when body-level error).
|
||||
* @param {number} [meta.code] - Last.fm error code from the response body.
|
||||
*/
|
||||
constructor(apiMessage, { status = 0, code = 0 } = {}) {
|
||||
const msg = String(apiMessage ?? "");
|
||||
super(
|
||||
/user not found/i.test(msg) ? "Last.fm user not found"
|
||||
: status ? `Last.fm HTTP ${status}: ${msg || `error ${code}`}`
|
||||
: `Last.fm error ${code}: ${msg}`
|
||||
);
|
||||
this.name = "LastFmApiError";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether an error is Last.fm's "user not found" (error 6) — the
|
||||
* expected outcome when a linked account was renamed or deleted. Matches
|
||||
* typed LastFmApiError messages as well as legacy raw-format errors from
|
||||
* before the typed-error rework.
|
||||
* @param {Error} err
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function isLastFmUserNotFound(err) {
|
||||
return /user not found/i.test(String(err?.message ?? ""));
|
||||
}
|
||||
|
||||
/** @type {Set<string>} @description Users already warned about as stale (dedup for background flows). */
|
||||
const _staleUserWarned = new Set();
|
||||
|
||||
/**
|
||||
* Report a stale linked user only once per bot+user pair (per process).
|
||||
* @param {object} manager - LastFmManager instance (used for botId scoping).
|
||||
* @param {string} userId
|
||||
* @returns {boolean} True the first time this user is reported.
|
||||
*/
|
||||
function noteStaleUser(manager, userId) {
|
||||
const key = `${manager?.botId ?? ""}:${userId}`;
|
||||
if (_staleUserWarned.has(key)) return false;
|
||||
if (_staleUserWarned.size >= 1000) _staleUserWarned.clear();
|
||||
_staleUserWarned.add(key);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @private Make an authenticated Last.fm API call. @async @param {object} params @param {string} apiSecret @param {boolean} [post=false] @returns {Promise<object>} @throws {LastFmApiError} On HTTP or Last.fm API error. */
|
||||
async function apiCall(params, apiSecret, post = false) {
|
||||
const allParams = { ...params };
|
||||
allParams.api_sig = buildSignature(allParams, apiSecret);
|
||||
@@ -46,14 +100,25 @@ async function apiCall(params, apiSecret, post = false) {
|
||||
const res = await fetch(url, opts);
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
throw new Error(`Last.fm HTTP ${res.status}: ${text.slice(0, 200)}`);
|
||||
let code = 0;
|
||||
let message = "";
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
if (parsed && typeof parsed === "object") {
|
||||
code = Number(parsed.error) || 0;
|
||||
message = String(parsed.message ?? "");
|
||||
}
|
||||
} catch (_) {
|
||||
message = text.slice(0, 200);
|
||||
}
|
||||
throw new LastFmApiError(message, { status: res.status, code });
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
if (data.error) {
|
||||
throw new Error(`Last.fm ${data.error}: ${data.message}`);
|
||||
throw new LastFmApiError(String(data.message ?? ""), { code: Number(data.error) || 0 });
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export { BASE_URL, normalizeTrackText, buildSignature, apiCall };
|
||||
export { BASE_URL, normalizeTrackText, buildSignature, apiCall, LastFmApiError, isLastFmUserNotFound, noteStaleUser };
|
||||
Reference in new issue
Block a user