|
|
|
@ -1,78 +1,100 @@
|
|
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Jellyfin.Data.Entities;
|
|
|
|
|
using Jellyfin.Data.Entities.Security;
|
|
|
|
|
using Jellyfin.Data.Enums;
|
|
|
|
|
using Jellyfin.Data.Events;
|
|
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Security;
|
|
|
|
|
using MediaBrowser.Model.Devices;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Devices
|
|
|
|
|
namespace Jellyfin.Server.Implementations.Devices
|
|
|
|
|
{
|
|
|
|
|
public class DeviceManager : IDeviceManager
|
|
|
|
|
{
|
|
|
|
|
private readonly JellyfinDbProvider _dbProvider;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly IAuthenticationRepository _authRepo;
|
|
|
|
|
private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new ();
|
|
|
|
|
|
|
|
|
|
public DeviceManager(IAuthenticationRepository authRepo, IUserManager userManager)
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
_dbProvider = dbProvider;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_authRepo = authRepo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>> DeviceOptionsUpdated;
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
|
|
|
|
|
{
|
|
|
|
|
_capabilitiesMap[deviceId] = capabilities;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateDeviceOptions(string deviceId, DeviceOptions options)
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task UpdateDeviceOptions(string deviceId, DeviceOptions options)
|
|
|
|
|
{
|
|
|
|
|
_authRepo.UpdateDeviceOptions(deviceId, options);
|
|
|
|
|
await using var dbContext = _dbProvider.CreateContext();
|
|
|
|
|
await dbContext.Database
|
|
|
|
|
.ExecuteSqlRawAsync($"UPDATE [DeviceOptions] SET [CustomName] = ${options.CustomName}")
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, options)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DeviceOptions GetDeviceOptions(string deviceId)
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public DeviceOptions? GetDeviceOptions(string deviceId)
|
|
|
|
|
{
|
|
|
|
|
return _authRepo.GetDeviceOptions(deviceId);
|
|
|
|
|
using var dbContext = _dbProvider.CreateContext();
|
|
|
|
|
return dbContext.DeviceOptions
|
|
|
|
|
.AsQueryable()
|
|
|
|
|
.FirstOrDefault(d => d.DeviceId == deviceId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ClientCapabilities GetCapabilities(string id)
|
|
|
|
|
{
|
|
|
|
|
return _capabilitiesMap.TryGetValue(id, out ClientCapabilities result)
|
|
|
|
|
return _capabilitiesMap.TryGetValue(id, out ClientCapabilities? result)
|
|
|
|
|
? result
|
|
|
|
|
: new ClientCapabilities();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DeviceInfo GetDevice(string id)
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task<DeviceInfo?> GetDevice(string id)
|
|
|
|
|
{
|
|
|
|
|
var session = _authRepo.Get(new AuthenticationInfoQuery
|
|
|
|
|
{
|
|
|
|
|
DeviceId = id
|
|
|
|
|
}).Items.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
var device = session == null ? null : ToDeviceInfo(session);
|
|
|
|
|
|
|
|
|
|
return device;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task<QueryResult<DeviceInfo>> GetDevices(DeviceQuery query)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<AuthenticationInfo> sessions = _authRepo.Get(new AuthenticationInfoQuery
|
|
|
|
|
{
|
|
|
|
|
// UserId = query.UserId
|
|
|
|
|
HasUser = true
|
|
|
|
|
}).Items;
|
|
|
|
|
await using var dbContext = _dbProvider.CreateContext();
|
|
|
|
|
var sessions = dbContext.Devices
|
|
|
|
|
.AsQueryable()
|
|
|
|
|
.OrderBy(d => d.DeviceId)
|
|
|
|
|
.ThenByDescending(d => d.DateLastActivity)
|
|
|
|
|
.AsAsyncEnumerable();
|
|
|
|
|
|
|
|
|
|
// TODO: DeviceQuery doesn't seem to be used from client. Not even Swagger.
|
|
|
|
|
if (query.SupportsSync.HasValue)
|
|
|
|
@ -89,28 +111,12 @@ namespace Emby.Server.Implementations.Devices
|
|
|
|
|
sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var array = sessions.Select(ToDeviceInfo).ToArray();
|
|
|
|
|
var array = await sessions.Select(ToDeviceInfo).ToArrayAsync();
|
|
|
|
|
|
|
|
|
|
return new QueryResult<DeviceInfo>(array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DeviceInfo ToDeviceInfo(AuthenticationInfo authInfo)
|
|
|
|
|
{
|
|
|
|
|
var caps = GetCapabilities(authInfo.DeviceId);
|
|
|
|
|
|
|
|
|
|
return new DeviceInfo
|
|
|
|
|
{
|
|
|
|
|
AppName = authInfo.AppName,
|
|
|
|
|
AppVersion = authInfo.AppVersion,
|
|
|
|
|
Id = authInfo.DeviceId,
|
|
|
|
|
LastUserId = authInfo.UserId,
|
|
|
|
|
LastUserName = authInfo.UserName,
|
|
|
|
|
Name = authInfo.DeviceName,
|
|
|
|
|
DateLastActivity = authInfo.DateLastActivity,
|
|
|
|
|
IconUrl = caps?.IconUrl
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool CanAccessDevice(User user, string deviceId)
|
|
|
|
|
{
|
|
|
|
|
if (user == null)
|
|
|
|
@ -128,17 +134,25 @@ namespace Emby.Server.Implementations.Devices
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
var capabilities = GetCapabilities(deviceId);
|
|
|
|
|
return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
|| !GetCapabilities(deviceId).SupportsPersistentIdentifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (capabilities != null && capabilities.SupportsPersistentIdentifier)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private DeviceInfo ToDeviceInfo(Device authInfo)
|
|
|
|
|
{
|
|
|
|
|
var caps = GetCapabilities(authInfo.DeviceId);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|