@@ -19,6 +19,8 @@ import { getCoverArtCache, setCoverArtCache } from "./coverArtCache";
19
19
const originalFetch = window . fetch ;
20
20
const fetchWithRetry = require ( "fetch-retry" ) ( originalFetch ) ;
21
21
22
+ let APIServiceInstance = new APIServiceClass ( `${ window . location . origin } /1` ) ;
23
+
22
24
const searchForSpotifyTrack = async (
23
25
spotifyToken ?: string ,
24
26
trackName ?: string ,
@@ -611,6 +613,7 @@ const getPageProps = async (): Promise<{
611
613
const apiService = new APIServiceClass (
612
614
api_url || `${ window . location . origin } /1`
613
615
) ;
616
+ APIServiceInstance = apiService ;
614
617
globalAppContext = {
615
618
APIService : apiService ,
616
619
websocketsUrl : websockets_url ,
@@ -799,44 +802,53 @@ const getAlbumArtFromReleaseGroupMBID = async (
799
802
} ;
800
803
801
804
const getAlbumArtFromReleaseMBID = async (
802
- userSubmittedReleaseMBID : string ,
803
- useReleaseGroupFallback : boolean | string = false ,
804
- APIService ?: APIServiceClass ,
805
+ userSubmittedReleaseMBID : string | undefined | null ,
806
+ userSubmittedReleaseGroupMBID : string | undefined | null ,
805
807
optionalSize ?: CAAThumbnailSizes ,
806
808
frontOnly ?: boolean
807
809
) : Promise < string | undefined > => {
808
810
try {
809
811
// Check cache first
810
- const cacheKey = `ca:${ userSubmittedReleaseMBID } -${ optionalSize } -${ useReleaseGroupFallback } ` ;
812
+ const cacheKey = `ca:${ userSubmittedReleaseMBID } -${ optionalSize } -${ userSubmittedReleaseGroupMBID } ` ;
811
813
const cachedCoverArt = await getCoverArtCache ( cacheKey ) ;
812
814
if ( cachedCoverArt ) {
813
815
return cachedCoverArt ;
814
816
}
815
-
816
- const CAAResponse = await fetchWithRetry (
817
- `https://coverartarchive.org/release/${ userSubmittedReleaseMBID } ` ,
818
- retryParams
819
- ) ;
820
- if ( CAAResponse . ok ) {
821
- const body : CoverArtArchiveResponse = await CAAResponse . json ( ) ;
822
- const coverArt = getThumbnailFromCAAResponse (
823
- body ,
824
- optionalSize ,
825
- frontOnly
817
+ if ( userSubmittedReleaseMBID ) {
818
+ const CAAResponse = await fetchWithRetry (
819
+ `https://coverartarchive.org/release/${ userSubmittedReleaseMBID } ` ,
820
+ retryParams
826
821
) ;
827
- // Here, make sure there is a front image, otherwise discard the hit.
828
- if ( coverArt ) {
829
- // Cache the successful result
830
- await setCoverArtCache ( cacheKey , coverArt ) ;
822
+ if ( CAAResponse . ok ) {
823
+ const body : CoverArtArchiveResponse = await CAAResponse . json ( ) ;
824
+ const coverArt = getThumbnailFromCAAResponse (
825
+ body ,
826
+ optionalSize ,
827
+ frontOnly
828
+ ) ;
829
+ // Here, make sure there is a front image, otherwise discard the hit.
830
+ if ( coverArt ) {
831
+ // Cache the successful result
832
+ await setCoverArtCache ( cacheKey , coverArt ) ;
833
+ }
834
+ return coverArt ;
831
835
}
832
- return coverArt ;
833
836
}
834
837
835
- if ( useReleaseGroupFallback ) {
836
- let releaseGroupMBID = useReleaseGroupFallback ;
837
- if ( ! _ . isString ( useReleaseGroupFallback ) && APIService ) {
838
- const releaseGroupResponse = ( await APIService . lookupMBRelease (
839
- userSubmittedReleaseMBID
838
+ /*
839
+ Fallback to fetching cover art for the Release Group.
840
+ If no RG MBID is available, first hit the MusicBrainz API
841
+ with the release MBID to get the RG MBID
842
+ */
843
+ if ( userSubmittedReleaseMBID || userSubmittedReleaseGroupMBID ) {
844
+ let releaseGroupMBID = userSubmittedReleaseGroupMBID ;
845
+ if (
846
+ ! _ . isString ( userSubmittedReleaseGroupMBID ) &&
847
+ _ . isString ( userSubmittedReleaseMBID )
848
+ ) {
849
+ const releaseGroupResponse = ( await APIServiceInstance . lookupMBRelease (
850
+ userSubmittedReleaseMBID ,
851
+ "release-groups"
840
852
) ) as MusicBrainzRelease & WithReleaseGroup ;
841
853
releaseGroupMBID = releaseGroupResponse [ "release-group" ] . id ;
842
854
}
@@ -914,8 +926,7 @@ const getAlbumArtFromListenMetadataKey = (
914
926
915
927
const getAlbumArtFromListenMetadata = async (
916
928
listen : BaseListenFormat ,
917
- spotifyUser ?: SpotifyUser ,
918
- APIService ?: APIServiceClass
929
+ spotifyUser ?: SpotifyUser
919
930
) : Promise < string | undefined > => {
920
931
if ( ! listen ) {
921
932
return undefined ;
@@ -939,16 +950,17 @@ const getAlbumArtFromListenMetadata = async (
939
950
const userSubmittedReleaseMBID =
940
951
listen . track_metadata ?. release_mbid ??
941
952
listen . track_metadata ?. additional_info ?. release_mbid ;
953
+ const userSubmittedReleaseGroupMBID =
954
+ listen . track_metadata ?. additional_info ?. release_group_mbid ;
942
955
const caaId = listen . track_metadata ?. mbid_mapping ?. caa_id ;
943
956
const caaReleaseMbid = listen . track_metadata ?. mbid_mapping ?. caa_release_mbid ;
944
- if ( userSubmittedReleaseMBID ) {
957
+ if ( userSubmittedReleaseMBID || userSubmittedReleaseGroupMBID ) {
945
958
// try getting the cover art using user submitted release mbid. if user submitted release mbid
946
959
// does not have a cover art and the mapper matched to a different release, try to fallback to
947
960
// release group cover art of the user submitted release mbid next
948
961
const userSubmittedReleaseAlbumArt = await getAlbumArtFromReleaseMBID (
949
962
userSubmittedReleaseMBID ,
950
- Boolean ( caaReleaseMbid ) && userSubmittedReleaseMBID !== caaReleaseMbid ,
951
- APIService ,
963
+ userSubmittedReleaseGroupMBID ,
952
964
undefined ,
953
965
true // we only want front images, otherwise skip
954
966
) ;
0 commit comments