koel/resources/assets/js/stores/playlist.ts

193 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import { difference, union, orderBy } from 'lodash'
import stub from '@/stubs/playlist'
import { http } from '@/services'
2022-04-20 09:37:22 +00:00
import { alerts, pluralize, arrayify } from '@/utils'
2022-04-15 14:24:30 +00:00
import { songStore } from '.'
import models from '@/config/smart-playlist/models'
import operators from '@/config/smart-playlist/operators'
2022-04-20 12:41:50 +00:00
import { reactive } from 'vue'
2022-04-15 14:24:30 +00:00
export const playlistStore = {
stub,
2022-04-20 12:41:50 +00:00
state: reactive({
2022-04-15 14:24:30 +00:00
playlists: [] as Playlist[]
2022-04-20 12:41:50 +00:00
}),
2022-04-15 14:24:30 +00:00
init (playlists: Playlist[]) {
this.all = this.sort(playlists)
this.all.forEach(playlist => this.setupPlaylist(playlist))
},
2022-04-20 15:35:00 +00:00
setupPlaylist (playlist: Playlist) {
2022-04-15 14:24:30 +00:00
playlist.songs = []
if (playlist.is_smart) {
this.setupSmartPlaylist(playlist)
}
},
/**
* Set up a smart playlist by properly construct its structure from serialized database values.
*/
2022-04-20 15:35:00 +00:00
setupSmartPlaylist: (playlist: Playlist) => {
2022-04-15 14:24:30 +00:00
playlist.rules.forEach(group => {
group.rules.forEach(rule => {
const model = models.find(model => model.name === rule.model as unknown as string)
if (!model) {
console.error(`Invalid model ${rule.model} found in smart playlist ${playlist.name} (ID ${playlist.id})`)
return
}
rule.model = model
})
})
},
get all (): Playlist[] {
return this.state.playlists
},
set all (value: Playlist[]) {
this.state.playlists = value
},
2022-04-20 15:35:00 +00:00
async fetchSongs (playlist: Playlist) {
2022-04-15 14:24:30 +00:00
const songIds = await http.get<string[]>(`playlist/${playlist.id}/songs`)
playlist.songs = songStore.byIds(songIds)
playlist.populated = true
return playlist
},
2022-04-20 15:35:00 +00:00
byId (id: number) {
2022-04-15 14:24:30 +00:00
return this.all.find(playlist => playlist.id === id)
},
/**
* Populate the playlist content by "objectifying" all songs in the playlist.
* (Initially, a playlist only contain the song IDs).
*/
2022-04-20 15:35:00 +00:00
populateContent: (playlist: Playlist) => {
2022-04-15 14:24:30 +00:00
playlist.songs = songStore.byIds(<string[]><unknown>playlist.songs)
},
getSongs: (playlist: Playlist): Song[] => playlist.songs,
/**
* Add a playlist/playlists into the store.
*/
add (playlists: Playlist | Playlist[]) {
2022-04-20 09:37:22 +00:00
const playlistsToAdd = arrayify(playlists)
2022-04-15 14:24:30 +00:00
playlistsToAdd.forEach(playlist => this.setupPlaylist(playlist))
this.all = this.sort(union(this.all, playlistsToAdd))
},
/**
* Remove a playlist/playlists from the store.
*/
remove (playlists: Playlist | Playlist[]) {
2022-04-20 09:37:22 +00:00
this.all = difference(this.all, arrayify(playlists))
2022-04-15 14:24:30 +00:00
},
2022-04-20 15:35:00 +00:00
async store (name: string, songs: Song[] = [], rules: SmartPlaylistRuleGroup[] = []) {
2022-04-15 14:24:30 +00:00
const songIds = songs.map(song => song.id)
const serializedRules = this.serializeSmartPlaylistRulesForStorage(rules)
const playlist = await http.post<Playlist>('playlist', { name, songs: songIds, rules: serializedRules })
playlist.songs = songs
this.populateContent(playlist)
this.add(playlist)
alerts.success(`Created playlist "${playlist.name}."`)
return playlist
},
2022-04-20 15:35:00 +00:00
async delete (playlist: Playlist) {
2022-04-15 14:24:30 +00:00
await http.delete(`playlist/${playlist.id}`)
this.remove(playlist)
},
2022-04-20 15:35:00 +00:00
async addSongs (playlist: Playlist, songs: Song[]) {
2022-04-15 14:24:30 +00:00
if (playlist.is_smart) {
return playlist
}
if (!playlist.populated) {
await this.fetchSongs(playlist)
}
const count = playlist.songs.length
playlist.songs = union(playlist.songs, songs)
if (count === playlist.songs.length) {
return playlist
}
await http.put(`playlist/${playlist.id}/sync`, { songs: playlist.songs.map(song => song.id) })
alerts.success(`Added ${pluralize(songs.length, 'song')} into "${playlist.name}."`)
return playlist
},
2022-04-20 15:35:00 +00:00
removeSongs: async (playlist: Playlist, songs: Song[]) => {
2022-04-15 14:24:30 +00:00
if (playlist.is_smart) {
return playlist
}
playlist.songs = difference(playlist.songs, songs)
await http.put(`playlist/${playlist.id}/sync`, { songs: playlist.songs.map(song => song.id) })
alerts.success(`Removed ${pluralize(songs.length, 'song')} from "${playlist.name}."`)
return playlist
},
2022-04-20 15:35:00 +00:00
async update (playlist: Playlist) {
2022-04-15 14:24:30 +00:00
const serializedRules = this.serializeSmartPlaylistRulesForStorage(playlist.rules)
await http.put(`playlist/${playlist.id}`, { name: playlist.name, rules: serializedRules })
alerts.success(`Updated playlist "${playlist.name}."`)
return playlist
},
createEmptySmartPlaylistRule: (): SmartPlaylistRule => ({
id: (new Date()).getTime(),
model: models[0],
operator: operators[0].operator,
value: ['']
}),
createEmptySmartPlaylistRuleGroup (): SmartPlaylistRuleGroup {
return {
id: (new Date()).getTime(),
rules: [this.createEmptySmartPlaylistRule()]
}
},
/**
* Serialize the rule (groups) to be ready for database.
*/
2022-04-20 09:37:22 +00:00
serializeSmartPlaylistRulesForStorage: (ruleGroups: SmartPlaylistRuleGroup[]): any[] | null => {
2022-04-15 14:24:30 +00:00
if (!ruleGroups || !ruleGroups.length) {
return null
}
const serializedGroups = JSON.parse(JSON.stringify(ruleGroups))
serializedGroups.forEach((group: any): void => {
group.rules.forEach((rule: any) => {
rule.model = rule.model.name
})
})
return serializedGroups
},
2022-04-20 15:35:00 +00:00
sort: (playlists: Playlist[]) => {
2022-04-15 14:24:30 +00:00
return orderBy(playlists, ['is_smart', 'name'], ['desc', 'asc'])
}
}