@@ -46,6 +46,19 @@ function upsertCachedArtist(mappedArtist) {
4646 _cachedArtists . unshift ( mappedArtist ) ;
4747}
4848
49+ function removeCachedArtistByMbid ( mbid ) {
50+ if ( ! mbid || ! Array . isArray ( _cachedArtists ) || _cachedArtists . length === 0 ) {
51+ return ;
52+ }
53+ _cachedArtists = _cachedArtists . filter (
54+ ( artist ) => artist ?. mbid !== mbid && artist ?. foreignArtistId !== mbid ,
55+ ) ;
56+ }
57+
58+ function sleep ( ms ) {
59+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
60+ }
61+
4962async function getLidarrClient ( ) {
5063 if ( ! lidarrClient ) {
5164 try {
@@ -292,8 +305,8 @@ export class LibraryManager {
292305 } ) ;
293306
294307 switch ( artist . monitorOption ) {
295- case "all" :
296308 case "existing" :
309+ case "all" :
297310 albumsToMonitor . push ( ...eligibleAlbums . filter ( ( album ) => ! album . monitored ) ) ;
298311 break ;
299312 case "latest" :
@@ -552,6 +565,69 @@ export class LibraryManager {
552565 }
553566 }
554567
568+ async ensureArtistMonitored ( artist , monitorOption = null ) {
569+ if ( ! artist || artist . monitored !== false ) {
570+ return artist ;
571+ }
572+
573+ const mbid = artist . mbid || artist . foreignArtistId ;
574+ if ( ! mbid ) {
575+ return artist ;
576+ }
577+
578+ const nextMonitorOption =
579+ monitorOption ||
580+ artist . monitorOption ||
581+ artist . addOptions ?. monitor ||
582+ "none" ;
583+ const updated = await this . updateArtist ( mbid , {
584+ monitored : true ,
585+ monitorOption : nextMonitorOption ,
586+ } ) ;
587+ return updated ?. error ? artist : updated ;
588+ }
589+
590+ async ensureRequestedAlbumMonitoring ( artistId , albumId , options = { } ) {
591+ const normalizedArtistId = String ( artistId || "" ) . trim ( ) ;
592+ const normalizedAlbumId = String ( albumId || "" ) . trim ( ) ;
593+ if ( ! normalizedArtistId || ! normalizedAlbumId ) {
594+ return { artist : null , album : null } ;
595+ }
596+
597+ let artist = await this . getArtistById ( normalizedArtistId ) ;
598+ if ( artist ?. monitored === false ) {
599+ artist = await this . ensureArtistMonitored ( artist , options . monitorOption ) ;
600+ }
601+
602+ let album = await this . getAlbumById ( normalizedAlbumId ) ;
603+ if ( album ?. monitored === false ) {
604+ album = await this . updateAlbum ( normalizedAlbumId , { monitored : true } ) ;
605+ }
606+
607+ return { artist, album } ;
608+ }
609+
610+ scheduleRequestedAlbumMonitoringRepair ( artistId , albumId , options = { } ) {
611+ const normalizedArtistId = String ( artistId || "" ) . trim ( ) ;
612+ const normalizedAlbumId = String ( albumId || "" ) . trim ( ) ;
613+ if ( ! normalizedArtistId || ! normalizedAlbumId ) return ;
614+
615+ for ( const delayMs of [ 1000 , 3000 , 8000 , 15000 ] ) {
616+ const timeout = setTimeout ( ( ) => {
617+ this . ensureRequestedAlbumMonitoring (
618+ normalizedArtistId ,
619+ normalizedAlbumId ,
620+ options ,
621+ ) . catch ( ( error ) => {
622+ console . error (
623+ `[LibraryManager] Failed to stabilize requested album monitoring: ${ error . message } ` ,
624+ ) ;
625+ } ) ;
626+ } , delayMs ) ;
627+ timeout . unref ?. ( ) ;
628+ }
629+ }
630+
555631 async getAllArtists ( ) {
556632 try {
557633 const lidarr = await getLidarrClient ( ) ;
@@ -686,8 +762,7 @@ export class LibraryManager {
686762 const artistPath = lidarrArtist . path ?? null ;
687763 const monitorOption =
688764 lidarrArtist . monitor || lidarrArtist . addOptions ?. monitor || "none" ;
689- const normalizedMonitorOption =
690- monitorOption === "existing" ? "all" : monitorOption ;
765+ const normalizedMonitorOption = monitorOption || "none" ;
691766 return {
692767 id : lidarrArtist . id ?. toString ( ) || lidarrArtist . foreignArtistId ,
693768 mbid : lidarrArtist . foreignArtistId ,
@@ -725,8 +800,7 @@ export class LibraryManager {
725800 ) {
726801 const monitorOption =
727802 updates . monitorOption || lidarrArtist . monitor || "none" ;
728- const normalizedMonitorOption =
729- monitorOption === "existing" ? "all" : monitorOption ;
803+ const normalizedMonitorOption = monitorOption || "none" ;
730804 await lidarr . updateArtistMonitoring ( lidarrArtist . id , monitorOption ) ;
731805 console . log (
732806 `[LibraryManager] Updated Lidarr monitoring for "${ lidarrArtist . artistName } " to "${ monitorOption } "` ,
@@ -771,6 +845,7 @@ export class LibraryManager {
771845 if ( ! lidarrArtist )
772846 return { success : false , error : "Artist not found in Lidarr" } ;
773847 await lidarr . deleteArtist ( lidarrArtist . id , deleteFiles ) ;
848+ removeCachedArtistByMbid ( mbid ) ;
774849 console . log (
775850 `[LibraryManager] Deleted artist "${ lidarrArtist . artistName } " from Lidarr` ,
776851 ) ;
@@ -805,7 +880,6 @@ export class LibraryManager {
805880 msg . includes ( "foreignalbumid" )
806881 ) ;
807882 } ;
808- const sleep = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
809883 const settings = getSettings ( ) ;
810884 const searchOnAdd = settings . integrations ?. lidarr ?. searchOnAdd ?? false ;
811885 const shouldTriggerSearch =
@@ -818,6 +892,11 @@ export class LibraryManager {
818892 }
819893 if ( shouldTriggerSearch ) {
820894 await lidarr . triggerAlbumSearch ( existingAlbum . id ) ;
895+ await this . ensureRequestedAlbumMonitoring ( artistId , existingAlbum . id ) ;
896+ this . scheduleRequestedAlbumMonitoringRepair (
897+ artistId ,
898+ existingAlbum . id ,
899+ ) ;
821900 }
822901 const refreshedExisting = await lidarr
823902 . getAlbum ( existingAlbum . id )
@@ -847,6 +926,12 @@ export class LibraryManager {
847926 }
848927 }
849928 if ( ! lidarrArtist ) return { error : "Artist not found in Lidarr" } ;
929+ if ( lidarrArtist . monitored === false ) {
930+ lidarrArtist = await lidarr . updateArtistMonitoring (
931+ artistId ,
932+ lidarrArtist . monitor || lidarrArtist . addOptions ?. monitor || "none" ,
933+ ) ;
934+ }
850935 const existing = await lidarr . getAlbumByMbid ( releaseGroupMbid ) ;
851936 const artistNumericId = parseInt ( artistId , 10 ) ;
852937 const sameArtistExisting =
@@ -912,6 +997,13 @@ export class LibraryManager {
912997 if ( ! lidarrAlbum ) {
913998 return { error : "Failed to add album to Lidarr" } ;
914999 }
1000+ if ( shouldTriggerSearch ) {
1001+ await this . ensureRequestedAlbumMonitoring ( artistId , lidarrAlbum . id ) ;
1002+ this . scheduleRequestedAlbumMonitoringRepair ( artistId , lidarrAlbum . id ) ;
1003+ lidarrAlbum = await lidarr
1004+ . getAlbum ( lidarrAlbum . id )
1005+ . catch ( ( ) => lidarrAlbum ) ;
1006+ }
9151007 const updatedArtist = await lidarr . getArtist ( artistId ) ;
9161008 return this . mapLidarrAlbum ( lidarrAlbum , updatedArtist ) ;
9171009 } catch ( error ) {
@@ -993,6 +1085,8 @@ export class LibraryManager {
9931085 throw error ;
9941086 }
9951087
1088+ artist = await this . ensureArtistMonitored ( artist ) ;
1089+
9961090 let existingAlbum = await lidarr . getAlbumByMbid ( normalizedAlbumMbid ) ;
9971091 if (
9981092 existingAlbum &&
0 commit comments