44using MediaBrowser . Controller . Entities ;
55using MediaBrowser . Controller . Entities . Movies ;
66using MediaBrowser . Controller . Library ;
7+ using MediaBrowser . Controller . Providers ;
78using Microsoft . Extensions . Logging ;
89
910namespace Gelato . Decorators ;
@@ -12,6 +13,8 @@ public sealed class CollectionManagerDecorator(
1213 ICollectionManager inner ,
1314 Lazy < GelatoManager > manager ,
1415 ILibraryManager libraryManager ,
16+ IProviderManager providerManager ,
17+ IDirectoryService directoryService ,
1518 ILogger < CollectionManagerDecorator > log
1619) : ICollectionManager
1720{
@@ -21,11 +24,7 @@ public event EventHandler<CollectionCreatedEventArgs>? CollectionCreated
2124 remove => inner . CollectionCreated -= value ;
2225 }
2326
24- public event EventHandler < CollectionModifiedEventArgs > ? ItemsAddedToCollection
25- {
26- add => inner . ItemsAddedToCollection += value ;
27- remove => inner . ItemsAddedToCollection -= value ;
28- }
27+ public event EventHandler < CollectionModifiedEventArgs > ? ItemsAddedToCollection ;
2928
3029 public event EventHandler < CollectionModifiedEventArgs > ? ItemsRemovedFromCollection
3130 {
@@ -38,64 +37,68 @@ public Task<BoxSet> CreateCollectionAsync(CollectionCreationOptions options) =>
3837
3938 public async Task AddToCollectionAsync ( Guid collectionId , IEnumerable < Guid > itemIds )
4039 {
41- var guids = itemIds as Guid [ ] ?? itemIds . ToArray ( ) ;
42- await inner . AddToCollectionAsync ( collectionId , guids ) . ConfigureAwait ( false ) ;
43-
4440 if ( libraryManager . GetItemById ( collectionId ) is not BoxSet collection )
45- return ;
46-
47- var gelatoItems = guids
48- . Select ( libraryManager . GetItemById )
49- . Where ( item => item is not null && item . IsGelato ( ) )
50- . ToList ( ) ;
51-
52- if ( gelatoItems . Count == 0 )
53- return ;
41+ throw new ArgumentException (
42+ "No collection exists with the supplied collectionId " + collectionId
43+ ) ;
5444
55- var needsFix = false ;
45+ List < BaseItem > ? itemList = null ;
46+ var linkedChildrenList = collection . GetLinkedChildren ( ) ;
47+ var currentLinkedChildrenIds = linkedChildrenList . Select ( i => i . Id ) . ToList ( ) ;
5648
57- for ( var i = 0 ; i < collection . LinkedChildren . Length ; i ++ )
49+ foreach ( var id in itemIds )
5850 {
59- var linkedChild = collection . LinkedChildren [ i ] ;
51+ var item =
52+ libraryManager . GetItemById ( id )
53+ ?? throw new ArgumentException ( "No item exists with the supplied Id " + id ) ;
6054
61- if (
62- string . IsNullOrEmpty ( linkedChild . LibraryItemId )
63- && ! string . IsNullOrEmpty ( linkedChild . Path )
64- )
55+ if ( ! currentLinkedChildrenIds . Contains ( id ) )
6556 {
66- var matchingItem = gelatoItems . FirstOrDefault ( item =>
67- item ? . Path == linkedChild . Path
68- ) ;
69-
70- if ( matchingItem != null )
71- {
72- log . LogDebug (
73- "Fixing Gelato LinkedChild with path {Path} for item {Id}" ,
74- linkedChild . Path ,
75- matchingItem . Id
76- ) ;
77-
78- collection . LinkedChildren [ i ] = new LinkedChild
79- {
80- LibraryItemId = matchingItem . Id . ToString ( "N" , CultureInfo . InvariantCulture ) ,
81- Type = LinkedChildType . Manual ,
82- } ;
83- needsFix = true ;
84- }
57+ ( itemList ??= [ ] ) . Add ( item ) ;
58+ linkedChildrenList . Add ( item ) ;
8559 }
8660 }
8761
88- if ( needsFix )
62+ if ( itemList is null )
63+ return ;
64+
65+ var originalLen = collection . LinkedChildren . Length ;
66+ LinkedChild [ ] newChildren = new LinkedChild [ originalLen + itemList . Count ] ;
67+ collection . LinkedChildren . CopyTo ( newChildren , 0 ) ;
68+
69+ for ( var i = 0 ; i < itemList . Count ; i ++ )
8970 {
71+ var item = itemList [ i ] ;
72+ newChildren [ originalLen + i ] = item . IsGelato ( )
73+ ? new LinkedChild
74+ {
75+ LibraryItemId = item . Id . ToString ( "N" , CultureInfo . InvariantCulture ) ,
76+ Type = LinkedChildType . Manual ,
77+ }
78+ : LinkedChild . Create ( item ) ;
79+
9080 log . LogDebug (
91- "Fixing {Count} Gelato items in collection {Name}" ,
92- gelatoItems . Count ,
81+ "Adding item {Id} (Gelato={IsGelato}) to collection {Name}" ,
82+ item . Id ,
83+ item . IsGelato ( ) ,
9384 collection . Name
9485 ) ;
95- await collection
96- . UpdateToRepositoryAsync ( ItemUpdateType . MetadataEdit , CancellationToken . None )
97- . ConfigureAwait ( false ) ;
9886 }
87+
88+ collection . LinkedChildren = newChildren ;
89+ collection . UpdateRatingToItems ( linkedChildrenList ) ;
90+
91+ await collection
92+ . UpdateToRepositoryAsync ( ItemUpdateType . MetadataEdit , CancellationToken . None )
93+ . ConfigureAwait ( false ) ;
94+
95+ providerManager . QueueRefresh (
96+ collection . Id ,
97+ new MetadataRefreshOptions ( directoryService ) { ForceSave = true } ,
98+ RefreshPriority . High
99+ ) ;
100+
101+ ItemsAddedToCollection ? . Invoke ( this , new CollectionModifiedEventArgs ( collection , itemList ) ) ;
99102 }
100103
101104 public Task RemoveFromCollectionAsync ( Guid collectionId , IEnumerable < Guid > itemIds ) =>
0 commit comments