Skip to content
Closed
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
6 changes: 6 additions & 0 deletions src/uSync.Migrations.Migrators/MigratorsBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.Extensions.DependencyInjection;

using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Extensions;

using uSync.BackOffice;
using uSync.Migrations.Migrators.Grid.Config.Migrators;
using uSync.Migrations.Migrators.Grid.Content.BlockMigrators;
using uSync.Migrations.Migrators.Grid.Helpers;
using uSync.Migrations.Migrators.NestedContent;

namespace uSync.Migrations.Migrators;

Expand All @@ -22,6 +25,9 @@ public static IUmbracoBuilder AddSyncMigrators(this IUmbracoBuilder builder)
builder.WithCollectionBuilder<SyncBlockMigratorCollectionBuilder>()
.Add(() => builder.TypeLoader.GetTypes<ISyncBlockMigrator>());

// nested content doc types become element types for block list.
builder.AddNotificationAsyncHandler<uSyncImportingItemNotification, NestedContentElementTypeImportingHandler>();

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using uSync.Core.DataTypes;
using uSync.Core.Extensions;
using uSync.Migrations.Core.Migrators;
using uSync.Migrations.Core.Tracking;

using static uSync.Migrations.Core.SyncMigrations;

Expand All @@ -20,17 +21,19 @@ namespace uSync.Migrations.Migrators.NestedContent;
internal class NestedContentConfigurationMigrator : SyncConfigurationMigratorBase, IConfigurationSerializer
{
private readonly IContentTypeService _contentTypeService;
private readonly ISyncMigrationTrackingService _trackingService;

public NestedContentConfigurationMigrator(IContentTypeService contentTypeService)
public NestedContentConfigurationMigrator(IContentTypeService contentTypeService, ISyncMigrationTrackingService trackingService)
{
_contentTypeService = contentTypeService;
_trackingService = trackingService;
}

public string Name => nameof(NestedContentConfigurationMigrator);
public string[] Editors => [SyncLegacyTypes.NestedContent, SyncLegacyTypes.OurNestedContent];

public override string? TargetEditor => Constants.PropertyEditors.Aliases.BlockList;
public override Task<IDictionary<string, object>> GetMigratedConfigurationAsync(string name, IDictionary<string, object> configuration)
public override async Task<IDictionary<string, object>> GetMigratedConfigurationAsync(string name, IDictionary<string, object> configuration)
{
var config = new BlockListConfiguration();

Expand All @@ -46,6 +49,12 @@ public override Task<IDictionary<string, object>> GetMigratedConfigurationAsync(
foreach (var contentType in contentTypesArray.OfType<JsonObject>())
{
if (contentType["ncAlias"]?.ToString() is not string alias) continue;

// block list can only use element types, so we track the doc type here
// (which is before content types are imported) to make it one as it comes in.
// see NestedContentElementTypeImportingHandler
await _trackingService.AddRenameAsync(NestedContentElementTypes.GetTrackingKey(alias), alias, name);

var contentTypeItem = _contentTypeService.Get(alias);
if (contentTypeItem is null) continue;
blocks.Add(new BlockListConfiguration.BlockConfiguration
Expand All @@ -57,7 +66,6 @@ public override Task<IDictionary<string, object>> GetMigratedConfigurationAsync(
config.Blocks = [.. blocks];
}

var result = config.SerializeJsonString().DeserializeJson<Dictionary<string, object>>() ?? configuration;
return Task.FromResult(result);
return config.SerializeJsonString().DeserializeJson<Dictionary<string, object>>() ?? configuration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Xml.Linq;

using Microsoft.Extensions.Logging;

using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Services;

using uSync.BackOffice;
using uSync.Core;
using uSync.Migrations.Core.Tracking;

namespace uSync.Migrations.Migrators.NestedContent;

/// <summary>
/// makes the doc types used by nested content element types as they are imported.
/// </summary>
/// <remarks>
/// Block List only allows element types, nested content allowed any doc type, so
/// without this, the migrated block list would point at doc types it can't use.
///
/// we change the XML before it is imported, so the doc type is saved as an element
/// type, and it stays that way when the same (legacy) file is imported again.
/// </remarks>
internal class NestedContentElementTypeImportingHandler : INotificationAsyncHandler<uSyncImportingItemNotification>
{
private readonly ISyncMigrationTrackingService _trackingService;
private readonly IContentTypeService _contentTypeService;
private readonly ILogger<NestedContentElementTypeImportingHandler> _logger;

public NestedContentElementTypeImportingHandler(
ISyncMigrationTrackingService trackingService,
IContentTypeService contentTypeService,
ILogger<NestedContentElementTypeImportingHandler> logger)
{
_trackingService = trackingService;
_contentTypeService = contentTypeService;
_logger = logger;
}

public async Task HandleAsync(uSyncImportingItemNotification notification, CancellationToken cancellationToken)
{
var node = notification.Item;
if (node is null || node.Name.LocalName != "ContentType") return;

var info = node.Element("Info");
if (info is null) return;

if (info.Element("IsElement").ValueOrDefault(false)) return;

var alias = node.GetAlias();
if (string.IsNullOrWhiteSpace(alias)) return;

if (await _trackingService.ExistsAsync(NestedContentElementTypes.GetTrackingKey(alias)) is false)
return;

// a doc type that can be created at the root is being used as a page,
// making it an element would stop that working.
if (info.Element("AllowAtRoot").ValueOrDefault(false))
{
_logger.LogWarning("MIGRATION WARNING: Content type {alias} is used in Nested Content but is allowed at the root, so it has not been made an element type. Block List will not be able to use it until it is.", alias);
return;
}

// likewise if there is already content of this type in the site.
var existing = _contentTypeService.Get(node.GetKey()) ?? _contentTypeService.Get(alias);
if (existing is not null && existing.IsElement is false && _contentTypeService.HasContentNodes(existing.Id))
{
_logger.LogWarning("MIGRATION WARNING: Content type {alias} is used in Nested Content but has content nodes, so it has not been made an element type. Block List will not be able to use it until it is.", alias);
return;
}

var isElement = info.Element("IsElement");
if (isElement is null)
info.Add(new XElement("IsElement", true));
else
isElement.Value = "true";

_logger.LogInformation("Migrating: Content type {alias} is used in Nested Content, making it an element type for Block List", alias);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace uSync.Migrations.Migrators.NestedContent;

/// <summary>
/// keys for tracking the doc types that nested content uses, so they
/// can be made element types when they are imported.
/// </summary>
/// <remarks>
/// nested content would let you use any doc type, block list will only
/// use element types. The datatype is imported before the content types,
/// so we record the aliases then, and flip the content type on its way in.
/// </remarks>
internal static class NestedContentElementTypes
{
private const string _keyPrefix = "NestedContentElement:";

public static string GetTrackingKey(string contentTypeAlias)
=> $"{_keyPrefix}{contentTypeAlias.ToLowerInvariant()}";
}
Loading