Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created August 8, 2024 16:11
Show Gist options
  • Save KevinJump/1fe978f0d51bead1eb43f6454559dbfc to your computer and use it in GitHub Desktop.
Save KevinJump/1fe978f0d51bead1eb43f6454559dbfc to your computer and use it in GitHub Desktop.
Example of how to intercept deletes during an uSync import
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;
}
}
}
@KevinJump
Copy link
Author

this code shows how to get notifications when uSync imports stuff, check to see if they are deletes and potentially cancel them based on the path of the item.

delete files don't have the path in, because they are deleted from the recycle bin, and when that happens the path is the recycle bin not where they might have been withing uSync.

this code would work as the starting point for working out where an item is and then if its in the right place deleting it, while ignoring anything that was getting deleted from outside of those folder(s).

@KevinJump
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment