Improve Telnyx JS intellisense

This commit is contained in:
Sipke Schoorstra 2021-05-31 11:26:24 +02:00
parent 0f7a708c0f
commit 967fbddf8b
7 changed files with 74 additions and 37 deletions

View file

@ -1,5 +1,7 @@
using Elsa.Activities.Telnyx.Extensions;
using System.ComponentModel;
using Elsa.Activities.Telnyx.Extensions;
using Elsa.Activities.Telnyx.Webhooks.Models;
using Elsa.Activities.Telnyx.Webhooks.Payloads.Abstract;
using Elsa.Activities.Telnyx.Webhooks.Payloads.Call;
using Elsa.ActivityResults;
using Elsa.Attributes;
@ -8,9 +10,12 @@ using Elsa.Services.Models;
namespace Elsa.Activities.Telnyx.Activities
{
[Browsable(false)]
[Trigger(Category = "Telnyx", Outcomes = new[]{ OutcomeNames.Done })]
public class Webhook : Activity
{
[ActivityOutput] public TelnyxWebhook? Model { get; set; }
[ActivityOutput] public Payload? Payload { get; set; }
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context) => context.WorkflowExecutionContext.IsFirstPass ? ExecuteInternal(context) : Suspend();
protected override IActivityExecutionResult OnResume(ActivityExecutionContext context) => ExecuteInternal(context);
@ -32,7 +37,8 @@ namespace Elsa.Activities.Telnyx.Activities
}
Model = webhookModel;
return Done(webhookModel);
Payload = webhookModel.Data.Payload;
return Done();
}
}
}

View file

@ -20,76 +20,70 @@ namespace Elsa.Activities.Telnyx.ActivityTypes
{
internal class NotificationActivityTypeProvider : IActivityTypeProvider
{
private readonly IWebhookFilterService _webhookFilterService;
private readonly IDescribesActivityType _describesActivityType;
private readonly IActivityActivator _activityActivator;
public NotificationActivityTypeProvider(IWebhookFilterService webhookFilterService)
public NotificationActivityTypeProvider(IDescribesActivityType describesActivityType, IActivityActivator activityActivator)
{
_webhookFilterService = webhookFilterService;
_describesActivityType = describesActivityType;
_activityActivator = activityActivator;
}
public const string NotificationAttribute = "TelnyxNotification";
public const string EventTypeAttribute = "EventType";
public ValueTask<IEnumerable<ActivityType>> GetActivityTypesAsync(CancellationToken cancellationToken = default)
{
var activityTypes = GetActivityTypes();
return new ValueTask<IEnumerable<ActivityType>>(activityTypes);
}
private IEnumerable<ActivityType> GetActivityTypes()
public async ValueTask<IEnumerable<ActivityType>> GetActivityTypesAsync(CancellationToken cancellationToken = default)
{
var payloadTypes = GetType().Assembly.GetAllWithBaseClass<Payload>().Where(x => x.GetCustomAttribute<WebhookAttribute>() != null).ToList();
var activityTypes = payloadTypes.Select(CreateWebhookActivityType).ToList();
var activityTypes = (await Task.WhenAll(payloadTypes.Select(async x => await CreateWebhookActivityTypeAsync(x, cancellationToken)))).ToList();
// Add variations on the same webhooks. The webhook filters will conditionally select the appropriate one.
activityTypes.Add(CreateWebhookActivityTypeVariation<CallInitiatedPayload>("BridgeCallInitiated", "Bridge Call Initiated", "Triggered when an incoming bridging call was received."));
activityTypes.Add(CreateWebhookActivityTypeVariation<CallHangupPayload>("OriginatorCallHangup", "Originator Call Hangup", "Triggered when an incoming call was hangup by the originator."));
activityTypes.Add(await CreateWebhookActivityTypeVariationAsync<CallInitiatedPayload>("BridgeCallInitiated", "Bridge Call Initiated", "Triggered when an incoming bridging call was received.", cancellationToken));
activityTypes.Add(await CreateWebhookActivityTypeVariationAsync<CallHangupPayload>("OriginatorCallHangup", "Originator Call Hangup", "Triggered when an incoming call was hangup by the originator.", cancellationToken));
return activityTypes;
}
private ActivityType CreateWebhookActivityTypeVariation<T>(string activityType, string displayName, string description)
private async Task<ActivityType> CreateWebhookActivityTypeVariationAsync<T>(string activityType, string displayName, string description, CancellationToken cancellationToken)
{
var hangupWebhookAttribute = typeof(T).GetCustomAttribute<WebhookAttribute>()!;
return CreateWebhookActivityType(new WebhookAttribute(hangupWebhookAttribute.EventType, activityType, displayName, description));
return await CreateWebhookActivityTypeAsync(typeof(T), new WebhookAttribute(hangupWebhookAttribute.EventType, activityType, displayName, description), cancellationToken);
}
private static ActivityType CreateWebhookActivityType(Type payloadType)
private async Task<ActivityType> CreateWebhookActivityTypeAsync(Type payloadType, CancellationToken cancellationToken)
{
var webhookAttribute = payloadType.GetCustomAttribute<WebhookAttribute>();
if (webhookAttribute == null)
throw new InvalidOperationException($"Make sure that the payload type is annotated with the ${nameof(WebhookAttribute)} attribute");
return CreateWebhookActivityType(webhookAttribute);
return await CreateWebhookActivityTypeAsync(payloadType, webhookAttribute, cancellationToken);
}
private static ActivityType CreateWebhookActivityType(WebhookAttribute webhookAttribute)
private async Task<ActivityType> CreateWebhookActivityTypeAsync(Type payloadType, WebhookAttribute webhookAttribute, CancellationToken cancellationToken)
{
var typeName = webhookAttribute.ActivityType;
var displayName = webhookAttribute.DisplayName;
var description = webhookAttribute.Description;
var descriptor = await _describesActivityType.DescribeAsync<Webhook>(cancellationToken);
descriptor.Description = webhookAttribute.Description;
descriptor.DisplayName = webhookAttribute.DisplayName;
descriptor.Type = webhookAttribute.ActivityType;
var outputProperties = descriptor.OutputProperties.Where(x => x.Name != nameof(Webhook.Payload)).ToList();
outputProperties.Add(new ActivityOutputDescriptor(nameof(Webhook.Payload), payloadType));
descriptor.OutputProperties = outputProperties.ToArray();
return new ActivityType
{
Describe = () => new ActivityDescriptor
{
Category = "Telnyx",
Description = description,
Type = typeName,
Outcomes = new[] { OutcomeNames.Done },
Traits = ActivityTraits.Trigger,
DisplayName = displayName
},
Description = description,
DisplayName = displayName,
TypeName = typeName,
Describe = () => descriptor,
Description = descriptor.Description,
DisplayName = descriptor.DisplayName,
TypeName = descriptor.Type,
Attributes = new Dictionary<string, object>
{
[NotificationAttribute] = true,
[EventTypeAttribute] = webhookAttribute.EventType
},
ActivateAsync = _ => new ValueTask<IActivity>(new Webhook()),
ActivateAsync = async context => await _activityActivator.ActivateActivityAsync<Webhook>(context, cancellationToken),
CanExecuteAsync = (context, instance) => instance.CanExecuteAsync(context),
ExecuteAsync = (context, instance) => instance.ExecuteAsync(context),
ResumeAsync = (context, instance) => instance.ResumeAsync(context),

View file

@ -18,6 +18,7 @@
<ItemGroup>
<ProjectReference Include="..\..\core\Elsa.Core\Elsa.Core.csproj" />
<ProjectReference Include="..\..\scripting\Elsa.Scripting.JavaScript\Elsa.Scripting.JavaScript.csproj" />
<ProjectReference Include="..\..\scripting\Elsa.Scripting.Liquid\Elsa.Scripting.Liquid.csproj" />
<ProjectReference Include="..\Elsa.Activities.Temporal.Common\Elsa.Activities.Temporal.Common.csproj" />
</ItemGroup>

View file

@ -9,6 +9,7 @@ using Elsa.Activities.Telnyx.Bookmarks;
using Elsa.Activities.Telnyx.Client.Services;
using Elsa.Activities.Telnyx.Liquid;
using Elsa.Activities.Telnyx.Options;
using Elsa.Activities.Telnyx.Scripting.JavaScript;
using Elsa.Activities.Telnyx.Webhooks.Consumers;
using Elsa.Activities.Telnyx.Webhooks.Events;
using Elsa.Activities.Telnyx.Webhooks.Filters;
@ -46,6 +47,7 @@ namespace Elsa.Activities.Telnyx.Extensions
.AddActivityTypeProvider<NotificationActivityTypeProvider>()
.AddBookmarkProvider<NotificationBookmarkProvider>()
.AddNotificationHandlers(typeof(TriggerWorkflows))
.AddJavaScriptTypeDefinitionProvider<TelnyxTypeDefinitionProvider>()
.AddScoped<IWebhookHandler, WebhookHandler>()
.AddSingleton<IWebhookFilterService, WebhookFilterService>()
.AddSingleton<IWebhookFilter, AttributeBasedWebhookFilter>()

View file

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using Elsa.Activities.Telnyx.Webhooks.Models;
using Elsa.Activities.Telnyx.Webhooks.Payloads.Call;
using Elsa.Scripting.JavaScript.Services;
namespace Elsa.Activities.Telnyx.Scripting.JavaScript
{
public class TelnyxTypeDefinitionProvider : TypeDefinitionProvider
{
public override IEnumerable<Type> CollectTypes(TypeDefinitionContext context)
{
return new[]
{
typeof(TelnyxWebhook),
typeof(CallAnsweredPayload),
typeof(CallBridgedPayload),
typeof(CallDtmfReceivedPayload),
typeof(CallGatherEndedPayload),
typeof(CallHangupPayload),
typeof(CallInitiatedPayload),
typeof(CallPayload),
typeof(CallPlayback),
typeof(CallPlaybackEndedPayload),
typeof(CallPlaybackStartedPayload),
typeof(CallRecordingSaved),
typeof(CallRecordingUrls),
typeof(CallSpeakEnded),
typeof(CallSpeakStarted),
};
}
}
}

View file

@ -8,7 +8,7 @@ using Elsa.Scripting.Liquid.Messages;
using Fluid;
using MediatR;
namespace Elsa.Activities.Telnyx.Liquid
namespace Elsa.Activities.Telnyx.Scripting.Liquid
{
public class ConfigureLiquidEngine : INotificationHandler<EvaluatingLiquidExpression>
{