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