diff --git a/backend/extensions/Squidex.Extensions/Actions/AzureServiceBus/AzureServiceBusFlowStep.cs b/backend/extensions/Squidex.Extensions/Actions/AzureServiceBus/AzureServiceBusFlowStep.cs new file mode 100644 index 000000000..f30f21f1e --- /dev/null +++ b/backend/extensions/Squidex.Extensions/Actions/AzureServiceBus/AzureServiceBusFlowStep.cs @@ -0,0 +1,97 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System.ComponentModel.DataAnnotations; +using Azure.Messaging.ServiceBus; +using Squidex.Flows; +using Squidex.Infrastructure.Validation; + +namespace Squidex.Extensions.Actions.AzureServiceBus; + +[FlowStep( + Title = "Azure Service Bus", + IconImage = "", + IconColor = "#0d9bf9", + Display = "Send to Azure Service Bus", + Description = "Send an event to an azure service bus queue or topic.", + ReadMore = "https://azure.microsoft.com/en-us/products/service-bus/")] +public sealed record AzureServiceBusFlowStep : FlowStep +{ + [LocalizedRequired] + [Display(Name = "Connection", Description = "The connection string to the service bus namespace.")] + [Editor(FlowStepEditor.Text)] + [Expression] + public string ConnectionString { get; set; } + + [LocalizedRequired] + [Display(Name = "Queue or Topic", Description = "The name of the queue or topic.")] + [Editor(FlowStepEditor.Text)] + [Expression] + public string QueueOrTopic { get; set; } + + [Display(Name = "Payload (Optional)", Description = "Leave it empty to use the full event as body.")] + [Editor(FlowStepEditor.TextArea)] + [Expression(ExpressionFallback.Envelope)] + public string? Payload { get; set; } + + [Display(Name = "Subject (Optional)", Description = "The subject of the message, commonly used for filtering in subscriptions.")] + [Editor(FlowStepEditor.Text)] + [Expression] + public string? Subject { get; set; } + + [Display(Name = "Session ID (Optional)", Description = "The session ID, required for session enabled queues and subscriptions.")] + [Editor(FlowStepEditor.Text)] + [Expression] + public string? SessionId { get; set; } + + [Display(Name = "Message ID (Optional)", Description = "The message ID, used for duplicate detection.")] + [Editor(FlowStepEditor.Text)] + [Expression] + public string? MessageId { get; set; } + + // The client owns the connection and is thread safe, senders are cheap and are created per message. + private static readonly ClientPool Clients = new ClientPool(key => + { + return new ServiceBusClient(key); + }); + + public async override ValueTask ExecuteAsync(FlowExecutionContext executionContext, + CancellationToken ct) + { + if (executionContext.IsSimulation) + { + executionContext.LogSkipSimulation(); + return Next(); + } + + var client = await Clients.GetClientAsync(ConnectionString); + + var message = new ServiceBusMessage(Payload); + + if (!string.IsNullOrWhiteSpace(Subject)) + { + message.Subject = Subject; + } + + if (!string.IsNullOrWhiteSpace(SessionId)) + { + message.SessionId = SessionId; + } + + if (!string.IsNullOrWhiteSpace(MessageId)) + { + message.MessageId = MessageId; + } + + await using var sender = client.CreateSender(QueueOrTopic); + + await sender.SendMessageAsync(message, ct); + + executionContext.Log($"Message sent to '{QueueOrTopic}'."); + return Next(); + } +} diff --git a/backend/extensions/Squidex.Extensions/Actions/AzureServiceBus/AzureServiceBusPlugin.cs b/backend/extensions/Squidex.Extensions/Actions/AzureServiceBus/AzureServiceBusPlugin.cs new file mode 100644 index 000000000..99661020b --- /dev/null +++ b/backend/extensions/Squidex.Extensions/Actions/AzureServiceBus/AzureServiceBusPlugin.cs @@ -0,0 +1,20 @@ +// ========================================================================== +// 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.AzureServiceBus; + +public sealed class AzureServiceBusPlugin : IPlugin +{ + public void ConfigureServices(IServiceCollection services, IConfiguration config) + { + services.AddFlowStep(); + } +} diff --git a/backend/extensions/Squidex.Extensions/Actions/CreateContent/CreateContentFlowStep.cs b/backend/extensions/Squidex.Extensions/Actions/CreateContent/CreateContentFlowStep.cs index 5e328349a..894ac2ea6 100644 --- a/backend/extensions/Squidex.Extensions/Actions/CreateContent/CreateContentFlowStep.cs +++ b/backend/extensions/Squidex.Extensions/Actions/CreateContent/CreateContentFlowStep.cs @@ -17,7 +17,9 @@ using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Reflection; using Squidex.Infrastructure.Validation; -using Command = Squidex.Domain.Apps.Entities.Contents.Commands.CreateContent; +using ContentDataCommand = Squidex.Domain.Apps.Entities.Contents.Commands.ContentDataCommand; +using CreateCommand = Squidex.Domain.Apps.Entities.Contents.Commands.CreateContent; +using UpsertCommand = Squidex.Domain.Apps.Entities.Contents.Commands.UpsertContent; namespace Squidex.Extensions.Actions.CreateContent; @@ -26,7 +28,7 @@ namespace Squidex.Extensions.Actions.CreateContent; IconImage = "", IconColor = "#3389ff", Display = "Create content", - Description = "Create a a new content item for any schema.")] + Description = "Create a new content item for any schema, or update it if it already exists.")] #pragma warning disable CS0618 // Type or member is obsolete public sealed record CreateContentFlowStep : FlowStep, IConvertibleToAction #pragma warning restore CS0618 // Type or member is obsolete @@ -47,19 +49,34 @@ public sealed record CreateContentFlowStep : FlowStep, IConvertibleToAction public string? Client { get; set; } [Display(Name = "Publish", Description = "Publish the content.")] - [Editor(FlowStepEditor.Text)] + [Editor(FlowStepEditor.Checkbox)] public bool Publish { get; set; } + [Display(Name = "ID (Optional)", Description = "The ID of the content. If set, the content is created when it does not exist and updated otherwise.")] + [Editor(FlowStepEditor.Text)] + [Expression] + public string? Id { get; set; } + + [Display(Name = "Patch", Description = "Only update the fields from the data when the content already exists. Requires an ID.")] + [Editor(FlowStepEditor.Checkbox)] + public bool Patch { get; set; } + + public override ValueTask ValidateAsync(FlowValidationContext validationContext, AddStepError addError, + CancellationToken ct) + { + if (Patch && string.IsNullOrWhiteSpace(Id)) + { + addError(nameof(Patch), "Patch requires an ID."); + } + + return base.ValidateAsync(validationContext, addError, ct); + } + public override async ValueTask ExecuteAsync(FlowExecutionContext executionContext, CancellationToken ct) { var @event = ((FlowEventContext)executionContext.Context).Event; - var command = new Command - { - AppId = @event.AppId, - }; - var schema = await executionContext.Resolve() .GetSchemaAsync(@event.AppId.Id, Schema, true, ct); @@ -76,6 +93,26 @@ public sealed record CreateContentFlowStep : FlowStep, IConvertibleToAction return Next(); } + var status = Publish ? Status.Published : (Status?)null; + + ContentDataCommand command; + if (!string.IsNullOrWhiteSpace(Id)) + { + command = new UpsertCommand + { + ContentId = DomainId.Create(Id.Trim()), + EnrichDefaults = true, + EnrichRequiredFields = false, + Patch = Patch, + Status = status + }; + } + else + { + command = new CreateCommand { Status = status }; + } + + command.AppId = @event.AppId; command.SchemaId = schema.NamedId(); command.FromRule = true; command.Data = executionContext.DeserializeJson(Data); @@ -89,15 +126,18 @@ public sealed record CreateContentFlowStep : FlowStep, IConvertibleToAction command.Actor = userEvent.Actor; } - if (Publish) - { - command.Status = Status.Published; - } - await executionContext.Resolve() .PublishAsync(command, ct); - executionContext.Log($"Content created for schema '{schema.Name}', ID: {command.ContentId}"); + if (command is UpsertCommand) + { + executionContext.Log($"Content upserted for schema '{schema.Name}', ID: {command.ContentId}"); + } + else + { + executionContext.Log($"Content created for schema '{schema.Name}', ID: {command.ContentId}"); + } + return Next(); } diff --git a/backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj b/backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj index 4c83f16a9..b40e13e97 100644 --- a/backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj +++ b/backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj @@ -11,6 +11,7 @@ + diff --git a/todo.md b/todo.md index cfffbf450..cfc1849a2 100644 --- a/todo.md +++ b/todo.md @@ -35,9 +35,9 @@ being postponed and declined. See | [2](#2-export-and-import-content-from-the-ui-as-jobs-3461-1366-3438) | Export/import content from the UI as jobs | Declined ×2 | 5381 | large | | [3](#3-scope-asset-permissions-to-folders-5921-5676-1229) | Scope asset permissions to folders | Open ×2 + Declined | 3857 | large | | [4](#4-dark-mode-and-per-app-theming-5720-816) | Dark mode and per-app theming | Open + Declined | 2102 | large | -| [5](#5-azure-service-bus-flow-step-4522) | Azure Service Bus flow step | Postponed | 1838 | medium | +| [5](#5-azure-service-bus-flow-step-4522) | ✅ **Done** — Azure Service Bus flow step | Postponed | 1838 | medium | | [6](#6-give-scripts-a-way-to-log-2822) | Give scripts a way to log | Unresolved | 2575 | medium | -| [7](#7-upsert-mode-for-the-create-content-flow-step-5616) | Upsert mode for "Create content" step | Unresolved | 786 | small | +| [7](#7-upsert-mode-for-the-create-content-flow-step-5616) | ✅ **Done** — Upsert mode for "Create content" step | Unresolved | 786 | small | | [8](#8-regenerate-a-client-secret-from-the-ui-5807) | Regenerate a client secret from the UI | Open | 1383 | small | | [9](#9-show-which-fields-are-required-when-publishing-4375) | ✅ **Done** — Mark "required when publishing" fields | Declined | 1707 | tiny | | [10](#10-let-users-upload-a-duplicate-asset-anyway-3144) | ✅ **Done** — "Upload anyway" for duplicate assets | Declined | 1356 | tiny | @@ -246,6 +246,8 @@ it look expensive in 2019. ## 5. Azure Service Bus flow step (#4522) +> ✅ **Implemented.** New `AzureServiceBusFlowStep` (`Actions/AzureServiceBus/`) sends to a queue or topic, with connection string, payload, subject, session ID and message ID configurable per step. + **Asked:** contribute a Service Bus action upstream. · Postponed (`INACTIVE`) · 1838 views · 4 likes · **3 votes** (joint highest of @@ -324,6 +326,8 @@ has a notion of user-visible log lines to imitate. ## 7. Upsert mode for the "Create content" flow step (#5616) +> ✅ **Implemented.** `CreateContentFlowStep` has an optional `ID` (expression) and a `Patch` flag. With an ID it issues `UpsertContent` (create if missing, update or patch otherwise); without one it creates as before. + **Asked:** a rule that creates content when it does not exist and updates it when it does.