Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V13: Delivery API composite id handler #15305

Merged
merged 5 commits into from
Nov 29, 2023
Merged
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
21 changes: 21 additions & 0 deletions src/Umbraco.Core/DeliveryApi/DeliveryApiCompositeIdHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Umbraco.Cms.Core.DeliveryApi;

public class DeliveryApiCompositeIdHandler : IDeliveryApiCompositeIdHandler
{
public string IndexId(int id, string culture) => $"{id}|{culture}";

public DeliveryApiIndexCompositeIdModel Decompose(string indexId)
{
var parts = indexId.Split(Constants.CharArrays.VerticalTab);
if (parts.Length == 2 && int.TryParse(parts[0], out var id))
{
return new DeliveryApiIndexCompositeIdModel
{
Id = id,
Culture = parts[1],
};
}

return new DeliveryApiIndexCompositeIdModel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Umbraco.Cms.Core.DeliveryApi;

public class DeliveryApiIndexCompositeIdModel
{
public int? Id { get; set; }

public string? Culture { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Umbraco.Cms.Core.DeliveryApi;

public interface IDeliveryApiCompositeIdHandler
{
string IndexId(int id, string culture);

DeliveryApiIndexCompositeIdModel Decompose(string indexId);
}
31 changes: 19 additions & 12 deletions src/Umbraco.Examine.Lucene/DeliveryApiContentIndex.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Examine;
using Examine.Lucene;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;
Expand All @@ -11,16 +14,30 @@ namespace Umbraco.Cms.Infrastructure.Examine;

public class DeliveryApiContentIndex : UmbracoExamineIndex
{
private readonly IDeliveryApiCompositeIdHandler _deliveryApiCompositeIdHandler;
private readonly ILogger<DeliveryApiContentIndex> _logger;

[Obsolete("Use the constructor that takes an IDeliveryApiCompositeIdHandler instead, scheduled for removal in v15")]
public DeliveryApiContentIndex(
ILoggerFactory loggerFactory,
string name,
IOptionsMonitor<LuceneDirectoryIndexOptions> indexOptions,
IHostingEnvironment hostingEnvironment,
IRuntimeState runtimeState)
: this(loggerFactory, name, indexOptions, hostingEnvironment, runtimeState, StaticServiceProvider.Instance.GetRequiredService<IDeliveryApiCompositeIdHandler>())
{
}

public DeliveryApiContentIndex(
ILoggerFactory loggerFactory,
string name,
IOptionsMonitor<LuceneDirectoryIndexOptions> indexOptions,
IHostingEnvironment hostingEnvironment,
IRuntimeState runtimeState,
IDeliveryApiCompositeIdHandler deliveryApiCompositeIdHandler)
: base(loggerFactory, name, indexOptions, hostingEnvironment, runtimeState)
{
_deliveryApiCompositeIdHandler = deliveryApiCompositeIdHandler;
PublishedValuesOnly = false;
EnableDefaultEventHandler = false;

Expand Down Expand Up @@ -108,18 +125,8 @@ protected override void PerformDeleteFromIndex(IEnumerable<string> itemIds, Acti

private (string? ContentId, string? Culture) ParseItemId(string id)
{
if (int.TryParse(id, out _))
{
return (id, null);
}

var parts = id.Split(Constants.CharArrays.VerticalTab);
if (parts.Length == 2 && int.TryParse(parts[0], out _))
{
return (parts[0], parts[1]);
}

return (null, null);
DeliveryApiIndexCompositeIdModel compositeIdModel = _deliveryApiCompositeIdHandler.Decompose(id);
return (compositeIdModel.Id.ToString() ?? id, compositeIdModel.Culture);
}

protected override void OnTransformingIndexValues(IndexingItemEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
Expand Down Expand Up @@ -57,6 +58,7 @@ public static IUmbracoBuilder AddExamine(this IUmbracoBuilder builder)
builder.Services.AddUnique<IDeliveryApiContentIndexHelper, DeliveryApiContentIndexHelper>();
builder.Services.AddSingleton<IDeliveryApiIndexingHandler, DeliveryApiIndexingHandler>();
builder.Services.AddSingleton<ExamineIndexRebuilder>();
builder.Services.AddUnique<IDeliveryApiCompositeIdHandler, DeliveryApiCompositeIdHandler>();

builder.AddNotificationHandler<ContentCacheRefresherNotification, ContentIndexingNotificationHandler>();
builder.AddNotificationHandler<PublicAccessCacheRefresherNotification, ContentIndexingNotificationHandler>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Examine;
using Examine.Search;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Changes;
Expand All @@ -18,19 +21,33 @@ internal sealed class DeliveryApiContentIndexHandleContentTypeChanges : Delivery
private readonly IDeliveryApiContentIndexValueSetBuilder _deliveryApiContentIndexValueSetBuilder;
private readonly IContentService _contentService;
private readonly IBackgroundTaskQueue _backgroundTaskQueue;
private readonly IDeliveryApiCompositeIdHandler _deliveryApiCompositeIdHandler;

[Obsolete("Use the constructor that takes an IDeliveryApiCompositeIdHandler instead, scheduled for removal in v15")]
public DeliveryApiContentIndexHandleContentTypeChanges(
IList<KeyValuePair<int, ContentTypeChangeTypes>> changes,
DeliveryApiIndexingHandler deliveryApiIndexingHandler,
IDeliveryApiContentIndexValueSetBuilder deliveryApiContentIndexValueSetBuilder,
IContentService contentService,
IBackgroundTaskQueue backgroundTaskQueue)
: this(changes, deliveryApiIndexingHandler, deliveryApiContentIndexValueSetBuilder, contentService, backgroundTaskQueue, StaticServiceProvider.Instance.GetRequiredService<IDeliveryApiCompositeIdHandler>())
{
}

public DeliveryApiContentIndexHandleContentTypeChanges(
IList<KeyValuePair<int, ContentTypeChangeTypes>> changes,
DeliveryApiIndexingHandler deliveryApiIndexingHandler,
IDeliveryApiContentIndexValueSetBuilder deliveryApiContentIndexValueSetBuilder,
IContentService contentService,
IBackgroundTaskQueue backgroundTaskQueue,
IDeliveryApiCompositeIdHandler deliveryApiCompositeIdHandler)
{
_changes = changes;
_deliveryApiIndexingHandler = deliveryApiIndexingHandler;
_deliveryApiContentIndexValueSetBuilder = deliveryApiContentIndexValueSetBuilder;
_contentService = contentService;
_backgroundTaskQueue = backgroundTaskQueue;
_deliveryApiCompositeIdHandler = deliveryApiCompositeIdHandler;
}

public void Execute() => _backgroundTaskQueue.QueueBackgroundWorkItem(_ =>
Expand Down Expand Up @@ -79,10 +96,13 @@ private void HandleUpdatedContentTypes(IEnumerable<int> updatedContentTypesIds,
var indexIdsByContentIds = indexIds
.Select(id =>
{
var parts = id.Split(Constants.CharArrays.VerticalTab);
return parts.Length == 2 && int.TryParse(parts[0], out var contentId)
? (ContentId: contentId, IndexId: id)
: throw new InvalidOperationException($"Delivery API identifier should be composite of ID and culture, got: {id}");
DeliveryApiIndexCompositeIdModel compositeIdModel = _deliveryApiCompositeIdHandler.Decompose(id);
if (compositeIdModel.Id is null)
{
throw new InvalidOperationException($"Delivery API identifier should be composite of ID and culture, got: {id}");
}

return (ContentId: compositeIdModel.Id.Value, IndexId: compositeIdModel.Culture!);
})
.GroupBy(tuple => tuple.ContentId)
.ToDictionary(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Examine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;
Expand All @@ -18,8 +20,10 @@ internal sealed class DeliveryApiContentIndexValueSetBuilder : IDeliveryApiConte
private readonly ILogger<DeliveryApiContentIndexValueSetBuilder> _logger;
private readonly IDeliveryApiContentIndexFieldDefinitionBuilder _deliveryApiContentIndexFieldDefinitionBuilder;
private readonly IMemberService _memberService;
private readonly IDeliveryApiCompositeIdHandler _deliveryApiCompositeIdHandler;
private DeliveryApiSettings _deliveryApiSettings;

[Obsolete("Please use ctor that takes an IDeliveryApiCompositeIdHandler. Scheduled for removal in v15")]
public DeliveryApiContentIndexValueSetBuilder(
ContentIndexHandlerCollection contentIndexHandlerCollection,
IContentService contentService,
Expand All @@ -28,12 +32,34 @@ public DeliveryApiContentIndexValueSetBuilder(
IDeliveryApiContentIndexFieldDefinitionBuilder deliveryApiContentIndexFieldDefinitionBuilder,
IOptionsMonitor<DeliveryApiSettings> deliveryApiSettings,
IMemberService memberService)
: this(
contentIndexHandlerCollection,
contentService,
publicAccessService,
logger,
deliveryApiContentIndexFieldDefinitionBuilder,
deliveryApiSettings,
memberService,
StaticServiceProvider.Instance.GetRequiredService<IDeliveryApiCompositeIdHandler>())
{
}

public DeliveryApiContentIndexValueSetBuilder(
ContentIndexHandlerCollection contentIndexHandlerCollection,
IContentService contentService,
IPublicAccessService publicAccessService,
ILogger<DeliveryApiContentIndexValueSetBuilder> logger,
IDeliveryApiContentIndexFieldDefinitionBuilder deliveryApiContentIndexFieldDefinitionBuilder,
IOptionsMonitor<DeliveryApiSettings> deliveryApiSettings,
IMemberService memberService,
IDeliveryApiCompositeIdHandler deliveryApiCompositeIdHandler)
{
_contentIndexHandlerCollection = contentIndexHandlerCollection;
_publicAccessService = publicAccessService;
_logger = logger;
_deliveryApiContentIndexFieldDefinitionBuilder = deliveryApiContentIndexFieldDefinitionBuilder;
_memberService = memberService;
_deliveryApiCompositeIdHandler = deliveryApiCompositeIdHandler;
_contentService = contentService;
_deliveryApiSettings = deliveryApiSettings.CurrentValue;
deliveryApiSettings.OnChange(settings => _deliveryApiSettings = settings);
Expand Down Expand Up @@ -73,7 +99,7 @@ public IEnumerable<ValueSet> GetValueSets(params IContent[] contents)

AddContentIndexHandlerFields(content, culture, fieldDefinitions, indexValues);

yield return new ValueSet(DeliveryApiContentIndexUtilites.IndexId(content, indexCulture), IndexTypes.Content, content.ContentType.Alias, indexValues);
yield return new ValueSet(_deliveryApiCompositeIdHandler.IndexId(content.Id, indexCulture), IndexTypes.Content, content.ContentType.Alias, indexValues);
}
}
}
Expand Down