| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 | 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 Jellyfin.Data.Queries;using Jellyfin.Extensions;using MediaBrowser.Common.Extensions;using MediaBrowser.Controller.Devices;using MediaBrowser.Controller.Library;using MediaBrowser.Model.Devices;using MediaBrowser.Model.Querying;using MediaBrowser.Model.Session;using Microsoft.EntityFrameworkCore;namespace Jellyfin.Server.Implementations.Devices{    /// <summary>    /// Manages the creation, updating, and retrieval of devices.    /// </summary>    public class DeviceManager : IDeviceManager    {        private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;        private readonly IUserManager _userManager;        private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new();        /// <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<JellyfinDbContext> dbProvider, IUserManager userManager)        {            _dbProvider = dbProvider;            _userManager = userManager;        }        /// <inheritdoc />        public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated;        /// <inheritdoc />        public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)        {            _capabilitiesMap[deviceId] = capabilities;        }        /// <inheritdoc />        public async Task UpdateDeviceOptions(string deviceId, string deviceName)        {            DeviceOptions? deviceOptions;            var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);            await using (dbContext.ConfigureAwait(false))            {                deviceOptions = await dbContext.DeviceOptions.FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false);                if (deviceOptions is null)                {                    deviceOptions = new DeviceOptions(deviceId);                    dbContext.DeviceOptions.Add(deviceOptions);                }                deviceOptions.CustomName = deviceName;                await dbContext.SaveChangesAsync().ConfigureAwait(false);            }            DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, deviceOptions)));        }        /// <inheritdoc />        public async Task<Device> CreateDevice(Device device)        {            var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);            await using (dbContext.ConfigureAwait(false))            {                dbContext.Devices.Add(device);                await dbContext.SaveChangesAsync().ConfigureAwait(false);            }            return device;        }        /// <inheritdoc />        public async Task<DeviceOptions> GetDeviceOptions(string deviceId)        {            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);        }        /// <inheritdoc />        public ClientCapabilities GetCapabilities(string deviceId)        {            return _capabilitiesMap.TryGetValue(deviceId, out ClientCapabilities? result)                ? result                : new ClientCapabilities();        }        /// <inheritdoc />        public async Task<DeviceInfo?> GetDevice(string id)        {            var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);            await using (dbContext.ConfigureAwait(false))            {                var device = await dbContext.Devices                    .Where(d => d.DeviceId == id)                    .OrderByDescending(d => d.DateLastActivity)                    .Include(d => d.User)                    .SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o })                    .FirstOrDefaultAsync()                    .ConfigureAwait(false);                var deviceInfo = device is null ? null : ToDeviceInfo(device.Device, device.Options);                return deviceInfo;            }        }        /// <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                    .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);                var count = await devices.CountAsync().ConfigureAwait(false);                if (query.Skip.HasValue)                {                    devices = devices.Skip(query.Skip.Value);                }                if (query.Limit.HasValue)                {                    devices = devices.Take(query.Limit.Value);                }                return new QueryResult<Device>(query.Skip, count, await devices.ToListAsync().ConfigureAwait(false));            }        }        /// <inheritdoc />        public async Task<QueryResult<DeviceInfo>> GetDeviceInfos(DeviceQuery query)        {            var devices = await GetDevices(query).ConfigureAwait(false);            return new QueryResult<DeviceInfo>(                devices.StartIndex,                devices.TotalRecordCount,                devices.Items.Select(device => ToDeviceInfo(device)).ToList());        }        /// <inheritdoc />        public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId)        {            var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);            await using (dbContext.ConfigureAwait(false))            {                var sessions = dbContext.Devices                    .Include(d => d.User)                    .OrderByDescending(d => d.DateLastActivity)                    .ThenBy(d => d.DeviceId)                    .SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o })                    .AsAsyncEnumerable();                if (userId.HasValue)                {                    var user = _userManager.GetUserById(userId.Value);                    if (user is null)                    {                        throw new ResourceNotFoundException();                    }                    sessions = sessions.Where(i => CanAccessDevice(user, i.Device.DeviceId));                }                var array = await sessions.Select(device => ToDeviceInfo(device.Device, device.Options)).ToArrayAsync().ConfigureAwait(false);                return new QueryResult<DeviceInfo>(array);            }        }        /// <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);            }        }        /// <inheritdoc />        public bool CanAccessDevice(User user, string deviceId)        {            ArgumentNullException.ThrowIfNull(user);            ArgumentException.ThrowIfNullOrEmpty(deviceId);            if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))            {                return true;            }            return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparison.OrdinalIgnoreCase)                   || !GetCapabilities(deviceId).SupportsPersistentIdentifier;        }        private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options = null)        {            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,            };        }    }}
 |