Skip to content

Commit

Permalink
V12/history (#546)
Browse files Browse the repository at this point in the history
* add history project

* save history json to disc

* Returns history contents

* added historyinfo tables

* details in infinite editor and delet

* localisation stuff

* rename solution

---------

Co-authored-by: Kevin Jump <[email protected]>
  • Loading branch information
henryjump and KevinJump authored Sep 29, 2023
1 parent f7d84f5 commit 51e2873
Show file tree
Hide file tree
Showing 15 changed files with 508 additions and 1 deletion.
1 change: 1 addition & 0 deletions dist/build-package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dotnet pack ..\uSync.Core\uSync.Core.csproj --no-restore --no-build -c $env -o $
dotnet pack ..\uSync.Community.Contrib\uSync.Community.Contrib.csproj --no-build --no-restore -c $env -o $outFolder -p:Version=$fullVersion -p:ContinuousIntegrationBuild=true
dotnet pack ..\uSync.Community.DataTypeSerializers\uSync.Community.DataTypeSerializers.csproj --no-build --no-restore -c $env -o $outFolder -p:Version=$fullVersion -p:ContinuousIntegrationBuild=true
dotnet pack ..\uSync.BackOffice\uSync.BackOffice.csproj --no-build --no-restore -c $env -o $outFolder -p:Version=$fullVersion -p:ContinuousIntegrationBuild=true
dotnet pack ..\uSync.History\uSync.History.csproj --no-build --no-restore -c $env -o $outFolder -p:Version=$fullVersion -p:ContinuousIntegrationBuild=true

dotnet pack ..\uSync.AutoTemplates\uSync.AutoTemplates.csproj --no-build --no-restore -c $env -o $outFolder -p:Version=$fullVersion -p:ContinuousIntegrationBuild=true

Expand Down
71 changes: 71 additions & 0 deletions uSync.History/Controllers/uSyncHistoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Newtonsoft.Json;
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Delete;
using Umbraco.Cms.Web.BackOffice.Controllers;
using Umbraco.Cms.Web.Common.Attributes;
using uSync.BackOffice;
using uSync.BackOffice.Configuration;
using uSync.BackOffice.Services;

namespace uSync.History.Controllers
{
[PluginController("uSync")]
public class uSyncHistoryController : UmbracoAuthorizedApiController
{
private readonly uSyncConfigService _configService;
private readonly SyncFileService _syncFileService;

public uSyncHistoryController(uSyncConfigService configService, SyncFileService syncFileService)
{
_configService = configService;
_syncFileService = syncFileService;
}

public bool GetApi() => true;

public IEnumerable<HistoryInfo> GetHistory()
{
var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder());
var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history"));
var files = _syncFileService.GetFiles(historyFolder, "*.json")
.Select(x => x.Substring(historyFolder.Length + 1));

var list = new List<HistoryInfo>();
foreach (var file in files)
{
list.Add(LoadHistory(file));
}

return list.OrderByDescending(x => x.Date);
}

public bool ClearHistory()
{
// 1. get history folder
var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder());
var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history"));
// 2. get history files
var files = _syncFileService.GetFiles(historyFolder, "*.json");
// 3. delet this
foreach (var file in files)
{
_syncFileService.DeleteFile(file);
}
// 4. truth
return true;
}

public HistoryInfo LoadHistory(string filePath)
{
var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder());
var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history"));
var fullPath = Path.Combine(historyFolder, filePath);
string contents = _syncFileService.LoadContent(fullPath);

var actions = JsonConvert.DeserializeObject<HistoryInfo>(contents);

actions.FilePath = filePath;

return actions;
}
}
}
21 changes: 21 additions & 0 deletions uSync.History/HistoryInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using uSync.BackOffice;

namespace uSync.History
{
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]

public class HistoryInfo
{
public IEnumerable<uSyncAction> Actions { get; set; }

public DateTime Date { get; set; }

public string Username { get; set; }

public string Method { get; set; }

public string FilePath { get; set; }
}
}
30 changes: 30 additions & 0 deletions uSync.History/uSync.History.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<StaticWebAssetBasePath>App_Plugins</StaticWebAssetBasePath>
</PropertyGroup>
<PropertyGroup>
<Product>uSync.History</Product>
<PackageId>uSync.History</PackageId>
<Title>uSync.History</Title>

<Authors>Kevin Jump and Henry Jump</Authors>
<Company>Jumoo</Company>

<PackageTags>umbraco usync</PackageTags>

<RepositoryUrl>https://github.com/KevinJump/uSync</RepositoryUrl>

<Description>Sync Umbraco to and from disk and between instances</Description>

<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>false</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\uSync.BackOffice\uSync.BackOffice.csproj" />
</ItemGroup>
</Project>
26 changes: 26 additions & 0 deletions uSync.History/uSyncHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using uSync.BackOffice.Models;

namespace uSync.History
{
public class uSyncHistory : ISyncAddOn
{
public string Name => "uSync History";

public string Version => "1.0";

public string Icon => "icon-calendar-alt";

public string View => "/App_Plugins/uSyncHistory/dashboard.html";

public string Alias => "uSyncHistory";

public string DisplayName => "History";

public int SortOrder => 20;
}
}
63 changes: 63 additions & 0 deletions uSync.History/uSyncHistoryComposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.AspNetCore.Routing;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Infrastructure.WebAssets;
using Umbraco.Extensions;
using uSync.BackOffice;
using uSync.BackOffice.Configuration;
using uSync.BackOffice.Controllers;
using uSync.BackOffice.Hubs;
using uSync.History.Controllers;

namespace uSync.History
{
public class uSyncHistoryComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.AddNotificationHandler<ServerVariablesParsingNotification, uSyncHistoryServerVariablesHandler>();

builder.AddNotificationHandler<uSyncImportCompletedNotification, uSyncHistoryNotificationHandler>();
builder.AddNotificationHandler<uSyncExportCompletedNotification, uSyncHistoryNotificationHandler>();
// don't add if the filter is already there .
if (!builder.ManifestFilters().Has<uSyncHistoryManifestFilter>())
{
// add the package manifest programatically.
builder.ManifestFilters().Append<uSyncHistoryManifestFilter>();
}
}
}

internal class uSyncHistoryServerVariablesHandler : INotificationHandler<ServerVariablesParsingNotification>
{
private readonly uSyncConfigService _uSyncConfig;
private readonly LinkGenerator _linkGenerator;
private readonly uSyncHubRoutes _uSyncHubRoutes;
private readonly IBackOfficeSecurityAccessor _securityAccessor;

/// <inheritdoc cref="INotificationHandler{TNotification}" />
public uSyncHistoryServerVariablesHandler(LinkGenerator linkGenerator,
uSyncConfigService uSyncConfigService,
uSyncHubRoutes hubRoutes,
IBackOfficeSecurityAccessor securityAccessor)
{
_linkGenerator = linkGenerator;
_uSyncConfig = uSyncConfigService;
_uSyncHubRoutes = hubRoutes;
_securityAccessor = securityAccessor;
}


/// <inheritdoc/>
public void Handle(ServerVariablesParsingNotification notification)
{
notification.ServerVariables.Add("uSyncHistory", new Dictionary<string, object>
{
{ "Service", _linkGenerator.GetUmbracoApiServiceBaseUrl<uSyncHistoryController>(controller => controller.GetApi()) },
});
}
}
}
23 changes: 23 additions & 0 deletions uSync.History/uSyncHistoryManifestFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Umbraco.Cms.Core.Manifest;

namespace uSync.History
{
internal class uSyncHistoryManifestFilter : IManifestFilter
{
public void Filter(List<PackageManifest> manifests)
{
manifests.Add(new PackageManifest
{
PackageId = "uSyncHistory",
PackageName = "uSync History",
Version = "1.0",
AllowPackageTelemetry = true,
BundleOptions = BundleOptions.Independent,
Scripts = new[]
{
"/App_Plugins/uSyncHistory/dashboard.controller.js"
}
});
}
}
}
69 changes: 69 additions & 0 deletions uSync.History/uSyncHistoryNotificationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Newtonsoft.Json;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Security;
using uSync.BackOffice;
using uSync.BackOffice.Configuration;
using uSync.BackOffice.Services;

namespace uSync.History
{
internal class uSyncHistoryNotificationHandler
: INotificationHandler<uSyncImportCompletedNotification>,
INotificationHandler<uSyncExportCompletedNotification>
{
private readonly uSyncConfigService _configService;
private readonly SyncFileService _syncFileService;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;

public uSyncHistoryNotificationHandler(uSyncConfigService configService, SyncFileService syncFileService, IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
{
_configService = configService;
_syncFileService = syncFileService;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
}

public void Handle(uSyncImportCompletedNotification notification)
{
var changeActions = notification.Actions
.Where(x => x.Change > Core.ChangeType.NoChange && x.Change < Core.ChangeType.Hidden)
.ToList();

if (changeActions.Any())
{
SaveActions(changeActions, "Import");
}
}

public void Handle(uSyncExportCompletedNotification notification)
{
var changeActions = notification.Actions
.Where(x => x.Change > Core.ChangeType.NoChange && x.Change < Core.ChangeType.Hidden)
.ToList();

if (changeActions.Any())
{
SaveActions(changeActions, "Export");
}
}

private void SaveActions(IEnumerable<uSyncAction> actions, string method)
{
var historyInfo = new HistoryInfo
{
Actions = actions,
Date = DateTime.Now,
Username = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser?.Username ?? "Background Process",
Method = method
};

var historyJson = JsonConvert.SerializeObject(historyInfo, Formatting.Indented);

var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder());
var historyFile = Path.Combine(rootFolder, "..", "history", DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".json");

_syncFileService.CreateFoldersForFile(historyFile);

_syncFileService.SaveFile(historyFile, historyJson);
}
}
}
14 changes: 14 additions & 0 deletions uSync.History/wwwroot/uSyncHistory/Lang/en-us.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<language alias="en" intName="English (US)" localName="English (US)" lcid="" culture="en-US">
<creator>
<name>Jumoo</name>
<link>http://jumoo.uk</link>
</creator>
<area alias="uSyncHistory">
<key alias="title">History</key>
<key alias="description">Things you have done.</key>
<key alias="empty">u have not Synced anything.</key>
<key alias="clearTitle">Are you sure?</key>
<key alias="clearMessage">All history files will be permanently deleted.</key>
</area>
</language>
Loading

0 comments on commit 51e2873

Please sign in to comment.