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