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

254 lines
9.2 KiB
C#
Raw Normal View History

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