2014-10-11 16:38:13 -04:00
|
|
|
using System;
|
2020-11-19 09:38:54 -05:00
|
|
|
using System.Collections.Concurrent;
|
2014-10-11 16:38:13 -04:00
|
|
|
using System.Linq;
|
2021-04-10 16:17:36 -04:00
|
|
|
using System.Threading.Tasks;
|
2020-05-15 17:20:07 -04:00
|
|
|
using Jellyfin.Data.Entities;
|
2021-04-10 16:17:36 -04:00
|
|
|
using Jellyfin.Data.Entities.Security;
|
2020-07-29 19:25:47 -04:00
|
|
|
using Jellyfin.Data.Enums;
|
2020-08-13 20:48:28 -04:00
|
|
|
using Jellyfin.Data.Events;
|
2019-01-13 14:20:41 -05:00
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Model.Devices;
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
using MediaBrowser.Model.Session;
|
2021-04-10 16:17:36 -04:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
namespace Jellyfin.Server.Implementations.Devices
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
|
|
|
public class DeviceManager : IDeviceManager
|
|
|
|
{
|
2021-04-10 16:17:36 -04:00
|
|
|
private readonly JellyfinDbProvider _dbProvider;
|
2014-10-11 16:38:13 -04:00
|
|
|
private readonly IUserManager _userManager;
|
2020-11-19 09:38:54 -05:00
|
|
|
private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new ();
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2021-04-10 16:17:36 -04: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 16:38:13 -04:00
|
|
|
{
|
2021-04-10 16:17:36 -04:00
|
|
|
_dbProvider = dbProvider;
|
2014-10-11 16:38:13 -04:00
|
|
|
_userManager = userManager;
|
|
|
|
}
|
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
/// <inheritdoc />
|
|
|
|
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated;
|
2020-11-19 09:38:54 -05:00
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
/// <inheritdoc />
|
2018-09-12 13:26:21 -04:00
|
|
|
public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2020-11-19 09:38:54 -05:00
|
|
|
_capabilitiesMap[deviceId] = capabilities;
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task UpdateDeviceOptions(string deviceId, DeviceOptions options)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2021-04-10 16:17:36 -04:00
|
|
|
await using var dbContext = _dbProvider.CreateContext();
|
|
|
|
await dbContext.Database
|
|
|
|
.ExecuteSqlRawAsync($"UPDATE [DeviceOptions] SET [CustomName] = ${options.CustomName}")
|
|
|
|
.ConfigureAwait(false);
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2020-04-05 12:10:56 -04:00
|
|
|
DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, options)));
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
/// <inheritdoc />
|
|
|
|
public DeviceOptions? GetDeviceOptions(string deviceId)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2021-04-10 16:17:36 -04:00
|
|
|
using var dbContext = _dbProvider.CreateContext();
|
|
|
|
return dbContext.DeviceOptions
|
|
|
|
.AsQueryable()
|
|
|
|
.FirstOrDefault(d => d.DeviceId == deviceId);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2014-10-11 16:38:13 -04:00
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
/// <inheritdoc />
|
2018-09-12 13:26:21 -04:00
|
|
|
public ClientCapabilities GetCapabilities(string id)
|
|
|
|
{
|
2021-04-10 16:17:36 -04:00
|
|
|
return _capabilitiesMap.TryGetValue(id, out ClientCapabilities? result)
|
2020-11-19 09:38:54 -05:00
|
|
|
? result
|
|
|
|
: new ClientCapabilities();
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task<DeviceInfo?> GetDevice(string id)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2021-04-10 16:17:36 -04: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 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task<QueryResult<DeviceInfo>> GetDevices(DeviceQuery query)
|
2014-10-11 16:38:13 -04:00
|
|
|
{
|
2021-04-10 16:17:36 -04:00
|
|
|
await using var dbContext = _dbProvider.CreateContext();
|
|
|
|
var sessions = dbContext.Devices
|
|
|
|
.AsQueryable()
|
|
|
|
.OrderBy(d => d.DeviceId)
|
|
|
|
.ThenByDescending(d => d.DateLastActivity)
|
|
|
|
.AsAsyncEnumerable();
|
2019-01-07 18:27:46 -05:00
|
|
|
|
2019-01-02 13:13:35 -05:00
|
|
|
// TODO: DeviceQuery doesn't seem to be used from client. Not even Swagger.
|
2014-12-15 00:49:04 -05:00
|
|
|
if (query.SupportsSync.HasValue)
|
|
|
|
{
|
|
|
|
var val = query.SupportsSync.Value;
|
|
|
|
|
2020-02-19 15:56:35 -05:00
|
|
|
sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == val);
|
2014-12-15 00:49:04 -05:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (!query.UserId.Equals(Guid.Empty))
|
2014-12-12 22:56:30 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
var user = _userManager.GetUserById(query.UserId);
|
2014-12-12 22:56:30 -05:00
|
|
|
|
2020-02-19 15:56:35 -05:00
|
|
|
sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
|
2014-12-31 01:24:49 -05:00
|
|
|
}
|
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
var array = await sessions.Select(ToDeviceInfo).ToArrayAsync();
|
2015-01-26 11:47:15 -05:00
|
|
|
|
2020-02-19 15:56:35 -05:00
|
|
|
return new QueryResult<DeviceInfo>(array);
|
2014-10-11 16:38:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
/// <inheritdoc />
|
2018-09-12 13:26:21 -04:00
|
|
|
public bool CanAccessDevice(User user, string deviceId)
|
2014-12-29 15:18:48 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
if (user == null)
|
2014-12-29 15:18:48 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
throw new ArgumentException("user not found");
|
2014-12-29 15:18:48 -05:00
|
|
|
}
|
2020-06-15 17:43:52 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (string.IsNullOrEmpty(deviceId))
|
2014-12-29 15:18:48 -05:00
|
|
|
{
|
2019-01-06 15:50:43 -05:00
|
|
|
throw new ArgumentNullException(nameof(deviceId));
|
2014-12-29 15:18:48 -05:00
|
|
|
}
|
|
|
|
|
2020-05-20 19:47:41 -04:00
|
|
|
if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
|
2020-05-12 22:10:35 -04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparer.OrdinalIgnoreCase)
|
|
|
|
|| !GetCapabilities(deviceId).SupportsPersistentIdentifier;
|
|
|
|
}
|
2014-12-29 15:18:48 -05:00
|
|
|
|
2021-04-10 16:17:36 -04:00
|
|
|
private DeviceInfo ToDeviceInfo(Device authInfo)
|
|
|
|
{
|
|
|
|
var caps = GetCapabilities(authInfo.DeviceId);
|
2014-12-29 15:18:48 -05:00
|
|
|
|
2021-04-10 16:17:36 -04: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 15:18:48 -05:00
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2018-12-28 10:48:26 -05:00
|
|
|
}
|