diff options
Diffstat (limited to 'Jellyfin.Server.Implementations')
10 files changed, 987 insertions, 97 deletions
diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs index d8d1b6fa8..d7a46e2d5 100644 --- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs +++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs @@ -27,6 +27,8 @@ namespace Jellyfin.Server.Implementations.Devices private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; private readonly IUserManager _userManager; private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new(); + private readonly ConcurrentDictionary<int, Device> _devices; + private readonly ConcurrentDictionary<string, DeviceOptions> _deviceOptions; /// <summary> /// Initializes a new instance of the <see cref="DeviceManager"/> class. @@ -37,6 +39,23 @@ namespace Jellyfin.Server.Implementations.Devices { _dbProvider = dbProvider; _userManager = userManager; + _devices = new ConcurrentDictionary<int, Device>(); + _deviceOptions = new ConcurrentDictionary<string, DeviceOptions>(); + + using var dbContext = _dbProvider.CreateDbContext(); + foreach (var device in dbContext.Devices + .OrderBy(d => d.Id) + .AsEnumerable()) + { + _devices.TryAdd(device.Id, device); + } + + foreach (var deviceOption in dbContext.DeviceOptions + .OrderBy(d => d.Id) + .AsEnumerable()) + { + _deviceOptions.TryAdd(deviceOption.DeviceId, deviceOption); + } } /// <inheritdoc /> @@ -66,6 +85,8 @@ namespace Jellyfin.Server.Implementations.Devices await dbContext.SaveChangesAsync().ConfigureAwait(false); } + _deviceOptions[deviceId] = deviceOptions; + DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, deviceOptions))); } @@ -76,25 +97,17 @@ namespace Jellyfin.Server.Implementations.Devices await using (dbContext.ConfigureAwait(false)) { dbContext.Devices.Add(device); - await dbContext.SaveChangesAsync().ConfigureAwait(false); + _devices.TryAdd(device.Id, device); } return device; } /// <inheritdoc /> - public async Task<DeviceOptions> GetDeviceOptions(string deviceId) + public DeviceOptions GetDeviceOptions(string deviceId) { - var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); - DeviceOptions? deviceOptions; - await using (dbContext.ConfigureAwait(false)) - { - deviceOptions = await dbContext.DeviceOptions - .AsNoTracking() - .FirstOrDefaultAsync(d => d.DeviceId == deviceId) - .ConfigureAwait(false); - } + _deviceOptions.TryGetValue(deviceId, out var deviceOptions); return deviceOptions ?? new DeviceOptions(deviceId); } @@ -108,57 +121,43 @@ namespace Jellyfin.Server.Implementations.Devices } /// <inheritdoc /> - public async Task<DeviceInfo?> GetDevice(string id) + public DeviceInfo? GetDevice(string id) { - var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); - await using (dbContext.ConfigureAwait(false)) - { - var device = await dbContext.Devices - .Where(d => d.DeviceId == id) - .OrderByDescending(d => d.DateLastActivity) - .Include(d => d.User) - .SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o }) - .FirstOrDefaultAsync() - .ConfigureAwait(false); + var device = _devices.Values.Where(d => d.DeviceId == id).OrderByDescending(d => d.DateLastActivity).FirstOrDefault(); + _deviceOptions.TryGetValue(id, out var deviceOption); - var deviceInfo = device is null ? null : ToDeviceInfo(device.Device, device.Options); - - return deviceInfo; - } + var deviceInfo = device is null ? null : ToDeviceInfo(device, deviceOption); + return deviceInfo; } /// <inheritdoc /> - public async Task<QueryResult<Device>> GetDevices(DeviceQuery query) + public QueryResult<Device> GetDevices(DeviceQuery query) { - var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); - await using (dbContext.ConfigureAwait(false)) + IEnumerable<Device> devices = _devices.Values + .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value)) + .Where(device => query.DeviceId == null || device.DeviceId == query.DeviceId) + .Where(device => query.AccessToken == null || device.AccessToken == query.AccessToken) + .OrderBy(d => d.Id) + .ToList(); + var count = devices.Count(); + + if (query.Skip.HasValue) { - var devices = dbContext.Devices - .OrderBy(d => d.Id) - .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value)) - .Where(device => query.DeviceId == null || device.DeviceId == query.DeviceId) - .Where(device => query.AccessToken == null || device.AccessToken == query.AccessToken); - - var count = await devices.CountAsync().ConfigureAwait(false); - - if (query.Skip.HasValue) - { - devices = devices.Skip(query.Skip.Value); - } - - if (query.Limit.HasValue) - { - devices = devices.Take(query.Limit.Value); - } + devices = devices.Skip(query.Skip.Value); + } - return new QueryResult<Device>(query.Skip, count, await devices.ToListAsync().ConfigureAwait(false)); + if (query.Limit.HasValue) + { + devices = devices.Take(query.Limit.Value); } + + return new QueryResult<Device>(query.Skip, count, devices.ToList()); } /// <inheritdoc /> - public async Task<QueryResult<DeviceInfo>> GetDeviceInfos(DeviceQuery query) + public QueryResult<DeviceInfo> GetDeviceInfos(DeviceQuery query) { - var devices = await GetDevices(query).ConfigureAwait(false); + var devices = GetDevices(query); return new QueryResult<DeviceInfo>( devices.StartIndex, @@ -167,38 +166,36 @@ namespace Jellyfin.Server.Implementations.Devices } /// <inheritdoc /> - public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId) + public QueryResult<DeviceInfo> GetDevicesForUser(Guid? userId) { - var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); - await using (dbContext.ConfigureAwait(false)) + IEnumerable<Device> devices = _devices.Values + .OrderByDescending(d => d.DateLastActivity) + .ThenBy(d => d.DeviceId); + + if (!userId.IsNullOrEmpty()) { - var sessions = dbContext.Devices - .Include(d => d.User) - .OrderByDescending(d => d.DateLastActivity) - .ThenBy(d => d.DeviceId) - .SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o }) - .AsAsyncEnumerable(); - - if (!userId.IsNullOrEmpty()) + var user = _userManager.GetUserById(userId.Value); + if (user is null) { - var user = _userManager.GetUserById(userId.Value); - if (user is null) - { - throw new ResourceNotFoundException(); - } - - sessions = sessions.Where(i => CanAccessDevice(user, i.Device.DeviceId)); + throw new ResourceNotFoundException(); } - var array = await sessions.Select(device => ToDeviceInfo(device.Device, device.Options)).ToArrayAsync().ConfigureAwait(false); - - return new QueryResult<DeviceInfo>(array); + devices = devices.Where(i => CanAccessDevice(user, i.DeviceId)); } + + var array = devices.Select(device => + { + _deviceOptions.TryGetValue(device.DeviceId, out var option); + return ToDeviceInfo(device, option); + }).ToArray(); + + return new QueryResult<DeviceInfo>(array); } /// <inheritdoc /> public async Task DeleteDevice(Device device) { + _devices.TryRemove(device.Id, out _); var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { @@ -208,6 +205,19 @@ namespace Jellyfin.Server.Implementations.Devices } /// <inheritdoc /> + public async Task UpdateDevice(Device device) + { + var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); + await using (dbContext.ConfigureAwait(false)) + { + dbContext.Devices.Update(device); + await dbContext.SaveChangesAsync().ConfigureAwait(false); + } + + _devices[device.Id] = device; + } + + /// <inheritdoc /> public bool CanAccessDevice(User user, string deviceId) { ArgumentNullException.ThrowIfNull(user); @@ -225,6 +235,11 @@ namespace Jellyfin.Server.Implementations.Devices private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options = null) { var caps = GetCapabilities(authInfo.DeviceId); + var user = _userManager.GetUserById(authInfo.UserId); + if (user is null) + { + throw new ResourceNotFoundException("User with UserId " + authInfo.UserId + " not found"); + } return new DeviceInfo { @@ -232,7 +247,7 @@ namespace Jellyfin.Server.Implementations.Devices AppVersion = authInfo.AppVersion, Id = authInfo.DeviceId, LastUserId = authInfo.UserId, - LastUserName = authInfo.User.Username, + LastUserName = user.Username, Name = authInfo.DeviceName, DateLastActivity = authInfo.DateLastActivity, IconUrl = caps.IconUrl, diff --git a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs index a88989840..ff29d69b4 100644 --- a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs +++ b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using EFCoreSecondLevelCacheInterceptor; using MediaBrowser.Common.Configuration; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; @@ -16,28 +15,13 @@ public static class ServiceCollectionExtensions /// Adds the <see cref="IDbContextFactory{TContext}"/> interface to the service collection with second level caching enabled. /// </summary> /// <param name="serviceCollection">An instance of the <see cref="IServiceCollection"/> interface.</param> - /// <param name="disableSecondLevelCache">Whether second level cache disabled..</param> /// <returns>The updated service collection.</returns> - public static IServiceCollection AddJellyfinDbContext(this IServiceCollection serviceCollection, bool disableSecondLevelCache) + public static IServiceCollection AddJellyfinDbContext(this IServiceCollection serviceCollection) { - if (!disableSecondLevelCache) - { - serviceCollection.AddEFSecondLevelCache(options => - options.UseMemoryCacheProvider() - .CacheAllQueries(CacheExpirationMode.Sliding, TimeSpan.FromMinutes(10)) - .UseCacheKeyPrefix("EF_") - // Don't cache null values. Remove this optional setting if it's not necessary. - .SkipCachingResults(result => result.Value is null or EFTableRows { RowsCount: 0 })); - } - serviceCollection.AddPooledDbContextFactory<JellyfinDbContext>((serviceProvider, opt) => { var applicationPaths = serviceProvider.GetRequiredService<IApplicationPaths>(); - var dbOpt = opt.UseSqlite($"Filename={Path.Combine(applicationPaths.DataPath, "jellyfin.db")}"); - if (!disableSecondLevelCache) - { - dbOpt.AddInterceptors(serviceProvider.GetRequiredService<SecondLevelCacheInterceptor>()); - } + opt.UseSqlite($"Filename={Path.Combine(applicationPaths.DataPath, "jellyfin.db")}"); }); return serviceCollection; diff --git a/Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj b/Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj index 7c4155bfc..20944ee4b 100644 --- a/Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj +++ b/Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj @@ -27,7 +27,6 @@ <ItemGroup> <PackageReference Include="AsyncKeyedLock" /> - <PackageReference Include="EFCoreSecondLevelCacheInterceptor" /> <PackageReference Include="System.Linq.Async" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" /> diff --git a/Jellyfin.Server.Implementations/JellyfinDbContext.cs b/Jellyfin.Server.Implementations/JellyfinDbContext.cs index ea99af004..150bc8bb4 100644 --- a/Jellyfin.Server.Implementations/JellyfinDbContext.cs +++ b/Jellyfin.Server.Implementations/JellyfinDbContext.cs @@ -83,6 +83,11 @@ public class JellyfinDbContext : DbContext /// </summary> public DbSet<TrickplayInfo> TrickplayInfos => Set<TrickplayInfo>(); + /// <summary> + /// Gets the <see cref="DbSet{TEntity}"/> containing the media segments. + /// </summary> + public DbSet<MediaSegment> MediaSegments => Set<MediaSegment>(); + /*public DbSet<Artwork> Artwork => Set<Artwork>(); public DbSet<Book> Books => Set<Book>(); diff --git a/Jellyfin.Server.Implementations/MediaSegments/MediaSegmentManager.cs b/Jellyfin.Server.Implementations/MediaSegments/MediaSegmentManager.cs new file mode 100644 index 000000000..7916d15c9 --- /dev/null +++ b/Jellyfin.Server.Implementations/MediaSegments/MediaSegmentManager.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Threading.Tasks; +using Jellyfin.Data.Entities; +using Jellyfin.Data.Enums; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Model.MediaSegments; +using Microsoft.EntityFrameworkCore; + +namespace Jellyfin.Server.Implementations.MediaSegments; + +/// <summary> +/// Manages media segments retrival and storage. +/// </summary> +public class MediaSegmentManager : IMediaSegmentManager +{ + private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; + + /// <summary> + /// Initializes a new instance of the <see cref="MediaSegmentManager"/> class. + /// </summary> + /// <param name="dbProvider">EFCore Database factory.</param> + public MediaSegmentManager(IDbContextFactory<JellyfinDbContext> dbProvider) + { + _dbProvider = dbProvider; + } + + /// <inheritdoc /> + public async Task<MediaSegmentDto> CreateSegmentAsync(MediaSegmentDto mediaSegment, string segmentProviderId) + { + ArgumentOutOfRangeException.ThrowIfLessThan(mediaSegment.EndTicks, mediaSegment.StartTicks); + + using var db = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); + db.MediaSegments.Add(Map(mediaSegment, segmentProviderId)); + await db.SaveChangesAsync().ConfigureAwait(false); + return mediaSegment; + } + + /// <inheritdoc /> + public async Task DeleteSegmentAsync(Guid segmentId) + { + using var db = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); + await db.MediaSegments.Where(e => e.Id.Equals(segmentId)).ExecuteDeleteAsync().ConfigureAwait(false); + } + + /// <inheritdoc /> + public async Task<IEnumerable<MediaSegmentDto>> GetSegmentsAsync(Guid itemId, IEnumerable<MediaSegmentType>? typeFilter) + { + using var db = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); + + var query = db.MediaSegments + .Where(e => e.ItemId.Equals(itemId)); + + if (typeFilter is not null) + { + query = query.Where(e => typeFilter.Contains(e.Type)); + } + + return query + .OrderBy(e => e.StartTicks) + .AsNoTracking() + .ToImmutableList() + .Select(Map); + } + + private static MediaSegmentDto Map(MediaSegment segment) + { + return new MediaSegmentDto() + { + Id = segment.Id, + EndTicks = segment.EndTicks, + ItemId = segment.ItemId, + StartTicks = segment.StartTicks, + Type = segment.Type + }; + } + + private static MediaSegment Map(MediaSegmentDto segment, string segmentProviderId) + { + return new MediaSegment() + { + Id = segment.Id, + EndTicks = segment.EndTicks, + ItemId = segment.ItemId, + StartTicks = segment.StartTicks, + Type = segment.Type, + SegmentProviderId = segmentProviderId + }; + } + + /// <inheritdoc /> + public bool HasSegments(Guid itemId) + { + using var db = _dbProvider.CreateDbContext(); + return db.MediaSegments.Any(e => e.ItemId.Equals(itemId)); + } + + /// <inheritdoc/> + public bool IsTypeSupported(BaseItem baseItem) + { + return baseItem.MediaType is Data.Enums.MediaType.Video or Data.Enums.MediaType.Audio; + } +} diff --git a/Jellyfin.Server.Implementations/Migrations/20240729140605_AddMediaSegments.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20240729140605_AddMediaSegments.Designer.cs new file mode 100644 index 000000000..c03cb4760 --- /dev/null +++ b/Jellyfin.Server.Implementations/Migrations/20240729140605_AddMediaSegments.Designer.cs @@ -0,0 +1,708 @@ +// <auto-generated /> +using System; +using Jellyfin.Server.Implementations; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Jellyfin.Server.Implementations.Migrations +{ + [DbContext(typeof(JellyfinDbContext))] + [Migration("20240729140605_AddMediaSegments")] + partial class AddMediaSegments + { + /// <inheritdoc /> + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.7"); + + modelBuilder.Entity("Jellyfin.Data.Entities.AccessSchedule", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<int>("DayOfWeek") + .HasColumnType("INTEGER"); + + b.Property<double>("EndHour") + .HasColumnType("REAL"); + + b.Property<double>("StartHour") + .HasColumnType("REAL"); + + b.Property<Guid>("UserId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AccessSchedules"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.ActivityLog", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<DateTime>("DateCreated") + .HasColumnType("TEXT"); + + b.Property<string>("ItemId") + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property<int>("LogSeverity") + .HasColumnType("INTEGER"); + + b.Property<string>("Name") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property<string>("Overview") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property<uint>("RowVersion") + .IsConcurrencyToken() + .HasColumnType("INTEGER"); + + b.Property<string>("ShortOverview") + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property<string>("Type") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property<Guid>("UserId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("DateCreated"); + + b.ToTable("ActivityLogs"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.CustomItemDisplayPreferences", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("Client") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property<Guid>("ItemId") + .HasColumnType("TEXT"); + + b.Property<string>("Key") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<Guid>("UserId") + .HasColumnType("TEXT"); + + b.Property<string>("Value") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UserId", "ItemId", "Client", "Key") + .IsUnique(); + + b.ToTable("CustomItemDisplayPreferences"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.DisplayPreferences", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<int>("ChromecastVersion") + .HasColumnType("INTEGER"); + + b.Property<string>("Client") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property<string>("DashboardTheme") + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property<bool>("EnableNextVideoInfoOverlay") + .HasColumnType("INTEGER"); + + b.Property<int?>("IndexBy") + .HasColumnType("INTEGER"); + + b.Property<Guid>("ItemId") + .HasColumnType("TEXT"); + + b.Property<int>("ScrollDirection") + .HasColumnType("INTEGER"); + + b.Property<bool>("ShowBackdrop") + .HasColumnType("INTEGER"); + + b.Property<bool>("ShowSidebar") + .HasColumnType("INTEGER"); + + b.Property<int>("SkipBackwardLength") + .HasColumnType("INTEGER"); + + b.Property<int>("SkipForwardLength") + .HasColumnType("INTEGER"); + + b.Property<string>("TvHome") + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property<Guid>("UserId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UserId", "ItemId", "Client") + .IsUnique(); + + b.ToTable("DisplayPreferences"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.HomeSection", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<int>("DisplayPreferencesId") + .HasColumnType("INTEGER"); + + b.Property<int>("Order") + .HasColumnType("INTEGER"); + + b.Property<int>("Type") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("DisplayPreferencesId"); + + b.ToTable("HomeSection"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.ImageInfo", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<DateTime>("LastModified") + .HasColumnType("TEXT"); + + b.Property<string>("Path") + .IsRequired() + .HasMaxLength(512) + .HasColumnType("TEXT"); + + b.Property<Guid?>("UserId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UserId") + .IsUnique(); + + b.ToTable("ImageInfos"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.ItemDisplayPreferences", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("Client") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property<int?>("IndexBy") + .HasColumnType("INTEGER"); + + b.Property<Guid>("ItemId") + .HasColumnType("TEXT"); + + b.Property<bool>("RememberIndexing") + .HasColumnType("INTEGER"); + + b.Property<bool>("RememberSorting") + .HasColumnType("INTEGER"); + + b.Property<string>("SortBy") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property<int>("SortOrder") + .HasColumnType("INTEGER"); + + b.Property<Guid>("UserId") + .HasColumnType("TEXT"); + + b.Property<int>("ViewType") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("ItemDisplayPreferences"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.MediaSegment", b => + { + b.Property<Guid>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property<long>("EndTicks") + .HasColumnType("INTEGER"); + + b.Property<Guid>("ItemId") + .HasColumnType("TEXT"); + + b.Property<long>("StartTicks") + .HasColumnType("INTEGER"); + + b.Property<int>("Type") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("MediaSegments"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.Permission", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<int>("Kind") + .HasColumnType("INTEGER"); + + b.Property<Guid?>("Permission_Permissions_Guid") + .HasColumnType("TEXT"); + + b.Property<uint>("RowVersion") + .IsConcurrencyToken() + .HasColumnType("INTEGER"); + + b.Property<Guid?>("UserId") + .HasColumnType("TEXT"); + + b.Property<bool>("Value") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("UserId", "Kind") + .IsUnique() + .HasFilter("[UserId] IS NOT NULL"); + + b.ToTable("Permissions"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.Preference", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<int>("Kind") + .HasColumnType("INTEGER"); + + b.Property<Guid?>("Preference_Preferences_Guid") + .HasColumnType("TEXT"); + + b.Property<uint>("RowVersion") + .IsConcurrencyToken() + .HasColumnType("INTEGER"); + + b.Property<Guid?>("UserId") + .HasColumnType("TEXT"); + + b.Property<string>("Value") + .IsRequired() + .HasMaxLength(65535) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UserId", "Kind") + .IsUnique() + .HasFilter("[UserId] IS NOT NULL"); + + b.ToTable("Preferences"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.Security.ApiKey", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("AccessToken") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<DateTime>("DateCreated") + .HasColumnType("TEXT"); + + b.Property<DateTime>("DateLastActivity") + .HasColumnType("TEXT"); + + b.Property<string>("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("AccessToken") + .IsUnique(); + + b.ToTable("ApiKeys"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.Security.Device", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("AccessToken") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<string>("AppName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property<string>("AppVersion") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property<DateTime>("DateCreated") + .HasColumnType("TEXT"); + + b.Property<DateTime>("DateLastActivity") + .HasColumnType("TEXT"); + + b.Property<DateTime>("DateModified") + .HasColumnType("TEXT"); + + b.Property<string>("DeviceId") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("TEXT"); + + b.Property<string>("DeviceName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("TEXT"); + + b.Property<bool>("IsActive") + .HasColumnType("INTEGER"); + + b.Property<Guid>("UserId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("DeviceId"); + + b.HasIndex("AccessToken", "DateLastActivity"); + + b.HasIndex("DeviceId", "DateLastActivity"); + + b.HasIndex("UserId", "DeviceId"); + + b.ToTable("Devices"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.Security.DeviceOptions", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("CustomName") + .HasColumnType("TEXT"); + + b.Property<string>("DeviceId") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("DeviceId") + .IsUnique(); + + b.ToTable("DeviceOptions"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.TrickplayInfo", b => + { + b.Property<Guid>("ItemId") + .HasColumnType("TEXT"); + + b.Property<int>("Width") + .HasColumnType("INTEGER"); + + b.Property<int>("Bandwidth") + .HasColumnType("INTEGER"); + + b.Property<int>("Height") + .HasColumnType("INTEGER"); + + b.Property<int>("Interval") + .HasColumnType("INTEGER"); + + b.Property<int>("ThumbnailCount") + .HasColumnType("INTEGER"); + + b.Property<int>("TileHeight") + .HasColumnType("INTEGER"); + + b.Property<int>("TileWidth") + .HasColumnType("INTEGER"); + + b.HasKey("ItemId", "Width"); + + b.ToTable("TrickplayInfos"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.User", b => + { + b.Property<Guid>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property<string>("AudioLanguagePreference") + .HasMaxLength(255) + .HasColumnType("TEXT"); + + b.Property<string>("AuthenticationProviderId") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("TEXT"); + + b.Property<string>("CastReceiverId") + .HasMaxLength(32) + .HasColumnType("TEXT"); + + b.Property<bool>("DisplayCollectionsView") + .HasColumnType("INTEGER"); + + b.Property<bool>("DisplayMissingEpisodes") + .HasColumnType("INTEGER"); + + b.Property<bool>("EnableAutoLogin") + .HasColumnType("INTEGER"); + + b.Property<bool>("EnableLocalPassword") + .HasColumnType("INTEGER"); + + b.Property<bool>("EnableNextEpisodeAutoPlay") + .HasColumnType("INTEGER"); + + b.Property<bool>("EnableUserPreferenceAccess") + .HasColumnType("INTEGER"); + + b.Property<bool>("HidePlayedInLatest") + .HasColumnType("INTEGER"); + + b.Property<long>("InternalId") + .HasColumnType("INTEGER"); + + b.Property<int>("InvalidLoginAttemptCount") + .HasColumnType("INTEGER"); + + b.Property<DateTime?>("LastActivityDate") + .HasColumnType("TEXT"); + + b.Property<DateTime?>("LastLoginDate") + .HasColumnType("TEXT"); + + b.Property<int?>("LoginAttemptsBeforeLockout") + .HasColumnType("INTEGER"); + + b.Property<int>("MaxActiveSessions") + .HasColumnType("INTEGER"); + + b.Property<int?>("MaxParentalAgeRating") + .HasColumnType("INTEGER"); + + b.Property<bool>("MustUpdatePassword") + .HasColumnType("INTEGER"); + + b.Property<string>("Password") + .HasMaxLength(65535) + .HasColumnType("TEXT"); + + b.Property<string>("PasswordResetProviderId") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("TEXT"); + + b.Property<bool>("PlayDefaultAudioTrack") + .HasColumnType("INTEGER"); + + b.Property<bool>("RememberAudioSelections") + .HasColumnType("INTEGER"); + + b.Property<bool>("RememberSubtitleSelections") + .HasColumnType("INTEGER"); + + b.Property<int?>("RemoteClientBitrateLimit") + .HasColumnType("INTEGER"); + + b.Property<uint>("RowVersion") + .IsConcurrencyToken() + .HasColumnType("INTEGER"); + + b.Property<string>("SubtitleLanguagePreference") + .HasMaxLength(255) + .HasColumnType("TEXT"); + + b.Property<int>("SubtitleMode") + .HasColumnType("INTEGER"); + + b.Property<int>("SyncPlayAccess") + .HasColumnType("INTEGER"); + + b.Property<string>("Username") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("TEXT") + .UseCollation("NOCASE"); + + b.HasKey("Id"); + + b.HasIndex("Username") + .IsUnique(); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.AccessSchedule", b => + { + b.HasOne("Jellyfin.Data.Entities.User", null) + .WithMany("AccessSchedules") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.DisplayPreferences", b => + { + b.HasOne("Jellyfin.Data.Entities.User", null) + .WithMany("DisplayPreferences") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.HomeSection", b => + { + b.HasOne("Jellyfin.Data.Entities.DisplayPreferences", null) + .WithMany("HomeSections") + .HasForeignKey("DisplayPreferencesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.ImageInfo", b => + { + b.HasOne("Jellyfin.Data.Entities.User", null) + .WithOne("ProfileImage") + .HasForeignKey("Jellyfin.Data.Entities.ImageInfo", "UserId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.ItemDisplayPreferences", b => + { + b.HasOne("Jellyfin.Data.Entities.User", null) + .WithMany("ItemDisplayPreferences") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.Permission", b => + { + b.HasOne("Jellyfin.Data.Entities.User", null) + .WithMany("Permissions") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.Preference", b => + { + b.HasOne("Jellyfin.Data.Entities.User", null) + .WithMany("Preferences") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.Security.Device", b => + { + b.HasOne("Jellyfin.Data.Entities.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.DisplayPreferences", b => + { + b.Navigation("HomeSections"); + }); + + modelBuilder.Entity("Jellyfin.Data.Entities.User", b => + { + b.Navigation("AccessSchedules"); + + b.Navigation("DisplayPreferences"); + + b.Navigation("ItemDisplayPreferences"); + + b.Navigation("Permissions"); + + b.Navigation("Preferences"); + + b.Navigation("ProfileImage"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Jellyfin.Server.Implementations/Migrations/20240729140605_AddMediaSegments.cs b/Jellyfin.Server.Implementations/Migrations/20240729140605_AddMediaSegments.cs new file mode 100644 index 000000000..24a8ffc42 --- /dev/null +++ b/Jellyfin.Server.Implementations/Migrations/20240729140605_AddMediaSegments.cs @@ -0,0 +1,38 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Jellyfin.Server.Implementations.Migrations +{ + /// <inheritdoc /> + public partial class AddMediaSegments : Migration + { + /// <inheritdoc /> + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "MediaSegments", + columns: table => new + { + Id = table.Column<Guid>(type: "TEXT", nullable: false), + ItemId = table.Column<Guid>(type: "TEXT", nullable: false), + Type = table.Column<int>(type: "INTEGER", nullable: false), + EndTicks = table.Column<long>(type: "INTEGER", nullable: false), + StartTicks = table.Column<long>(type: "INTEGER", nullable: false), + SegmentProviderId = table.Column<string>(type: "TEXT", nullable: false), + }, + constraints: table => + { + table.PrimaryKey("PK_MediaSegments", x => x.Id); + }); + } + + /// <inheritdoc /> + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "MediaSegments"); + } + } +} diff --git a/Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs b/Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs index f725ababe..cdeeb6d87 100644 --- a/Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs +++ b/Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs @@ -1,4 +1,4 @@ -// <auto-generated /> +// <auto-generated /> using System; using Jellyfin.Server.Implementations; using Microsoft.EntityFrameworkCore; @@ -15,7 +15,7 @@ namespace Jellyfin.Server.Implementations.Migrations protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.11"); + modelBuilder.HasAnnotation("ProductVersion", "8.0.7"); modelBuilder.Entity("Jellyfin.Data.Entities.AccessSchedule", b => { @@ -270,6 +270,32 @@ namespace Jellyfin.Server.Implementations.Migrations b.ToTable("ItemDisplayPreferences"); }); + modelBuilder.Entity("Jellyfin.Data.Entities.MediaSegment", b => + { + b.Property<Guid>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property<long>("EndTicks") + .HasColumnType("INTEGER"); + + b.Property<Guid>("ItemId") + .HasColumnType("TEXT"); + + b.Property<long>("StartTicks") + .HasColumnType("INTEGER"); + + b.Property<int>("Type") + .HasColumnType("INTEGER"); + + b.Property<string>("SegmentProviderId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("MediaSegments"); + }); + modelBuilder.Entity("Jellyfin.Data.Entities.Permission", b => { b.Property<int>("Id") diff --git a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs index 6bda12c5b..2ae722982 100644 --- a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs +++ b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs @@ -4,7 +4,10 @@ using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; +using Jellyfin.Data.Queries; +using Jellyfin.Extensions; using MediaBrowser.Controller; +using MediaBrowser.Controller.Devices; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; using Microsoft.AspNetCore.Http; @@ -17,15 +20,18 @@ namespace Jellyfin.Server.Implementations.Security { private readonly IDbContextFactory<JellyfinDbContext> _jellyfinDbProvider; private readonly IUserManager _userManager; + private readonly IDeviceManager _deviceManager; private readonly IServerApplicationHost _serverApplicationHost; public AuthorizationContext( IDbContextFactory<JellyfinDbContext> jellyfinDb, IUserManager userManager, + IDeviceManager deviceManager, IServerApplicationHost serverApplicationHost) { _jellyfinDbProvider = jellyfinDb; _userManager = userManager; + _deviceManager = deviceManager; _serverApplicationHost = serverApplicationHost; } @@ -121,7 +127,11 @@ namespace Jellyfin.Server.Implementations.Security var dbContext = await _jellyfinDbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { - var device = await dbContext.Devices.FirstOrDefaultAsync(d => d.AccessToken == token).ConfigureAwait(false); + var device = _deviceManager.GetDevices( + new DeviceQuery + { + AccessToken = token + }).Items.FirstOrDefault(); if (device is not null) { @@ -178,8 +188,7 @@ namespace Jellyfin.Server.Implementations.Security if (updateToken) { - dbContext.Devices.Update(device); - await dbContext.SaveChangesAsync().ConfigureAwait(false); + await _deviceManager.UpdateDevice(device).ConfigureAwait(false); } } else diff --git a/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs b/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs index e40b541a3..634aea9f0 100644 --- a/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs +++ b/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs @@ -60,10 +60,10 @@ public sealed class DeviceAccessHost : IHostedService private async Task UpdateDeviceAccess(User user) { - var existing = (await _deviceManager.GetDevices(new DeviceQuery + var existing = _deviceManager.GetDevices(new DeviceQuery { UserId = user.Id - }).ConfigureAwait(false)).Items; + }).Items; foreach (var device in existing) { |
