Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions uSync.Core/Documents/ISyncDocumentUrlCleaner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

namespace uSync.Core.Documents;

public interface ISyncDocumentUrlCleaner
{
void CleanUrlsForDocument(Guid key);
}
36 changes: 36 additions & 0 deletions uSync.Core/Documents/SyncDocumentUrlCleaner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.Logging;

using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;

namespace uSync.Core.Documents;

internal class SyncDocumentUrlCleaner : ISyncDocumentUrlCleaner
{
private readonly ICoreScopeProvider _scopeProvider;
private readonly IDocumentUrlRepository _documentUrlRepository;
private readonly ILogger<SyncDocumentUrlCleaner> _logger;

public SyncDocumentUrlCleaner(IDocumentUrlRepository documentUrlRepository, ILogger<SyncDocumentUrlCleaner> logger, ICoreScopeProvider scopeProvider)
{
_documentUrlRepository = documentUrlRepository;
_logger = logger;
_scopeProvider = scopeProvider;
}

public void CleanUrlsForDocument(Guid key)
{
try
{
using (_scopeProvider.CreateCoreScope(autoComplete: true))
{
_logger.LogDebug("Cleaning urls for document {DocumentKey}", key);
_documentUrlRepository.DeleteByDocumentKey([key]);
}
}
catch(Exception ex)
{
_logger.LogError(ex, "Error cleaning urls for document {DocumentKey}", key);
}
}
}
16 changes: 14 additions & 2 deletions uSync.Core/Serialization/Serializers/ContentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Umbraco.Cms.Core.Strings;
using Umbraco.Extensions;

using uSync.Core.Documents;
using uSync.Core.Extensions;
using uSync.Core.Mapping;
using uSync.Core.Models;
Expand All @@ -23,6 +24,7 @@ public class ContentSerializer : ContentSerializerBase<IContent>, ISyncSerialize
protected readonly IUserService userService;

protected readonly ITemplateService _templateService;
protected readonly ISyncDocumentUrlCleaner _urlCleaner;

public ContentSerializer(
IEntityService entityService,
Expand All @@ -33,14 +35,16 @@ public ContentSerializer(
IContentService contentService,
SyncValueMapperCollection syncMappers,
IUserService userService,
ITemplateService templateService)
ITemplateService templateService,
ISyncDocumentUrlCleaner urlCleaner)
: base(entityService, languageService, relationService, shortStringHelper, logger, UmbracoObjectTypes.Document, syncMappers)
{
this.contentService = contentService;

this.relationAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
this.userService = userService;
_templateService = templateService;
_urlCleaner = urlCleaner;
}

#region Serialization
Expand Down Expand Up @@ -298,7 +302,7 @@ public override async Task<SyncAttempt<IContent>> DeserializeSecondPassAsync(ICo
var changes = await DeserializeSchedulesAsync(item, node, options);
if (changes.Count != 0)
return SyncAttempt<IContent>.Succeed(item.Name ?? item.Id.ToString(), item, ChangeType.Import, "" ?? string.Empty, true,
[..details, ..changes]);
[.. details, .. changes]);

// if we have changed the sort order, then we return a change, else it was no change.
return SyncAttempt<IContent>.Succeed(item.Name ?? item.Id.ToString(), item,
Expand Down Expand Up @@ -794,4 +798,12 @@ public override Task DeleteItemAsync(IContent item)
}
});
}

protected override Task OnKeyChange(IContent item, Guid oldKey, Guid newKey)
{
// key changes need to clean the DocumentUrl cache.
_urlCleaner.CleanUrlsForDocument(oldKey);
return Task.CompletedTask;
}
}

11 changes: 11 additions & 0 deletions uSync.Core/Serialization/Serializers/ContentSerializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ protected virtual async Task<IEnumerable<uSyncChange>> DeserializeBaseAsync(TObj
{
changes.AddUpdate(uSyncConstants.Xml.Key, item.Key, key);
logger.LogTrace("{Id} Setting Key {Key}", item.Id, key);

if (item.Id > 0)
await OnKeyChange(item, item.Key, key);

item.Key = key;
}

Expand All @@ -426,6 +430,13 @@ protected virtual async Task<IEnumerable<uSyncChange>> DeserializeBaseAsync(TObj
return changes;
}

protected virtual Task OnKeyChange(TObject item, Guid oldKey, Guid newKey)
{
// nothing to do here, but subclasses might need to act on key changes.
logger.LogDebug("{id} Key changed from {oldKey} to {newKey}", item.Id, oldKey, newKey);
return Task.CompletedTask;
}

protected IEnumerable<uSyncChange> DeserializeName(TObject item, XElement node, SyncSerializerOptions options)
{
var nameNode = node.Element(uSyncConstants.Xml.Info)?.Element("NodeName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;

using uSync.Core.Documents;
using uSync.Core.Extensions;
using uSync.Core.Mapping;
using uSync.Core.Models;
Expand All @@ -29,8 +30,9 @@ public ContentTemplateSerializer(
IContentTypeService contentTypeService,
SyncValueMapperCollection syncMappers,
IUserService userService,
ITemplateService templateService)
: base(entityService, languageService, relationService, shortStringHelper, logger, contentService, syncMappers, userService, templateService)
ITemplateService templateService,
ISyncDocumentUrlCleaner urlCleaner)
: base(entityService, languageService, relationService, shortStringHelper, logger, contentService, syncMappers, userService, templateService, urlCleaner)
{
_contentTypeService = contentTypeService;
this.umbracoObjectType = UmbracoObjectTypes.DocumentBlueprint;
Expand Down
4 changes: 4 additions & 0 deletions uSync.Core/uSyncCoreBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using uSync.Core.Cache;
using uSync.Core.DataTypes;
using uSync.Core.Dependency;
using uSync.Core.Documents;
using uSync.Core.Mapping;
using uSync.Core.Roots.Configs;
using uSync.Core.Serialization;
Expand Down Expand Up @@ -33,6 +34,9 @@ public static IUmbracoBuilder AdduSyncCore(this IUmbracoBuilder builder)

builder.Services.AddSingleton<uSyncCapabilityChecker>();

// document url cleaner, for key changes
builder.Services.AddSingleton<ISyncDocumentUrlCleaner ,SyncDocumentUrlCleaner>();

// cache for entity items, we use it to speed up lookups.
builder.Services.AddSingleton<SyncEntityCache>();

Expand Down