Skip to content

Commit

Permalink
Refined the Content webhooks mostly to remove Messages & State proper…
Browse files Browse the repository at this point in the history
…ties
  • Loading branch information
warrenbuckley committed Nov 22, 2023
1 parent 29dc36d commit 3897c56
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
Expand All @@ -24,4 +23,15 @@ public ContentCopiedWebhookEvent(
}

public override string Alias => Constants.WebhookEvents.Aliases.ContentCopied;

public override object? ConvertNotificationToRequestPayload(ContentCopiedNotification notification)
{
return new
{
notification.Copy,
notification.Original,
notification.ParentId,
notification.RelateToOriginal
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
Expand All @@ -24,4 +23,15 @@ public ContentDeletedVersionsWebhookEvent(
}

public override string Alias => Constants.WebhookEvents.Aliases.ContentDeletedVersions;

public override object? ConvertNotificationToRequestPayload(ContentDeletedVersionsNotification notification)
{
return new
{
notification.Id,
notification.DeletePriorVersions,
notification.SpecificVersion,
notification.DateToRetain
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;

namespace Umbraco.Cms.Core.Webhooks.Events.Content;

[WebhookEvent("Content Emptied Recycle Bin", Constants.WebhookEvents.Types.Content)]
[WebhookEvent("Content Recycle Bin Emptied", Constants.WebhookEvents.Types.Content)]
public class ContentEmptiedRecycleBinWebhookEvent : WebhookEventContentBase<ContentEmptiedRecycleBinNotification, IContent>
{
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ public ContentMovedToRecycleBinWebhookEvent(
}

public override string Alias => Constants.WebhookEvents.Aliases.ContentMovedToRecycleBin;

public override object? ConvertNotificationToRequestPayload(ContentMovedToRecycleBinNotification notification)
=> notification.MoveInfoCollection;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
Expand All @@ -24,4 +23,7 @@ public ContentMovedWebhookEvent(
}

public override string Alias => Constants.WebhookEvents.Aliases.ContentMoved;

public override object? ConvertNotificationToRequestPayload(ContentMovedNotification notification)
=> notification.MoveInfoCollection;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override IEnumerable<IContent> GetEntitiesFromNotification(ContentRoll
return null;
}

// Get preview/saved version of content as a rollback
// Get preview/saved version of content for a rollback
IPublishedContent? publishedContent = publishedSnapshot.Content.GetById(true, entity.Key);
return publishedContent is null ? null : _apiContentBuilder.Build(publishedContent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Umbraco.Cms.Core.Webhooks.Events.Content;

[WebhookEvent("Content Sorted", Constants.WebhookEvents.Types.Content)]
public class ContentSortedWebhookEvent : WebhookEventContentBase<ContentSortedNotification, IContent>
public class ContentSortedWebhookEvent : WebhookEventBase<ContentSortedNotification>
{
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
private readonly IApiContentBuilder _apiContentBuilder;
Expand All @@ -35,16 +35,19 @@ public ContentSortedWebhookEvent(

public override string Alias => Constants.WebhookEvents.Aliases.ContentSorted;

protected override IEnumerable<IContent> GetEntitiesFromNotification(ContentSortedNotification notification) => notification.SortedEntities;

protected override object? ConvertEntityToRequestPayload(IContent entity)
public override object? ConvertNotificationToRequestPayload(ContentSortedNotification notification)
{
if (_publishedSnapshotAccessor.TryGetPublishedSnapshot(out IPublishedSnapshot? publishedSnapshot) is false || publishedSnapshot!.Content is null)
{
return null;
}

IPublishedContent? publishedContent = publishedSnapshot.Content.GetById(entity.Key);
return publishedContent is null ? null : _apiContentBuilder.Build(publishedContent);
var sortedEntities = new List<object?>();
foreach (var entity in notification.SortedEntities)
{
IPublishedContent? publishedContent = publishedSnapshot.Content.GetById(entity.Key);
object? payload = publishedContent is null ? null : _apiContentBuilder.Build(publishedContent);
sortedEntities.Add(payload);
}
return sortedEntities;
}

Check warning on line 52 in src/Umbraco.Core/Webhooks/Events/Content/ContentSortedWebhookEvent.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

❌ New issue: Complex Method

ConvertNotificationToRequestPayload has a cyclomatic complexity of 10, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
}

0 comments on commit 3897c56

Please sign in to comment.