DeviceManager.cs 9.4 KB

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