Skip to content

Commit

Permalink
feat: get and set updated queue w/o blocked tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrani committed May 15, 2024
1 parent 14b7aa1 commit 78cd6d4
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions src/models/queue.js
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 78cd6d4

Please sign in to comment.