Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Jellyfin.Plugin.Webhook/Helpers/HandlebarsFunctionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,71 @@ namespace Jellyfin.Plugin.Webhook.Helpers;
/// </summary>
public static class HandlebarsFunctionHelpers
{
private static readonly HandlebarsBlockHelper StringEqualityAllHelper = (output, options, context, arguments) =>
{
if (arguments.Length < 2)
{
throw new HandlebarsException("{{if_all}} helper requires at least two arguments");
}

if (arguments.Length % 2 != 0)
{
throw new HandlebarsException("{{if_all}} helper must have an even number of arguments");
}

bool isMatch = true;
for (int i = 0; i < arguments.Length; i += 2)
{
if (!string.Equals(GetStringValue(arguments[i]), GetStringValue(arguments[i + 1]), StringComparison.OrdinalIgnoreCase))
{
isMatch = false;
break;
}
}

if (isMatch)
{
options.Template(output, context);
}
else
{
options.Inverse(output, context);
}
};

private static readonly HandlebarsBlockHelper StringEqualityAnyHelper = (output, options, context, arguments) =>
{
if (arguments.Length < 2)
{
throw new HandlebarsException("{{if_any}} helper requires at least two arguments");
}

if (arguments.Length % 2 != 0)
{
throw new HandlebarsException("{{if_any}} helper must have an even number of arguments");
}

var argument = GetStringValue(arguments[0]);
bool isMatch = false;
for (int i = 0; i < arguments.Length; i += 2)
{
if (string.Equals(GetStringValue(arguments[i]), GetStringValue(arguments[i + 1]), StringComparison.OrdinalIgnoreCase))
{
isMatch = true;
break;
}
}

if (isMatch)
{
options.Template(output, context);
}
else
{
options.Inverse(output, context);
}
};

private static readonly HandlebarsBlockHelper StringEqualityHelper = (output, options, context, arguments) =>
{
if (arguments.Length != 2)
Expand Down Expand Up @@ -64,6 +129,8 @@ public static class HandlebarsFunctionHelpers
/// </summary>
public static void RegisterHelpers()
{
Handlebars.RegisterHelper("if_all", StringEqualityAllHelper);
Handlebars.RegisterHelper("if_any", StringEqualityAnyHelper);
Handlebars.RegisterHelper("if_equals", StringEqualityHelper);
Handlebars.RegisterHelper("if_exist", StringExistHelper);
Handlebars.RegisterHelper("link_to", (writer, context, parameters) =>
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ See [Templates](Jellyfin.Plugin.Webhook/Templates) for sample templates.

#### Helpers:

- if_all
- if _all_ pairs of the following parameters equals each other case insensitive (logical AND)
- if_any
- if _any_ pair of the following parameters equals each other case insensitive (logical OR)
- if_equals
- if first parameter equals second parameter case insensitive
- if_exist
Expand Down