2022-07-24 11:47:18 +00:00
|
|
|
import { reactive, UnwrapNestedRefs } from 'vue'
|
2022-10-11 15:28:43 +00:00
|
|
|
import { differenceBy, merge, unionBy } from 'lodash'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { cache, http } from '@/services'
|
2022-07-24 11:47:18 +00:00
|
|
|
import { arrayify, logger } from '@/utils'
|
2022-06-10 10:47:46 +00:00
|
|
|
import { songStore } from '@/stores'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
const UNKNOWN_ALBUM_ID = 1
|
|
|
|
|
|
|
|
export const albumStore = {
|
2022-07-24 11:47:18 +00:00
|
|
|
vault: new Map<number, UnwrapNestedRefs<Album>>(),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
state: reactive({
|
2022-09-12 15:33:41 +00:00
|
|
|
albums: [] as Album[]
|
2022-04-21 09:38:24 +00:00
|
|
|
}),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
byId (id: number) {
|
|
|
|
return this.vault.get(id)
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
removeByIds (ids: number[]) {
|
2022-07-22 21:56:13 +00:00
|
|
|
this.state.albums = differenceBy(this.state.albums, ids.map(id => this.byId(id)), 'id')
|
2022-09-15 09:07:25 +00:00
|
|
|
ids.forEach(id => {
|
|
|
|
this.vault.delete(id)
|
|
|
|
cache.remove(['album', id])
|
|
|
|
})
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-07-22 21:56:13 +00:00
|
|
|
isUnknown: (album: Album | number) => {
|
|
|
|
if (typeof album === 'number') return album === UNKNOWN_ALBUM_ID
|
|
|
|
return album.id === UNKNOWN_ALBUM_ID
|
|
|
|
},
|
|
|
|
|
|
|
|
syncWithVault (albums: Album | Album[]) {
|
|
|
|
return arrayify(albums).map(album => {
|
|
|
|
let local = this.vault.get(album.id)
|
|
|
|
local = reactive(local ? merge(local, album) : album)
|
|
|
|
this.vault.set(album.id, local)
|
|
|
|
|
|
|
|
return local
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
/**
|
|
|
|
* Upload a cover for an album.
|
|
|
|
*
|
|
|
|
* @param {Album} album The album object
|
|
|
|
* @param {string} cover The content data string of the cover
|
|
|
|
*/
|
2022-07-08 14:53:04 +00:00
|
|
|
async uploadCover (album: Album, cover: string) {
|
2024-01-27 11:24:34 +00:00
|
|
|
album.cover = (await http.put<{ cover_url: string }>(`album/${album.id}/cover`, { cover })).cover_url
|
2022-06-10 10:47:46 +00:00
|
|
|
songStore.byAlbum(album).forEach(song => song.album_cover = album.cover)
|
2022-07-08 14:53:04 +00:00
|
|
|
|
|
|
|
// sync to vault
|
2022-09-12 15:33:41 +00:00
|
|
|
this.byId(album.id)!.cover = album.cover
|
2022-07-08 14:53:04 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
return album.cover
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
/**
|
|
|
|
* Fetch the (blurry) thumbnail-sized version of an album's cover.
|
|
|
|
*/
|
|
|
|
fetchThumbnail: async (id: number) => {
|
2022-09-15 09:07:25 +00:00
|
|
|
return (await http.get<{ thumbnailUrl: string }>(`album/${id}/thumbnail`)).thumbnailUrl
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
async resolve (id: number) {
|
|
|
|
let album = this.byId(id)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
if (!album) {
|
2022-07-24 11:47:18 +00:00
|
|
|
try {
|
|
|
|
album = this.syncWithVault(
|
2022-09-15 09:07:25 +00:00
|
|
|
await cache.remember<Album>(['album', id], async () => await http.get<Album>(`albums/${id}`))
|
2022-07-24 11:47:18 +00:00
|
|
|
)[0]
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e)
|
|
|
|
}
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
return album
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-07-22 21:56:13 +00:00
|
|
|
async paginate (page: number) {
|
2022-09-15 09:07:25 +00:00
|
|
|
const resource = await http.get<PaginatorResource>(`albums?page=${page}`)
|
2022-07-22 21:56:13 +00:00
|
|
|
this.state.albums = unionBy(this.state.albums, this.syncWithVault(resource.data), 'id')
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
return resource.links.next ? ++resource.meta.current_page : null
|
2022-10-18 14:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async fetchForArtist (artist: Artist | number) {
|
|
|
|
const id = typeof artist === 'number' ? artist : artist.id
|
|
|
|
|
|
|
|
return this.syncWithVault(
|
|
|
|
await cache.remember<Album[]>(['artist-albums', id], async () => await http.get<Album[]>(`artists/${id}/albums`))
|
|
|
|
)
|
2022-06-10 10:47:46 +00:00
|
|
|
}
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|