| | 1 | | using System; |
| | 2 | | using System.Collections.Concurrent; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Jellyfin.Data.Entities; |
| | 7 | | using Jellyfin.Data.Entities.Security; |
| | 8 | | using Jellyfin.Data.Enums; |
| | 9 | | using Jellyfin.Data.Events; |
| | 10 | | using Jellyfin.Data.Queries; |
| | 11 | | using Jellyfin.Extensions; |
| | 12 | | using MediaBrowser.Common.Extensions; |
| | 13 | | using MediaBrowser.Controller.Devices; |
| | 14 | | using MediaBrowser.Controller.Library; |
| | 15 | | using MediaBrowser.Model.Devices; |
| | 16 | | using MediaBrowser.Model.Querying; |
| | 17 | | using MediaBrowser.Model.Session; |
| | 18 | | using Microsoft.EntityFrameworkCore; |
| | 19 | |
|
| | 20 | | namespace Jellyfin.Server.Implementations.Devices |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Manages the creation, updating, and retrieval of devices. |
| | 24 | | /// </summary> |
| | 25 | | public class DeviceManager : IDeviceManager |
| | 26 | | { |
| | 27 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | 28 | | private readonly IUserManager _userManager; |
| 22 | 29 | | private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new(); |
| | 30 | | private readonly ConcurrentDictionary<int, Device> _devices; |
| | 31 | | private readonly ConcurrentDictionary<string, DeviceOptions> _deviceOptions; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of the <see cref="DeviceManager"/> class. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="dbProvider">The database provider.</param> |
| | 37 | | /// <param name="userManager">The user manager.</param> |
| | 38 | | public DeviceManager(IDbContextFactory<JellyfinDbContext> dbProvider, IUserManager userManager) |
| | 39 | | { |
| 22 | 40 | | _dbProvider = dbProvider; |
| 22 | 41 | | _userManager = userManager; |
| 22 | 42 | | _devices = new ConcurrentDictionary<int, Device>(); |
| 22 | 43 | | _deviceOptions = new ConcurrentDictionary<string, DeviceOptions>(); |
| | 44 | |
|
| 22 | 45 | | using var dbContext = _dbProvider.CreateDbContext(); |
| 44 | 46 | | foreach (var device in dbContext.Devices |
| 22 | 47 | | .OrderBy(d => d.Id) |
| 22 | 48 | | .AsEnumerable()) |
| | 49 | | { |
| 0 | 50 | | _devices.TryAdd(device.Id, device); |
| | 51 | | } |
| | 52 | |
|
| 44 | 53 | | foreach (var deviceOption in dbContext.DeviceOptions |
| 22 | 54 | | .OrderBy(d => d.Id) |
| 22 | 55 | | .AsEnumerable()) |
| | 56 | | { |
| 0 | 57 | | _deviceOptions.TryAdd(deviceOption.DeviceId, deviceOption); |
| | 58 | | } |
| 22 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <inheritdoc /> |
| | 62 | | public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated; |
| | 63 | |
|
| | 64 | | /// <inheritdoc /> |
| | 65 | | public void SaveCapabilities(string deviceId, ClientCapabilities capabilities) |
| | 66 | | { |
| 0 | 67 | | _capabilitiesMap[deviceId] = capabilities; |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <inheritdoc /> |
| | 71 | | public async Task UpdateDeviceOptions(string deviceId, string deviceName) |
| | 72 | | { |
| | 73 | | DeviceOptions? deviceOptions; |
| | 74 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 75 | | await using (dbContext.ConfigureAwait(false)) |
| | 76 | | { |
| | 77 | | deviceOptions = await dbContext.DeviceOptions.FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).Confi |
| | 78 | | if (deviceOptions is null) |
| | 79 | | { |
| | 80 | | deviceOptions = new DeviceOptions(deviceId); |
| | 81 | | dbContext.DeviceOptions.Add(deviceOptions); |
| | 82 | | } |
| | 83 | |
|
| | 84 | | deviceOptions.CustomName = deviceName; |
| | 85 | | await dbContext.SaveChangesAsync().ConfigureAwait(false); |
| | 86 | | } |
| | 87 | |
|
| | 88 | | _deviceOptions[deviceId] = deviceOptions; |
| | 89 | |
|
| | 90 | | DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, Devi |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <inheritdoc /> |
| | 94 | | public async Task<Device> CreateDevice(Device device) |
| | 95 | | { |
| | 96 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 97 | | await using (dbContext.ConfigureAwait(false)) |
| | 98 | | { |
| | 99 | | dbContext.Devices.Add(device); |
| | 100 | | await dbContext.SaveChangesAsync().ConfigureAwait(false); |
| | 101 | | _devices.TryAdd(device.Id, device); |
| | 102 | | } |
| | 103 | |
|
| | 104 | | return device; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | /// <inheritdoc /> |
| | 108 | | public DeviceOptions GetDeviceOptions(string deviceId) |
| | 109 | | { |
| 16 | 110 | | _deviceOptions.TryGetValue(deviceId, out var deviceOptions); |
| | 111 | |
|
| 16 | 112 | | return deviceOptions ?? new DeviceOptions(deviceId); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | /// <inheritdoc /> |
| | 116 | | public ClientCapabilities GetCapabilities(string deviceId) |
| | 117 | | { |
| 16 | 118 | | return _capabilitiesMap.TryGetValue(deviceId, out ClientCapabilities? result) |
| 16 | 119 | | ? result |
| 16 | 120 | | : new ClientCapabilities(); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | /// <inheritdoc /> |
| | 124 | | public DeviceInfo? GetDevice(string id) |
| | 125 | | { |
| 0 | 126 | | var device = _devices.Values.Where(d => d.DeviceId == id).OrderByDescending(d => d.DateLastActivity).FirstOr |
| 0 | 127 | | _deviceOptions.TryGetValue(id, out var deviceOption); |
| | 128 | |
|
| 0 | 129 | | var deviceInfo = device is null ? null : ToDeviceInfo(device, deviceOption); |
| 0 | 130 | | return deviceInfo; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | /// <inheritdoc /> |
| | 134 | | public QueryResult<Device> GetDevices(DeviceQuery query) |
| | 135 | | { |
| 128 | 136 | | IEnumerable<Device> devices = _devices.Values |
| 128 | 137 | | .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value)) |
| 128 | 138 | | .Where(device => query.DeviceId is null || device.DeviceId == query.DeviceId) |
| 128 | 139 | | .Where(device => query.AccessToken is null || device.AccessToken == query.AccessToken) |
| 128 | 140 | | .OrderBy(d => d.Id) |
| 128 | 141 | | .ToList(); |
| 128 | 142 | | var count = devices.Count(); |
| | 143 | |
|
| 128 | 144 | | if (query.Skip.HasValue) |
| | 145 | | { |
| 0 | 146 | | devices = devices.Skip(query.Skip.Value); |
| | 147 | | } |
| | 148 | |
|
| 128 | 149 | | if (query.Limit.HasValue) |
| | 150 | | { |
| 0 | 151 | | devices = devices.Take(query.Limit.Value); |
| | 152 | | } |
| | 153 | |
|
| 128 | 154 | | return new QueryResult<Device>(query.Skip, count, devices.ToList()); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | /// <inheritdoc /> |
| | 158 | | public QueryResult<DeviceInfo> GetDeviceInfos(DeviceQuery query) |
| | 159 | | { |
| 0 | 160 | | var devices = GetDevices(query); |
| | 161 | |
|
| 0 | 162 | | return new QueryResult<DeviceInfo>( |
| 0 | 163 | | devices.StartIndex, |
| 0 | 164 | | devices.TotalRecordCount, |
| 0 | 165 | | devices.Items.Select(device => ToDeviceInfo(device)).ToList()); |
| | 166 | | } |
| | 167 | |
|
| | 168 | | /// <inheritdoc /> |
| | 169 | | public QueryResult<DeviceInfo> GetDevicesForUser(Guid? userId) |
| | 170 | | { |
| 0 | 171 | | IEnumerable<Device> devices = _devices.Values |
| 0 | 172 | | .OrderByDescending(d => d.DateLastActivity) |
| 0 | 173 | | .ThenBy(d => d.DeviceId); |
| | 174 | |
|
| 0 | 175 | | if (!userId.IsNullOrEmpty()) |
| | 176 | | { |
| 0 | 177 | | var user = _userManager.GetUserById(userId.Value); |
| 0 | 178 | | if (user is null) |
| | 179 | | { |
| 0 | 180 | | throw new ResourceNotFoundException(); |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | devices = devices.Where(i => CanAccessDevice(user, i.DeviceId)); |
| | 184 | | } |
| | 185 | |
|
| 0 | 186 | | var array = devices.Select(device => |
| 0 | 187 | | { |
| 0 | 188 | | _deviceOptions.TryGetValue(device.DeviceId, out var option); |
| 0 | 189 | | return ToDeviceInfo(device, option); |
| 0 | 190 | | }).ToArray(); |
| | 191 | |
|
| 0 | 192 | | return new QueryResult<DeviceInfo>(array); |
| | 193 | | } |
| | 194 | |
|
| | 195 | | /// <inheritdoc /> |
| | 196 | | public async Task DeleteDevice(Device device) |
| | 197 | | { |
| | 198 | | _devices.TryRemove(device.Id, out _); |
| | 199 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 200 | | await using (dbContext.ConfigureAwait(false)) |
| | 201 | | { |
| | 202 | | dbContext.Devices.Remove(device); |
| | 203 | | await dbContext.SaveChangesAsync().ConfigureAwait(false); |
| | 204 | | } |
| | 205 | | } |
| | 206 | |
|
| | 207 | | /// <inheritdoc /> |
| | 208 | | public async Task UpdateDevice(Device device) |
| | 209 | | { |
| | 210 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 211 | | await using (dbContext.ConfigureAwait(false)) |
| | 212 | | { |
| | 213 | | dbContext.Devices.Update(device); |
| | 214 | | await dbContext.SaveChangesAsync().ConfigureAwait(false); |
| | 215 | | } |
| | 216 | |
|
| | 217 | | _devices[device.Id] = device; |
| | 218 | | } |
| | 219 | |
|
| | 220 | | /// <inheritdoc /> |
| | 221 | | public bool CanAccessDevice(User user, string deviceId) |
| | 222 | | { |
| 16 | 223 | | ArgumentNullException.ThrowIfNull(user); |
| 16 | 224 | | ArgumentException.ThrowIfNullOrEmpty(deviceId); |
| | 225 | |
|
| 16 | 226 | | if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator |
| | 227 | | { |
| 16 | 228 | | return true; |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparison.OrdinalIgnoreCa |
| 0 | 232 | | || !GetCapabilities(deviceId).SupportsPersistentIdentifier; |
| | 233 | | } |
| | 234 | |
|
| | 235 | | private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options = null) |
| | 236 | | { |
| 0 | 237 | | var caps = GetCapabilities(authInfo.DeviceId); |
| 0 | 238 | | var user = _userManager.GetUserById(authInfo.UserId); |
| 0 | 239 | | if (user is null) |
| | 240 | | { |
| 0 | 241 | | throw new ResourceNotFoundException("User with UserId " + authInfo.UserId + " not found"); |
| | 242 | | } |
| | 243 | |
|
| 0 | 244 | | return new DeviceInfo |
| 0 | 245 | | { |
| 0 | 246 | | AppName = authInfo.AppName, |
| 0 | 247 | | AppVersion = authInfo.AppVersion, |
| 0 | 248 | | Id = authInfo.DeviceId, |
| 0 | 249 | | LastUserId = authInfo.UserId, |
| 0 | 250 | | LastUserName = user.Username, |
| 0 | 251 | | Name = authInfo.DeviceName, |
| 0 | 252 | | DateLastActivity = authInfo.DateLastActivity, |
| 0 | 253 | | IconUrl = caps.IconUrl, |
| 0 | 254 | | CustomName = options?.CustomName, |
| 0 | 255 | | }; |
| | 256 | | } |
| | 257 | | } |
| | 258 | | } |