Skip to content

Conversation

@Zeegaan
Copy link
Member

@Zeegaan Zeegaan commented Nov 10, 2023

Notes

Notifications was introduced in #15174, and with it came cancelling by notification 👀
This meant that you could cancel Save/Delete operations, but the frontend did not match!
For example, if you canceled deleting a webhook, the controllers would return 200 OK, and thus the frontend would think that you had deleted the entity and thus remove it from your screen. It would only come back after a refresh.

This PR remedies that by implementing the attempt pattern also seen in v14, and with the OperationStatus, now returns the correct status codes & notifications 🚀

How to test

  • Implement WebhookNotificationsComposer
  • Run umbraco
  • Create a webhook (Url and events are up to you)
  • Try to delete it
  • Assert that you get errors and are not moved from the "Are you sure you want to delete" prompt.

This should also be tested with creating / updating, this can be done by moving around the notification.CancelOperation(new EventMessage("", "")); in the WebhookNotificationsComposer .

WebhookNotificationsComposer

using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;

namespace Umbraco.Cms.Web.UI;

public class WebhookNotificationsComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.AddNotificationHandler<WebhookSavingNotification, WebhookNotificationHandler>();
        builder.AddNotificationHandler<WebhookSavedNotification, WebhookNotificationHandler>();
        builder.AddNotificationHandler<WebhookDeletingNotification, WebhookNotificationHandler>();
        builder.AddNotificationHandler<WebhookDeletedNotification, WebhookNotificationHandler>();
    }
}

public class WebhookNotificationHandler :
    INotificationHandler<WebhookSavingNotification>,
    INotificationHandler<WebhookSavedNotification>,
    INotificationHandler<WebhookDeletingNotification>,
    INotificationHandler<WebhookDeletedNotification>
{
    private readonly ILogger<WebhookNotificationHandler> _logger;

    public WebhookNotificationHandler(ILogger<WebhookNotificationHandler> logger) => _logger = logger;

    public void Handle(WebhookSavingNotification notification)
    {
        foreach (Webhook savedEntity in notification.SavedEntities)
        {
            _logger.LogInformation($"Saving webhook: {savedEntity.Url}");
        }
    }

    public void Handle(WebhookSavedNotification notification)
    {
        foreach (Webhook savedEntity in notification.SavedEntities)
        {
            _logger.LogInformation($"Saved webhook: {savedEntity.Url}");
        }
    }

    public void Handle(WebhookDeletingNotification notification)
    {
        notification.CancelOperation(new EventMessage("", ""));
        foreach (Webhook savedEntity in notification.DeletedEntities)
        {
            _logger.LogInformation($"Deleting webhook: {savedEntity.Url}");
        }
    }

    public void Handle(WebhookDeletedNotification notification)
    {
        foreach (Webhook savedEntity in notification.DeletedEntities)
        {
            _logger.LogInformation($"Deleted webhook: {savedEntity.Url}");
        }
    }
}

@Zeegaan Zeegaan requested a review from elit0451 November 10, 2023 13:57
@Zeegaan Zeegaan changed the title V13/feature/refactor webhookservice to attempt pattern V13: Refactor webhookservice to attempt pattern Nov 10, 2023
…tempt-pattern

# Conflicts:
#	src/Umbraco.Web.BackOffice/Controllers/WebhookController.cs
@elit0451 elit0451 changed the base branch from v13/dev to release/13.0 November 13, 2023 10:19
@Zeegaan Zeegaan changed the base branch from release/13.0 to v13/dev November 13, 2023 10:28
Copy link
Contributor

@elit0451 elit0451 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 💪 💪 Merging

@elit0451 elit0451 merged commit 0e8c92a into v13/dev Nov 13, 2023
@elit0451 elit0451 deleted the v13/feature/refactor-webhookservice-to-attempt-pattern branch November 13, 2023 17:36
@bjarnef bjarnef mentioned this pull request Nov 15, 2023
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants