Skip to content

Commit

Permalink
refactor: better align collection support with JF core
Browse files Browse the repository at this point in the history
- Better align the collection support in the plugin with the collection
  support in core JF by enforcing the requirement of at least two
  entries before creating a collection by default, but also allow the
  requirement to be toggled off so it will always create the collections regardless of the number of items within the collection.
  • Loading branch information
revam committed May 18, 2024
1 parent 53eaf82 commit 7562996
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
22 changes: 16 additions & 6 deletions Shokofin/Collections/CollectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class CollectionManager

private readonly ShokoAPIManager ApiManager;

private static int MinCollectionSize => Plugin.Instance.Configuration.CollectionMinSizeOfTwo ? 1 : 0;

public CollectionManager(ILibraryManager libraryManager, ICollectionManager collectionManager, ILogger<CollectionManager> logger, IIdLookup lookup, ShokoAPIManager apiManager)
{
LibraryManager = libraryManager;
Expand Down Expand Up @@ -82,10 +84,12 @@ private async Task ReconstructMovieSeriesCollections(IProgress<double> progress,

movieDict.Add(movie, (fileInfo, seasonInfo, showInfo));
}
// Filter to only "seasons" with at least (`MinCollectionSize` + 1) movies in them.
var seasonDict = movieDict.Values
.Select(tuple => tuple.seasonInfo)
.DistinctBy(seasonInfo => seasonInfo.Id)
.ToDictionary(seasonInfo => seasonInfo.Id);
.GroupBy(seasonInfo => seasonInfo.Id)
.Where(groupBy => groupBy.Count() > MinCollectionSize)
.ToDictionary(groupBy => groupBy.Key, groupBy => groupBy.First());

cancellationToken.ThrowIfCancellationRequested();
progress.Report(30);
Expand Down Expand Up @@ -229,15 +233,21 @@ private async Task ReconstructSharedCollections(IProgress<double> progress, Canc
cancellationToken.ThrowIfCancellationRequested();
progress.Report(30);

// Filter to only collections with at least (`MinCollectionSize` + 1) entries in them.
var groupsDict = await Task
.WhenAll(
movieDict.Values
.Select(tuple => tuple.seasonInfo)
.DistinctBy(seasonInfo => seasonInfo.Id)
.Select(seasonInfo => seasonInfo.Shoko.IDs.ParentGroup.ToString())
.Concat(showDict.Values.Select(showInfo => showInfo.CollectionId).Where(collectionId => !string.IsNullOrEmpty(collectionId)).OfType<string>())
.Distinct()
.Select(groupId => ApiManager.GetCollectionInfoForGroup(groupId))
.Concat(
showDict.Values
.Select(showInfo => showInfo.CollectionId)
.Where(collectionId => !string.IsNullOrEmpty(collectionId))
.OfType<string>()
)
.GroupBy(collectionId => collectionId)
.Where(groupBy => groupBy.Count() > MinCollectionSize)
.Select(groupBy => ApiManager.GetCollectionInfoForGroup(groupBy.Key))
)
.ContinueWith(task => task.Result.ToDictionary(x => x!.Id, x => x!));
var finalGroups = new Dictionary<string, CollectionInfo>();
Expand Down
7 changes: 7 additions & 0 deletions Shokofin/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ public virtual string PrettyUrl
/// </summary>
public CollectionCreationType CollectionGrouping { get; set; }

/// <summary>
/// Add a minimum requirement of two entries with the same collection id
/// before creating a collection for them.
/// </summary>
public bool CollectionMinSizeOfTwo { get; set; }

/// <summary>
/// Determines how seasons are ordered within a show.
/// </summary>
Expand Down Expand Up @@ -378,6 +384,7 @@ public PluginConfiguration()
AddMissingMetadata = true;
MarkSpecialsWhenGrouped = true;
CollectionGrouping = CollectionCreationType.None;
CollectionMinSizeOfTwo = true;
UserList = new();
MediaFolders = new();
IgnoredFolders = new[] { ".streams", "@recently-snapshot" };
Expand Down
3 changes: 3 additions & 0 deletions Shokofin/Configuration/configController.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ async function defaultSubmit(form) {
config.UseGroupsForShows = form.querySelector("#UseGroupsForShows").checked;
config.SeasonOrdering = form.querySelector("#SeasonOrdering").value;
config.CollectionGrouping = form.querySelector("#CollectionGrouping").value;
config.CollectionMinSizeOfTwo = form.querySelector("#CollectionMinSizeOfTwo").checked;
config.SeparateMovies = form.querySelector("#SeparateMovies").checked;
config.SpecialsPlacement = form.querySelector("#SpecialsPlacement").value;
config.MovieSpecialsAsExtraFeaturettes = form.querySelector("#MovieSpecialsAsExtraFeaturettes").checked;
Expand Down Expand Up @@ -482,6 +483,7 @@ async function syncSettings(form) {
config.SeasonOrdering = form.querySelector("#SeasonOrdering").value;
config.SeparateMovies = form.querySelector("#SeparateMovies").checked;
config.CollectionGrouping = form.querySelector("#CollectionGrouping").value;
config.CollectionMinSizeOfTwo = form.querySelector("#CollectionMinSizeOfTwo").checked;
config.SpecialsPlacement = form.querySelector("#SpecialsPlacement").value;
config.MovieSpecialsAsExtraFeaturettes = form.querySelector("#MovieSpecialsAsExtraFeaturettes").checked;
config.AddMissingMetadata = form.querySelector("#AddMissingMetadata").checked;
Expand Down Expand Up @@ -795,6 +797,7 @@ export default function (page) {
form.querySelector("#SeasonOrderingContainer").setAttribute("hidden", "");
}
form.querySelector("#CollectionGrouping").value = config.CollectionGrouping || "Default";
form.querySelector("#CollectionMinSizeOfTwo").checked = config.CollectionMinSizeOfTwo != null ? config.CollectionMinSizeOfTwo : true;
form.querySelector("#SeparateMovies").checked = config.SeparateMovies != null ? config.SeparateMovies : true;
form.querySelector("#SpecialsPlacement").value = config.SpecialsPlacement === "Default" ? "AfterSeason" : config.SpecialsPlacement;
form.querySelector("#MovieSpecialsAsExtraFeaturettes").checked = config.MovieSpecialsAsExtraFeaturettes || false;
Expand Down
7 changes: 7 additions & 0 deletions Shokofin/Configuration/configPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ <h3>Library Settings</h3>
</select>
<div class="fieldDescription">Determines what entities to group into collections.</div>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input is="emby-checkbox" type="checkbox" id="CollectionMinSizeOfTwo" />
<span>Require two entries for a collection</span>
</label>
<div class="fieldDescription checkboxFieldDescription">Add a minimum requirement of two entries with the same collection id before creating a collection for them.</div>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input is="emby-checkbox" type="checkbox" id="MovieSpecialsAsExtraFeaturettes" />
Expand Down

0 comments on commit 7562996

Please sign in to comment.