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

244 lines
9.3 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();
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;
}
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
}
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);
}
2021-05-21 03:56:59 +00:00
return device;
}
2021-04-10 20:17:36 +00:00
/// <inheritdoc />
public async Task<DeviceOptions> GetDeviceOptions(string deviceId)
2018-09-12 17:26:21 +00: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 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 async Task<DeviceInfo?> GetDevice(string id)
2014-10-11 20:38:13 +00:00
{
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);
2021-04-10 20:17:36 +00:00
var deviceInfo = device is null ? null : ToDeviceInfo(device.Device, device.Options);
2021-04-10 20:17:36 +00:00
return deviceInfo;
}
2014-10-11 20:38:13 +00:00
}
2021-05-21 03:56:59 +00: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 03:56:59 +00:00
var count = await devices.CountAsync().ConfigureAwait(false);
2021-05-21 03:56:59 +00:00
if (query.Skip.HasValue)
{
devices = devices.Skip(query.Skip.Value);
}
2021-06-19 19:24:42 +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, await devices.ToListAsync().ConfigureAwait(false));
2021-05-21 03:56:59 +00:00
}
}
/// <inheritdoc />
public async Task<QueryResult<DeviceInfo>> GetDeviceInfos(DeviceQuery query)
{
var devices = await GetDevices(query).ConfigureAwait(false);
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 async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId)
2014-10-11 20:38:13 +00:00
{
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
2014-12-15 05:49:04 +00:00
{
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();
2014-12-13 03:56:30 +00:00
if (userId.HasValue)
{
var user = _userManager.GetUserById(userId.Value);
if (user is null)
{
throw new ResourceNotFoundException();
}
sessions = sessions.Where(i => CanAccessDevice(user, i.Device.DeviceId));
}
2014-12-31 06:24:49 +00:00
var array = await sessions.Select(device => ToDeviceInfo(device.Device, device.Options)).ToArrayAsync().ConfigureAwait(false);
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 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
}
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
}