Created
August 8, 2024 16:11
-
-
Save KevinJump/1fe978f0d51bead1eb43f6454559dbfc to your computer and use it in GitHub Desktop.
Example of how to intercept deletes during an uSync import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Cms.Core.Composing; | |
using Umbraco.Cms.Core.DependencyInjection; | |
using Umbraco.Cms.Core.Events; | |
using Umbraco.Cms.Core.Services; | |
using uSync.BackOffice; | |
using uSync.Core; | |
using static Umbraco.Cms.Core.Constants; | |
namespace uSync.Examples; | |
public class ExampleComposer : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
public class uSyncDeleteNotificationHandler : INotificationHandler<uSyncImportingItemNotification> | |
{ | |
private readonly IContentService _contentService; | |
/// <summary> | |
/// DONT DO THIS, WORK THE PATH OUT (At least the first time, then cache it) | |
/// </summary> | |
private string _workingPath = "-1,1041"; | |
public uSyncDeleteNotificationHandler(IContentService contentService) | |
{ | |
_contentService = contentService; | |
} | |
public void Handle(uSyncImportingItemNotification notification) | |
{ | |
if (notification.Item.IsEmptyItem() is false) | |
return; | |
// we only care about documents ? | |
if (notification.Handler.EntityType != UdiEntityType.Document) | |
return; | |
var actionType = notification.Item.Attribute("Change").ValueOrDefault<SyncActionType>(SyncActionType.None); | |
if (actionType == SyncActionType.Delete) | |
{ | |
// this is a delete, | |
// you could do lookups here to see if the item exists and is in the locations you want it to be deleted from | |
var existing = _contentService.GetById(notification.Item.GetKey()); | |
if (existing is null) return; // probibly already deleted. | |
// check the path | |
var path = existing.Path; | |
// annoyingly here the path is a string of numbers (e.g -1,1024,1941,1943) so you would need to either | |
// a know the paths of the nodes at your root (and cache them!) | |
// or b work out the path based on the nodes so load each node from the ids build a path (v-slow) | |
// if its in our target paths, we want the delete to happen so just return. | |
if (!path.StartsWith(_workingPath)) return; | |
// if we get here we don't want the delete to happen. | |
notification.Cancel = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As an example of how we lookup paths inside uSync
https://github.com/KevinJump/uSync/blob/v14/dev/uSync.Core/Serialization/Serializers/ContentSerializerBase.cs#L735-L792
but this uses internal caching of both entity lookups and previous path segments for speed, so i don't think it could be just 'picked' up for the example above, but it had the basics of how the path is calculated in it.