Skip to content

Commit

Permalink
Merge pull request #67 from macearl/fix-unnecessary-updates
Browse files Browse the repository at this point in the history
add additional checks to prevent unnecessary updates, improves kodi-j…
  • Loading branch information
danieladov authored Jan 5, 2025
2 parents c416c70 + 8238bde commit 582d4c8
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions Jellyfin.Plugin.MergeVersions/MergeVersionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ private async Task MergeVersions(List<Guid> ids)
.LinkedAlternateVersions.Where(l => items.Any(i => i.Path == l.Path))
.ToList();

foreach (var item in items.Where(i => !i.Id.Equals(primaryVersion.Id)))
var alternateVersionsChanged = false;
foreach (var item in items.Where(i =>
!i.Id.Equals(primaryVersion.Id) &&
!alternateVersionsOfPrimary.Any(l => l.ItemId == i.Id)))
{
item.SetPrimaryVersionId(
primaryVersion.Id.ToString("N", CultureInfo.InvariantCulture)
Expand All @@ -210,31 +213,15 @@ await item.UpdateToRepositoryAsync(
)
.ConfigureAwait(false);

if (
!alternateVersionsOfPrimary.Any(i =>
string.Equals(i.Path, item.Path, StringComparison.OrdinalIgnoreCase)
)
)
{
alternateVersionsOfPrimary.Add(
new LinkedChild { Path = item.Path, ItemId = item.Id }
);
}
// TODO: due to check in foreach it can't be an alternate version yet?
AddToAlternateVersionsIfNotPresent(alternateVersionsOfPrimary,
new LinkedChild { Path = item.Path,
ItemId = item.Id });

foreach (var linkedItem in item.LinkedAlternateVersions)
{
if (
!alternateVersionsOfPrimary.Any(i =>
string.Equals(
i.Path,
linkedItem.Path,
StringComparison.OrdinalIgnoreCase
)
)
)
{
alternateVersionsOfPrimary.Add(linkedItem);
}
AddToAlternateVersionsIfNotPresent(alternateVersionsOfPrimary,
linkedItem);
}

if (item.LinkedAlternateVersions.Length > 0)
Expand All @@ -246,12 +233,16 @@ await item.UpdateToRepositoryAsync(
)
.ConfigureAwait(false);
}
alternateVersionsChanged = true;
}

primaryVersion.LinkedAlternateVersions = alternateVersionsOfPrimary.ToArray();
await primaryVersion
.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None)
.ConfigureAwait(false);
if (alternateVersionsChanged)
{
primaryVersion.LinkedAlternateVersions = alternateVersionsOfPrimary.ToArray();
await primaryVersion
.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None)
.ConfigureAwait(false);
}
}

private async Task DeleteAlternateSources(Guid itemId)
Expand Down Expand Up @@ -304,6 +295,19 @@ private bool IsElegible(BaseItem item)
return true;
}

private void AddToAlternateVersionsIfNotPresent(List<LinkedChild> alternateVersions,
LinkedChild newVersion)
{
if (!alternateVersions.Any(
i => string.Equals(i.Path,
newVersion.Path,
StringComparison.OrdinalIgnoreCase
)))
{
alternateVersions.Add(newVersion);
}
}

private void OnTimerElapsed() { }

public void Dispose()
Expand Down

0 comments on commit 582d4c8

Please sign in to comment.