using System; using System.Collections.Generic; using System.Linq; using Elsa.ActivityResults; using Elsa.Attributes; using Elsa.Design; using Elsa.Serialization; using Elsa.Services; using Elsa.Services.Models; namespace Elsa.Activities.UserTask.Activities { /// /// Stores a set of possible user actions and halts the workflow until one of the actions has been performed. /// [ActivityDefinition( Category = "User Tasks", Description = "Triggers when a user action is received.", Outcomes = new[] { OutcomeNames.Done, "x => x.state.actions" } )] public class UserTask : Activity { private readonly ITokenSerializer serializer; [ActivityProperty( Type = ActivityPropertyTypes.List, Hint = "Enter a comma-separated list of available actions" )] public ICollection Actions { get; set; } = new List(); public UserTask(ITokenSerializer serializer) { this.serializer = serializer; } protected override bool OnCanExecute(ActivityExecutionContext context) { var userAction = GetUserAction(context); return Actions.Contains(userAction, StringComparer.OrdinalIgnoreCase); } protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context) => Suspend(); protected override IActivityExecutionResult OnResume(ActivityExecutionContext context) { var userAction = GetUserAction(context); return Done(userAction); } private string GetUserAction(ActivityExecutionContext context) => (string)context.Input; } }