From 259fe4522c4fdcd7467500780fc70ccd75108bf2 Mon Sep 17 00:00:00 2001 From: LJQ Date: Tue, 17 Oct 2023 17:25:41 +0800 Subject: [PATCH 1/3] Update /Device endpoint to return CustomName --- .../Devices/DeviceManager.cs | 10 +++++++++- MediaBrowser.Model/Devices/DeviceInfo.cs | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs index a4b4c19599..80abe01abc 100644 --- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs +++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs @@ -177,7 +177,7 @@ namespace Jellyfin.Server.Implementations.Devices .OrderByDescending(d => d.DateLastActivity) .ThenBy(d => d.DeviceId) .AsAsyncEnumerable(); - + IAsyncEnumerable deviceOptions = dbContext.DeviceOptions.AsAsyncEnumerable(); if (supportsSync.HasValue) { sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == supportsSync.Value); @@ -195,6 +195,14 @@ namespace Jellyfin.Server.Implementations.Devices } var array = await sessions.Select(device => ToDeviceInfo(device)).ToArrayAsync().ConfigureAwait(false); + await foreach (var deviceOption in deviceOptions) + { + var deviceInfo = array.FirstOrDefault(d => d.Id.Equals(deviceOption.DeviceId, StringComparison.OrdinalIgnoreCase)); + if (deviceInfo != null) + { + deviceInfo.CustomName = deviceOption.CustomName; + } + } return new QueryResult(array); } diff --git a/MediaBrowser.Model/Devices/DeviceInfo.cs b/MediaBrowser.Model/Devices/DeviceInfo.cs index 7a1c7a7382..4962992a0a 100644 --- a/MediaBrowser.Model/Devices/DeviceInfo.cs +++ b/MediaBrowser.Model/Devices/DeviceInfo.cs @@ -15,6 +15,8 @@ namespace MediaBrowser.Model.Devices public string Name { get; set; } + public string CustomName { get; set; } + /// /// Gets or sets the access token. /// From 2f9b44fcd04bf7974d42b6db3f9e7e500ae8093c Mon Sep 17 00:00:00 2001 From: LJQ Date: Wed, 18 Oct 2023 00:41:33 +0800 Subject: [PATCH 2/3] Switch to LINQ and updated /Device/Info endpoint to return in accordance to the updated API doc --- .../Devices/DeviceManager.cs | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs index 80abe01abc..807d703eb7 100644 --- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs +++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs @@ -110,21 +110,21 @@ namespace Jellyfin.Server.Implementations.Devices /// public async Task GetDevice(string id) { - Device? device; var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { - device = await dbContext.Devices + var device = await dbContext.Devices .Where(d => d.DeviceId == id) .OrderByDescending(d => d.DateLastActivity) .Include(d => d.User) + .SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o }) .FirstOrDefaultAsync() .ConfigureAwait(false); + + var deviceInfo = device is null ? null : ToDeviceInfo(device.Device, device.Options); + + return deviceInfo; } - - var deviceInfo = device is null ? null : ToDeviceInfo(device); - - return deviceInfo; } /// @@ -172,15 +172,15 @@ namespace Jellyfin.Server.Implementations.Devices var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { - IAsyncEnumerable sessions = dbContext.Devices + var sessions = dbContext.Devices .Include(d => d.User) .OrderByDescending(d => d.DateLastActivity) .ThenBy(d => d.DeviceId) + .SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o }) .AsAsyncEnumerable(); - IAsyncEnumerable deviceOptions = dbContext.DeviceOptions.AsAsyncEnumerable(); if (supportsSync.HasValue) { - sessions = sessions.Where(i => GetCapabilities(i.DeviceId).SupportsSync == supportsSync.Value); + sessions = sessions.Where(i => GetCapabilities(i.Device.DeviceId).SupportsSync == supportsSync.Value); } if (userId.HasValue) @@ -191,18 +191,10 @@ namespace Jellyfin.Server.Implementations.Devices throw new ResourceNotFoundException(); } - sessions = sessions.Where(i => CanAccessDevice(user, i.DeviceId)); + sessions = sessions.Where(i => CanAccessDevice(user, i.Device.DeviceId)); } - var array = await sessions.Select(device => ToDeviceInfo(device)).ToArrayAsync().ConfigureAwait(false); - await foreach (var deviceOption in deviceOptions) - { - var deviceInfo = array.FirstOrDefault(d => d.Id.Equals(deviceOption.DeviceId, StringComparison.OrdinalIgnoreCase)); - if (deviceInfo != null) - { - deviceInfo.CustomName = deviceOption.CustomName; - } - } + var array = await sessions.Select(device => ToDeviceInfo(device.Device, device.Options)).ToArrayAsync().ConfigureAwait(false); return new QueryResult(array); } @@ -250,5 +242,23 @@ namespace Jellyfin.Server.Implementations.Devices IconUrl = caps.IconUrl }; } + + private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options) + { + var caps = GetCapabilities(authInfo.DeviceId); + + return new DeviceInfo + { + AppName = authInfo.AppName, + AppVersion = authInfo.AppVersion, + Id = authInfo.DeviceId, + LastUserId = authInfo.UserId, + LastUserName = authInfo.User.Username, + Name = authInfo.DeviceName, + DateLastActivity = authInfo.DateLastActivity, + IconUrl = caps.IconUrl, + CustomName = options?.CustomName, + }; + } } } From 98bc2fea8b20a52edfd142fbbf2ffbf79b0608b0 Mon Sep 17 00:00:00 2001 From: LJQ Date: Wed, 18 Oct 2023 01:40:36 +0800 Subject: [PATCH 3/3] Removed dupe ToDeviceInfo --- .../Devices/DeviceManager.cs | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs index 807d703eb7..d8eee12467 100644 --- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs +++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs @@ -226,24 +226,7 @@ namespace Jellyfin.Server.Implementations.Devices || !GetCapabilities(deviceId).SupportsPersistentIdentifier; } - private DeviceInfo ToDeviceInfo(Device authInfo) - { - var caps = GetCapabilities(authInfo.DeviceId); - - return new DeviceInfo - { - AppName = authInfo.AppName, - AppVersion = authInfo.AppVersion, - Id = authInfo.DeviceId, - LastUserId = authInfo.UserId, - LastUserName = authInfo.User.Username, - Name = authInfo.DeviceName, - DateLastActivity = authInfo.DateLastActivity, - IconUrl = caps.IconUrl - }; - } - - private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options) + private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options = null) { var caps = GetCapabilities(authInfo.DeviceId);