From 03f363917337aa6d38e37ca999428f997461461b Mon Sep 17 00:00:00 2001 From: bkapustik <82807109+bkapustik@users.noreply.github.com> Date: Wed, 6 Mar 2024 13:50:13 +0100 Subject: [PATCH 1/3] feat(Algolia): application can be turned off by not including it in the program and does not crash if the nugget is referenced --- Directory.Build.props | 2 +- .../Admin/AlgoliaAdminModule.cs | 11 ++++- .../AlgoliaSearchModule.cs | 17 ++++++- .../AlgoliaStartupExtensions.cs | 44 +++++++++++++------ .../Indexing/AlgoliaOptions.cs | 9 ++++ 5 files changed, 66 insertions(+), 17 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 16b0953..2e55871 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -5,7 +5,7 @@ $(Company) Copyright © $(Company) $([System.DateTime]::Now.Year) $(Company)™ - 3.0.0 + 3.0.1 MIT diff --git a/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs b/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs index c2a16d0..ddf2c1e 100644 --- a/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs +++ b/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs @@ -5,6 +5,7 @@ using Kentico.Xperience.Algolia.Admin; using Kentico.Xperience.Algolia.Indexing; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; [assembly: RegisterModule(typeof(AlgoliaAdminModule))] @@ -23,10 +24,18 @@ public AlgoliaAdminModule() : base(nameof(AlgoliaAdminModule)) { } protected override void OnInit(ModuleInitParameters parameters) { base.OnInit(parameters); - RegisterClientModule("kentico", "xperience-integrations-algolia"); var services = parameters.Services; + var options = services.GetRequiredService>(); + + if (!options.Value.IsConfigured) + { + return; + } + + RegisterClientModule("kentico", "xperience-integrations-algolia"); + installer = services.GetRequiredService(); storageService = services.GetRequiredService(); diff --git a/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs b/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs index 15c9ea9..6f3ad99 100644 --- a/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs +++ b/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs @@ -8,6 +8,7 @@ using Kentico.Xperience.Algolia.Admin; using Kentico.Xperience.Algolia.Indexing; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; [assembly: RegisterModule(typeof(AlgoliaSearchModule))] @@ -37,11 +38,17 @@ protected override void OnInit(ModuleInitParameters parameters) var services = parameters.Services; + var options = services.GetRequiredService>(); + + if (!options.Value.IsConfigured) + { + return; + } + algoliaTaskLogger = services.GetRequiredService(); appSettingsService = services.GetRequiredService(); conversionService = services.GetRequiredService(); - WebPageEvents.Publish.Execute += HandleEvent; WebPageEvents.Delete.Execute += HandleEvent; ContentItemEvents.Publish.Execute += HandleContentItemEvent; @@ -102,7 +109,15 @@ private void HandleContentItemEvent(object? sender, CMSEventArgs e) public static void AddRegisteredIndices() { + var options = Service.Resolve>(); + + if (!options.Value.IsConfigured) + { + return; + } + var configurationStorageService = Service.Resolve(); + var indices = configurationStorageService.GetAllIndexData(); AlgoliaIndexStore.Instance.SetIndicies(indices); diff --git a/src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs b/src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs index 678dde7..d827447 100644 --- a/src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs +++ b/src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs @@ -18,15 +18,31 @@ public static class AlgoliaStartupExtensions /// /// The service collection. /// The application configuration. - private static IServiceCollection AddKenticoAlgoliaInternal(this IServiceCollection services, IConfiguration configuration) => - services.AddSingleton() - .Configure(configuration.GetSection(AlgoliaOptions.CMS_ALGOLIA_SECTION_NAME)) - .AddSingleton(s => - { - var options = s.GetRequiredService>(); + private static IServiceCollection AddKenticoAlgoliaInternal(this IServiceCollection services, IConfiguration configuration) + { + var algoliaSection = configuration.GetSection(AlgoliaOptions.CMS_ALGOLIA_SECTION_NAME); + var algoliaOptions = algoliaSection.GetChildren(); - return new InsightsClient(options.Value.ApplicationId, options.Value.ApiKey); - }) + bool isConfigured = false; + + if (algoliaOptions.Single(x => x.Key == nameof(AlgoliaOptions.SearchKey)).Value != "" + && algoliaOptions.Single(x => x.Key == nameof(AlgoliaOptions.ApiKey)).Value != "" + && algoliaOptions.Single(x => x.Key == nameof(AlgoliaOptions.ApplicationId)).Value != "") + { + isConfigured = true; + } + + services + .Configure(algoliaSection) + .PostConfigure(options => options.IsConfigured = isConfigured); + + if (!isConfigured) + { + return services; + } + + return services + .AddSingleton() .AddSingleton(s => { var options = s.GetRequiredService>(); @@ -37,12 +53,12 @@ private static IServiceCollection AddKenticoAlgoliaInternal(this IServiceCollect return new SearchClient(configuration); }) - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); - + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + } /// /// Adds Algolia services and custom module to application with customized options provided by the /// in the action. diff --git a/src/Kentico.Xperience.Algolia/Indexing/AlgoliaOptions.cs b/src/Kentico.Xperience.Algolia/Indexing/AlgoliaOptions.cs index 7e6648b..c073c76 100644 --- a/src/Kentico.Xperience.Algolia/Indexing/AlgoliaOptions.cs +++ b/src/Kentico.Xperience.Algolia/Indexing/AlgoliaOptions.cs @@ -10,6 +10,15 @@ public sealed class AlgoliaOptions /// public const string CMS_ALGOLIA_SECTION_NAME = "CMSAlgoliaSearch"; + /// + /// /// Turn off functionality if application is not configured in the appsettings + /// + public bool IsConfigured + { + get; + set; + } = false; + /// /// Algolia application ID. /// From 86cec06edb51a9d828b15cbbd045e633dd6a27d8 Mon Sep 17 00:00:00 2001 From: bkapustik <82807109+bkapustik@users.noreply.github.com> Date: Wed, 6 Mar 2024 14:00:55 +0100 Subject: [PATCH 2/3] Algolia application module changed in the administration --- .../Admin/UIPages/AlgoliaApplication.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kentico.Xperience.Algolia/Admin/UIPages/AlgoliaApplication.cs b/src/Kentico.Xperience.Algolia/Admin/UIPages/AlgoliaApplication.cs index 4f1df56..112c27b 100644 --- a/src/Kentico.Xperience.Algolia/Admin/UIPages/AlgoliaApplication.cs +++ b/src/Kentico.Xperience.Algolia/Admin/UIPages/AlgoliaApplication.cs @@ -7,7 +7,7 @@ identifier: AlgoliaApplicationPage.IDENTIFIER, type: typeof(AlgoliaApplicationPage), slug: "algolia", - name: "Search", + name: "Algolia Search", category: BaseApplicationCategories.DEVELOPMENT, icon: Icons.Magnifier, templateName: TemplateNames.SECTION_LAYOUT)] From e3e90bef88a52daedb66f84ec9eed22c55ddd8d3 Mon Sep 17 00:00:00 2001 From: bkapustik <82807109+bkapustik@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:50:07 +0100 Subject: [PATCH 3/3] docs(Algolia): update required version of .net to .net 8 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 146dff1..b570dab 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,9 @@ This integration enables you to create [Algolia](https://www.algolia.com/) searc ## Dependencies -- [ASP.NET Core 6.0](https://dotnet.microsoft.com/en-us/download) +- [ASP.NET Core 8.0](https://dotnet.microsoft.com/en-us/download) - [Xperience by Kentico](https://docs.xperience.io/xp/changelog) - +- [Algolia](https://www.algolia.com/doc/) ## Package Installation Add the package to your application using the .NET CLI