mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.7 KiB
53 lines
1.7 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using Squidex.Domain.Apps.Core.HandleRules;
|
|
using Squidex.Domain.Apps.Core.Rules.EnrichedEvents;
|
|
using Squidex.Domain.Apps.Core.Scripting;
|
|
|
|
#pragma warning disable MA0048 // File name must match type name
|
|
|
|
namespace Squidex.Extensions.Actions.Script
|
|
{
|
|
public sealed class ScriptActionHandler : RuleActionHandler<ScriptAction, ScriptJob>
|
|
{
|
|
private readonly IScriptEngine scriptEngine;
|
|
|
|
public ScriptActionHandler(RuleEventFormatter formatter, IScriptEngine scriptEngine)
|
|
: base(formatter)
|
|
{
|
|
this.scriptEngine = scriptEngine;
|
|
}
|
|
|
|
protected override Task<(string Description, ScriptJob Data)> CreateJobAsync(EnrichedEvent @event, ScriptAction action)
|
|
{
|
|
var job = new ScriptJob { Script = action.Script, Event = @event };
|
|
|
|
return Task.FromResult(($"Run a script", job));
|
|
}
|
|
|
|
protected override async Task<Result> ExecuteJobAsync(ScriptJob job,
|
|
CancellationToken ct = default)
|
|
{
|
|
var vars = new ScriptVars
|
|
{
|
|
["event"] = job.Event
|
|
};
|
|
|
|
var result = await scriptEngine.ExecuteAsync(vars, job.Script, ct: ct);
|
|
|
|
return Result.Success(result?.ToString());
|
|
}
|
|
}
|
|
|
|
public sealed class ScriptJob
|
|
{
|
|
public EnrichedEvent Event { get; set; }
|
|
|
|
public string Script { get; set; }
|
|
}
|
|
}
|
|
|