Update saved metadata on primary change

This commit is contained in:
Shadowghost
2026-02-25 21:03:46 +01:00
parent 4bd9dbe910
commit 2d0d497961
6 changed files with 42 additions and 13 deletions

View File

@@ -441,7 +441,7 @@ namespace Emby.Server.Implementations.Library
newPrimary.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).GetAwaiter().GetResult();
// Re-route playlist/collection references from deleted primary to new primary
_itemRepository.RerouteLinkedChildren(video.Id, newPrimary.Id);
RerouteLinkedChildReferencesAsync(video.Id, newPrimary.Id).GetAwaiter().GetResult();
// Update remaining alternates to point to new primary
foreach (var alternate in alternateVersions.Skip(1))
@@ -455,7 +455,7 @@ namespace Emby.Server.Implementations.Library
else if (item is Video alternateVideo && alternateVideo.PrimaryVersionId.HasValue)
{
// If deleting an alternate version, re-route references to its primary
_itemRepository.RerouteLinkedChildren(alternateVideo.Id, alternateVideo.PrimaryVersionId.Value);
RerouteLinkedChildReferencesAsync(alternateVideo.Id, alternateVideo.PrimaryVersionId.Value).GetAwaiter().GetResult();
}
var children = item.IsFolder
@@ -3655,9 +3655,26 @@ namespace Emby.Server.Implementations.Library
}
/// <inheritdoc />
public int RerouteLinkedChildReferences(Guid fromChildId, Guid toChildId)
public async Task RerouteLinkedChildReferencesAsync(Guid fromChildId, Guid toChildId)
{
return _itemRepository.RerouteLinkedChildren(fromChildId, toChildId);
var affectedParentIds = _itemRepository.RerouteLinkedChildren(fromChildId, toChildId);
// Update in-memory LinkedChildren and re-save metadata (NFO) for affected parents
foreach (var parentId in affectedParentIds)
{
if (GetItemById(parentId) is Folder parent)
{
foreach (var lc in parent.LinkedChildren)
{
if (lc.ItemId.HasValue && lc.ItemId.Value.Equals(fromChildId))
{
lc.ItemId = toChildId;
}
}
await RunMetadataSavers(parent, ItemUpdateType.MetadataEdit).ConfigureAwait(false);
}
}
}
/// <inheritdoc />

View File

@@ -223,7 +223,7 @@ public class VideosController : BaseJellyfinApiController
await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
// Re-route any playlist/collection references from this item to the primary
_libraryManager.RerouteLinkedChildReferences(item.Id, primaryVersion.Id);
await _libraryManager.RerouteLinkedChildReferencesAsync(item.Id, primaryVersion.Id).ConfigureAwait(false);
if (!alternateVersionsOfPrimary.Any(i => i.ItemId.HasValue && i.ItemId.Value.Equals(item.Id)))
{

View File

@@ -4439,10 +4439,22 @@ public sealed class BaseItemRepository
}
/// <inheritdoc/>
public int RerouteLinkedChildren(Guid fromChildId, Guid toChildId)
public IReadOnlyList<Guid> RerouteLinkedChildren(Guid fromChildId, Guid toChildId)
{
using var context = _dbProvider.CreateDbContext();
// Collect all affected parent IDs before modifying
var affectedParentIds = context.LinkedChildren
.Where(lc => lc.ChildId == fromChildId && lc.ChildType == DbLinkedChildType.Manual)
.Select(lc => lc.ParentId)
.Distinct()
.ToList();
if (affectedParentIds.Count == 0)
{
return affectedParentIds;
}
// Get parents that already reference toChildId (to avoid duplicates)
var parentsWithTarget = context.LinkedChildren
.Where(lc => lc.ChildId == toChildId && lc.ChildType == DbLinkedChildType.Manual)
@@ -4450,7 +4462,7 @@ public sealed class BaseItemRepository
.ToHashSet();
// Update references that won't create duplicates
var updated = context.LinkedChildren
context.LinkedChildren
.Where(lc => lc.ChildId == fromChildId
&& lc.ChildType == DbLinkedChildType.Manual
&& !parentsWithTarget.Contains(lc.ParentId))
@@ -4463,7 +4475,7 @@ public sealed class BaseItemRepository
&& parentsWithTarget.Contains(lc.ParentId))
.ExecuteDelete();
return updated;
return affectedParentIds;
}
/// <inheritdoc/>

View File

@@ -590,7 +590,7 @@ namespace MediaBrowser.Controller.Entities
await oldPrimary.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false);
// Re-route playlist/collection references from old primary to new primary
LibraryManager.RerouteLinkedChildReferences(oldPrimary.Id, newPrimary.Id);
await LibraryManager.RerouteLinkedChildReferencesAsync(oldPrimary.Id, newPrimary.Id).ConfigureAwait(false);
}
// After removing items, reattach any detached user data to remaining children

View File

@@ -767,8 +767,8 @@ namespace MediaBrowser.Controller.Library
/// </summary>
/// <param name="fromChildId">The child ID to re-route from.</param>
/// <param name="toChildId">The child ID to re-route to.</param>
/// <returns>Number of references updated.</returns>
int RerouteLinkedChildReferences(Guid fromChildId, Guid toChildId);
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task RerouteLinkedChildReferencesAsync(Guid fromChildId, Guid toChildId);
/// <summary>
/// Gets legacy query filters for filtering UI.

View File

@@ -254,8 +254,8 @@ public interface IItemRepository
/// </summary>
/// <param name="fromChildId">The child ID to re-route from.</param>
/// <param name="toChildId">The child ID to re-route to.</param>
/// <returns>Number of references updated.</returns>
int RerouteLinkedChildren(Guid fromChildId, Guid toChildId);
/// <returns>List of parent item IDs whose LinkedChildren were modified.</returns>
IReadOnlyList<Guid> RerouteLinkedChildren(Guid fromChildId, Guid toChildId);
/// <summary>
/// Creates or updates a LinkedChild entry linking a parent to a child item.