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

264 lines
9.6 KiB
C#
Raw Normal View History

2014-10-11 20:38:13 +00:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
2014-10-11 20:38:13 +00:00
using System.Linq;
2021-04-10 20:17:36 +00:00
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
2021-04-10 20:17:36 +00:00
using Jellyfin.Data.Entities.Security;
2020-07-29 23:25:47 +00:00
using Jellyfin.Data.Enums;
using Jellyfin.Data.Events;
2021-05-21 03:56:59 +00:00
using Jellyfin.Data.Queries;
2021-12-20 12:31:07 +00: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 20:17:36 +00:00
using Microsoft.EntityFrameworkCore;
2014-10-11 20:38:13 +00:00
2021-04-10 20:17:36 +00:00
namespace Jellyfin.Server.Implementations.Devices
2014-10-11 20:38:13 +00:00
{
2021-04-10 21:11:59 +00:00
/// <summary>
/// Manages the creation, updating, and retrieval of devices.
/// </summary>
2014-10-11 20:38:13 +00:00
public class DeviceManager : IDeviceManager
{
2023-01-16 17:14:44 +00:00
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
2014-10-11 20:38:13 +00:00
private readonly IUserManager _userManager;
2021-12-24 17:28:27 +00:00
private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new();
private readonly IDictionary<int, Device> _devices;
private readonly IDictionary<string, DeviceOptions> _deviceOptions;
2018-09-12 17:26:21 +00:00
2021-04-10 20:17:36 +00: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 17:14:44 +00:00
public DeviceManager(IDbContextFactory<JellyfinDbContext> dbProvider, IUserManager userManager)
2014-10-11 20:38:13 +00:00
{
2021-04-10 20:17:36 +00:00
_dbProvider = dbProvider;
2014-10-11 20:38:13 +00:00
_userManager = userManager;
_devices = new ConcurrentDictionary<int, Device>();
_deviceOptions = new ConcurrentDictionary<string, DeviceOptions>();
using var dbContext = _dbProvider.CreateDbContext();
foreach (var device in dbContext.Devices
.Include(d => d.User)
.OrderBy(d => d.Id)
.AsEnumerable())
{
_devices.Add(device.Id, device);
}
foreach (var deviceOption in dbContext.DeviceOptions
.OrderBy(d => d.Id)
.AsEnumerable())
{
_deviceOptions.Add(deviceOption.DeviceId, deviceOption);
}
2014-10-11 20:38:13 +00:00
}
2021-04-10 20:17:36 +00:00
/// <inheritdoc />
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated;
2021-04-10 20:17:36 +00:00
/// <inheritdoc />
2018-09-12 17:26:21 +00:00
public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
2014-10-11 20:38:13 +00:00
{
_capabilitiesMap[deviceId] = capabilities;
2018-09-12 17:26:21 +00:00
}
2014-10-11 20:38:13 +00:00
2021-04-10 20:17:36 +00:00
/// <inheritdoc />
2021-07-13 23:30:11 +00:00
public async Task UpdateDeviceOptions(string deviceId, string deviceName)
2018-09-12 17:26:21 +00:00
{
DeviceOptions? deviceOptions;
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
2021-06-19 19:24:26 +00:00
{
deviceOptions = await dbContext.DeviceOptions.FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false);
2022-12-05 14:00:20 +00:00
if (deviceOptions is null)
{
deviceOptions = new DeviceOptions(deviceId);
dbContext.DeviceOptions.Add(deviceOptions);
}
deviceOptions.CustomName = deviceName;
await dbContext.SaveChangesAsync().ConfigureAwait(false);
2021-06-19 19:24:26 +00:00
}
_deviceOptions[deviceId] = deviceOptions;
2021-07-13 23:30:11 +00:00
DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, deviceOptions)));
2018-09-12 17:26:21 +00:00
}
2014-10-11 20:38:13 +00:00
2021-05-21 03:56:59 +00: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 03:56:59 +00:00
await dbContext.SaveChangesAsync().ConfigureAwait(false);
var newDevice = await dbContext.Devices
.Include(d => d.User)
.FirstOrDefaultAsync(d => d.Id == device.Id)
.ConfigureAwait(false);
_devices.Add(device.Id, newDevice!);
}
2021-05-21 03:56:59 +00:00
return device;
}
2021-04-10 20:17:36 +00:00
/// <inheritdoc />
public DeviceOptions GetDeviceOptions(string deviceId)
2018-09-12 17:26:21 +00:00
{
_deviceOptions.TryGetValue(deviceId, out var deviceOptions);
return deviceOptions ?? new DeviceOptions(deviceId);
2018-09-12 17:26:21 +00:00
}
2014-10-11 20:38:13 +00:00
2021-04-10 20:17:36 +00:00
/// <inheritdoc />
2021-04-10 21:11:59 +00:00
public ClientCapabilities GetCapabilities(string deviceId)
2018-09-12 17:26:21 +00:00
{
2021-04-10 21:11:59 +00:00
return _capabilitiesMap.TryGetValue(deviceId, out ClientCapabilities? result)
? result
: new ClientCapabilities();
2014-10-11 20:38:13 +00:00
}
2021-04-10 20:17:36 +00:00
/// <inheritdoc />
public DeviceInfo? GetDevice(string id)
2014-10-11 20:38:13 +00:00
{
var device = _devices.Values.OrderByDescending(d => d.DateLastActivity).FirstOrDefault(d => d.DeviceId == id);
_deviceOptions.TryGetValue(id, out var deviceOption);
2021-04-10 20:17:36 +00:00
var deviceInfo = device is null ? null : ToDeviceInfo(device, deviceOption);
return deviceInfo;
2014-10-11 20:38:13 +00:00
}
2021-05-21 03:56:59 +00:00
/// <inheritdoc />
public QueryResult<Device> GetDevices(DeviceQuery query)
2021-05-21 03:56:59 +00:00
{
var devices = _devices.Values.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 canGetCountDirectly = devices.TryGetNonEnumeratedCount(out var count);
if (!canGetCountDirectly)
{
count = devices.Count();
}
2021-06-19 19:24:42 +00:00
if (query.Skip.HasValue)
{
devices = devices.Skip(query.Skip.Value);
}
2021-05-21 03:56:59 +00:00
if (query.Limit.HasValue)
{
devices = devices.Take(query.Limit.Value);
2021-05-21 03:56:59 +00:00
}
return new QueryResult<Device>(query.Skip, count, devices.ToList());
2021-05-21 03:56:59 +00:00
}
/// <inheritdoc />
public QueryResult<DeviceInfo> GetDeviceInfos(DeviceQuery query)
2021-05-21 03:56:59 +00:00
{
var devices = GetDevices(query);
2021-05-21 03:56:59 +00:00
2022-01-20 15:46:17 +00:00
return new QueryResult<DeviceInfo>(
devices.StartIndex,
devices.TotalRecordCount,
devices.Items.Select(device => ToDeviceInfo(device)).ToList());
2021-05-21 03:56:59 +00:00
}
2021-04-10 20:17:36 +00:00
/// <inheritdoc />
public QueryResult<DeviceInfo> GetDevicesForUser(Guid? userId)
2014-10-11 20:38:13 +00:00
{
var devices = _devices.Values
.OrderByDescending(d => d.DateLastActivity)
.ThenBy(d => d.DeviceId)
.AsEnumerable();
if (!userId.IsNullOrEmpty())
2014-12-15 05:49:04 +00:00
{
var user = _userManager.GetUserById(userId.Value);
if (user is null)
{
throw new ResourceNotFoundException();
}
2014-12-31 06:24:49 +00: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);
}).ToArray();
return new QueryResult<DeviceInfo>(array);
2014-10-11 20:38:13 +00:00
}
2021-05-21 03:56:59 +00:00
/// <inheritdoc />
public async Task DeleteDevice(Device device)
{
var id = _devices.FirstOrDefault(x => x.Value.Equals(device)).Key;
_devices.Remove(id);
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
dbContext.Devices.Remove(device);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
2021-05-21 03:56:59 +00: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 20:17:36 +00:00
/// <inheritdoc />
2018-09-12 17:26:21 +00:00
public bool CanAccessDevice(User user, string deviceId)
2014-12-29 20:18:48 +00:00
{
ArgumentNullException.ThrowIfNull(user);
ArgumentException.ThrowIfNullOrEmpty(deviceId);
2014-12-29 20:18:48 +00:00
if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
2020-05-13 02:10:35 +00:00
{
return true;
}
2021-12-20 12:31:07 +00:00
return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparison.OrdinalIgnoreCase)
2021-04-10 20:17:36 +00:00
|| !GetCapabilities(deviceId).SupportsPersistentIdentifier;
}
2014-12-29 20:18:48 +00:00
2023-10-17 17:40:36 +00:00
private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options = null)
{
var caps = GetCapabilities(authInfo.DeviceId);
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,
CustomName = options?.CustomName,
};
}
2018-09-12 17:26:21 +00:00
}
2018-12-28 15:48:26 +00:00
}