From 78cd6d4403fe1b84376182c100efb4eedfa24884 Mon Sep 17 00:00:00 2001 From: cdrani Date: Wed, 15 May 2024 08:31:13 -0600 Subject: [PATCH] feat: get and set updated queue w/o blocked tracks --- src/models/queue.js | 69 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/src/models/queue.js b/src/models/queue.js index a5a47ec2..cfb361ab 100644 --- a/src/models/queue.js +++ b/src/models/queue.js @@ -1,17 +1,80 @@ import { currentData } from '../data/current' +import Dispatcher from '../events/dispatcher' export default class Queue { - constructor() {} + constructor() { + this._nextQueuedTracks = [] + this._userBlockedTracks = [] + this._dispatcher = new Dispatcher() + } get addedToQueue() { - return document.querySelector('[aria-label="Next in queue"]') + return document.querySelector('[aria-label="Next in queue"]')?.children || [] } get nextInQueue() { - return document.querySelector('[aria-label="Next up"]') + return document.querySelector('[aria-label="Next up"]')?.children || [] + } + + get tracksInQueue() { + return [...this.addedToQueue, ...this.nextInQueue].map(div => { + const songInfo = Array.from(div.querySelectorAll('p > span')) + + const songTitle = songInfo.at(0).innerText + + const songArtistsInfo = songInfo.at(1).querySelectorAll('span > a') + const songArtists = Array.from(songArtistsInfo).map(a => a.innerText).join(', ') + + return `${songTitle} by ${songArtists}` + }) } get blockedTracks() { return currentData.blockedTracks } + + async #dispatchQueueList() { + return await this._dispatcher.sendEvent({ + eventType: 'queue.get', + detail: { key: 'queue.get', values: {} }, + }) + } + + async #dispatchQueueSetter() { + return await this._dispatcher.sendEvent({ + eventType: 'queue.set', + detail: { key: 'queue.set', values: { next_tracks: this._nextQueuedTracks } }, + }) + } + + async setQueuedTracks() { + if (!this._userBlockedTracks.length) return + if (!this._nextQueuedTracks.length) return + + await this.#dispatchQueueSetter() + } + + async getQueuedTracks() { + this._userBlockedTracks = this.filterUnBlockedTracks() + if (!this._userBlockedTracks.length) return + + const queueList = await this.#dispatchQueueList() + + const spotifyQueuedTracks = queueList?.data?.player_state?.next_tracks + this._nextQueuedTracks = this.filterQueuedTracks({ + spotifyQueuedTracks, + userBlockedTracks: this._userBlockedTracks + }) + } + + filterUnBlockedTracks() { + const blocked = this.blockedTracks + const tracksInQueue = this.tracksInQueue + return blocked.filter(track => tracksInQueue.includes(track.id)) + } + + filterQueuedTracks({ spotifyQueuedTracks, userBlockedTracks }) { + const userBlockedTrackIds = userBlockedTracks.map(track => `spotify:track:${track.trackId}`) + return spotifyQueuedTracks.filter(item => !userBlockedTrackIds.includes(item.uri)) + } }