Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add additional checks to prevent unnecessary updates, improves kodi-j… #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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