From 3459655bb401595f62c21513964876b2f4549fed Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Mon, 21 Sep 2020 16:53:00 +0200 Subject: [PATCH 1/2] Use GeneralCommandType enum in GeneralCommand name --- CONTRIBUTORS.md | 1 + Emby.Dlna/PlayTo/PlayToController.cs | 87 +++++++++---------- .../Session/SessionManager.cs | 4 +- Jellyfin.Api/Controllers/SessionController.cs | 14 +-- MediaBrowser.Model/Session/GeneralCommand.cs | 2 +- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index f1fe65064b..efd83012e9 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -135,6 +135,7 @@ - [YouKnowBlom](https://github.com/YouKnowBlom) - [KristupasSavickas](https://github.com/KristupasSavickas) - [Pusta](https://github.com/pusta) + - [nielsvanvelzen](https://github.com/nielsvanvelzen) # Emby Contributors diff --git a/Emby.Dlna/PlayTo/PlayToController.cs b/Emby.Dlna/PlayTo/PlayToController.cs index 328759c5bc..460ac2d8d1 100644 --- a/Emby.Dlna/PlayTo/PlayToController.cs +++ b/Emby.Dlna/PlayTo/PlayToController.cs @@ -669,62 +669,57 @@ namespace Emby.Dlna.PlayTo private Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken) { - if (Enum.TryParse(command.Name, true, out GeneralCommandType commandType)) - { - switch (commandType) - { - case GeneralCommandType.VolumeDown: - return _device.VolumeDown(cancellationToken); - case GeneralCommandType.VolumeUp: - return _device.VolumeUp(cancellationToken); - case GeneralCommandType.Mute: - return _device.Mute(cancellationToken); - case GeneralCommandType.Unmute: - return _device.Unmute(cancellationToken); - case GeneralCommandType.ToggleMute: - return _device.ToggleMute(cancellationToken); - case GeneralCommandType.SetAudioStreamIndex: - if (command.Arguments.TryGetValue("Index", out string index)) + switch (command.Name) + { + case GeneralCommandType.VolumeDown: + return _device.VolumeDown(cancellationToken); + case GeneralCommandType.VolumeUp: + return _device.VolumeUp(cancellationToken); + case GeneralCommandType.Mute: + return _device.Mute(cancellationToken); + case GeneralCommandType.Unmute: + return _device.Unmute(cancellationToken); + case GeneralCommandType.ToggleMute: + return _device.ToggleMute(cancellationToken); + case GeneralCommandType.SetAudioStreamIndex: + if (command.Arguments.TryGetValue("Index", out string index)) + { + if (int.TryParse(index, NumberStyles.Integer, _usCulture, out var val)) { - if (int.TryParse(index, NumberStyles.Integer, _usCulture, out var val)) - { - return SetAudioStreamIndex(val); - } - - throw new ArgumentException("Unsupported SetAudioStreamIndex value supplied."); + return SetAudioStreamIndex(val); } - throw new ArgumentException("SetAudioStreamIndex argument cannot be null"); - case GeneralCommandType.SetSubtitleStreamIndex: - if (command.Arguments.TryGetValue("Index", out index)) - { - if (int.TryParse(index, NumberStyles.Integer, _usCulture, out var val)) - { - return SetSubtitleStreamIndex(val); - } + throw new ArgumentException("Unsupported SetAudioStreamIndex value supplied."); + } - throw new ArgumentException("Unsupported SetSubtitleStreamIndex value supplied."); + throw new ArgumentException("SetAudioStreamIndex argument cannot be null"); + case GeneralCommandType.SetSubtitleStreamIndex: + if (command.Arguments.TryGetValue("Index", out index)) + { + if (int.TryParse(index, NumberStyles.Integer, _usCulture, out var val)) + { + return SetSubtitleStreamIndex(val); } - throw new ArgumentException("SetSubtitleStreamIndex argument cannot be null"); - case GeneralCommandType.SetVolume: - if (command.Arguments.TryGetValue("Volume", out string vol)) - { - if (int.TryParse(vol, NumberStyles.Integer, _usCulture, out var volume)) - { - return _device.SetVolume(volume, cancellationToken); - } + throw new ArgumentException("Unsupported SetSubtitleStreamIndex value supplied."); + } - throw new ArgumentException("Unsupported volume value supplied."); + throw new ArgumentException("SetSubtitleStreamIndex argument cannot be null"); + case GeneralCommandType.SetVolume: + if (command.Arguments.TryGetValue("Volume", out string vol)) + { + if (int.TryParse(vol, NumberStyles.Integer, _usCulture, out var volume)) + { + return _device.SetVolume(volume, cancellationToken); } - throw new ArgumentException("Volume argument cannot be null"); - default: - return Task.CompletedTask; - } - } + throw new ArgumentException("Unsupported volume value supplied."); + } - return Task.CompletedTask; + throw new ArgumentException("Volume argument cannot be null"); + default: + return Task.CompletedTask; + } } private async Task SetAudioStreamIndex(int? newIndex) diff --git a/Emby.Server.Implementations/Session/SessionManager.cs b/Emby.Server.Implementations/Session/SessionManager.cs index ca8e0e29bb..e42d478533 100644 --- a/Emby.Server.Implementations/Session/SessionManager.cs +++ b/Emby.Server.Implementations/Session/SessionManager.cs @@ -1037,7 +1037,7 @@ namespace Emby.Server.Implementations.Session var generalCommand = new GeneralCommand { - Name = GeneralCommandType.DisplayMessage.ToString() + Name = GeneralCommandType.DisplayMessage }; generalCommand.Arguments["Header"] = command.Header; @@ -1268,7 +1268,7 @@ namespace Emby.Server.Implementations.Session { var generalCommand = new GeneralCommand { - Name = GeneralCommandType.DisplayContent.ToString(), + Name = GeneralCommandType.DisplayContent, Arguments = { ["ItemId"] = command.ItemId, diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index b00675d679..5a2a3cdc09 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -217,16 +217,15 @@ namespace Jellyfin.Api.Controllers [FromRoute, Required] string sessionId, [FromRoute, Required] string command) { - var name = command; - if (Enum.TryParse(name, true, out GeneralCommandType commandType)) + if (!Enum.TryParse(command, true, out GeneralCommandType commandType)) { - name = commandType.ToString(); + return BadRequest(); } var currentSession = RequestHelpers.GetSession(_sessionManager, _authContext, Request); var generalCommand = new GeneralCommand { - Name = name, + Name = commandType, ControllingUserId = currentSession.UserId }; @@ -249,11 +248,16 @@ namespace Jellyfin.Api.Controllers [FromRoute, Required] string sessionId, [FromRoute, Required] string command) { + if (!Enum.TryParse(command, true, out GeneralCommandType commandType)) + { + return BadRequest(); + } + var currentSession = RequestHelpers.GetSession(_sessionManager, _authContext, Request); var generalCommand = new GeneralCommand { - Name = command, + Name = commandType, ControllingUserId = currentSession.UserId }; diff --git a/MediaBrowser.Model/Session/GeneralCommand.cs b/MediaBrowser.Model/Session/GeneralCommand.cs index 9794bd2929..77bb6bcf77 100644 --- a/MediaBrowser.Model/Session/GeneralCommand.cs +++ b/MediaBrowser.Model/Session/GeneralCommand.cs @@ -8,7 +8,7 @@ namespace MediaBrowser.Model.Session { public class GeneralCommand { - public string Name { get; set; } + public GeneralCommandType Name { get; set; } public Guid ControllingUserId { get; set; } From 891c538f818be7a339fcdfacaf34adb4a329d514 Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Mon, 21 Sep 2020 17:49:45 +0200 Subject: [PATCH 2/2] Use GeneralCommandType in SessionController parameters --- Jellyfin.Api/Controllers/SessionController.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index 5a2a3cdc09..c257a46dcf 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -215,17 +215,12 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status204NoContent)] public ActionResult SendSystemCommand( [FromRoute, Required] string sessionId, - [FromRoute, Required] string command) + [FromRoute, Required] GeneralCommandType command) { - if (!Enum.TryParse(command, true, out GeneralCommandType commandType)) - { - return BadRequest(); - } - var currentSession = RequestHelpers.GetSession(_sessionManager, _authContext, Request); var generalCommand = new GeneralCommand { - Name = commandType, + Name = command, ControllingUserId = currentSession.UserId }; @@ -246,18 +241,13 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status204NoContent)] public ActionResult SendGeneralCommand( [FromRoute, Required] string sessionId, - [FromRoute, Required] string command) + [FromRoute, Required] GeneralCommandType command) { - if (!Enum.TryParse(command, true, out GeneralCommandType commandType)) - { - return BadRequest(); - } - var currentSession = RequestHelpers.GetSession(_sessionManager, _authContext, Request); var generalCommand = new GeneralCommand { - Name = commandType, + Name = command, ControllingUserId = currentSession.UserId };