From 81e3e5ca4883309b38377729e0bbd1b9f163ec7c Mon Sep 17 00:00:00 2001 From: Brian Arnold Date: Fri, 26 Mar 2021 10:02:23 -0400 Subject: [PATCH 1/8] Changed SessionController.SendMessageCommand implementation receive data in the POST body, as that is how the jellyfin-web client currently posts the data to the server. Resolves: #5628 --- Jellyfin.Api/Controllers/SessionController.cs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index 0703d4255b..ba68b4cbf8 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -313,9 +313,7 @@ namespace Jellyfin.Api.Controllers /// Issues a command to a client to display a message to the user. /// /// The session id. - /// The message test. - /// The message header. - /// The message timeout. If omitted the user will have to confirm viewing the message. + /// The object containing Header, Message Text, and TimeoutMs. /// Message sent. /// A . [HttpPost("Sessions/{sessionId}/Message")] @@ -323,18 +321,25 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status204NoContent)] public ActionResult SendMessageCommand( [FromRoute, Required] string sessionId, - [FromQuery, Required] string text, - [FromQuery] string? header, - [FromQuery] long? timeoutMs) + [FromBody] MessageCommand command) { - var command = new MessageCommand + if (command == null) { - Header = string.IsNullOrEmpty(header) ? "Message from Server" : header, - TimeoutMs = timeoutMs, - Text = text + throw new ArgumentException("Request body may not be null"); + } + //Need to check if message.Text is null, since [Required] can't be applied to properties of a deserialized object. + if (string.IsNullOrWhiteSpace(command.Text)) + { + throw new ArgumentNullException("Message Text may not be empty."); + } + var nullCorrectedCommand = new MessageCommand + { + Header = string.IsNullOrWhiteSpace(command.Header) ? "Message from Server" : command.Header, + TimeoutMs = command.TimeoutMs, + Text = command.Text }; - _sessionManager.SendMessageCommand(RequestHelpers.GetSession(_sessionManager, _authContext, Request).Id, sessionId, command, CancellationToken.None); + _sessionManager.SendMessageCommand(RequestHelpers.GetSession(_sessionManager, _authContext, Request).Id, sessionId, nullCorrectedCommand, CancellationToken.None); return NoContent(); } From f114ba57dd24ed38786a478df7a641f81dd9d5c7 Mon Sep 17 00:00:00 2001 From: Brian Arnold Date: Fri, 26 Mar 2021 10:37:59 -0400 Subject: [PATCH 2/8] Fixed comment and code block runes to match coding standards required by Jellyfin team. --- Jellyfin.Api/Controllers/SessionController.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index ba68b4cbf8..82aa32a7ab 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -327,11 +327,13 @@ namespace Jellyfin.Api.Controllers { throw new ArgumentException("Request body may not be null"); } - //Need to check if message.Text is null, since [Required] can't be applied to properties of a deserialized object. + + // Need to check if message.Text is null, since [Required] can't be applied to properties of a deserialized object. if (string.IsNullOrWhiteSpace(command.Text)) { throw new ArgumentNullException("Message Text may not be empty."); } + var nullCorrectedCommand = new MessageCommand { Header = string.IsNullOrWhiteSpace(command.Header) ? "Message from Server" : command.Header, From 998833ea6e4c52f842949029b278008b276e3e6b Mon Sep 17 00:00:00 2001 From: "Brian C. Arnold" Date: Sat, 27 Mar 2021 23:28:08 -0400 Subject: [PATCH 3/8] Removed null check for body object and user [Required] attribute in replacement. --- Jellyfin.Api/Controllers/SessionController.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index 82aa32a7ab..34dfb6ce7f 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -321,12 +321,8 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status204NoContent)] public ActionResult SendMessageCommand( [FromRoute, Required] string sessionId, - [FromBody] MessageCommand command) + [FromBody, Required] MessageCommand command) { - if (command == null) - { - throw new ArgumentException("Request body may not be null"); - } // Need to check if message.Text is null, since [Required] can't be applied to properties of a deserialized object. if (string.IsNullOrWhiteSpace(command.Text)) From ca25301e649342ef5598822fdd9ffc6eb2cd5065 Mon Sep 17 00:00:00 2001 From: Brian Arnold Date: Mon, 29 Mar 2021 10:10:44 -0400 Subject: [PATCH 4/8] Added Required attribute to Text property of MessageCommand. --- Jellyfin.Api/Controllers/SessionController.cs | 7 ------- MediaBrowser.Model/Session/MessageCommand.cs | 4 +++- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index 34dfb6ce7f..14686222be 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -323,13 +323,6 @@ namespace Jellyfin.Api.Controllers [FromRoute, Required] string sessionId, [FromBody, Required] MessageCommand command) { - - // Need to check if message.Text is null, since [Required] can't be applied to properties of a deserialized object. - if (string.IsNullOrWhiteSpace(command.Text)) - { - throw new ArgumentNullException("Message Text may not be empty."); - } - var nullCorrectedCommand = new MessageCommand { Header = string.IsNullOrWhiteSpace(command.Header) ? "Message from Server" : command.Header, diff --git a/MediaBrowser.Model/Session/MessageCommand.cs b/MediaBrowser.Model/Session/MessageCommand.cs index 09abfbb3f2..0ee3c720e5 100644 --- a/MediaBrowser.Model/Session/MessageCommand.cs +++ b/MediaBrowser.Model/Session/MessageCommand.cs @@ -1,12 +1,14 @@ #nullable disable #pragma warning disable CS1591 +using System.ComponentModel.DataAnnotations; + namespace MediaBrowser.Model.Session { public class MessageCommand { public string Header { get; set; } - + [Required(AllowEmptyStrings = false)] public string Text { get; set; } public long? TimeoutMs { get; set; } From 54107ae88262d835cac2d5a6f335b0c10d050b1a Mon Sep 17 00:00:00 2001 From: Brian Arnold Date: Mon, 29 Mar 2021 11:40:07 -0400 Subject: [PATCH 5/8] Fix spacing requirement for MessageCommand. --- MediaBrowser.Model/Session/MessageCommand.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/MediaBrowser.Model/Session/MessageCommand.cs b/MediaBrowser.Model/Session/MessageCommand.cs index 0ee3c720e5..cc9db8e6c5 100644 --- a/MediaBrowser.Model/Session/MessageCommand.cs +++ b/MediaBrowser.Model/Session/MessageCommand.cs @@ -8,6 +8,7 @@ namespace MediaBrowser.Model.Session public class MessageCommand { public string Header { get; set; } + [Required(AllowEmptyStrings = false)] public string Text { get; set; } From 80fe48fda972eff0d6b58ac8a9e2f63d44a1b23e Mon Sep 17 00:00:00 2001 From: BrianCArnold Date: Tue, 30 Mar 2021 07:29:40 -0400 Subject: [PATCH 6/8] Update SessionController.cs Removed unnecessary construction of POCO to represent data from POST body --- Jellyfin.Api/Controllers/SessionController.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index 14686222be..b64cbe30c7 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -323,12 +323,10 @@ namespace Jellyfin.Api.Controllers [FromRoute, Required] string sessionId, [FromBody, Required] MessageCommand command) { - var nullCorrectedCommand = new MessageCommand + if (string.IsNullOrWhiteSpace(command.Header)) { - Header = string.IsNullOrWhiteSpace(command.Header) ? "Message from Server" : command.Header, - TimeoutMs = command.TimeoutMs, - Text = command.Text - }; + command.Header = "Message from Server"; + } _sessionManager.SendMessageCommand(RequestHelpers.GetSession(_sessionManager, _authContext, Request).Id, sessionId, nullCorrectedCommand, CancellationToken.None); From af03b280bc91f65712af2cc854e8bf1ad349748a Mon Sep 17 00:00:00 2001 From: BrianCArnold Date: Wed, 31 Mar 2021 01:23:51 -0400 Subject: [PATCH 7/8] Update SessionController.cs --- Jellyfin.Api/Controllers/SessionController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index b64cbe30c7..1981e213e8 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -323,7 +323,7 @@ namespace Jellyfin.Api.Controllers [FromRoute, Required] string sessionId, [FromBody, Required] MessageCommand command) { - if (string.IsNullOrWhiteSpace(command.Header)) + if (string.IsNullOrWhiteSpace(command.Header)) { command.Header = "Message from Server"; } From 4fa2a32d81e263ef51140e2ae8259a02fdfb0d7d Mon Sep 17 00:00:00 2001 From: BrianCArnold Date: Wed, 31 Mar 2021 01:24:38 -0400 Subject: [PATCH 8/8] Apply suggestions from code review Co-authored-by: Claus Vium --- Jellyfin.Api/Controllers/SessionController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jellyfin.Api/Controllers/SessionController.cs b/Jellyfin.Api/Controllers/SessionController.cs index 1981e213e8..7bd0b6918f 100644 --- a/Jellyfin.Api/Controllers/SessionController.cs +++ b/Jellyfin.Api/Controllers/SessionController.cs @@ -325,10 +325,10 @@ namespace Jellyfin.Api.Controllers { if (string.IsNullOrWhiteSpace(command.Header)) { - command.Header = "Message from Server"; + command.Header = "Message from Server"; } - _sessionManager.SendMessageCommand(RequestHelpers.GetSession(_sessionManager, _authContext, Request).Id, sessionId, nullCorrectedCommand, CancellationToken.None); + _sessionManager.SendMessageCommand(RequestHelpers.GetSession(_sessionManager, _authContext, Request).Id, sessionId, command, CancellationToken.None); return NoContent(); }