// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.Threading; using System.Threading.Tasks; using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.Rules.EnrichedEvents; using Squidex.Domain.Apps.Entities.Comments.Commands; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; namespace Squidex.Extensions.Actions.Comment { public sealed class CommentActionHandler : RuleActionHandler { private const string Description = "Send a Comment"; private readonly ICommandBus commandBus; public CommentActionHandler(RuleEventFormatter formatter, ICommandBus commandBus) : base(formatter) { Guard.NotNull(commandBus, nameof(commandBus)); this.commandBus = commandBus; } protected override async Task<(string Description, CreateComment Data)> CreateJobAsync(EnrichedEvent @event, CommentAction action) { if (@event is EnrichedContentEvent contentEvent) { var ruleJob = new CreateComment { AppId = contentEvent.AppId, }; ruleJob.Text = await FormatAsync(action.Text, @event); if (!string.IsNullOrEmpty(action.Client)) { ruleJob.Actor = new RefToken(RefTokenType.Client, action.Client); } else { ruleJob.Actor = contentEvent.Actor; } ruleJob.CommentsId = contentEvent.Id.ToString(); return (Description, ruleJob); } return ("Ignore", new CreateComment()); } protected override async Task ExecuteJobAsync(CreateComment job, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(job.CommentsId)) { return Result.Ignored(); } var command = job; command.FromRule = true; await commandBus.PublishAsync(command); return Result.Success($"Commented: {job.Text}"); } } }