scrobbles store

This commit is contained in:
Henry Jameson 2026-08-18 18:25:05 +03:00
commit d77163cf91

40
src/stores/scrobbles.js Normal file
View file

@ -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)
})
}
}
})