jellyfin/Jellyfin.Server.Implementations/Devices/DeviceManager.cs

247 lines
9.1 KiB
C#
Raw Normal View History

2014-10-11 22:38:13 +02:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
2014-10-11 22:38:13 +02:00
using System.Linq;
2021-04-10 22:17:36 +02:00
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
2021-04-10 22:17:36 +02:00
using Jellyfin.Data.Entities.Security;
2020-07-30 01:25:47 +02:00
using Jellyfin.Data.Enums;
using Jellyfin.Data.Events;
2021-05-21 05:56:59 +02:00
using Jellyfin.Data.Queries;
2021-12-20 13:31:07 +01:00
using Jellyfin.Extensions;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Devices;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Session;
2021-04-10 22:17:36 +02:00
using Microsoft.EntityFrameworkCore;
2014-10-11 22:38:13 +02:00
2021-04-10 22:17:36 +02:00
namespace Jellyfin.Server.Implementations.Devices
2014-10-11 22:38:13 +02:00
{
2021-04-10 23:11:59 +02:00
/// <summary>
/// Manages the creation, updating, and retrieval of devices.
/// </summary>
2014-10-11 22:38:13 +02:00
public class DeviceManager : IDeviceManager
{
2023-01-16 18:14:44 +01:00
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
2014-10-11 22:38:13 +02:00
private readonly IUserManager _userManager;
2021-12-24 18:28:27 +01:00
private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new();
2018-09-12 19:26:21 +02:00
2021-04-10 22:17:36 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="DeviceManager"/> class.
/// </summary>
/// <param name="dbProvider">The database provider.</param>
/// <param name="userManager">The user manager.</param>
2023-01-16 18:14:44 +01:00
public DeviceManager(IDbContextFactory<JellyfinDbContext> dbProvider, IUserManager userManager)
2014-10-11 22:38:13 +02:00
{
2021-04-10 22:17:36 +02:00
_dbProvider = dbProvider;
2014-10-11 22:38:13 +02:00
_userManager = userManager;
}
2021-04-10 22:17:36 +02:00
/// <inheritdoc />
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated;
2021-04-10 22:17:36 +02:00
/// <inheritdoc />
2018-09-12 19:26:21 +02:00
public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
2014-10-11 22:38:13 +02:00
{
_capabilitiesMap[deviceId] = capabilities;
2018-09-12 19:26:21 +02:00
}
2014-10-11 22:38:13 +02:00
2021-04-10 22:17:36 +02:00
/// <inheritdoc />
2021-07-14 01:30:11 +02:00
public async Task UpdateDeviceOptions(string deviceId, string deviceName)
2018-09-12 19:26:21 +02:00
{
DeviceOptions? deviceOptions;
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
2021-06-19 21:24:26 +02:00
{
deviceOptions = await dbContext.DeviceOptions.FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false);
2022-12-05 15:00:20 +01:00
if (deviceOptions is null)
{
deviceOptions = new DeviceOptions(deviceId);
dbContext.DeviceOptions.Add(deviceOptions);
}
deviceOptions.CustomName = deviceName;
await dbContext.SaveChangesAsync().ConfigureAwait(false);
2021-06-19 21:24:26 +02:00
}
2021-07-14 01:30:11 +02:00
DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, deviceOptions)));
2018-09-12 19:26:21 +02:00
}
2014-10-11 22:38:13 +02:00
2021-05-21 05:56:59 +02:00
/// <inheritdoc />
public async Task<Device> CreateDevice(Device device)
{
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
dbContext.Devices.Add(device);
2021-05-21 05:56:59 +02:00
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
2021-05-21 05:56:59 +02:00
return device;
}
2021-04-10 22:17:36 +02:00
/// <inheritdoc />
public async Task<DeviceOptions> GetDeviceOptions(string deviceId)
2018-09-12 19:26:21 +02:00
{
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);
}
return deviceOptions ?? new DeviceOptions(deviceId);
2018-09-12 19:26:21 +02:00
}
2014-10-11 22:38:13 +02:00
2021-04-10 22:17:36 +02:00
/// <inheritdoc />
2021-04-10 23:11:59 +02:00
public ClientCapabilities GetCapabilities(string deviceId)
2018-09-12 19:26:21 +02:00
{
2021-04-10 23:11:59 +02:00
return _capabilitiesMap.TryGetValue(deviceId, out ClientCapabilities? result)
? result
: new ClientCapabilities();
2014-10-11 22:38:13 +02:00
}
2021-04-10 22:17:36 +02:00
/// <inheritdoc />
public async Task<DeviceInfo?> GetDevice(string id)
2014-10-11 22:38:13 +02:00
{
Device? device;
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
device = await dbContext.Devices
.Where(d => d.DeviceId == id)
.OrderByDescending(d => d.DateLastActivity)
.Include(d => d.User)
.FirstOrDefaultAsync()
.ConfigureAwait(false);
}
2021-04-10 22:17:36 +02:00
2022-12-05 15:00:20 +01:00
var deviceInfo = device is null ? null : ToDeviceInfo(device);
2021-04-10 22:17:36 +02:00
return deviceInfo;
2014-10-11 22:38:13 +02:00
}
2021-05-21 05:56:59 +02:00
/// <inheritdoc />
public async Task<QueryResult<Device>> GetDevices(DeviceQuery query)
{
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
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);
2021-05-21 05:56:59 +02:00
var count = await devices.CountAsync().ConfigureAwait(false);
2021-05-21 05:56:59 +02:00
if (query.Skip.HasValue)
{
devices = devices.Skip(query.Skip.Value);
}
2021-06-19 21:24:42 +02:00
if (query.Limit.HasValue)
{
devices = devices.Take(query.Limit.Value);
}
2021-05-21 05:56:59 +02:00
return new QueryResult<Device>(query.Skip, count, await devices.ToListAsync().ConfigureAwait(false));
2021-05-21 05:56:59 +02:00
}
}
/// <inheritdoc />
public async Task<QueryResult<DeviceInfo>> GetDeviceInfos(DeviceQuery query)
{
var devices = await GetDevices(query).ConfigureAwait(false);
2022-01-20 16:46:17 +01:00
return new QueryResult<DeviceInfo>(
devices.StartIndex,
devices.TotalRecordCount,
devices.Items.Select(device => ToDeviceInfo(device)).ToList());
2021-05-21 05:56:59 +02:00
}
2021-04-10 22:17:36 +02:00
/// <inheritdoc />
2021-05-21 02:39:22 +02:00
public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId, bool? supportsSync)
2014-10-11 22:38:13 +02:00
{
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
2014-12-15 06:49:04 +01:00
{
IAsyncEnumerable<Device> sessions = dbContext.Devices
.Include(d => d.User)
.OrderByDescending(d => d.DateLastActivity)
.ThenBy(d => d.DeviceId)
.AsAsyncEnumerable();
2014-12-15 06:49:04 +01:00
if (supportsSync.HasValue)
{
sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == supportsSync.Value);
}
2014-12-13 04:56:30 +01:00
if (userId.HasValue)
{
var user = _userManager.GetUserById(userId.Value);
if (user is null)
{
throw new ResourceNotFoundException();
}
sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
}
2014-12-31 07:24:49 +01:00
var array = await sessions.Select(device => ToDeviceInfo(device)).ToArrayAsync().ConfigureAwait(false);
return new QueryResult<DeviceInfo>(array);
}
2014-10-11 22:38:13 +02:00
}
2021-05-21 05:56:59 +02:00
/// <inheritdoc />
public async Task DeleteDevice(Device device)
{
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
dbContext.Devices.Remove(device);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
2021-05-21 05:56:59 +02:00
}
2021-04-10 22:17:36 +02:00
/// <inheritdoc />
2018-09-12 19:26:21 +02:00
public bool CanAccessDevice(User user, string deviceId)
2014-12-29 21:18:48 +01:00
{
ArgumentNullException.ThrowIfNull(user);
ArgumentException.ThrowIfNullOrEmpty(deviceId);
2014-12-29 21:18:48 +01:00
if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
2020-05-13 04:10:35 +02:00
{
return true;
}
2021-12-20 13:31:07 +01:00
return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparison.OrdinalIgnoreCase)
2021-04-10 22:17:36 +02:00
|| !GetCapabilities(deviceId).SupportsPersistentIdentifier;
}
2014-12-29 21:18:48 +01:00
2021-04-10 22:17:36 +02:00
private DeviceInfo ToDeviceInfo(Device authInfo)
{
var caps = GetCapabilities(authInfo.DeviceId);
2014-12-29 21:18:48 +01:00
2021-04-10 22:17:36 +02:00
return new DeviceInfo
{
AppName = authInfo.AppName,
AppVersion = authInfo.AppVersion,
Id = authInfo.DeviceId,
LastUserId = authInfo.UserId,
LastUserName = authInfo.User.Username,
Name = authInfo.DeviceName,
DateLastActivity = authInfo.DateLastActivity,
IconUrl = caps.IconUrl
};
2014-12-29 21:18:48 +01:00
}
2018-09-12 19:26:21 +02:00
}
2018-12-28 16:48:26 +01:00
}