Fix merge mappings of 2 library items
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 20 Dec 2025 20:41:10 +0000 (21:41 +0100)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 20 Dec 2025 20:41:10 +0000 (21:41 +0100)
music_assistant/controllers/media/base.py

index a82d9392591c9e71731af53c411720c8c99ef7f0..afeaae8e6e8b0f005046149c3a9cd9020ff0d1ab 100644 (file)
@@ -607,11 +607,28 @@ class MediaControllerBase[ItemCls: "MediaItemType"](metaclass=ABCMeta):
             # ignore if the mapping is already present
             if provider_mapping not in library_item.provider_mappings:
                 new_mappings.add(provider_mapping)
-        if new_mappings:
-            library_item.provider_mappings.update(new_mappings)
-            self.mass.music.match_provider_instances(library_item)
-            await self.set_provider_mappings(db_id, library_item.provider_mappings)
-            self.mass.signal_event(EventType.MEDIA_ITEM_UPDATED, library_item.uri, library_item)
+        if not new_mappings:
+            return
+        # handle special case where the user wants to merge 2 library items
+        for mapping in new_mappings:
+            if _library_item := await self.get_library_item_by_prov_id(
+                mapping.item_id, mapping.provider_instance
+            ):
+                if _library_item.item_id != library_item.item_id:
+                    # merging items
+                    self.logger.debug(
+                        "merging item id %s into item id %s based on provider mapping %s/%s",
+                        _library_item.item_id,
+                        library_item.item_id,
+                        mapping.provider_instance,
+                        mapping.item_id,
+                    )
+                    await self.remove_item_from_library(_library_item.item_id, recursive=True)
+                    break
+        library_item.provider_mappings.update(new_mappings)
+        self.mass.music.match_provider_instances(library_item)
+        await self.set_provider_mappings(db_id, library_item.provider_mappings)
+        self.mass.signal_event(EventType.MEDIA_ITEM_UPDATED, library_item.uri, library_item)
 
     @final
     async def remove_provider_mapping(