From 0bbe2d609dd04afed4b0c229aa4f1cdb47e50f9a Mon Sep 17 00:00:00 2001 From: wvffle Date: Mon, 28 Nov 2022 23:37:06 +0000 Subject: [PATCH] Fetch sources if we have none --- front/src/composables/audio/tracks.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/front/src/composables/audio/tracks.ts b/front/src/composables/audio/tracks.ts index 4b9276a2a..3c305c003 100644 --- a/front/src/composables/audio/tracks.ts +++ b/front/src/composables/audio/tracks.ts @@ -1,4 +1,5 @@ import type { QueueTrack, QueueTrackSource } from '~/composables/audio/queue' +import type { Track, Upload } from '~/types' import type { Sound } from '~/api/player' import { createGlobalState, syncRef, useTimeoutFn, whenever } from '@vueuse/core' @@ -12,13 +13,28 @@ import { soundImplementation } from '~/api/player' import useLRUCache from '~/composables/data/useLRUCache' import store from '~/store' +import axios from 'axios' + const ALLOWED_PLAY_TYPES: (CanPlayTypeResult | undefined)[] = ['maybe', 'probably'] const AUDIO_ELEMENT = document.createElement('audio') const soundPromises = new Map>() const soundCache = useLRUCache({ max: 10 }) -const getTrackSources = (track: QueueTrack): QueueTrackSource[] => { +export const fetchTrackSources = async (id: number): Promise => { + const { uploads } = await axios.get(`tracks/${id}/`) + .then(response => response.data as Track, () => ({ uploads: [] as Upload[] })) + + return uploads.map(upload => ({ + uuid: upload.uuid, + duration: upload.duration, + mimetype: upload.mimetype, + bitrate: upload.bitrate, + url: store.getters['instance/absoluteUrl'](upload.listen_url) + })) +} + +const getTrackSources = async (track: QueueTrack): Promise => { const token = store.state.auth.authenticated && store.state.auth.scopedTokens.listen const appendToken = (url: string) => { if (token) { @@ -30,6 +46,10 @@ const getTrackSources = (track: QueueTrack): QueueTrackSource[] => { return url } + if (track.sources.length === 0) { + track.sources = await fetchTrackSources(track.id) + } + const sources: QueueTrackSource[] = track.sources .map((source) => ({ ...source, @@ -65,7 +85,7 @@ export const useTracks = createGlobalState(() => { } const createSoundPromise = async () => { - const sources = getTrackSources(track) + const sources = await getTrackSources(track) const { playNext } = useQueue() const SoundImplementation = soundImplementation.value