aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations
diff options
context:
space:
mode:
Diffstat (limited to 'Jellyfin.Server.Implementations')
-rw-r--r--Jellyfin.Server.Implementations/Devices/DeviceManager.cs157
-rw-r--r--Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs20
-rw-r--r--Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj1
-rw-r--r--Jellyfin.Server.Implementations/Security/AuthorizationContext.cs15
-rw-r--r--Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs4
5 files changed, 102 insertions, 95 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/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)
{