From d77163cf91035f0f49c6b02e03c5f563f8c59634 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Tue, 18 Aug 2026 18:25:05 +0300 Subject: [PATCH] scrobbles store --- src/stores/scrobbles.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/stores/scrobbles.js diff --git a/src/stores/scrobbles.js b/src/stores/scrobbles.js new file mode 100644 index 000000000..749353257 --- /dev/null +++ b/src/stores/scrobbles.js @@ -0,0 +1,40 @@ +import { defineStore } from 'pinia' +import { + fetchScrobbles, +} from 'src/api/public.js' +import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js' + +export const defaultState = () => ({ + scrobblesNextFetch: new Map(), +}) + +export const useScrobblesStore = defineStore('scrobbles', { + state: defaultState, + actions: { + getLatestScrobble(userId) { + const scrobblesSupport = + useInstanceCapabilitiesStore().pleromaScrobblesAvailable + + if (!scrobblesSupport) { + return + } + + if (this.scrobblesNextFetch.get(userId) > Date.now()) { + return + } + + this.scrobblesNextFetch.set(userId, Date.now() + 24 * 60 * 60 * 1000) + + fetchScrobbles({ accountId: userId }) + .then(({ data: scrobbles }) => { + useUsersStore().findUser(userId).latestScrobble = scrobbles[0] + + this.scrobblesNextFetch.set(user.id, Date.now() + 60 * 1000) + }) + .catch((e) => { + useInstanceCapabilitiesStore().set('pleromaScrobblesAvailable', false) + console.warn('cannot fetch scrobbles', e) + }) + } + } +})