2014-10-11 20:38:13 +00:00
|
|
|
using System;
|
2020-11-19 14:38:54 +00:00
|
|
|
using System.Collections.Concurrent;
|
2022-10-21 09:55:32 +00:00
|
|
|
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;
|
2020-05-15 21:20:07 +00:00
|
|
|
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;
|
2020-08-14 00:48:28 +00:00
|
|
|
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;
|
2023-02-04 16:56:12 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2019-01-13 19:20:41 +00:00
|
|
|
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;
|
2020-11-19 14:38:54 +00:00
|
|
|
|
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
|
|
|
{
|
2020-11-19 14:38:54 +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
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
DeviceOptions? deviceOptions;
|
|
|
|
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
|
|
|
|
await using (dbContext.ConfigureAwait(false))
|
2021-06-19 19:24:26 +00:00
|
|
|
{
|
2023-01-16 16:49:59 +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)
|
2022-10-21 09:55:32 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
|
|
|
|
await using (dbContext.ConfigureAwait(false))
|
|
|
|
{
|
|
|
|
dbContext.Devices.Add(device);
|
2021-05-21 03:56:59 +00:00
|
|
|
|
2022-10-21 09:55:32 +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 />
|
2021-06-27 20:42:26 +00:00
|
|
|
public async Task<DeviceOptions> GetDeviceOptions(string deviceId)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2022-10-21 09:55:32 +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);
|
|
|
|
}
|
2021-06-27 20:42:26 +00:00
|
|
|
|
|
|
|
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)
|
2020-11-19 14:38:54 +00:00
|
|
|
? 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
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
|
|
|
|
await using (dbContext.ConfigureAwait(false))
|
|
|
|
{
|
2023-10-17 16:41:33 +00:00
|
|
|
var device = await dbContext.Devices
|
2022-10-21 09:55:32 +00:00
|
|
|
.Where(d => d.DeviceId == id)
|
|
|
|
.OrderByDescending(d => d.DateLastActivity)
|
|
|
|
.Include(d => d.User)
|
2023-10-17 16:41:33 +00:00
|
|
|
.SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o })
|
2022-10-21 09:55:32 +00:00
|
|
|
.FirstOrDefaultAsync()
|
|
|
|
.ConfigureAwait(false);
|
2021-04-10 20:17:36 +00:00
|
|
|
|
2023-10-17 16:41:33 +00:00
|
|
|
var deviceInfo = device is null ? null : ToDeviceInfo(device.Device, device.Options);
|
2021-04-10 20:17:36 +00:00
|
|
|
|
2023-10-17 16:41:33 +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)
|
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
|
|
|
|
await using (dbContext.ConfigureAwait(false))
|
|
|
|
{
|
2023-01-16 16:49:59 +00:00
|
|
|
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
|
|
|
|
2022-10-21 09:55:32 +00:00
|
|
|
var count = await devices.CountAsync().ConfigureAwait(false);
|
2021-05-21 03:56:59 +00:00
|
|
|
|
2022-10-21 09:55:32 +00:00
|
|
|
if (query.Skip.HasValue)
|
|
|
|
{
|
|
|
|
devices = devices.Skip(query.Skip.Value);
|
|
|
|
}
|
2021-06-19 19:24:42 +00:00
|
|
|
|
2022-10-21 09:55:32 +00:00
|
|
|
if (query.Limit.HasValue)
|
|
|
|
{
|
|
|
|
devices = devices.Take(query.Limit.Value);
|
|
|
|
}
|
2021-05-21 03:56:59 +00:00
|
|
|
|
2022-10-21 09:55:32 +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 />
|
2021-05-21 00:39:22 +00:00
|
|
|
public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId, bool? supportsSync)
|
2014-10-11 20:38:13 +00:00
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
|
|
|
|
await using (dbContext.ConfigureAwait(false))
|
2014-12-15 05:49:04 +00:00
|
|
|
{
|
2023-10-17 16:41:33 +00:00
|
|
|
var sessions = dbContext.Devices
|
2022-10-21 09:55:32 +00:00
|
|
|
.Include(d => d.User)
|
|
|
|
.OrderByDescending(d => d.DateLastActivity)
|
|
|
|
.ThenBy(d => d.DeviceId)
|
2023-10-17 16:41:33 +00:00
|
|
|
.SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o })
|
2022-10-21 09:55:32 +00:00
|
|
|
.AsAsyncEnumerable();
|
|
|
|
if (supportsSync.HasValue)
|
|
|
|
{
|
2023-10-17 16:41:33 +00:00
|
|
|
sessions = sessions.Where(i => GetCapabilities(i.Device.DeviceId).SupportsSync == supportsSync.Value);
|
2022-10-21 09:55:32 +00:00
|
|
|
}
|
2014-12-13 03:56:30 +00:00
|
|
|
|
2022-10-21 09:55:32 +00:00
|
|
|
if (userId.HasValue)
|
|
|
|
{
|
|
|
|
var user = _userManager.GetUserById(userId.Value);
|
2023-02-04 16:56:12 +00:00
|
|
|
if (user is null)
|
|
|
|
{
|
|
|
|
throw new ResourceNotFoundException();
|
|
|
|
}
|
2022-10-21 09:55:32 +00:00
|
|
|
|
2023-10-17 16:41:33 +00:00
|
|
|
sessions = sessions.Where(i => CanAccessDevice(user, i.Device.DeviceId));
|
2022-10-21 09:55:32 +00:00
|
|
|
}
|
2014-12-31 06:24:49 +00:00
|
|
|
|
2023-10-17 16:41:33 +00:00
|
|
|
var array = await sessions.Select(device => ToDeviceInfo(device.Device, device.Options)).ToArrayAsync().ConfigureAwait(false);
|
2015-01-26 16:47:15 +00:00
|
|
|
|
2022-10-21 09:55:32 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-10-21 09:55:32 +00:00
|
|
|
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
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(user);
|
2022-10-13 17:08:00 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(deviceId);
|
2014-12-29 20:18:48 +00:00
|
|
|
|
2020-05-20 23:47:41 +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
|
|
|
|
2021-04-10 20:17:36 +00:00
|
|
|
private DeviceInfo ToDeviceInfo(Device authInfo)
|
|
|
|
{
|
|
|
|
var caps = GetCapabilities(authInfo.DeviceId);
|
2014-12-29 20:18:48 +00:00
|
|
|
|
2021-04-10 20:17:36 +00: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 20:18:48 +00:00
|
|
|
}
|
2023-10-17 16:41:33 +00:00
|
|
|
|
|
|
|
private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|