Files
jellyfin-jellyfin-1/Jellyfin.Server.Implementations/Devices/DeviceManager.cs

312 lines
11 KiB
C#
Raw Normal View History

2014-10-11 16:38:13 -04:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
2014-10-11 16:38:13 -04:00
using System.Linq;
2021-04-10 16:17:36 -04:00
using System.Threading.Tasks;
using Jellyfin.Data;
using Jellyfin.Data.Dtos;
using Jellyfin.Data.Events;
2021-05-20 23:56:59 -04:00
using Jellyfin.Data.Queries;
2025-03-25 15:30:22 +00:00
using Jellyfin.Database.Implementations;
2025-03-25 16:45:00 +01:00
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Database.Implementations.Entities.Security;
2025-03-25 15:30:22 +00:00
using Jellyfin.Database.Implementations.Enums;
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.Dto;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Session;
2021-04-10 16:17:36 -04:00
using Microsoft.EntityFrameworkCore;
2014-10-11 16:38:13 -04:00
2021-04-10 16:17:36 -04:00
namespace Jellyfin.Server.Implementations.Devices
2014-10-11 16:38:13 -04:00
{
2021-04-10 17:11:59 -04:00
/// <summary>
/// Manages the creation, updating, and retrieval of devices.
/// </summary>
2014-10-11 16:38:13 -04:00
public class DeviceManager : IDeviceManager
{
2023-01-16 12:14:44 -05:00
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
2014-10-11 16:38:13 -04:00
private readonly IUserManager _userManager;
2021-12-24 18:28:27 +01:00
private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new();
private readonly ConcurrentDictionary<int, Device> _devices;
private readonly ConcurrentDictionary<string, DeviceOptions> _deviceOptions;
2018-09-12 19:26:21 +02:00
2021-04-10 16:17:36 -04: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 12:14:44 -05:00
public DeviceManager(IDbContextFactory<JellyfinDbContext> dbProvider, IUserManager userManager)
2014-10-11 16:38:13 -04:00
{
2021-04-10 16:17:36 -04:00
_dbProvider = dbProvider;
2014-10-11 16:38:13 -04:00
_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);
}
2014-10-11 16:38:13 -04:00
}
2021-04-10 16:17:36 -04:00
/// <inheritdoc />
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated;
2021-04-10 16:17:36 -04:00
/// <inheritdoc />
2018-09-12 19:26:21 +02:00
public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
2014-10-11 16:38:13 -04:00
{
_capabilitiesMap[deviceId] = capabilities;
2018-09-12 19:26:21 +02:00
}
2014-10-11 16:38:13 -04:00
2021-04-10 16:17:36 -04:00
/// <inheritdoc />
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 15:24:26 -04: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 15:24:26 -04:00
}
_deviceOptions[deviceId] = deviceOptions;
2021-07-13 19:30:11 -04: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 16:38:13 -04:00
2021-05-20 23:56:59 -04: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);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
_devices.TryAdd(device.Id, device);
}
2021-05-20 23:56:59 -04:00
return device;
}
2021-04-10 16:17:36 -04:00
/// <inheritdoc />
public DeviceOptionsDto? GetDeviceOptions(string deviceId)
2018-09-12 19:26:21 +02:00
{
if (_deviceOptions.TryGetValue(deviceId, out var deviceOptions))
{
return ToDeviceOptionsDto(deviceOptions);
}
return null;
2018-09-12 19:26:21 +02:00
}
2014-10-11 16:38:13 -04:00
2021-04-10 16:17:36 -04:00
/// <inheritdoc />
public ClientCapabilities GetCapabilities(string? deviceId)
2018-09-12 19:26:21 +02:00
{
if (deviceId is null)
{
return new();
}
2021-04-10 17:11:59 -04:00
return _capabilitiesMap.TryGetValue(deviceId, out ClientCapabilities? result)
? result
: new();
2014-10-11 16:38:13 -04:00
}
2021-04-10 16:17:36 -04:00
/// <inheritdoc />
public DeviceInfoDto? GetDevice(string id)
2014-10-11 16:38:13 -04:00
{
var device = _devices.Values.Where(d => d.DeviceId == id).OrderByDescending(d => d.DateLastActivity).FirstOrDefault();
_deviceOptions.TryGetValue(id, out var deviceOption);
2021-04-10 16:17:36 -04:00
var deviceInfo = device is null ? null : ToDeviceInfo(device, deviceOption);
return deviceInfo is null ? null : ToDeviceInfoDto(deviceInfo);
2014-10-11 16:38:13 -04:00
}
2021-05-20 23:56:59 -04:00
/// <inheritdoc />
public QueryResult<Device> GetDevices(DeviceQuery query)
2021-05-20 23:56:59 -04:00
{
IEnumerable<Device> devices = _devices.Values
.Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value))
2024-09-17 20:29:43 +02:00
.Where(device => query.DeviceId is null || device.DeviceId == query.DeviceId)
.Where(device => query.AccessToken is null || device.AccessToken == query.AccessToken)
.OrderBy(d => d.Id)
.ToList();
var count = devices.Count();
if (query.Skip.HasValue)
{
devices = devices.Skip(query.Skip.Value);
}
2021-05-20 23:56:59 -04:00
if (query.Limit.HasValue)
{
devices = devices.Take(query.Limit.Value);
2021-05-20 23:56:59 -04:00
}
return new QueryResult<Device>(query.Skip, count, devices.ToList());
2021-05-20 23:56:59 -04:00
}
/// <inheritdoc />
public QueryResult<DeviceInfo> GetDeviceInfos(DeviceQuery query)
2021-05-20 23:56:59 -04:00
{
var devices = GetDevices(query);
2021-05-20 23:56:59 -04:00
2022-01-20 08:46:17 -07:00
return new QueryResult<DeviceInfo>(
devices.StartIndex,
devices.TotalRecordCount,
devices.Items.Select(device => ToDeviceInfo(device)).ToList());
2021-05-20 23:56:59 -04:00
}
2021-04-10 16:17:36 -04:00
/// <inheritdoc />
public QueryResult<DeviceInfoDto> GetDevicesForUser(Guid? userId)
2014-10-11 16:38:13 -04:00
{
IEnumerable<Device> devices = _devices.Values
.OrderByDescending(d => d.DateLastActivity)
.ThenBy(d => d.DeviceId);
if (!userId.IsNullOrEmpty())
2014-12-15 00:49:04 -05:00
{
var user = _userManager.GetUserById(userId.Value);
if (user is null)
{
throw new ResourceNotFoundException();
}
2014-12-31 01:24:49 -05:00
devices = devices.Where(i => CanAccessDevice(user, i.DeviceId));
}
var array = devices.Select(device =>
{
_deviceOptions.TryGetValue(device.DeviceId, out var option);
return ToDeviceInfo(device, option);
})
.Select(ToDeviceInfoDto)
.ToArray();
return new QueryResult<DeviceInfoDto>(array);
2014-10-11 16:38:13 -04:00
}
2021-05-20 23:56:59 -04:00
/// <inheritdoc />
public async Task DeleteDevice(Device device)
{
_devices.TryRemove(device.Id, out _);
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
dbContext.Devices.Remove(device);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
2021-05-20 23:56:59 -04:00
}
/// <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;
}
2021-04-10 16:17:36 -04:00
/// <inheritdoc />
2018-09-12 19:26:21 +02:00
public bool CanAccessDevice(User user, string deviceId)
2014-12-29 15:18:48 -05:00
{
ArgumentNullException.ThrowIfNull(user);
ArgumentException.ThrowIfNullOrEmpty(deviceId);
2014-12-29 15:18:48 -05:00
if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
2020-05-12 22:10:35 -04:00
{
return true;
}
2021-12-20 13:31:07 +01:00
return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparison.OrdinalIgnoreCase)
2021-04-10 16:17:36 -04:00
|| !GetCapabilities(deviceId).SupportsPersistentIdentifier;
}
2014-12-29 15:18:48 -05:00
2023-10-18 01:40:36 +08:00
private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options = null)
{
var caps = GetCapabilities(authInfo.DeviceId);
var user = _userManager.GetUserById(authInfo.UserId) ?? throw new ResourceNotFoundException("User with UserId " + authInfo.UserId + " not found");
return new()
{
AppName = authInfo.AppName,
AppVersion = authInfo.AppVersion,
Id = authInfo.DeviceId,
LastUserId = authInfo.UserId,
LastUserName = user.Username,
Name = authInfo.DeviceName,
DateLastActivity = authInfo.DateLastActivity,
IconUrl = caps.IconUrl,
CustomName = options?.CustomName,
};
}
private DeviceOptionsDto ToDeviceOptionsDto(DeviceOptions options)
{
return new()
{
Id = options.Id,
DeviceId = options.DeviceId,
CustomName = options.CustomName,
};
}
private DeviceInfoDto ToDeviceInfoDto(DeviceInfo info)
{
return new()
{
Name = info.Name,
CustomName = info.CustomName,
AccessToken = info.AccessToken,
Id = info.Id,
LastUserName = info.LastUserName,
AppName = info.AppName,
AppVersion = info.AppVersion,
LastUserId = info.LastUserId,
DateLastActivity = info.DateLastActivity,
Capabilities = ToClientCapabilitiesDto(info.Capabilities),
IconUrl = info.IconUrl
};
}
/// <inheritdoc />
public ClientCapabilitiesDto ToClientCapabilitiesDto(ClientCapabilities capabilities)
{
return new()
{
PlayableMediaTypes = capabilities.PlayableMediaTypes,
SupportedCommands = capabilities.SupportedCommands,
SupportsMediaControl = capabilities.SupportsMediaControl,
SupportsPersistentIdentifier = capabilities.SupportsPersistentIdentifier,
DeviceProfile = capabilities.DeviceProfile,
AppStoreUrl = capabilities.AppStoreUrl,
IconUrl = capabilities.IconUrl
};
}
2018-09-12 19:26:21 +02:00
}
2018-12-28 16:48:26 +01:00
}