Skip to content

Commit

Permalink
Add serializers and handlers for webhooks. (#613)
Browse files Browse the repository at this point in the history
* Add serializers and handlers for webhooks.

* Webhook Comments
  • Loading branch information
KevinJump committed Apr 17, 2024
1 parent 42b418e commit 23d1b90
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 15 deletions.
20 changes: 15 additions & 5 deletions uSync.BackOffice/BackOfficeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ public static class Priorites
/// </summary>
public const int DataTypeMappings = USYNC_RESERVED_LOWER + 220;

/// <summary>
/// RelationTypes priority
/// </summary>
public const int RelationTypes = USYNC_RESERVED_LOWER + 230;
}
/// <summary>
/// RelationTypes priority
/// </summary>
public const int RelationTypes = USYNC_RESERVED_LOWER + 230;

/// <summary>
/// Webhooks priority.
/// </summary>
public const int Webhooks = USYNC_RESERVED_LOWER + 250;
}

/// <summary>
/// Default group names
Expand Down Expand Up @@ -238,6 +243,11 @@ public static class Handlers
/// </summary>
public const string TemplateHandler = "TemplateHandler";

/// <summary>
/// WebhooksHandler name
/// </summary>
public const string WebhookHandler = "WebhookHandler";


}
}
84 changes: 84 additions & 0 deletions uSync.BackOffice/SyncHandlers/Handlers/WebhookHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Extensions.Logging;

using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;

using uSync.BackOffice.Configuration;
using uSync.BackOffice.Services;
using uSync.Core;

using static Umbraco.Cms.Core.Constants;

namespace uSync.BackOffice.SyncHandlers.Handlers;

/// <summary>
/// handler for webhook events.
/// </summary>
[SyncHandler(uSyncConstants.Handlers.WebhookHandler, "Webhooks", "Webhooks",
uSyncConstants.Priorites.Webhooks,
Icon = "icon-filter-arrows",
EntityType = UdiEntityType.Webhook,
IsTwoPass = false
)]
public class WebhookHandler : SyncHandlerRoot<IWebhook, IWebhook>, ISyncHandler,
INotificationHandler<SavedNotification<IWebhook>>,
INotificationHandler<DeletedNotification<IWebhook>>,
INotificationHandler<SavingNotification<IWebhook>>,
INotificationHandler<DeletingNotification<IWebhook>>
{
private readonly IWebhookService _webhookService;

/// <summary>
/// constructor
/// </summary>
public WebhookHandler(
ILogger<SyncHandlerRoot<IWebhook, IWebhook>> logger,
AppCaches appCaches,
IShortStringHelper shortStringHelper,
SyncFileService syncFileService,
uSyncEventService mutexService,
uSyncConfigService uSyncConfig,
ISyncItemFactory itemFactory,
IWebhookService webhookService)
: base(logger, appCaches, shortStringHelper, syncFileService, mutexService, uSyncConfig, itemFactory)
{
_webhookService = webhookService;
}

/// <inheritdoc/>
protected override IEnumerable<uSyncAction> DeleteMissingItems(IWebhook parent, IEnumerable<Guid> keysToKeep, bool reportOnly)
{
return [];
}

/// <inheritdoc/>
protected override IEnumerable<IWebhook> GetChildItems(IWebhook parent)
{
if (parent == null)
{
return _webhookService.GetAllAsync(0, 1000).Result.Items;
}

return [];
}

/// <inheritdoc/>
protected override IEnumerable<IWebhook> GetFolders(IWebhook parent) => [];

/// <inheritdoc/>
protected override IWebhook GetFromService(IWebhook item)
=> _webhookService.GetAsync(item.Key).Result;

/// <inheritdoc/>
protected override string GetItemName(IWebhook item) => item.Key.ToString();
}
21 changes: 14 additions & 7 deletions uSync.BackOffice/uSyncBackOfficeBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,14 @@ internal static void AddHandlerNotifications(this IUmbracoBuilder builder)
builder.AddNotificationHandler<TemplateSavedNotification, TemplateHandler>();
builder.AddNotificationHandler<TemplateDeletedNotification, TemplateHandler>();

// roots - pre-notifications for stopping things
builder
.AddNotificationHandler<ContentTypeSavingNotification, ContentTypeHandler>()
.AddNotificationHandler<ContentTypeDeletingNotification, ContentTypeHandler>()
.AddNotificationHandler<ContentTypeMovingNotification, ContentTypeHandler>()
builder.AddNotificationHandler<WebhookSavedNotification, WebhookHandler>();
builder.AddNotificationHandler<WebhookDeletedNotification, WebhookHandler>();

// roots - pre-notifications for stopping things
builder
.AddNotificationHandler<ContentTypeSavingNotification, ContentTypeHandler>()
.AddNotificationHandler<ContentTypeDeletingNotification, ContentTypeHandler>()
.AddNotificationHandler<ContentTypeMovingNotification, ContentTypeHandler>()

.AddNotificationHandler<MediaTypeSavingNotification, MediaTypeHandler>()
.AddNotificationHandler<MediaTypeDeletingNotification, MediaTypeHandler>()
Expand Down Expand Up @@ -192,8 +195,12 @@ internal static void AddHandlerNotifications(this IUmbracoBuilder builder)
.AddNotificationHandler<RelationTypeSavingNotification, RelationTypeHandler>()
.AddNotificationHandler<RelationTypeDeletingNotification, RelationTypeHandler>()

.AddNotificationHandler<TemplateSavingNotification, TemplateHandler>()
.AddNotificationHandler<TemplateDeletingNotification, TemplateHandler>();

.AddNotificationHandler<TemplateSavingNotification, TemplateHandler>()
.AddNotificationHandler<TemplateDeletingNotification, TemplateHandler>()

.AddNotificationHandler<WebhookSavingNotification, WebhookHandler>()
.AddNotificationHandler<WebhookDeletingNotification, WebhookHandler>();


// content ones
Expand Down
Loading

0 comments on commit 23d1b90

Please sign in to comment.