Browse Source

Comment action handler. (#439)

pull/440/head
Sebastian Stehle 6 years ago
committed by GitHub
parent
commit
dcb11a7d13
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      backend/extensions/Squidex.Extensions/Actions/Comment/CommentAction.cs
  2. 85
      backend/extensions/Squidex.Extensions/Actions/Comment/CommentActionHandler.cs
  3. 21
      backend/extensions/Squidex.Extensions/Actions/Comment/CommentPlugin.cs

32
backend/extensions/Squidex.Extensions/Actions/Comment/CommentAction.cs

@ -0,0 +1,32 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Core.HandleRules;
using Squidex.Domain.Apps.Core.Rules;
namespace Squidex.Extensions.Actions.Comment
{
[RuleAction(
Title = "Comment",
IconImage = "<svg version='1.1' xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'><path d='M20.016 15.984v-12h-16.031v14.016l2.016-2.016h14.016zM20.016 2.016c1.078 0 1.969 0.891 1.969 1.969v12c0 1.078-0.891 2.016-1.969 2.016h-14.016l-3.984 3.984v-18c0-1.078 0.891-1.969 1.969-1.969h16.031z'></path></svg>",
IconColor = "#3389ff",
Display = "Create comment",
Description = "Create a comment for a content event.")]
public sealed class CommentAction : RuleAction
{
[Required]
[Display(Name = "Text", Description = "The comment text.")]
[DataType(DataType.MultilineText)]
[Formattable]
public string Text { get; set; }
[Display(Name = "Client", Description = "An optional client name.")]
[DataType(DataType.Text)]
public string Client { get; set; }
}
}

85
backend/extensions/Squidex.Extensions/Actions/Comment/CommentActionHandler.cs

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

21
backend/extensions/Squidex.Extensions/Actions/Comment/CommentPlugin.cs

@ -0,0 +1,21 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Squidex.Infrastructure.Plugins;
namespace Squidex.Extensions.Actions.Comment
{
public sealed class CommentPlugin : IPlugin
{
public void ConfigureServices(IServiceCollection services, IConfiguration config)
{
services.AddRuleAction<CommentAction, CommentActionHandler>();
}
}
}
Loading…
Cancel
Save