diff --git a/src/Umbraco.Core/Constants-UdiEntityType.cs b/src/Umbraco.Core/Constants-UdiEntityType.cs index f65c290516..beae3391b1 100644 --- a/src/Umbraco.Core/Constants-UdiEntityType.cs +++ b/src/Umbraco.Core/Constants-UdiEntityType.cs @@ -46,6 +46,8 @@ public static partial class Constants public const string RelationType = "relation-type"; + public const string Webhook = "webhook"; + // forms public const string FormsForm = "forms-form"; public const string FormsPreValue = "forms-prevalue"; diff --git a/src/Umbraco.Core/Extensions/UdiGetterExtensions.cs b/src/Umbraco.Core/Extensions/UdiGetterExtensions.cs index 03ed07f2fe..fda84c1013 100644 --- a/src/Umbraco.Core/Extensions/UdiGetterExtensions.cs +++ b/src/Umbraco.Core/Extensions/UdiGetterExtensions.cs @@ -358,6 +358,21 @@ public static class UdiGetterExtensions return new GuidUdi(Constants.UdiEntityType.RelationType, entity.Key).EnsureClosed(); } + /// + /// Gets the entity identifier of the entity. + /// + /// The entity. + /// The entity identifier of the entity. + public static GuidUdi GetUdi(this Webhook entity) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + + return new GuidUdi(Constants.UdiEntityType.Webhook, entity.Key).EnsureClosed(); + } + /// /// Gets the entity identifier of the entity. /// diff --git a/src/Umbraco.Core/Notifications/WebhookDeletedNotification .cs b/src/Umbraco.Core/Notifications/WebhookDeletedNotification .cs new file mode 100644 index 0000000000..516d52012c --- /dev/null +++ b/src/Umbraco.Core/Notifications/WebhookDeletedNotification .cs @@ -0,0 +1,12 @@ +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Notifications; + +public class WebhookDeletedNotification : DeletedNotification +{ + public WebhookDeletedNotification(Webhook target, EventMessages messages) + : base(target, messages) + { + } +} diff --git a/src/Umbraco.Core/Notifications/WebhookDeletingNotification.cs b/src/Umbraco.Core/Notifications/WebhookDeletingNotification.cs new file mode 100644 index 0000000000..f703113370 --- /dev/null +++ b/src/Umbraco.Core/Notifications/WebhookDeletingNotification.cs @@ -0,0 +1,17 @@ +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Notifications; + +public class WebhookDeletingNotification : DeletingNotification +{ + public WebhookDeletingNotification(Webhook target, EventMessages messages) + : base(target, messages) + { + } + + public WebhookDeletingNotification(IEnumerable target, EventMessages messages) + : base(target, messages) + { + } +} diff --git a/src/Umbraco.Core/Notifications/WebhookSavedNotification.cs b/src/Umbraco.Core/Notifications/WebhookSavedNotification.cs new file mode 100644 index 0000000000..efd4fc3707 --- /dev/null +++ b/src/Umbraco.Core/Notifications/WebhookSavedNotification.cs @@ -0,0 +1,17 @@ +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Notifications; + +public class WebhookSavedNotification : SavedNotification +{ + public WebhookSavedNotification(Webhook target, EventMessages messages) + : base(target, messages) + { + } + + public WebhookSavedNotification(IEnumerable target, EventMessages messages) + : base(target, messages) + { + } +} diff --git a/src/Umbraco.Core/Notifications/WebhookSavingNotification.cs b/src/Umbraco.Core/Notifications/WebhookSavingNotification.cs new file mode 100644 index 0000000000..69dee928c8 --- /dev/null +++ b/src/Umbraco.Core/Notifications/WebhookSavingNotification.cs @@ -0,0 +1,17 @@ +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Notifications; + +public class WebhookSavingNotification : SavingNotification +{ + public WebhookSavingNotification(Webhook target, EventMessages messages) + : base(target, messages) + { + } + + public WebhookSavingNotification(IEnumerable target, EventMessages messages) + : base(target, messages) + { + } +} diff --git a/src/Umbraco.Core/Services/IWebhookService.cs b/src/Umbraco.Core/Services/IWebhookService.cs index d38adaa999..38306839ba 100644 --- a/src/Umbraco.Core/Services/IWebhookService.cs +++ b/src/Umbraco.Core/Services/IWebhookService.cs @@ -8,7 +8,7 @@ public interface IWebhookService /// Creates a webhook. /// /// to create. - Task CreateAsync(Webhook webhook); + Task CreateAsync(Webhook webhook); /// /// Updates a webhook. diff --git a/src/Umbraco.Core/Services/WebhookService.cs b/src/Umbraco.Core/Services/WebhookService.cs index e6d5f405b4..40a827c51e 100644 --- a/src/Umbraco.Core/Services/WebhookService.cs +++ b/src/Umbraco.Core/Services/WebhookService.cs @@ -1,4 +1,6 @@ +using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Core.Persistence.Repositories; using Umbraco.Cms.Core.Scoping; @@ -8,18 +10,33 @@ public class WebhookService : IWebhookService { private readonly ICoreScopeProvider _provider; private readonly IWebhookRepository _webhookRepository; + private readonly IEventMessagesFactory _eventMessagesFactory; - public WebhookService(ICoreScopeProvider provider, IWebhookRepository webhookRepository) + public WebhookService(ICoreScopeProvider provider, IWebhookRepository webhookRepository, IEventMessagesFactory eventMessagesFactory) { _provider = provider; _webhookRepository = webhookRepository; + _eventMessagesFactory = eventMessagesFactory; } /// - public async Task CreateAsync(Webhook webhook) + public async Task CreateAsync(Webhook webhook) { using ICoreScope scope = _provider.CreateCoreScope(); + + EventMessages eventMessages = _eventMessagesFactory.Get(); + var savingNotification = new WebhookSavingNotification(webhook, eventMessages); + if (scope.Notifications.PublishCancelable(savingNotification)) + { + scope.Complete(); + return null; + } + Webhook created = await _webhookRepository.CreateAsync(webhook); + + scope.Notifications.Publish( + new WebhookSavedNotification(webhook, eventMessages).WithStateFrom(savingNotification)); + scope.Complete(); return created; @@ -37,6 +54,14 @@ public class WebhookService : IWebhookService throw new ArgumentException("Webhook does not exist"); } + EventMessages eventMessages = _eventMessagesFactory.Get(); + var savingNotification = new WebhookSavingNotification(webhook, eventMessages); + if (scope.Notifications.PublishCancelable(savingNotification)) + { + scope.Complete(); + return; + } + currentWebhook.Enabled = webhook.Enabled; currentWebhook.ContentTypeKeys = webhook.ContentTypeKeys; currentWebhook.Events = webhook.Events; @@ -44,6 +69,10 @@ public class WebhookService : IWebhookService currentWebhook.Headers = webhook.Headers; await _webhookRepository.UpdateAsync(currentWebhook); + + scope.Notifications.Publish( + new WebhookSavedNotification(webhook, eventMessages).WithStateFrom(savingNotification)); + scope.Complete(); } @@ -54,7 +83,17 @@ public class WebhookService : IWebhookService Webhook? webhook = await _webhookRepository.GetAsync(key); if (webhook is not null) { + EventMessages eventMessages = _eventMessagesFactory.Get(); + var deletingNotification = new WebhookDeletingNotification(webhook, eventMessages); + if (scope.Notifications.PublishCancelable(deletingNotification)) + { + scope.Complete(); + return; + } + await _webhookRepository.DeleteAsync(webhook); + scope.Notifications.Publish( + new WebhookDeletedNotification(webhook, eventMessages).WithStateFrom(deletingNotification)); } scope.Complete(); diff --git a/src/Umbraco.Core/UdiParser.cs b/src/Umbraco.Core/UdiParser.cs index 1442d43eb2..40a676d659 100644 --- a/src/Umbraco.Core/UdiParser.cs +++ b/src/Umbraco.Core/UdiParser.cs @@ -1,4 +1,4 @@ -using System.Collections.Concurrent; +using System.Collections.Concurrent; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; @@ -231,5 +231,6 @@ public sealed class UdiParser { Constants.UdiEntityType.PartialView, UdiType.StringUdi }, { Constants.UdiEntityType.PartialViewMacro, UdiType.StringUdi }, { Constants.UdiEntityType.Stylesheet, UdiType.StringUdi }, + { Constants.UdiEntityType.Webhook, UdiType.GuidUdi }, }; } diff --git a/src/Umbraco.Core/Webhooks/WebhookEventBase.cs b/src/Umbraco.Core/Webhooks/WebhookEventBase.cs index de15e23a00..529fe4191b 100644 --- a/src/Umbraco.Core/Webhooks/WebhookEventBase.cs +++ b/src/Umbraco.Core/Webhooks/WebhookEventBase.cs @@ -1,4 +1,4 @@ -using Microsoft.Extensions.Options; +using Microsoft.Extensions.Options; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Core.Events; diff --git a/src/Umbraco.Web.BackOffice/Controllers/WebhookController.cs b/src/Umbraco.Web.BackOffice/Controllers/WebhookController.cs index 68d668645e..5e76d54ef4 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/WebhookController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/WebhookController.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Mapping; using Umbraco.Cms.Core.Models;