From de3d112434573e52f4a82d42a2a50e325257e862 Mon Sep 17 00:00:00 2001 From: wvffle Date: Wed, 19 Oct 2022 11:00:44 +0000 Subject: [PATCH] Add listen submitting (Fix #1874) --- front/src/composables/audio/player.ts | 33 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/front/src/composables/audio/player.ts b/front/src/composables/audio/player.ts index f11248dce..fa61e6566 100644 --- a/front/src/composables/audio/player.ts +++ b/front/src/composables/audio/player.ts @@ -1,11 +1,14 @@ +import { tryOnMounted, useIntervalFn, useRafFn, useStorage, whenever } from '@vueuse/core' import { currentSound, createTrack } from '~/composables/audio/tracks' -import { tryOnMounted, useIntervalFn, useRafFn, useStorage } from '@vueuse/core' import { computed, ref, watch, watchEffect, type Ref } from 'vue' - -import useQueue from '~/composables/audio/useQueue' import { setGain } from './audio-api' -const { currentIndex } = useQueue() +import store from '~/store' +import axios from 'axios' + +import useQueue from '~/composables/audio/useQueue' + +const { currentIndex, currentTrack } = useQueue() export const isPlaying = ref(false) @@ -99,6 +102,28 @@ useIntervalFn(() => { currentTime.value = sound.currentTime }, 1000) +// Submit listens +const listenSubmitted = ref(false) +whenever(listenSubmitted, async () => { + console.log('Listening submitted!') + if (!store.state.auth.authenticated) return + if (!currentTrack.value) return + + await axios.post('history/listenings/', { track: currentTrack.value.id }) + .catch((error) => console.error('Could not record track in history', error)) +}) + +watch(currentTime, (time) => { + const sound = currentSound.value + if (!sound) { + listenSubmitted.value = false + return + } + + // https://listenbrainz.readthedocs.io/en/latest/users/api/core.html?highlight=half#post--1-submit-listens + listenSubmitted.value = time > Math.min(sound.duration / 2, 4 * 60) +}) + // Seeking export const seekBy = async (seconds: number) => { const sound = currentSound.value