DeviceManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Data.Entities;
  6. using Jellyfin.Data.Entities.Security;
  7. using Jellyfin.Data.Enums;
  8. using Jellyfin.Data.Events;
  9. using Jellyfin.Data.Queries;
  10. using MediaBrowser.Controller.Devices;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Model.Devices;
  13. using MediaBrowser.Model.Querying;
  14. using MediaBrowser.Model.Session;
  15. using Microsoft.EntityFrameworkCore;
  16. namespace Jellyfin.Server.Implementations.Devices
  17. {
  18. /// <summary>
  19. /// Manages the creation, updating, and retrieval of devices.
  20. /// </summary>
  21. public class DeviceManager : IDeviceManager
  22. {
  23. private readonly JellyfinDbProvider _dbProvider;
  24. private readonly IUserManager _userManager;
  25. private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new ();
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="DeviceManager"/> class.
  28. /// </summary>
  29. /// <param name="dbProvider">The database provider.</param>
  30. /// <param name="userManager">The user manager.</param>
  31. public DeviceManager(JellyfinDbProvider dbProvider, IUserManager userManager)
  32. {
  33. _dbProvider = dbProvider;
  34. _userManager = userManager;
  35. }
  36. /// <inheritdoc />
  37. public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>>? DeviceOptionsUpdated;
  38. /// <inheritdoc />
  39. public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
  40. {
  41. _capabilitiesMap[deviceId] = capabilities;
  42. }
  43. /// <inheritdoc />
  44. public async Task UpdateDeviceOptions(string deviceId, DeviceOptions options)
  45. {
  46. await using var dbContext = _dbProvider.CreateContext();
  47. var deviceOptions = await dbContext.DeviceOptions.AsQueryable().FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false);
  48. if (deviceOptions == null)
  49. {
  50. deviceOptions = new DeviceOptions(deviceId);
  51. dbContext.DeviceOptions.Add(deviceOptions);
  52. }
  53. deviceOptions.CustomName = options.CustomName;
  54. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  55. DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, options)));
  56. }
  57. /// <inheritdoc />
  58. public async Task<Device> CreateDevice(Device device)
  59. {
  60. await using var dbContext = _dbProvider.CreateContext();
  61. dbContext.Devices.Add(device);
  62. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  63. return device;
  64. }
  65. /// <inheritdoc />
  66. public async Task<DeviceOptions?> GetDeviceOptions(string deviceId)
  67. {
  68. await using var dbContext = _dbProvider.CreateContext();
  69. return await dbContext.DeviceOptions
  70. .AsQueryable()
  71. .FirstOrDefaultAsync(d => d.DeviceId == deviceId)
  72. .ConfigureAwait(false);
  73. }
  74. /// <inheritdoc />
  75. public ClientCapabilities GetCapabilities(string deviceId)
  76. {
  77. return _capabilitiesMap.TryGetValue(deviceId, out ClientCapabilities? result)
  78. ? result
  79. : new ClientCapabilities();
  80. }
  81. /// <inheritdoc />
  82. public async Task<DeviceInfo?> GetDevice(string id)
  83. {
  84. await using var dbContext = _dbProvider.CreateContext();
  85. var device = await dbContext.Devices
  86. .AsQueryable()
  87. .Where(d => d.DeviceId == id)
  88. .OrderByDescending(d => d.DateLastActivity)
  89. .Include(d => d.User)
  90. .FirstOrDefaultAsync()
  91. .ConfigureAwait(false);
  92. var deviceInfo = device == null ? null : ToDeviceInfo(device);
  93. return deviceInfo;
  94. }
  95. /// <inheritdoc />
  96. public async Task<QueryResult<Device>> GetDevices(DeviceQuery query)
  97. {
  98. await using var dbContext = _dbProvider.CreateContext();
  99. var devices = dbContext.Devices.AsQueryable();
  100. if (query.UserId.HasValue)
  101. {
  102. devices = devices.Where(device => device.UserId == query.UserId.Value);
  103. }
  104. if (query.DeviceId != null)
  105. {
  106. devices = devices.Where(device => device.DeviceId == query.DeviceId);
  107. }
  108. if (query.AccessToken != null)
  109. {
  110. devices = devices.Where(device => device.AccessToken == query.AccessToken);
  111. }
  112. var count = await devices.CountAsync().ConfigureAwait(false);
  113. if (query.Skip.HasValue)
  114. {
  115. devices = devices.Skip(query.Skip.Value);
  116. }
  117. if (query.Limit.HasValue)
  118. {
  119. devices = devices.Take(query.Limit.Value);
  120. }
  121. return new QueryResult<Device>
  122. {
  123. Items = await devices.ToListAsync().ConfigureAwait(false),
  124. StartIndex = query.Skip ?? 0,
  125. TotalRecordCount = count
  126. };
  127. }
  128. /// <inheritdoc />
  129. public async Task<QueryResult<DeviceInfo>> GetDeviceInfos(DeviceQuery query)
  130. {
  131. var devices = await GetDevices(query).ConfigureAwait(false);
  132. return new QueryResult<DeviceInfo>
  133. {
  134. Items = devices.Items.Select(ToDeviceInfo).ToList(),
  135. StartIndex = devices.StartIndex,
  136. TotalRecordCount = devices.TotalRecordCount
  137. };
  138. }
  139. /// <inheritdoc />
  140. public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId, bool? supportsSync)
  141. {
  142. await using var dbContext = _dbProvider.CreateContext();
  143. var sessions = dbContext.Devices
  144. .AsQueryable()
  145. .OrderBy(d => d.DeviceId)
  146. .ThenByDescending(d => d.DateLastActivity)
  147. .AsAsyncEnumerable();
  148. if (supportsSync.HasValue)
  149. {
  150. sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == supportsSync.Value);
  151. }
  152. if (userId.HasValue)
  153. {
  154. var user = _userManager.GetUserById(userId.Value);
  155. sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
  156. }
  157. var array = await sessions.Select(ToDeviceInfo).ToArrayAsync().ConfigureAwait(false);
  158. return new QueryResult<DeviceInfo>(array);
  159. }
  160. /// <inheritdoc />
  161. public async Task DeleteDevice(Device device)
  162. {
  163. await using var dbContext = _dbProvider.CreateContext();
  164. dbContext.Devices.Remove(device);
  165. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  166. }
  167. /// <inheritdoc />
  168. public bool CanAccessDevice(User user, string deviceId)
  169. {
  170. if (user == null)
  171. {
  172. throw new ArgumentException("user not found");
  173. }
  174. if (string.IsNullOrEmpty(deviceId))
  175. {
  176. throw new ArgumentNullException(nameof(deviceId));
  177. }
  178. if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
  179. {
  180. return true;
  181. }
  182. return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparer.OrdinalIgnoreCase)
  183. || !GetCapabilities(deviceId).SupportsPersistentIdentifier;
  184. }
  185. private DeviceInfo ToDeviceInfo(Device authInfo)
  186. {
  187. var caps = GetCapabilities(authInfo.DeviceId);
  188. return new DeviceInfo
  189. {
  190. AppName = authInfo.AppName,
  191. AppVersion = authInfo.AppVersion,
  192. Id = authInfo.DeviceId,
  193. LastUserId = authInfo.UserId,
  194. LastUserName = authInfo.User.Username,
  195. Name = authInfo.DeviceName,
  196. DateLastActivity = authInfo.DateLastActivity,
  197. IconUrl = caps.IconUrl
  198. };
  199. }
  200. }
  201. }