Skip to content

Commit

Permalink
Merge pull request #158 from piotrkrajewskicn/feature/custom-record-v…
Browse files Browse the repository at this point in the history
…alues

Possibility to add custom record values in Algolia index
  • Loading branch information
acoumb authored Feb 7, 2024
2 parents 8a385e0 + 2ea3f04 commit 4493e50
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Integrations.Search.Algolia;
using Umbraco.Cms.Integrations.Search.Algolia.Builders;
using Umbraco.Cms.Integrations.Search.Algolia.Configuration;
using Umbraco.Cms.Integrations.Search.Algolia.Extensions;
using Umbraco.Cms.Integrations.Search.Algolia.Handlers;
Expand All @@ -32,6 +33,8 @@ public void Compose(IUmbracoBuilder builder)

builder.Services.AddScoped<IAlgoliaIndexDefinitionStorage<AlgoliaIndex>, AlgoliaIndexDefinitionStorage>();

builder.Services.AddScoped<IRecordBuilderFactory, RecordBuilderFactory>();

builder.Services.AddScoped<IAlgoliaSearchPropertyIndexValueFactory, AlgoliaSearchPropertyIndexValueFactory>();

builder.AddAlgoliaConverters();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Integrations.Search.Algolia.Models;
Expand All @@ -10,21 +9,25 @@ namespace Umbraco.Cms.Integrations.Search.Algolia.Builders
{
public class ContentRecordBuilder
{
private readonly Record _record = new();
private Record _record = new();

private readonly IUserService _userService;

private readonly IPublishedUrlProvider _urlProvider;

private readonly IAlgoliaSearchPropertyIndexValueFactory _algoliaSearchPropertyIndexValueFactory;

public ContentRecordBuilder(IUserService userService, IPublishedUrlProvider urlProvider, IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory)
private readonly IRecordBuilderFactory _recordBuilderFactory;

public ContentRecordBuilder(IUserService userService, IPublishedUrlProvider urlProvider, IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory, IRecordBuilderFactory recordBuilderFactory)
{
_userService = userService;

_urlProvider = urlProvider;

_algoliaSearchPropertyIndexValueFactory = algoliaSearchPropertyIndexValueFactory;

_recordBuilderFactory = recordBuilderFactory;
}

public ContentRecordBuilder BuildFromContent(IContent content, Func<IProperty, bool> filter = null)
Expand All @@ -47,6 +50,7 @@ public ContentRecordBuilder BuildFromContent(IContent content, Func<IProperty, b
_record.Path = content.Path;
_record.ContentTypeAlias = content.ContentType.Alias;
_record.Url = _urlProvider.GetUrl(content.Id);
_record.Data = new();

if (content.PublishedCultures.Count() > 0)
{
Expand Down Expand Up @@ -79,9 +83,28 @@ public ContentRecordBuilder BuildFromContent(IContent content, Func<IProperty, b
}
}

AddCustomValues(_record, content);

return this;
}

public Record Build() => _record;

protected void SetRecord(Record record)
{
_record = record;
}

protected virtual ContentRecordBuilder AddCustomValues(Record record, IContent content)
{
var recordBuilderService = _recordBuilderFactory.GetRecordBuilderService(content);
if (recordBuilderService == null)
{
return this;
}

_record = recordBuilderService.GetRecord(content, record);
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Integrations.Search.Algolia.Models;

namespace Umbraco.Cms.Integrations.Search.Algolia.Builders
{
public interface IRecordBuilder
{
Record GetRecord(IContent content, Record record);
}

public interface IRecordBuilder<in TContentType>
: IRecordBuilder where TContentType : PublishedContentModel
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Umbraco.Cms.Core.Models;

namespace Umbraco.Cms.Integrations.Search.Algolia.Builders
{
public interface IRecordBuilderFactory
{
IRecordBuilder GetRecordBuilderService(IContent content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Web;

namespace Umbraco.Cms.Integrations.Search.Algolia.Builders
{
public class RecordBuilderFactory : IRecordBuilderFactory
{
private readonly IServiceProvider _serviceProvider;

private readonly IUmbracoContextFactory _umbracoContextFactory;

private readonly IDictionary<string, Type> _recordBuilderCache = new Dictionary<string, Type>();

public RecordBuilderFactory(IServiceProvider serviceProvider, IUmbracoContextFactory umbracoContextFactory)
{
_serviceProvider = serviceProvider;
_umbracoContextFactory = umbracoContextFactory;
}

public IRecordBuilder GetRecordBuilderService(IContent content)
{
using var context = _umbracoContextFactory.EnsureUmbracoContext();

var contentType = context.UmbracoContext.Content
?.GetById(true, content.Id)
?.GetType();

if (contentType == null)
{
return null;
}
var serviceType = GetRecordBuilderType(contentType);

return _serviceProvider.GetService(serviceType) as IRecordBuilder;
}

private Type GetRecordBuilderType(Type contentType)
{
var dictionaryKey = contentType.FullName;
if (_recordBuilderCache.ContainsKey(dictionaryKey))
{
return _recordBuilderCache[dictionaryKey];
}

var type = typeof(IRecordBuilder<>).MakeGenericType(contentType);
_recordBuilderCache[dictionaryKey] = type;
return type;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public class SearchController : UmbracoAuthorizedApiController

private readonly IAlgoliaIndexDefinitionStorage<AlgoliaIndex> _indexStorage;

private readonly UmbracoHelper _umbracoHelper;

private readonly IUserService _userService;

private readonly IPublishedUrlProvider _urlProvider;
Expand All @@ -43,26 +41,26 @@ public class SearchController : UmbracoAuthorizedApiController

private readonly ILogger<SearchController> _logger;

private readonly IRecordBuilderFactory _recordBuilderFactory;

public SearchController(
IAlgoliaIndexService indexService,
IAlgoliaSearchService<SearchResponse<Record>> searchService,
IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage,
UmbracoHelper umbracoHelper,
IUserService userService,
IPublishedUrlProvider urlProvider,
IContentService contentService,
IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory,
IUmbracoContextFactory umbracoContextFactory,
ILogger<SearchController> logger)
ILogger<SearchController> logger,
IRecordBuilderFactory recordBuilderFactory)
{
_indexService = indexService;

_searchService = searchService;

_indexStorage = indexStorage;

_umbracoHelper = umbracoHelper;

_userService = userService;

_urlProvider = urlProvider;
Expand All @@ -74,6 +72,8 @@ public SearchController(
_umbracoContextFactory = umbracoContextFactory;

_logger = logger;

_recordBuilderFactory = recordBuilderFactory;
}

[HttpGet]
Expand Down Expand Up @@ -136,7 +136,7 @@ public async Task<IActionResult> BuildIndex([FromBody] IndexConfiguration indexC

foreach (var contentItem in contentItems.Where(p => !p.Trashed))
{
var record = new ContentRecordBuilder(_userService, _urlProvider, _algoliaSearchPropertyIndexValueFactory)
var record = new ContentRecordBuilder(_userService, _urlProvider, _algoliaSearchPropertyIndexValueFactory, _recordBuilderFactory)
.BuildFromContent(contentItem, (p) => contentDataItem.Properties.Any(q => q.Alias == p.Alias))
.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text.Json;
using Umbraco.Cms.Integrations.Search.Algolia.Models;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Core.Web;

namespace Umbraco.Cms.Integrations.Search.Algolia.Handlers
{
Expand All @@ -34,6 +35,8 @@ public class AlgoliaContentCacheRefresherHandler : INotificationAsyncHandler<Con

private readonly IAlgoliaSearchPropertyIndexValueFactory _algoliaSearchPropertyIndexValueFactory;

private readonly IRecordBuilderFactory _recordBuilderFactory;

public AlgoliaContentCacheRefresherHandler(
IServerRoleAccessor serverRoleAccessor,
ILogger<AlgoliaContentCacheRefresherHandler> logger,
Expand All @@ -42,7 +45,8 @@ public AlgoliaContentCacheRefresherHandler(
IAlgoliaIndexService indexService,
IUserService userService,
IPublishedUrlProvider urlProvider,
IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory)
IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory,
IRecordBuilderFactory recordBuilderFactory)
{
_serverRoleAccessor = serverRoleAccessor;
_contentService = contentService;
Expand All @@ -52,6 +56,7 @@ public AlgoliaContentCacheRefresherHandler(
_userService = userService;
_urlProvider = urlProvider;
_algoliaSearchPropertyIndexValueFactory = algoliaSearchPropertyIndexValueFactory;
_recordBuilderFactory = recordBuilderFactory;
}

public async Task HandleAsync(ContentCacheRefresherNotification notification, CancellationToken cancellationToken)
Expand Down Expand Up @@ -98,7 +103,7 @@ protected async Task RebuildIndex(IEnumerable<IContent> entities)
.FirstOrDefault(p => p.ContentType.Alias == entity.ContentType.Alias);
if (indexConfiguration == null || indexConfiguration.ContentType.Alias != entity.ContentType.Alias) continue;

var record = new ContentRecordBuilder(_userService, _urlProvider, _algoliaSearchPropertyIndexValueFactory)
var record = new ContentRecordBuilder(_userService, _urlProvider, _algoliaSearchPropertyIndexValueFactory, _recordBuilderFactory)
.BuildFromContent(entity, (p) => indexConfiguration.Properties.Any(q => q.Alias == p.Alias))
.Build();

Expand Down
21 changes: 18 additions & 3 deletions src/Umbraco.Cms.Integrations.Search.Algolia/Models/Record.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json.Serialization;

namespace Umbraco.Cms.Integrations.Search.Algolia.Models
namespace Umbraco.Cms.Integrations.Search.Algolia.Models
{
public class Record
{
Expand All @@ -9,6 +7,23 @@ public Record()
Data = new Dictionary<string, object>();
}

public Record(Record record)
{
ContentTypeAlias = record.ContentTypeAlias;
ObjectID = record.ObjectID;
Id = record.Id;
Name = record.Name;
CreateDate = record.CreateDate;
CreatorName = record.CreatorName;
UpdateDate = record.UpdateDate;
WriterName = record.WriterName;
TemplateId = record.TemplateId;
Level = record.Level;
Path = record.Path;
Url = record.Url;
Data = record.Data;
}

public string ObjectID { get; set; }

public int Id { get; set; }
Expand Down

0 comments on commit 4493e50

Please sign in to comment.