mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-07-06 06:12:52 +01:00
Coalesce alternate-version progress onto primary in resume filter
This commit is contained in:
@@ -167,9 +167,13 @@ namespace Emby.Server.Implementations.Dto
|
||||
|
||||
// Batch-fetch user data for all items
|
||||
Dictionary<Guid, UserItemData>? userDataBatch = null;
|
||||
IReadOnlyDictionary<Guid, VersionResumeData>? resumeDataBatch = null;
|
||||
if (user is not null && options.EnableUserData)
|
||||
{
|
||||
userDataBatch = _userDataRepository.GetUserDataBatch(accessibleItems, user);
|
||||
|
||||
// For items with alternate versions, the most recently played version drives resume.
|
||||
resumeDataBatch = _userDataRepository.GetResumeUserDataBatch(accessibleItems, user);
|
||||
}
|
||||
|
||||
// Pre-compute collection folders once to avoid N+1 queries in CanDelete
|
||||
@@ -248,7 +252,8 @@ namespace Emby.Server.Implementations.Dto
|
||||
allCollectionFolders,
|
||||
childCountBatch,
|
||||
playedCountBatch,
|
||||
artistsBatch);
|
||||
artistsBatch,
|
||||
resumeDataBatch?.GetValueOrDefault(item.Id));
|
||||
|
||||
if (item is LiveTvChannel tvChannel)
|
||||
{
|
||||
@@ -309,7 +314,8 @@ namespace Emby.Server.Implementations.Dto
|
||||
List<Folder>? allCollectionFolders = null,
|
||||
Dictionary<Guid, int>? childCountBatch = null,
|
||||
Dictionary<Guid, (int Played, int Total)>? playedCountBatch = null,
|
||||
IReadOnlyDictionary<string, MusicArtist[]>? artistsBatch = null)
|
||||
IReadOnlyDictionary<string, MusicArtist[]>? artistsBatch = null,
|
||||
VersionResumeData? resumeData = null)
|
||||
{
|
||||
var dto = new BaseItemDto
|
||||
{
|
||||
@@ -353,7 +359,8 @@ namespace Emby.Server.Implementations.Dto
|
||||
options,
|
||||
userData,
|
||||
childCountBatch,
|
||||
playedCountBatch);
|
||||
playedCountBatch,
|
||||
resumeData);
|
||||
}
|
||||
|
||||
if (item is IHasMediaSources
|
||||
@@ -538,7 +545,8 @@ namespace Emby.Server.Implementations.Dto
|
||||
DtoOptions options,
|
||||
UserItemData? userData = null,
|
||||
Dictionary<Guid, int>? childCountBatch = null,
|
||||
Dictionary<Guid, (int Played, int Total)>? playedCountBatch = null)
|
||||
Dictionary<Guid, (int Played, int Total)>? playedCountBatch = null,
|
||||
VersionResumeData? resumeData = null)
|
||||
{
|
||||
if (item.IsFolder)
|
||||
{
|
||||
@@ -600,6 +608,9 @@ namespace Emby.Server.Implementations.Dto
|
||||
// Use pre-fetched user data
|
||||
dto.UserData = GetUserItemDataDto(userData, item.Id);
|
||||
item.FillUserDataDtoValues(dto.UserData, userData, dto, user, options);
|
||||
|
||||
// For items with alternate versions, the most recently played version drives resume.
|
||||
resumeData?.ApplyTo(dto.UserData);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -229,7 +229,11 @@ namespace Emby.Server.Implementations.Library
|
||||
list.Add(source);
|
||||
}
|
||||
|
||||
return SortMediaSources(list).ToArray();
|
||||
var preferredId = mediaSources.Count > 0 && Guid.TryParse(mediaSources[0].Id, out var topSourceId)
|
||||
? topSourceId
|
||||
: item.Id;
|
||||
|
||||
return SortMediaSources(list, preferredId).ToArray();
|
||||
}
|
||||
|
||||
/// <inheritdoc />>
|
||||
@@ -400,6 +404,72 @@ namespace Emby.Server.Implementations.Library
|
||||
source.SupportsDirectStream = user.HasPermission(PermissionKind.EnablePlaybackRemuxing);
|
||||
}
|
||||
}
|
||||
|
||||
sources = SetAlternateVersionResumeStates(item, sources, user);
|
||||
}
|
||||
|
||||
return sources;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Populates each source's own playback position for the user and, when the queried item is a
|
||||
/// primary, moves the most recently played version to the front so that resuming without an
|
||||
/// explicit source selection plays the version that was last watched. A directly queried
|
||||
/// alternate version keeps its own source first.
|
||||
/// </summary>
|
||||
/// <param name="item">The queried item.</param>
|
||||
/// <param name="sources">The item's media sources.</param>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <returns>The media sources, reordered when a version drives resume.</returns>
|
||||
private IReadOnlyList<MediaSourceInfo> SetAlternateVersionResumeStates(BaseItem item, IReadOnlyList<MediaSourceInfo> sources, User user)
|
||||
{
|
||||
// For a video, multiple sources means alternate versions.
|
||||
if (item is not Video video || sources.Count < 2)
|
||||
{
|
||||
return sources;
|
||||
}
|
||||
|
||||
var versions = video.GetAllVersions();
|
||||
if (versions.Count < 2)
|
||||
{
|
||||
return sources;
|
||||
}
|
||||
|
||||
var userDataByVersion = _userDataManager.GetUserDataBatch(versions, user);
|
||||
var dataBySourceId = new Dictionary<string, UserItemData>(versions.Count, StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var version in versions)
|
||||
{
|
||||
if (userDataByVersion.TryGetValue(version.Id, out var data))
|
||||
{
|
||||
dataBySourceId[version.Id.ToString("N", CultureInfo.InvariantCulture)] = data;
|
||||
}
|
||||
}
|
||||
|
||||
MediaSourceInfo resumeSource = null;
|
||||
UserItemData resumeData = null;
|
||||
foreach (var source in sources)
|
||||
{
|
||||
if (source.Id is null
|
||||
|| !dataBySourceId.TryGetValue(source.Id, out var data)
|
||||
|| data.PlaybackPositionTicks <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
source.PlaybackPositionTicks = data.PlaybackPositionTicks;
|
||||
|
||||
if (resumeData is null || (data.LastPlayedDate ?? DateTime.MinValue) > (resumeData.LastPlayedDate ?? DateTime.MinValue))
|
||||
{
|
||||
resumeSource = source;
|
||||
resumeData = data;
|
||||
}
|
||||
}
|
||||
|
||||
if (resumeSource is not null && !video.PrimaryVersionId.HasValue && !ReferenceEquals(sources[0], resumeSource))
|
||||
{
|
||||
var reordered = new List<MediaSourceInfo>(sources.Count) { resumeSource };
|
||||
reordered.AddRange(sources.Where(s => !ReferenceEquals(s, resumeSource)));
|
||||
return reordered;
|
||||
}
|
||||
|
||||
return sources;
|
||||
|
||||
@@ -247,6 +247,72 @@ namespace Emby.Server.Implementations.Library
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public VersionResumeData? GetResumeUserData(User user, BaseItem item)
|
||||
{
|
||||
return GetResumeUserDataBatch([item], user).GetValueOrDefault(item.Id);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyDictionary<Guid, VersionResumeData> GetResumeUserDataBatch(IReadOnlyList<BaseItem> items, User user)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(user);
|
||||
|
||||
var result = new Dictionary<Guid, VersionResumeData>();
|
||||
List<(Guid PrimaryId, IReadOnlyList<Video> Versions)>? versionGroups = null;
|
||||
List<BaseItem>? allVersions = null;
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
// Only primary items aggregate over their versions; a directly queried version keeps its own data.
|
||||
if (item is not Video video
|
||||
|| video.PrimaryVersionId.HasValue
|
||||
|| (video.LinkedAlternateVersions.Length == 0 && !video.HasLocalAlternateVersions))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var versions = video.GetAllVersions();
|
||||
if (versions.Count < 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
(versionGroups ??= []).Add((item.Id, versions));
|
||||
(allVersions ??= []).AddRange(versions);
|
||||
}
|
||||
|
||||
if (versionGroups is null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
var userDataByVersion = GetUserDataBatch(allVersions!.DistinctBy(i => i.Id).ToList(), user);
|
||||
|
||||
foreach (var (primaryId, versions) in versionGroups)
|
||||
{
|
||||
Video? resumeVersion = null;
|
||||
UserItemData? resumeData = null;
|
||||
foreach (var version in versions)
|
||||
{
|
||||
if (userDataByVersion.TryGetValue(version.Id, out var data)
|
||||
&& data.PlaybackPositionTicks > 0
|
||||
&& (resumeData is null || (data.LastPlayedDate ?? DateTime.MinValue) > (resumeData.LastPlayedDate ?? DateTime.MinValue)))
|
||||
{
|
||||
resumeVersion = version;
|
||||
resumeData = data;
|
||||
}
|
||||
}
|
||||
|
||||
if (resumeData is not null)
|
||||
{
|
||||
result[primaryId] = new VersionResumeData(resumeData, resumeVersion!.RunTimeTicks);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the internal key.
|
||||
/// </summary>
|
||||
@@ -281,6 +347,10 @@ namespace Emby.Server.Implementations.Library
|
||||
var dto = GetUserItemDataDto(userData, item.Id);
|
||||
|
||||
item.FillUserDataDtoValues(dto, userData, itemDto, user, options);
|
||||
|
||||
// For an item with alternate versions, surface the most recently played version's resume point.
|
||||
GetResumeUserData(user, item)?.ApplyTo(dto);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
|
||||
@@ -519,7 +519,6 @@ public sealed partial class BaseItemRepository
|
||||
// In-progress user data rows; alternate versions track their own progress.
|
||||
var inProgress = context.UserData
|
||||
.Where(ud => ud.UserId == userId && ud.PlaybackPositionTicks > 0);
|
||||
var resumableItemIds = inProgress.Select(ud => ud.ItemId);
|
||||
|
||||
if (hasSeries)
|
||||
{
|
||||
@@ -544,13 +543,26 @@ public sealed partial class BaseItemRepository
|
||||
.Where(s => s.HasInProgress || (s.HasPlayed && s.HasUnplayed))
|
||||
.Select(s => s.SeriesId);
|
||||
|
||||
// Non-series items: resumable if the item or any of its alternate versions has
|
||||
// PlaybackPositionTicks > 0. Alternate versions (PrimaryVersionId set) are excluded
|
||||
// from the base query, so coalesce their progress onto the primary's id.
|
||||
var resumableMovieIds = context.BaseItems
|
||||
.Where(bi => bi.UserData!.Any(ud => ud.UserId == userId && ud.PlaybackPositionTicks > 0))
|
||||
.Select(bi => bi.PrimaryVersionId ?? bi.Id);
|
||||
|
||||
baseQuery = baseQuery.Where(e =>
|
||||
(e.Type == seriesTypeName && resumableSeriesIds.Contains(e.Id) == isResumable)
|
||||
|| (e.Type != seriesTypeName && resumableItemIds.Contains(e.Id) == isResumable));
|
||||
|| (e.Type != seriesTypeName && resumableMovieIds.Contains(e.Id) == isResumable));
|
||||
}
|
||||
else
|
||||
{
|
||||
baseQuery = baseQuery.Where(e => resumableItemIds.Contains(e.Id) == isResumable);
|
||||
// Resumable if the item or any of its alternate versions has PlaybackPositionTicks > 0.
|
||||
// Alternate versions (PrimaryVersionId set) are excluded from the base query, so
|
||||
// coalesce their progress onto the primary's id.
|
||||
var resumableMovieIds = context.BaseItems
|
||||
.Where(bi => bi.UserData!.Any(ud => ud.UserId == userId && ud.PlaybackPositionTicks > 0))
|
||||
.Select(bi => bi.PrimaryVersionId ?? bi.Id);
|
||||
baseQuery = baseQuery.Where(e => resumableMovieIds.Contains(e.Id) == isResumable);
|
||||
}
|
||||
|
||||
if (isResumable)
|
||||
|
||||
@@ -62,6 +62,24 @@ namespace MediaBrowser.Controller.Library
|
||||
/// <returns>A dictionary mapping item IDs to their user data.</returns>
|
||||
Dictionary<Guid, UserItemData> GetUserDataBatch(IReadOnlyList<BaseItem> items, User user);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user data that should drive resume for a multi-version item: the data of the most
|
||||
/// recently played alternate version (including the item itself) that has a resume point.
|
||||
/// </summary>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <param name="item">The item.</param>
|
||||
/// <returns>The resume version's data, or <c>null</c> when the item has no versions or none has a resume point.</returns>
|
||||
VersionResumeData? GetResumeUserData(User user, BaseItem item);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resume-driving user data for multiple items in a single batch operation.
|
||||
/// See <see cref="GetResumeUserData(User, BaseItem)"/>.
|
||||
/// </summary>
|
||||
/// <param name="items">The items to get resume data for.</param>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <returns>A dictionary mapping item ids to their resume version's data; items without one are omitted.</returns>
|
||||
IReadOnlyDictionary<Guid, VersionResumeData> GetResumeUserDataBatch(IReadOnlyList<BaseItem> items, User user);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user data dto.
|
||||
/// </summary>
|
||||
|
||||
30
MediaBrowser.Controller/Library/VersionResumeData.cs
Normal file
30
MediaBrowser.Controller/Library/VersionResumeData.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Model.Dto;
|
||||
|
||||
namespace MediaBrowser.Controller.Library
|
||||
{
|
||||
/// <summary>
|
||||
/// The user data of the alternate version that should drive resume for a multi-version item.
|
||||
/// </summary>
|
||||
/// <param name="UserData">The resume version's user data.</param>
|
||||
/// <param name="RunTimeTicks">The resume version's runtime, used for the progress percentage.</param>
|
||||
public record VersionResumeData(UserItemData UserData, long? RunTimeTicks)
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies the resume version's playback state to the supplied user data dto, so that an item
|
||||
/// whose most recent progress lives on an alternate version still reports that progress.
|
||||
/// </summary>
|
||||
/// <param name="dto">The user data dto to update.</param>
|
||||
public void ApplyTo(UserItemDataDto dto)
|
||||
{
|
||||
dto.PlaybackPositionTicks = UserData.PlaybackPositionTicks;
|
||||
dto.Played = UserData.Played;
|
||||
dto.LastPlayedDate = UserData.LastPlayedDate;
|
||||
|
||||
if (RunTimeTicks > 0 && UserData.PlaybackPositionTicks > 0)
|
||||
{
|
||||
dto.PlayedPercentage = 100.0 * UserData.PlaybackPositionTicks / RunTimeTicks.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,11 @@ namespace MediaBrowser.Model.Dto
|
||||
|
||||
public long? RunTimeTicks { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the playback position for this specific source.
|
||||
/// </summary>
|
||||
public long? PlaybackPositionTicks { get; set; }
|
||||
|
||||
public bool ReadAtNativeFramerate { get; set; }
|
||||
|
||||
public bool IgnoreDts { get; set; }
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Dto;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Controller.Tests.Library;
|
||||
|
||||
public class VersionResumeDataTests
|
||||
{
|
||||
[Fact]
|
||||
public void ApplyTo_OverridesResumeFieldsAndPercentage()
|
||||
{
|
||||
var lastPlayed = new DateTime(2026, 1, 2, 0, 0, 0, DateTimeKind.Utc);
|
||||
var resume = new VersionResumeData(
|
||||
new UserItemData { Key = "version", PlaybackPositionTicks = 25, Played = true, LastPlayedDate = lastPlayed },
|
||||
RunTimeTicks: 100);
|
||||
|
||||
var dto = new UserItemDataDto { Key = "primary", PlaybackPositionTicks = 1, Played = false, PlayedPercentage = 1 };
|
||||
|
||||
resume.ApplyTo(dto);
|
||||
|
||||
Assert.Equal(25, dto.PlaybackPositionTicks);
|
||||
Assert.True(dto.Played);
|
||||
Assert.Equal(lastPlayed, dto.LastPlayedDate);
|
||||
|
||||
// The percentage is based on the resume version's own runtime, not the primary's.
|
||||
Assert.NotNull(dto.PlayedPercentage);
|
||||
Assert.Equal(25.0, dto.PlayedPercentage.Value, 5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ApplyTo_WithoutRuntime_LeavesPercentageUntouched()
|
||||
{
|
||||
var resume = new VersionResumeData(new UserItemData { Key = "version", PlaybackPositionTicks = 25 }, null);
|
||||
var dto = new UserItemDataDto { Key = "primary", PlayedPercentage = 42 };
|
||||
|
||||
resume.ApplyTo(dto);
|
||||
|
||||
Assert.Equal(25, dto.PlaybackPositionTicks);
|
||||
Assert.NotNull(dto.PlayedPercentage);
|
||||
Assert.Equal(42.0, dto.PlayedPercentage.Value, 5);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
#pragma warning disable RS0030 // Do not use banned APIs: Guid == is required inside EF expression trees to mirror the production query shapes.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Jellyfin.Database.Implementations;
|
||||
using Jellyfin.Database.Implementations.Entities;
|
||||
using Jellyfin.Database.Implementations.Locking;
|
||||
using Jellyfin.Database.Providers.Sqlite;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Tests.Item;
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the alternate-version-aware query shapes used by the resume filter
|
||||
/// (BaseItemRepository.TranslateQuery) and the DatePlayed ordering (OrderMapper) translate
|
||||
/// and evaluate correctly on the SQLite provider.
|
||||
/// </summary>
|
||||
public sealed class AlternateVersionQueryTranslationTests : IDisposable
|
||||
{
|
||||
private readonly SqliteConnection _connection;
|
||||
private readonly DbContextOptions<JellyfinDbContext> _dbOptions;
|
||||
|
||||
public AlternateVersionQueryTranslationTests()
|
||||
{
|
||||
_connection = new SqliteConnection("Data Source=:memory:");
|
||||
_connection.Open();
|
||||
|
||||
_dbOptions = new DbContextOptionsBuilder<JellyfinDbContext>()
|
||||
.UseSqlite(_connection)
|
||||
.Options;
|
||||
|
||||
using var ctx = CreateDbContext();
|
||||
ctx.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResumeFilter_VersionProgress_SurfacesPrimary()
|
||||
{
|
||||
Guid userId, primaryId, otherId;
|
||||
|
||||
using (var ctx = CreateDbContext())
|
||||
{
|
||||
(userId, primaryId, otherId) = Seed(ctx);
|
||||
}
|
||||
|
||||
using (var ctx = CreateDbContext())
|
||||
{
|
||||
// Mirrors the resumable filter in BaseItemRepository.TranslateQuery: progress on any
|
||||
// version coalesces onto the primary's id.
|
||||
var resumableMovieIds = ctx.BaseItems
|
||||
.Where(bi => bi.UserData!.Any(ud => ud.UserId == userId && ud.PlaybackPositionTicks > 0))
|
||||
.Select(bi => bi.PrimaryVersionId ?? bi.Id);
|
||||
|
||||
// Scope to the seeded items; EnsureCreated also seeds a placeholder row.
|
||||
var seededIds = new[] { primaryId, otherId };
|
||||
|
||||
var resumable = ctx.BaseItems
|
||||
.Where(e => seededIds.Contains(e.Id) && e.PrimaryVersionId == null)
|
||||
.Where(e => resumableMovieIds.Contains(e.Id))
|
||||
.Select(e => e.Id)
|
||||
.ToList();
|
||||
|
||||
Assert.Equal([primaryId], resumable);
|
||||
|
||||
// The inverse (not-resumable) direction must exclude the primary as well.
|
||||
var notResumable = ctx.BaseItems
|
||||
.Where(e => seededIds.Contains(e.Id) && e.PrimaryVersionId == null)
|
||||
.Where(e => resumableMovieIds.Contains(e.Id) == false)
|
||||
.Select(e => e.Id)
|
||||
.ToList();
|
||||
|
||||
Assert.Equal([otherId], notResumable);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DatePlayedOrdering_VersionProgress_SortsPrimaryByVersionDate()
|
||||
{
|
||||
Guid userId, primaryId, otherId;
|
||||
|
||||
using (var ctx = CreateDbContext())
|
||||
{
|
||||
(userId, primaryId, otherId) = Seed(ctx);
|
||||
}
|
||||
|
||||
using (var ctx = CreateDbContext())
|
||||
{
|
||||
// Scope to the seeded items; EnsureCreated also seeds a placeholder row.
|
||||
var seededIds = new[] { primaryId, otherId };
|
||||
|
||||
// Mirrors the DatePlayed mapping in OrderMapper.
|
||||
var ordered = ctx.BaseItems
|
||||
.Where(e => seededIds.Contains(e.Id) && e.PrimaryVersionId == null)
|
||||
.OrderByDescending(e => ctx.UserData
|
||||
.Where(w => w.UserId == userId && (w.ItemId == e.Id || w.Item!.PrimaryVersionId == e.Id))
|
||||
.Max(f => f.LastPlayedDate))
|
||||
.Select(e => e.Id)
|
||||
.ToList();
|
||||
|
||||
// The movie whose only progress is on its alternate version sorts before the unplayed one.
|
||||
Assert.Equal([primaryId, otherId], ordered);
|
||||
}
|
||||
}
|
||||
|
||||
private static (Guid UserId, Guid PrimaryId, Guid OtherId) Seed(JellyfinDbContext ctx)
|
||||
{
|
||||
var user = new User("test", "auth-provider", "reset-provider");
|
||||
ctx.Users.Add(user);
|
||||
|
||||
var primary = new BaseItemEntity { Id = Guid.NewGuid(), Type = "MediaBrowser.Controller.Entities.Movies.Movie" };
|
||||
var version = new BaseItemEntity { Id = Guid.NewGuid(), Type = "MediaBrowser.Controller.Entities.Movies.Movie", PrimaryVersionId = primary.Id };
|
||||
var other = new BaseItemEntity { Id = Guid.NewGuid(), Type = "MediaBrowser.Controller.Entities.Movies.Movie" };
|
||||
ctx.BaseItems.AddRange(primary, version, other);
|
||||
|
||||
// Progress only on the alternate version.
|
||||
ctx.UserData.Add(new UserData
|
||||
{
|
||||
ItemId = version.Id,
|
||||
Item = version,
|
||||
UserId = user.Id,
|
||||
User = user,
|
||||
CustomDataKey = version.Id.ToString("N"),
|
||||
PlaybackPositionTicks = 1000,
|
||||
LastPlayedDate = new DateTime(2026, 1, 2, 0, 0, 0, DateTimeKind.Utc)
|
||||
});
|
||||
|
||||
ctx.SaveChanges();
|
||||
return (user.Id, primary.Id, other.Id);
|
||||
}
|
||||
|
||||
private JellyfinDbContext CreateDbContext()
|
||||
{
|
||||
return new JellyfinDbContext(
|
||||
_dbOptions,
|
||||
NullLogger<JellyfinDbContext>.Instance,
|
||||
new SqliteDatabaseProvider(null!, NullLogger<SqliteDatabaseProvider>.Instance),
|
||||
new NoLockBehavior(NullLogger<NoLockBehavior>.Instance));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoMoq;
|
||||
using Castle.Components.DictionaryAdapter;
|
||||
@@ -7,6 +9,8 @@ using Emby.Server.Implementations.Library;
|
||||
using Jellyfin.Database.Implementations.Entities;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.LiveTv;
|
||||
using MediaBrowser.Controller.MediaSegments;
|
||||
using MediaBrowser.Model.Dto;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Globalization;
|
||||
@@ -144,5 +148,111 @@ namespace Jellyfin.Server.Implementations.Tests.Library
|
||||
_mediaSourceManager.SetDefaultAudioAndSubtitleStreamIndices(_item, mediaInfo, _user);
|
||||
Assert.Equal(expectedIndex, mediaInfo.DefaultAudioStreamIndex);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetStaticMediaSources_PrimaryQueried_PopulatesPerVersionPositionsAndDefaultsToMostRecent()
|
||||
{
|
||||
var (primary, alt1, alt2) = SetupVersionGroup();
|
||||
SetupUserDataBatch(new Dictionary<Guid, UserItemData>
|
||||
{
|
||||
[alt1.Id] = new UserItemData { Key = "alt1", PlaybackPositionTicks = 10, LastPlayedDate = new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc) },
|
||||
[alt2.Id] = new UserItemData { Key = "alt2", PlaybackPositionTicks = 20, LastPlayedDate = new DateTime(2026, 1, 2, 0, 0, 0, DateTimeKind.Utc) }
|
||||
});
|
||||
|
||||
var sources = _mediaSourceManager.GetStaticMediaSources(primary, false, _user);
|
||||
|
||||
// Each version carries its own resume point; the primary has none.
|
||||
Assert.Equal((long?)10, sources.First(s => s.Id == alt1.Id.ToString("N")).PlaybackPositionTicks);
|
||||
Assert.Equal((long?)20, sources.First(s => s.Id == alt2.Id.ToString("N")).PlaybackPositionTicks);
|
||||
Assert.Null(sources.First(s => s.Id == primary.Id.ToString("N")).PlaybackPositionTicks);
|
||||
|
||||
// The most recently played version is the default source, so resuming plays the right file.
|
||||
Assert.Equal(alt2.Id.ToString("N"), sources[0].Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetStaticMediaSources_AlternateQueried_KeepsOwnSourceFirst()
|
||||
{
|
||||
var (primary, alt1, alt2) = SetupVersionGroup();
|
||||
SetupUserDataBatch(new Dictionary<Guid, UserItemData>
|
||||
{
|
||||
[alt2.Id] = new UserItemData { Key = "alt2", PlaybackPositionTicks = 20, LastPlayedDate = new DateTime(2026, 1, 2, 0, 0, 0, DateTimeKind.Utc) }
|
||||
});
|
||||
|
||||
var sources = _mediaSourceManager.GetStaticMediaSources(alt1, false, _user);
|
||||
|
||||
// An explicitly opened version keeps its own source first, even when a sibling was
|
||||
// played more recently, but the sibling's resume point is still populated.
|
||||
Assert.Equal(alt1.Id.ToString("N"), sources[0].Id);
|
||||
Assert.Equal((long?)20, sources.First(s => s.Id == alt2.Id.ToString("N")).PlaybackPositionTicks);
|
||||
Assert.Equal(3, sources.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetStaticMediaSources_NoProgress_KeepsQueriedItemFirst()
|
||||
{
|
||||
var (primary, _, _) = SetupVersionGroup();
|
||||
SetupUserDataBatch([]);
|
||||
|
||||
var sources = _mediaSourceManager.GetStaticMediaSources(primary, false, _user);
|
||||
|
||||
Assert.Equal(primary.Id.ToString("N"), sources[0].Id);
|
||||
Assert.All(sources, s => Assert.Null(s.PlaybackPositionTicks));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetStaticMediaSources_NoUser_DoesNotTouchUserData()
|
||||
{
|
||||
var (primary, _, _) = SetupVersionGroup();
|
||||
|
||||
var sources = _mediaSourceManager.GetStaticMediaSources(primary, false);
|
||||
|
||||
Assert.Equal(primary.Id.ToString("N"), sources[0].Id);
|
||||
_mockUserDataManager.Verify(x => x.GetUserDataBatch(It.IsAny<IReadOnlyList<BaseItem>>(), It.IsAny<User>()), Times.Never);
|
||||
}
|
||||
|
||||
private void SetupUserDataBatch(Dictionary<Guid, UserItemData> userData)
|
||||
{
|
||||
_mockUserDataManager
|
||||
.Setup(x => x.GetUserDataBatch(It.IsAny<IReadOnlyList<BaseItem>>(), It.IsAny<User>()))
|
||||
.Returns((IReadOnlyList<BaseItem> items, User _) => items
|
||||
.Where(i => userData.ContainsKey(i.Id))
|
||||
.ToDictionary(i => i.Id, i => userData[i.Id]));
|
||||
}
|
||||
|
||||
private static (Video Primary, Video Alt1, Video Alt2) SetupVersionGroup()
|
||||
{
|
||||
var primary = new Video { Id = Guid.NewGuid(), Path = "/Movies/Movie/Movie.mkv" };
|
||||
var alt1 = new Video { Id = Guid.NewGuid(), Path = "/Movies/Movie/Movie - 1080p.mkv", PrimaryVersionId = primary.Id };
|
||||
var alt2 = new Video { Id = Guid.NewGuid(), Path = "/Movies/Movie/Movie - 4K.mkv", PrimaryVersionId = primary.Id };
|
||||
|
||||
// BaseItem.GetMediaSources runs against the static service locators.
|
||||
var mediaSourceManager = new Mock<IMediaSourceManager>();
|
||||
mediaSourceManager.Setup(x => x.GetPathProtocol(It.IsAny<string>())).Returns(MediaProtocol.File);
|
||||
mediaSourceManager.Setup(x => x.GetMediaStreams(It.IsAny<Guid>())).Returns(new List<MediaStream>());
|
||||
mediaSourceManager.Setup(x => x.GetMediaAttachments(It.IsAny<Guid>())).Returns(new List<MediaAttachment>());
|
||||
|
||||
var segmentManager = new Mock<IMediaSegmentManager>();
|
||||
segmentManager.Setup(x => x.IsTypeSupported(It.IsAny<BaseItem>())).Returns(false);
|
||||
|
||||
var libraryManager = new Mock<ILibraryManager>();
|
||||
libraryManager.Setup(x => x.GetLinkedAlternateVersions(It.IsAny<Video>())).Returns(Array.Empty<Video>());
|
||||
libraryManager.Setup(x => x.GetLocalAlternateVersionIds(primary)).Returns(new[] { alt1.Id, alt2.Id });
|
||||
libraryManager.Setup(x => x.GetLocalAlternateVersionIds(alt1)).Returns(Array.Empty<Guid>());
|
||||
libraryManager.Setup(x => x.GetLocalAlternateVersionIds(alt2)).Returns(Array.Empty<Guid>());
|
||||
libraryManager.Setup(x => x.GetItemById(primary.Id)).Returns(primary);
|
||||
libraryManager.Setup(x => x.GetItemById(alt1.Id)).Returns(alt1);
|
||||
libraryManager.Setup(x => x.GetItemById(alt2.Id)).Returns(alt2);
|
||||
|
||||
var recordingsManager = new Mock<IRecordingsManager>();
|
||||
recordingsManager.Setup(x => x.GetActiveRecordingInfo(It.IsAny<string>())).Returns((ActiveRecordingInfo?)null);
|
||||
|
||||
BaseItem.MediaSegmentManager = segmentManager.Object;
|
||||
BaseItem.MediaSourceManager = mediaSourceManager.Object;
|
||||
BaseItem.LibraryManager = libraryManager.Object;
|
||||
Video.RecordingsManager = recordingsManager.Object;
|
||||
|
||||
return (primary, alt1, alt2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user