Implement option to trim leading/trailing whitespace from message body (#220)

This commit is contained in:
Daniel Tolnai 2024-03-24 17:19:29 +01:00 committed by GitHub
parent 01ce5b2041
commit a9171e837e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 1 deletions

View File

@ -116,6 +116,10 @@
<input is="emby-checkbox" type="checkbox" data-name="chkSendAllProperties"/>
<span>Send All Properties (ignores template)</span>
</label>
<label class="checkboxContainer">
<input is="emby-checkbox" type="checkbox" data-name="chkTrimWhitespace"/>
<span>Trim leading and trailing whitespace from message body before sending</span>
</label>
<label class="checkboxContainer">
<input is="emby-checkbox" type="checkbox" data-name="chkSkipEmptyMessageBody"/>
<span>Do not send when message body is empty</span>

View File

@ -162,6 +162,7 @@
element.querySelector("[data-name=txtWebhookName]").value = config.WebhookName || "";
element.querySelector("[data-name=txtWebhookUri]").value = config.WebhookUri || "";
element.querySelector("[data-name=chkSendAllProperties]").checked = config.SendAllProperties || false;
element.querySelector("[data-name=chkTrimWhitespace]").checked = config.TrimWhitespace || false;
element.querySelector("[data-name=chkSkipEmptyMessageBody]").checked = config.SkipEmptyMessageBody || false;
element.querySelector("[data-name=txtTemplate]").value = Webhook.atou(config.Template || "");
@ -183,6 +184,7 @@
config.WebhookName = element.querySelector("[data-name=txtWebhookName]").value || "";
config.WebhookUri = element.querySelector("[data-name=txtWebhookUri]").value || "";
config.SendAllProperties = element.querySelector("[data-name=chkSendAllProperties]").checked || false;
config.TrimWhitespace = element.querySelector("[data-name=chkTrimWhitespace]").checked || false;
config.SkipEmptyMessageBody = element.querySelector("[data-name=chkSkipEmptyMessageBody]").checked || false;
config.Template = Webhook.utoa(element.querySelector("[data-name=txtTemplate]").value || "");

View File

@ -67,6 +67,11 @@ public abstract class BaseOption
/// </summary>
public bool SendAllProperties { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to trim the message body before sending.
/// </summary>
public bool TrimWhitespace { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to skip sending an empty message body.
/// </summary>
@ -98,8 +103,10 @@ public abstract class BaseOption
/// <returns>The string message body.</returns>
public string GetMessageBody(Dictionary<string, object> data)
{
return SendAllProperties
var body = SendAllProperties
? JsonSerializer.Serialize(data, JsonDefaults.Options)
: GetCompiledTemplate()(data);
return TrimWhitespace ? body.Trim() : body;
}
}