8
8
using Umbraco . Cms . Core . Routing ;
9
9
using Umbraco . Cms . Integrations . Search . Algolia . Migrations ;
10
10
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 ;
12
16
13
17
namespace Umbraco . Cms . Integrations . Search . Algolia . Handlers
14
18
{
15
- public class AlgoliaContentCacheRefresherHandler : BaseContentHandler , INotificationAsyncHandler < ContentCacheRefresherNotification >
19
+ public class AlgoliaContentCacheRefresherHandler : INotificationAsyncHandler < ContentCacheRefresherNotification >
16
20
{
21
+ private readonly IServerRoleAccessor _serverRoleAccessor ;
22
+
17
23
private readonly IContentService _contentService ;
18
24
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
+
19
37
public AlgoliaContentCacheRefresherHandler (
38
+ IServerRoleAccessor serverRoleAccessor ,
20
39
ILogger < AlgoliaContentCacheRefresherHandler > logger ,
21
40
IContentService contentService ,
22
41
IAlgoliaIndexDefinitionStorage < AlgoliaIndex > indexStorage ,
23
42
IAlgoliaIndexService indexService ,
24
43
IUserService userService ,
25
44
IPublishedUrlProvider urlProvider ,
26
45
IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory )
27
- : base ( logger , indexStorage , indexService , userService , urlProvider , algoliaSearchPropertyIndexValueFactory )
28
46
{
47
+ _serverRoleAccessor = serverRoleAccessor ;
29
48
_contentService = contentService ;
49
+ _logger = logger ;
50
+ _indexStorage = indexStorage ;
51
+ _indexService = indexService ;
52
+ _userService = userService ;
53
+ _urlProvider = urlProvider ;
54
+ _algoliaSearchPropertyIndexValueFactory = algoliaSearchPropertyIndexValueFactory ;
30
55
}
31
56
32
57
public async Task HandleAsync ( ContentCacheRefresherNotification notification , CancellationToken cancellationToken )
@@ -36,6 +61,20 @@ public async Task HandleAsync(ContentCacheRefresherNotification notification, Ca
36
61
return ;
37
62
}
38
63
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
+
39
78
var refreshedContent = _contentService
40
79
. GetByIds (
41
80
payloads
@@ -45,26 +84,36 @@ public async Task HandleAsync(ContentCacheRefresherNotification notification, Ca
45
84
await RebuildIndex ( refreshedContent ) ;
46
85
}
47
86
48
- public void Handle ( ContentCacheRefresherNotification notification )
87
+ protected async Task RebuildIndex ( IEnumerable < IContent > entities )
49
88
{
50
- if ( notification . MessageObject is not ContentCacheRefresher . JsonPayload [ ] payloads )
89
+ try
51
90
{
52
- return ;
53
- }
91
+ var indices = _indexStorage . Get ( ) ;
54
92
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 )
59
94
{
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 ( ) ;
62
104
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 ) ;
65
108
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 } ") ;
68
117
}
69
118
}
70
119
}
0 commit comments