DeviceManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. await dbContext.Database
  48. .ExecuteSqlRawAsync($"UPDATE [DeviceOptions] SET [CustomName] = ${options.CustomName}")
  49. .ConfigureAwait(false);
  50. DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, options)));
  51. }
  52. /// <inheritdoc />
  53. public async Task<Device> CreateDevice(Device device)
  54. {
  55. await using var dbContext = _dbProvider.CreateContext();
  56. dbContext.Devices.Add(device);
  57. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  58. return device;
  59. }
  60. /// <inheritdoc />
  61. public async Task<DeviceOptions?> GetDeviceOptions(string deviceId)
  62. {
  63. await using var dbContext = _dbProvider.CreateContext();
  64. return await dbContext.DeviceOptions
  65. .AsQueryable()
  66. .FirstOrDefaultAsync(d => d.DeviceId == deviceId)
  67. .ConfigureAwait(false);
  68. }
  69. /// <inheritdoc />
  70. public ClientCapabilities GetCapabilities(string deviceId)
  71. {
  72. return _capabilitiesMap.TryGetValue(deviceId, out ClientCapabilities? result)
  73. ? result
  74. : new ClientCapabilities();
  75. }
  76. /// <inheritdoc />
  77. public async Task<DeviceInfo?> GetDevice(string id)
  78. {
  79. await using var dbContext = _dbProvider.CreateContext();
  80. var device = await dbContext.Devices
  81. .AsQueryable()
  82. .Where(d => d.DeviceId == id)
  83. .OrderByDescending(d => d.DateLastActivity)
  84. .Include(d => d.User)
  85. .FirstOrDefaultAsync()
  86. .ConfigureAwait(false);
  87. var deviceInfo = device == null ? null : ToDeviceInfo(device);
  88. return deviceInfo;
  89. }
  90. /// <inheritdoc />
  91. public async Task<QueryResult<Device>> GetDevices(DeviceQuery query)
  92. {
  93. await using var dbContext = _dbProvider.CreateContext();
  94. var devices = dbContext.Devices.AsQueryable();
  95. if (query.UserId.HasValue)
  96. {
  97. devices = devices.Where(device => device.UserId == query.UserId.Value);
  98. }
  99. if (query.DeviceId != null)
  100. {
  101. devices = devices.Where(device => device.DeviceId == query.DeviceId);
  102. }
  103. if (query.AccessToken != null)
  104. {
  105. devices = devices.Where(device => device.AccessToken == query.AccessToken);
  106. }
  107. if (query.Skip.HasValue)
  108. {
  109. devices = devices.Skip(query.Skip.Value);
  110. }
  111. var count = await devices.CountAsync().ConfigureAwait(false);
  112. if (query.Limit.HasValue)
  113. {
  114. devices = devices.Take(query.Limit.Value);
  115. }
  116. return new QueryResult<Device>
  117. {
  118. Items = await devices.ToListAsync().ConfigureAwait(false),
  119. StartIndex = query.Skip ?? 0,
  120. TotalRecordCount = count
  121. };
  122. }
  123. /// <inheritdoc />
  124. public async Task<QueryResult<DeviceInfo>> GetDeviceInfos(DeviceQuery query)
  125. {
  126. var devices = await GetDevices(query).ConfigureAwait(false);
  127. return new QueryResult<DeviceInfo>
  128. {
  129. Items = devices.Items.Select(ToDeviceInfo).ToList(),
  130. StartIndex = devices.StartIndex,
  131. TotalRecordCount = devices.TotalRecordCount
  132. };
  133. }
  134. /// <inheritdoc />
  135. public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId, bool? supportsSync)
  136. {
  137. await using var dbContext = _dbProvider.CreateContext();
  138. var sessions = dbContext.Devices
  139. .AsQueryable()
  140. .OrderBy(d => d.DeviceId)
  141. .ThenByDescending(d => d.DateLastActivity)
  142. .AsAsyncEnumerable();
  143. if (supportsSync.HasValue)
  144. {
  145. sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == supportsSync.Value);
  146. }
  147. if (userId.HasValue)
  148. {
  149. var user = _userManager.GetUserById(userId.Value);
  150. sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId));
  151. }
  152. var array = await sessions.Select(ToDeviceInfo).ToArrayAsync().ConfigureAwait(false);
  153. return new QueryResult<DeviceInfo>(array);
  154. }
  155. /// <inheritdoc />
  156. public async Task DeleteDevice(Device device)
  157. {
  158. await using var dbContext = _dbProvider.CreateContext();
  159. }
  160. /// <inheritdoc />
  161. public bool CanAccessDevice(User user, string deviceId)
  162. {
  163. if (user == null)
  164. {
  165. throw new ArgumentException("user not found");
  166. }
  167. if (string.IsNullOrEmpty(deviceId))
  168. {
  169. throw new ArgumentNullException(nameof(deviceId));
  170. }
  171. if (user.HasPermission(PermissionKind.EnableAllDevices) || user.HasPermission(PermissionKind.IsAdministrator))
  172. {
  173. return true;
  174. }
  175. return user.GetPreference(PreferenceKind.EnabledDevices).Contains(deviceId, StringComparer.OrdinalIgnoreCase)
  176. || !GetCapabilities(deviceId).SupportsPersistentIdentifier;
  177. }
  178. private DeviceInfo ToDeviceInfo(Device authInfo)
  179. {
  180. var caps = GetCapabilities(authInfo.DeviceId);
  181. return new DeviceInfo
  182. {
  183. AppName = authInfo.AppName,
  184. AppVersion = authInfo.AppVersion,
  185. Id = authInfo.DeviceId,
  186. LastUserId = authInfo.UserId,
  187. LastUserName = authInfo.User.Username,
  188. Name = authInfo.DeviceName,
  189. DateLastActivity = authInfo.DateLastActivity,
  190. IconUrl = caps.IconUrl
  191. };
  192. }
  193. }
  194. }