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

feat(Algolia): application can be turned off by not including it in t… #36

Merged
merged 3 commits into from
Mar 18, 2024
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Authors>$(Company)</Authors>
<Copyright>Copyright © $(Company) $([System.DateTime]::Now.Year)</Copyright>
<Trademark>$(Company)™</Trademark>
<VersionPrefix>3.0.0</VersionPrefix>
<VersionPrefix>3.0.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<PackageLicenseExpression>MIT</PackageLicenseExpression>

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]

Expand All @@ -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<IOptions<AlgoliaOptions>>();

if (!options.Value.IsConfigured)
{
return;
}

RegisterClientModule("kentico", "xperience-integrations-algolia");

installer = services.GetRequiredService<AlgoliaModuleInstaller>();
storageService = services.GetRequiredService<IAlgoliaConfigurationStorageService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
17 changes: 16 additions & 1 deletion src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]

Expand Down Expand Up @@ -37,11 +38,17 @@ protected override void OnInit(ModuleInitParameters parameters)

var services = parameters.Services;

var options = services.GetRequiredService<IOptions<AlgoliaOptions>>();

if (!options.Value.IsConfigured)
{
return;
}

algoliaTaskLogger = services.GetRequiredService<IAlgoliaTaskLogger>();
appSettingsService = services.GetRequiredService<IAppSettingsService>();
conversionService = services.GetRequiredService<IConversionService>();


WebPageEvents.Publish.Execute += HandleEvent;
WebPageEvents.Delete.Execute += HandleEvent;
ContentItemEvents.Publish.Execute += HandleContentItemEvent;
Expand Down Expand Up @@ -102,7 +109,15 @@ private void HandleContentItemEvent(object? sender, CMSEventArgs e)

public static void AddRegisteredIndices()
{
var options = Service.Resolve<IOptions<AlgoliaOptions>>();

if (!options.Value.IsConfigured)
{
return;
}

var configurationStorageService = Service.Resolve<IAlgoliaConfigurationStorageService>();

var indices = configurationStorageService.GetAllIndexData();

AlgoliaIndexStore.Instance.SetIndicies(indices);
Expand Down
44 changes: 30 additions & 14 deletions src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,31 @@ public static class AlgoliaStartupExtensions
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configuration">The application configuration.</param>
private static IServiceCollection AddKenticoAlgoliaInternal(this IServiceCollection services, IConfiguration configuration) =>
services.AddSingleton<AlgoliaModuleInstaller>()
.Configure<AlgoliaOptions>(configuration.GetSection(AlgoliaOptions.CMS_ALGOLIA_SECTION_NAME))
.AddSingleton<IInsightsClient>(s =>
{
var options = s.GetRequiredService<IOptions<AlgoliaOptions>>();
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<AlgoliaOptions>(algoliaSection)
.PostConfigure<AlgoliaOptions>(options => options.IsConfigured = isConfigured);

if (!isConfigured)
{
return services;
}

return services
.AddSingleton<AlgoliaModuleInstaller>()
.AddSingleton<ISearchClient>(s =>
{
var options = s.GetRequiredService<IOptions<AlgoliaOptions>>();
Expand All @@ -37,12 +53,12 @@ private static IServiceCollection AddKenticoAlgoliaInternal(this IServiceCollect

return new SearchClient(configuration);
})
.AddSingleton<IAlgoliaClient, DefaultAlgoliaClient>()
.AddSingleton<IAlgoliaTaskLogger, DefaultAlgoliaTaskLogger>()
.AddSingleton<IAlgoliaTaskProcessor, DefaultAlgoliaTaskProcessor>()
.AddSingleton<IAlgoliaConfigurationStorageService, DefaultAlgoliaConfigurationStorageService>()
.AddSingleton<IAlgoliaIndexService, DefaultAlgoliaIndexService>();

.AddSingleton<IAlgoliaClient, DefaultAlgoliaClient>()
.AddSingleton<IAlgoliaTaskLogger, DefaultAlgoliaTaskLogger>()
.AddSingleton<IAlgoliaTaskProcessor, DefaultAlgoliaTaskProcessor>()
.AddSingleton<IAlgoliaConfigurationStorageService, DefaultAlgoliaConfigurationStorageService>()
.AddSingleton<IAlgoliaIndexService, DefaultAlgoliaIndexService>();
}
/// <summary>
/// Adds Algolia services and custom module to application with customized options provided by the <see cref="IAlgoliaBuilder"/>
/// in the <paramref name="configure" /> action.
Expand Down
9 changes: 9 additions & 0 deletions src/Kentico.Xperience.Algolia/Indexing/AlgoliaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ public sealed class AlgoliaOptions
/// </summary>
public const string CMS_ALGOLIA_SECTION_NAME = "CMSAlgoliaSearch";

/// <summary>
/// /// Turn off functionality if application is not configured in the appsettings
/// </summary>
public bool IsConfigured
{
get;
set;
} = false;

/// <summary>
/// Algolia application ID.
/// </summary>
Expand Down
Loading