Skip to content

Commit 8fda2b5

Browse files
committed
PR updates
1 parent 3f86cdf commit 8fda2b5

File tree

2 files changed

+66
-99
lines changed

2 files changed

+66
-99
lines changed

src/Umbraco.Cms.Integrations.Search.Algolia/Handlers/AlgoliaContentCacheRefresherHandler.cs

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,50 @@
88
using Umbraco.Cms.Core.Routing;
99
using Umbraco.Cms.Integrations.Search.Algolia.Migrations;
1010
using Umbraco.Cms.Integrations.Search.Algolia.Services;
11-
using Umbraco.Cms.Core.Models.PublishedContent;
11+
using Umbraco.Cms.Core.Models;
12+
using Umbraco.Cms.Integrations.Search.Algolia.Builders;
13+
using System.Text.Json;
14+
using Umbraco.Cms.Integrations.Search.Algolia.Models;
15+
using Umbraco.Cms.Core.Sync;
1216

1317
namespace Umbraco.Cms.Integrations.Search.Algolia.Handlers
1418
{
15-
public class AlgoliaContentCacheRefresherHandler : BaseContentHandler, INotificationAsyncHandler<ContentCacheRefresherNotification>
19+
public class AlgoliaContentCacheRefresherHandler : INotificationAsyncHandler<ContentCacheRefresherNotification>
1620
{
21+
private readonly IServerRoleAccessor _serverRoleAccessor;
22+
1723
private readonly IContentService _contentService;
1824

25+
private readonly ILogger _logger;
26+
27+
private readonly IAlgoliaIndexDefinitionStorage<AlgoliaIndex> _indexStorage;
28+
29+
private readonly IAlgoliaIndexService _indexService;
30+
31+
private readonly IUserService _userService;
32+
33+
private readonly IPublishedUrlProvider _urlProvider;
34+
35+
private readonly IAlgoliaSearchPropertyIndexValueFactory _algoliaSearchPropertyIndexValueFactory;
36+
1937
public AlgoliaContentCacheRefresherHandler(
38+
IServerRoleAccessor serverRoleAccessor,
2039
ILogger<AlgoliaContentCacheRefresherHandler> logger,
2140
IContentService contentService,
2241
IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage,
2342
IAlgoliaIndexService indexService,
2443
IUserService userService,
2544
IPublishedUrlProvider urlProvider,
2645
IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory)
27-
: base(logger, indexStorage, indexService, userService, urlProvider, algoliaSearchPropertyIndexValueFactory)
2846
{
47+
_serverRoleAccessor = serverRoleAccessor;
2948
_contentService = contentService;
49+
_logger = logger;
50+
_indexStorage = indexStorage;
51+
_indexService = indexService;
52+
_userService = userService;
53+
_urlProvider = urlProvider;
54+
_algoliaSearchPropertyIndexValueFactory = algoliaSearchPropertyIndexValueFactory;
3055
}
3156

3257
public async Task HandleAsync(ContentCacheRefresherNotification notification, CancellationToken cancellationToken)
@@ -36,6 +61,20 @@ public async Task HandleAsync(ContentCacheRefresherNotification notification, Ca
3661
return;
3762
}
3863

64+
switch (_serverRoleAccessor.CurrentServerRole)
65+
{
66+
case ServerRole.Subscriber:
67+
_logger.LogDebug("Umbraco Forms scheduled record deletion task will not run on subscriber servers.");
68+
return;
69+
case ServerRole.Unknown:
70+
_logger.LogDebug("Umbraco Forms scheduled record deletion task will not run on servers with unknown role.");
71+
return;
72+
case ServerRole.Single:
73+
case ServerRole.SchedulingPublisher:
74+
default:
75+
break;
76+
}
77+
3978
var refreshedContent = _contentService
4079
.GetByIds(
4180
payloads
@@ -45,26 +84,36 @@ public async Task HandleAsync(ContentCacheRefresherNotification notification, Ca
4584
await RebuildIndex(refreshedContent);
4685
}
4786

48-
public void Handle(ContentCacheRefresherNotification notification)
87+
protected async Task RebuildIndex(IEnumerable<IContent> entities)
4988
{
50-
if (notification.MessageObject is not ContentCacheRefresher.JsonPayload[] payloads)
89+
try
5190
{
52-
return;
53-
}
91+
var indices = _indexStorage.Get();
5492

55-
foreach (ContentCacheRefresher.JsonPayload payload in payloads)
56-
{
57-
if (payload.ChangeTypes != TreeChangeTypes.RefreshNode
58-
&& payload.ChangeTypes != TreeChangeTypes.RefreshBranch)
93+
foreach (var entity in entities)
5994
{
60-
return;
61-
}
95+
foreach (var index in indices)
96+
{
97+
var indexConfiguration = JsonSerializer.Deserialize<List<ContentData>>(index.SerializedData)
98+
.FirstOrDefault(p => p.ContentType.Alias == entity.ContentType.Alias);
99+
if (indexConfiguration == null || indexConfiguration.ContentType.Alias != entity.ContentType.Alias) continue;
100+
101+
var record = new ContentRecordBuilder(_userService, _urlProvider, _algoliaSearchPropertyIndexValueFactory)
102+
.BuildFromContent(entity, (p) => indexConfiguration.Properties.Any(q => q.Alias == p.Alias))
103+
.Build();
62104

63-
var x = _contentService.GetById(1205);
64-
var y = x.Published;
105+
var result = entity.Trashed || !entity.Published
106+
? await _indexService.DeleteData(index.Name, entity.Key.ToString())
107+
: await _indexService.UpdateData(index.Name, record);
65108

66-
// You can do stuff with the ID of the refreshed content, for instance getting it from the content service.
67-
var refeshedContent = _contentService.GetById(payload.Id);
109+
if (result.Failure)
110+
_logger.LogError($"Failed to update data for Algolia index: {result}");
111+
}
112+
}
113+
}
114+
catch (Exception ex)
115+
{
116+
_logger.LogError($"Failed to update data for Algolia index: {ex.Message}");
68117
}
69118
}
70119
}

src/Umbraco.Cms.Integrations.Search.Algolia/Handlers/BaseContentHandler.cs

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)