diff --git a/src/AlgoliaSearchModule.cs b/src/AlgoliaSearchModule.cs index 36ffbf1..c4f6726 100644 --- a/src/AlgoliaSearchModule.cs +++ b/src/AlgoliaSearchModule.cs @@ -7,7 +7,7 @@ using CMS.DataEngine; using CMS.DocumentEngine; using CMS.Helpers; - +using DocumentFormat.OpenXml.Wordprocessing; using Kentico.Xperience.Algolia.Extensions; using Kentico.Xperience.Algolia.Services; @@ -60,7 +60,8 @@ protected override void OnInit() DocumentEvents.Delete.Before += HandleDocumentEvent; DocumentEvents.Update.Before += HandleDocumentEvent; - DocumentEvents.Update.Before += ForceMoveUpdate; + DocumentEvents.Update.After += ForceMoveUpdate; + DocumentEvents.Delete.After += ForceMoveUpdate; DocumentEvents.Insert.After += HandleDocumentEvent; WorkflowEvents.Publish.After += HandleWorkflowEvent; WorkflowEvents.Archive.After += HandleWorkflowEvent; @@ -108,15 +109,15 @@ private void HandleDocumentEvent(object sender, DocumentEventArgs e) private void ForceMoveUpdate(object sender, DocumentEventArgs e) { - var columns = e.Node.ChangedColumns(); - if (columns.Any(col => col.Equals(nameof(TreeNode.NodeParentID), System.StringComparison.OrdinalIgnoreCase))) + if (!EventShouldContinue(e.Node)) { - algoliaTaskLogger.HandleEvent(e.Node, WorkflowEvents.Publish.Name); + return; + } - foreach (var child in e.Node.AllChildren) - { - algoliaTaskLogger.HandleEvent(child, WorkflowEvents.Publish.Name); - } + var columns = e.Node.ChangedColumns(); + if (columns.Any(x => x.Equals(nameof(TreeNode.NodeParentID), System.StringComparison.OrdinalIgnoreCase))) + { + algoliaTaskLogger.HandleEventAfter(e.Node, e.CurrentHandler.Name); } } } diff --git a/src/Services/IAlgoliaTaskLogger.cs b/src/Services/IAlgoliaTaskLogger.cs index 3be8d35..2847993 100644 --- a/src/Services/IAlgoliaTaskLogger.cs +++ b/src/Services/IAlgoliaTaskLogger.cs @@ -17,5 +17,6 @@ public interface IAlgoliaTaskLogger /// The that triggered the event. /// The name of the Xperience event that was triggered. void HandleEvent(TreeNode node, string eventName); + void HandleEventAfter(TreeNode node, string eventName); } } diff --git a/src/Services/Implementations/DefaultAlgoliaTaskLogger.cs b/src/Services/Implementations/DefaultAlgoliaTaskLogger.cs index b453b9e..be03c39 100644 --- a/src/Services/Implementations/DefaultAlgoliaTaskLogger.cs +++ b/src/Services/Implementations/DefaultAlgoliaTaskLogger.cs @@ -51,6 +51,27 @@ public void HandleEvent(TreeNode node, string eventName) } } + public void HandleEventAfter(TreeNode node, string eventName) + { + foreach (var indexName in IndexStore.Instance.GetAll().Select(index => index.IndexName)) + { + if (!node.IsIndexedByIndex(indexName)) + { + continue; + } + + try + { + var queueItem = new AlgoliaQueueItem(node, GetTaskType(node, eventName), indexName, node.ChangedColumns()); + AlgoliaQueueWorker.Current.EnqueueAlgoliaQueueItem(queueItem); + } + catch (InvalidOperationException ex) + { + eventLogService.LogException(nameof(DefaultAlgoliaTaskLogger), nameof(HandleEvent), ex); + } + } + } + private AlgoliaTaskType GetTaskType(TreeNode node, string eventName) { if (eventName.Equals(DocumentEvents.Insert.Name, StringComparison.OrdinalIgnoreCase) ||