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

Added uSync Connector #38

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using HC.PageNotFoundManager.Notifications;
using HC.PageNotFoundManager.uSync.Handlers;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;

namespace HC.PageNotFoundManager.uSync.Composers
{
public class RegisterNotifications : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.AddNotificationHandler<OnConfigurationSavedNotification, PageNotFoundManagerHandler>();
}
}
}
22 changes: 22 additions & 0 deletions src/HC.PageNotFoundManager.uSync/Consts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace HC.PageNotFoundManager.uSync
{
internal static class Consts
{
public const string Group = "PageNotFoundManager";

public static class Configuration
{
public const string ItemType = "PageNotFoundManagerConfiguration";

public const string SerializerName = "PageNotFoundManager Configuration Serializer";

public const string HandlerName = "Configuration";

public const string SerializerFolder = "PageNotFoundManager";

public const string EntityType = "PageNotFoundManager-Configuration";

public const string FileName = "PageNotFoundManagerConfiguration";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Version>1.0.0</Version>
<InformationalVersion>1.0.0</InformationalVersion>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Copyright>2024 Nik Rimington</Copyright>
<Authors>Nik Rimington, Aaron Sadler</Authors>
<ContentTargetFolders>.</ContentTargetFolders>
<Product>HotChilli.Umbraco.PageNotFound.uSync</Product>
<PackageId>HotChilli.Umbraco.PageNotFound.uSync</PackageId>
<Title>HotChilli.Umbraco.PageNotFound.uSync</Title>
<Description>Umbraco packaged for setting 404 response pages.</Description>
<Product>HotChilli.Umbraco.PageNotFound</Product>
<PackageTags>umbraco plugin package Umbraco-Marketplace usync pagenotfound pagenotfoundmanager</PackageTags>
<PackageProjectUrl>https://github.com/NikRimington/HotChilli.Umbraco.PageNotFoundManager</PackageProjectUrl>
<PackageIcon>logo.png</PackageIcon>
<PackageIconUrl>https://raw.githubusercontent.com/NikRimington/HotChilli.Umbraco.PageNotFoundManager/main/docs/img/logo.png</PackageIconUrl>
<RepositoryUrl>https://github.com/NikRimington/HotChilli.Umbraco.PageNotFoundManager</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>ReadMe.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
<None Include="../../docs/img/logo.png" Pack="true" PackPath="\" />
<None Include="../../LICENSE" Pack="true" PackPath="" />
<None Include="../../ReadMe.md" Pack="true" PackPath="\" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Umbraco.Cms.Core" Version="[10.8.2, 11)" />
<PackageReference Include="uSync.Core" Version="[10.6.1, 11)" />
<PackageReference Include="uSync.BackOffice" Version="[10.6.1, 11)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Umbraco.Cms.Core" Version="[12.3.4, 13)" />
<PackageReference Include="uSync.Core" Version="[12.0.2, 13)" />
<PackageReference Include="uSync.BackOffice" Version="[12.0.2, 13)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Umbraco.Cms.Core" Version="[13.0.1, 14)" />
<PackageReference Include="uSync.Core" Version="[13.0.1, 14)" />
<PackageReference Include="uSync.BackOffice" Version="[13.0.1, 14)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HC.PageNotFoundManager\HC.PageNotFoundManager.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using HC.PageNotFoundManager.Notifications;
using HC.PageNotFoundManager.Services;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Strings;
using uSync.BackOffice;
using uSync.BackOffice.Configuration;
using uSync.BackOffice.Services;
using uSync.BackOffice.SyncHandlers;
using uSync.Core;
using PageNotFound = HC.PageNotFoundManager.Models.PageNotFound;

namespace HC.PageNotFoundManager.uSync.Handlers
{
[SyncHandler("pageNotFoundManagerHander", Consts.Configuration.HandlerName, Consts.Configuration.SerializerFolder, 1,
Icon = "icon-settings usync-addon-icon", EntityType = Consts.Configuration.EntityType)]
public class PageNotFoundManagerHandler : SyncHandlerRoot<IEnumerable<PageNotFound>, IEnumerable<PageNotFound>>, ISyncHandler,
INotificationHandler<OnConfigurationSavedNotification>
{
public override string Group => Consts.Group;

private readonly IDatabaseService databaseService;

public PageNotFoundManagerHandler(ILogger<SyncHandlerRoot<IEnumerable<PageNotFound>, IEnumerable<PageNotFound>>> logger, AppCaches appCaches, IShortStringHelper shortStringHelper, SyncFileService syncFileService, uSyncEventService mutexService, uSyncConfigService uSyncConfig, ISyncItemFactory itemFactory, IDatabaseService databaseService) : base(logger, appCaches, shortStringHelper, syncFileService, mutexService, uSyncConfig, itemFactory)
{
this.databaseService = databaseService;

itemContainerType = UmbracoObjectTypes.Unknown;
}

public override IEnumerable<uSyncAction> ExportAll(IEnumerable<PageNotFound> items, string folder, HandlerSettings config,
SyncUpdateCallback callback)
{
var item = databaseService.LoadFromDb();

var actions = new List<uSyncAction>();
if (item != null)
{
actions.AddRange(Export(item, Path.Combine(rootFolder, DefaultFolder), DefaultConfig));
}

return actions;
}

protected override IEnumerable<IEnumerable<PageNotFound>> GetChildItems(IEnumerable<PageNotFound> parent)
{
throw new NotImplementedException();
}

protected override IEnumerable<IEnumerable<PageNotFound>> GetFolders(IEnumerable<PageNotFound> parent)
{
throw new NotImplementedException();
}

public void Handle(OnConfigurationSavedNotification notification)
{
if (!ShouldProcess()) return;

try
{
if (notification.Configuration != null)
{
var attempts = Export(notification.Configuration, Path.Combine(rootFolder, DefaultFolder), DefaultConfig);
foreach (var attempt in attempts.Where(x => x.Success))
{
CleanUp(notification.Configuration, attempt.FileName, Path.Combine(rootFolder, DefaultFolder));
}
}
}
catch (Exception ex)
{
logger.LogWarning(ex, "uSync Save error");
}
}

protected override IEnumerable<uSyncAction> DeleteMissingItems(IEnumerable<PageNotFound> items, IEnumerable<Guid> keysToKeep, bool reportOnly)
=> Enumerable.Empty<uSyncAction>();

protected override IEnumerable<PageNotFound> GetFromService(IEnumerable<PageNotFound> items)
=> databaseService.LoadFromDb() ?? new List<PageNotFound>();

protected override string GetItemFileName(IEnumerable<PageNotFound> items)
=> Consts.Configuration.FileName;

protected override string GetItemName(IEnumerable<PageNotFound> item)
{
throw new NotImplementedException();
}

private bool ShouldProcess()
{
if (_mutexService.IsPaused) return false;
if (!DefaultConfig.Enabled) return false;
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Xml.Linq;
using HC.PageNotFoundManager.Models;
using HC.PageNotFoundManager.Services;
using Microsoft.Extensions.Logging;
using uSync.Core;
using uSync.Core.Models;
using uSync.Core.Serialization;

namespace HC.PageNotFoundManager.uSync.Serializers
{
[SyncSerializer("6ca9a472-98f3-42f1-82ad-d1a8cbde5fc5", Consts.Configuration.SerializerName,
Consts.Configuration.ItemType)]
public class PageNotFoundManagerSerializer : SyncSerializerRoot<IEnumerable<PageNotFound>>,
ISyncSerializer<IEnumerable<PageNotFound>>
{
private readonly IDatabaseService databaseService;

private readonly ICacheService cacheService;

public PageNotFoundManagerSerializer(ILogger<SyncSerializerRoot<IEnumerable<PageNotFound>>> logger, IDatabaseService databaseService, ICacheService cacheService) : base(logger)
{
this.databaseService = databaseService;
this.cacheService = cacheService;
}

protected override SyncAttempt<XElement> SerializeCore(IEnumerable<PageNotFound> item,
SyncSerializerOptions options)
{
var node = new XElement(ItemType,
new XAttribute("Alias", ItemAlias(item)),
new XAttribute("Key", ItemKey(item)));

node.Add(item.Select(item =>
new XElement("PageNotFound",
new XElement("ParentId", item.ParentId),
new XElement("NotFoundPageId", item.NotFoundPageId)
)
));

return SyncAttempt<XElement>.Succeed(Consts.Configuration.ItemType, node, typeof(IEnumerable<PageNotFound>), ChangeType.Export);
}

protected override SyncAttempt<IEnumerable<PageNotFound>> DeserializeCore(XElement node,
SyncSerializerOptions options)
{
var item = new List<PageNotFound>();

foreach (var element in node.Elements("PageNotFound"))
{
item.Add(new PageNotFound
{
ParentId = element.Element("ParentId").ValueOrDefault(Guid.Empty),
NotFoundPageId = element.Element("NotFoundPageId").ValueOrDefault(Guid.Empty)
});
}

return SyncAttempt<IEnumerable<PageNotFound>>.Succeed("Configuration", item, ChangeType.Import, Array.Empty<uSyncChange>());
}

public override IEnumerable<PageNotFound> FindItem(int id)
{
throw new NotImplementedException();
}

public override IEnumerable<PageNotFound> FindItem(Guid key) => databaseService.LoadFromDb() ?? new List<PageNotFound>();

public override IEnumerable<PageNotFound> FindItem(string alias)
{
throw new NotImplementedException();
}

public override void SaveItem(IEnumerable<PageNotFound> items)
{
foreach (var item in items)
{
databaseService.InsertToDb(item.ParentId, item.NotFoundPageId);
}

cacheService.RefreshCache();
}

public override void DeleteItem(IEnumerable<PageNotFound> item)
{
throw new NotImplementedException();
}

public override string ItemAlias(IEnumerable<PageNotFound> item) => "pageNotFoundManager";

public override Guid ItemKey(IEnumerable<PageNotFound> item) => Guid.Parse("cc346b54-54f8-46c0-9290-1214e9639a58");
}
}
8 changes: 6 additions & 2 deletions src/HC.PageNotFoundManager/Caching/CacheRefresher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using HC.PageNotFoundManager.Config;
using HC.PageNotFoundManager.Models;
using HC.PageNotFoundManager.Services;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
Expand All @@ -14,15 +15,18 @@ public class PageNotFoundCacheRefresher : PayloadCacheRefresherBase<CacheRefresh

private readonly IPageNotFoundConfig config;

private readonly ICacheService cacheService;

public PageNotFoundCacheRefresher(
AppCaches appCaches,
IPageNotFoundConfig config,
IJsonSerializer jsonSerializer,
IEventAggregator eventAggregator,
ICacheRefresherNotificationFactory cacheRefresherNotificationFactory)
ICacheRefresherNotificationFactory cacheRefresherNotificationFactory, ICacheService cacheService)
: base(appCaches, jsonSerializer, eventAggregator, cacheRefresherNotificationFactory)
{
this.config = config ?? throw new ArgumentNullException(nameof(config));
this.cacheService = cacheService;
}

public override string Name => "PageNotFoundManager Cache Refresher";
Expand All @@ -36,7 +40,7 @@ public override void Refresh(PageNotFoundRequest[] payloads)
config.SetNotFoundPage(payload.ParentId, payload.NotFoundPageId, false);
}

config.RefreshCache();
cacheService.RefreshCache();
}
}
}
16 changes: 16 additions & 0 deletions src/HC.PageNotFoundManager/Composers/RegisterServicesComposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using HC.PageNotFoundManager.Services;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;

namespace HC.PageNotFoundManager.Composers
{
public class RegisterServicesComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.AddTransient<IDatabaseService, DatabaseService>();
builder.Services.AddTransient<ICacheService, CacheService>();
}
}
}
Loading