pleroma-fe/src/stores/scrobbles.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2026-08-18 18:25:05 +03:00
import { defineStore } from 'pinia'
2026-08-18 21:35:06 +03:00
2026-08-18 18:25:05 +03:00
import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js'
2026-08-18 21:35:06 +03:00
import { useUsersStore } from 'src/stores/users.js'
import { fetchScrobbles } from 'src/api/public.js'
2026-08-18 18:25:05 +03:00
export const defaultState = () => ({
scrobblesNextFetch: new Map(),
})
export const useScrobblesStore = defineStore('scrobbles', {
state: defaultState,
actions: {
getLatestScrobble(userId) {
const scrobblesSupport =
2026-08-18 21:35:06 +03:00
useInstanceCapabilitiesStore().pleromaScrobblesAvailable
2026-08-18 18:25:05 +03:00
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]
2026-08-18 21:35:06 +03:00
this.scrobblesNextFetch.set(userId, Date.now() + 60 * 1000)
2026-08-18 18:25:05 +03:00
})
.catch((e) => {
useInstanceCapabilitiesStore().set('pleromaScrobblesAvailable', false)
console.warn('cannot fetch scrobbles', e)
})
2026-08-18 21:35:06 +03:00
},
},
2026-08-18 18:25:05 +03:00
})