DeviceManager.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. namespace Jellyfin.Server.Implementations.Devices
  20. {
  21. /// <summary>
  22. /// Manages the creation, updating, and retrieval of devices.
  23. /// </summary>
  24. public class DeviceManager : IDeviceManager
  25. {
  26. private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
  27. private readonly IUserManager _userManager;
  28. private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new();
  29. private readonly IDictionary<int, Device> _devices;
  30. private readonly IDictionary<string, DeviceOptions> _deviceOptions;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="DeviceManager"/> class.
  33. /// </summary>
  34. /// <param name="dbProvider">The database provider.</param>
  35. /// <param name="userManager">The user manager.</param>
  36. public DeviceManager(IDbContextFactory<JellyfinDbContext> dbProvider, IUserManager userManager)
  37. {
  38. _dbProvider = dbProvider;
  39. _userManager = userManager;
  40. _devices = new ConcurrentDictionary<int, Device>();
  41. _deviceOptions = new ConcurrentDictionary<string, DeviceOptions>();
  42. using var dbContext = _dbProvider.CreateDbContext();
  43. foreach (var device in dbContext.Devices
  44. .Include(d => d.User)
  45. .OrderBy(d => d.Id)
  46. .AsEnumerable())
  47. {
  48. _devices.Add(device.Id, device);
  49. }
  50. foreach (var deviceOption in dbContext.DeviceOptions
  51. .OrderBy(d => d.Id)
  52. .AsEnumerable())
  53. {
  54. _deviceOptions.Add(deviceOption.DeviceId, deviceOption);
  55. }
  56. }
  57. /// <inheritdoc />
  58. public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated;
  59. /// <inheritdoc />
  60. public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
  61. {
  62. _capabilitiesMap[deviceId] = capabilities;
  63. }
  64. /// <inheritdoc />
  65. public async Task UpdateDeviceOptions(string deviceId, string deviceName)
  66. {
  67. DeviceOptions? deviceOptions;
  68. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  69. await using (dbContext.ConfigureAwait(false))
  70. {
  71. deviceOptions = await dbContext.DeviceOptions.FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false);
  72. if (deviceOptions is null)
  73. {
  74. deviceOptions = new DeviceOptions(deviceId);
  75. dbContext.DeviceOptions.Add(deviceOptions);
  76. }
  77. deviceOptions.CustomName = deviceName;
  78. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  79. }
  80. _deviceOptions[deviceId] = deviceOptions;
  81. DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, deviceOptions)));
  82. }
  83. /// <inheritdoc />
  84. public async Task<Device> CreateDevice(Device device)
  85. {
  86. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  87. await using (dbContext.ConfigureAwait(false))
  88. {
  89. dbContext.Devices.Add(device);
  90. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  91. var newDevice = await dbContext.Devices
  92. .Include(d => d.User)
  93. .FirstOrDefaultAsync(d => d.Id == device.Id)
  94. .ConfigureAwait(false);
  95. _devices.Add(device.Id, newDevice!);
  96. }
  97. return device;
  98. }
  99. /// <inheritdoc />
  100. public DeviceOptions GetDeviceOptions(string deviceId)
  101. {
  102. _deviceOptions.TryGetValue(deviceId, out var deviceOptions);
  103. return deviceOptions ?? new DeviceOptions(deviceId);
  104. }
  105. /// <inheritdoc />
  106. public ClientCapabilities GetCapabilities(string deviceId)
  107. {
  108. return _capabilitiesMap.TryGetValue(deviceId, out ClientCapabilities? result)
  109. ? result
  110. : new ClientCapabilities();
  111. }
  112. /// <inheritdoc />
  113. public DeviceInfo? GetDevice(string id)
  114. {
  115. var device = _devices.Values.OrderByDescending(d => d.DateLastActivity).FirstOrDefault(d => d.DeviceId == id);
  116. _deviceOptions.TryGetValue(id, out var deviceOption);
  117. var deviceInfo = device is null ? null : ToDeviceInfo(device, deviceOption);
  118. return deviceInfo;
  119. }
  120. /// <inheritdoc />
  121. public QueryResult<Device> GetDevices(DeviceQuery query)
  122. {
  123. var devices = _devices.Values.OrderBy(d => d.Id)
  124. .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value))
  125. .Where(device => query.DeviceId == null || device.DeviceId == query.DeviceId)
  126. .Where(device => query.AccessToken == null || device.AccessToken == query.AccessToken);
  127. var canGetCountDirectly = devices.TryGetNonEnumeratedCount(out var count);
  128. if (!canGetCountDirectly)
  129. {
  130. count = devices.Count();
  131. }
  132. if (query.Skip.HasValue)
  133. {
  134. devices = devices.Skip(query.Skip.Value);
  135. }
  136. if (query.Limit.HasValue)
  137. {
  138. devices = devices.Take(query.Limit.Value);
  139. }
  140. return new QueryResult<Device>(query.Skip, count, devices.ToList());
  141. }
  142. /// <inheritdoc />
  143. public QueryResult<DeviceInfo> GetDeviceInfos(DeviceQuery query)
  144. {
  145. var devices = GetDevices(query);
  146. return new QueryResult<DeviceInfo>(
  147. devices.StartIndex,
  148. devices.TotalRecordCount,
  149. devices.Items.Select(device => ToDeviceInfo(device)).ToList());
  150. }
  151. /// <inheritdoc />
  152. public QueryResult<DeviceInfo> GetDevicesForUser(Guid? userId)
  153. {
  154. var devices = _devices.Values
  155. .OrderByDescending(d => d.DateLastActivity)
  156. .ThenBy(d => d.DeviceId)
  157. .AsEnumerable();
  158. if (!userId.IsNullOrEmpty())
  159. {
  160. var user = _userManager.GetUserById(userId.Value);
  161. if (user is null)
  162. {
  163. throw new ResourceNotFoundException();
  164. }
  165. devices = devices.Where(i => CanAccessDevice(user, i.DeviceId));
  166. }
  167. var array = devices.Select(device =>
  168. {
  169. _deviceOptions.TryGetValue(device.DeviceId, out var option);
  170. return ToDeviceInfo(device, option);
  171. }).ToArray();
  172. return new QueryResult<DeviceInfo>(array);
  173. }
  174. /// <inheritdoc />
  175. public async Task DeleteDevice(Device device)
  176. {
  177. var id = _devices.FirstOrDefault(x => x.Value.Equals(device)).Key;
  178. _devices.Remove(id);
  179. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  180. await using (dbContext.ConfigureAwait(false))
  181. {
  182. dbContext.Devices.Remove(device);
  183. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  184. }
  185. }
  186. /// <inheritdoc />
  187. public async Task UpdateDevice(Device device)
  188. {
  189. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  190. await using (dbContext.ConfigureAwait(false))
  191. {
  192. dbContext.Devices.Update(device);
  193. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  194. }
  195. _devices[device.Id] = device;
  196. }
  197. /// <inheritdoc />
  198. public bool CanAccessDevice(User user, string deviceId)
  199. {
  200. ArgumentNullException.ThrowIfNull(user);
  201. ArgumentException.ThrowIfNullOrEmpty(deviceId);
  202. if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
  203. {
  204. return true;
  205. }
  206. return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparison.OrdinalIgnoreCase)
  207. || !GetCapabilities(deviceId).SupportsPersistentIdentifier;
  208. }
  209. private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options = null)
  210. {
  211. var caps = GetCapabilities(authInfo.DeviceId);
  212. return new DeviceInfo
  213. {
  214. AppName = authInfo.AppName,
  215. AppVersion = authInfo.AppVersion,
  216. Id = authInfo.DeviceId,
  217. LastUserId = authInfo.UserId,
  218. LastUserName = authInfo.User.Username,
  219. Name = authInfo.DeviceName,
  220. DateLastActivity = authInfo.DateLastActivity,
  221. IconUrl = caps.IconUrl,
  222. CustomName = options?.CustomName,
  223. };
  224. }
  225. }
  226. }