Skip to content

Commit 28fe2de

Browse files
committed
Allow multiple subscription hooks with a single content item & helper method for triggering
1 parent eeccbda commit 28fe2de

File tree

5 files changed

+59
-34
lines changed

5 files changed

+59
-34
lines changed

src/Umbraco.Cms.Integrations.Automation.Zapier/Components/NewContentPublishedComponent.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
5-
5+
using Umbraco.Cms.Integrations.Automation.Zapier.Helpers;
66
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
77
using Umbraco.Core;
88
using Umbraco.Core.Composing;
@@ -47,24 +47,19 @@ public void Terminate()
4747

4848
private void ContentServiceOnPublished(IContentService sender, ContentPublishedEventArgs e)
4949
{
50+
var triggerHelper = new TriggerHelper(_zapierService);
51+
5052
foreach (var node in e.PublishedEntities)
5153
{
52-
if (_zapierSubscriptionHookService.TryGetByAlias(node.ContentType.Alias, out var zapContentConfig))
54+
if (_zapierSubscriptionHookService.TryGetByAlias(node.ContentType.Alias, out var zapContentConfigList))
5355
{
54-
var content = new Dictionary<string, string>
56+
foreach (var zapContentConfig in zapContentConfigList)
5557
{
56-
{Constants.Content.Id, node.Id.ToString()},
57-
{Constants.Content.Name, node.Name},
58-
{Constants.Content.PublishDate, DateTime.UtcNow.ToString()}
59-
};
60-
61-
var t = Task.Run(
62-
async () => await _zapierService.TriggerAsync(zapContentConfig.HookUrl, content));
63-
64-
var result = t.Result;
58+
var result = triggerHelper.Execute(zapContentConfig.HookUrl, node.Id.ToString(), node.Name);
6559

66-
if (!string.IsNullOrEmpty(result))
67-
_logger.Error<NewContentPublishedComponent>(result);
60+
if (!string.IsNullOrEmpty(result))
61+
_logger.Error<NewContentPublishedComponent>(result);
62+
}
6863
}
6964
}
7065
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Components/NewContentPublishedNotification.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using Umbraco.Cms.Core.Events;
99
using Umbraco.Cms.Core.Notifications;
10+
using Umbraco.Cms.Integrations.Automation.Zapier.Helpers;
1011
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
1112

1213
namespace Umbraco.Cms.Integrations.Automation.Zapier.Components
@@ -30,24 +31,19 @@ public NewContentPublishedNotification(ZapierSubscriptionHookService zapierSubsc
3031

3132
public void Handle(ContentPublishedNotification notification)
3233
{
34+
var triggerHelper = new TriggerHelper(_zapierService);
35+
3336
foreach (var node in notification.PublishedEntities)
3437
{
35-
if (_zapierSubscriptionHookService.TryGetByAlias(node.ContentType.Alias, out var zapContentConfig))
38+
if (_zapierSubscriptionHookService.TryGetByAlias(node.ContentType.Alias, out var zapContentConfigList))
3639
{
37-
var content = new Dictionary<string, string>
40+
foreach (var zapContentConfig in zapContentConfigList)
3841
{
39-
{Constants.Content.Id, node.Id.ToString()},
40-
{Constants.Content.Name, node.Name},
41-
{Constants.Content.PublishDate, DateTime.UtcNow.ToString()}
42-
};
43-
44-
var t = Task.Run(
45-
async () => await _zapierService.TriggerAsync(zapContentConfig.HookUrl, content));
46-
47-
var result = t.Result;
42+
var result = triggerHelper.Execute(zapContentConfig.HookUrl, node.Id.ToString(), node.Name);
4843

49-
if (!string.IsNullOrEmpty(result))
50-
_logger.LogError(result);
44+
if (!string.IsNullOrEmpty(result))
45+
_logger.LogError(result);
46+
}
5147
}
5248
}
5349
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Controllers/ContentController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public IEnumerable<ContentTypeDto> GetContentTypes()
7979
var contentTypes = _contentTypeService.GetAll();
8080

8181
return contentTypes
82-
.Where(p => !_zapierSubscriptionHookService.TryGetByAlias(p.Alias, out var contentConfig))
8382
.Select(q => new ContentTypeDto
8483
{
8584
Id = q.Id,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
5+
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
6+
7+
namespace Umbraco.Cms.Integrations.Automation.Zapier.Helpers
8+
{
9+
public class TriggerHelper
10+
{
11+
private readonly ZapierService _zapierService;
12+
13+
public TriggerHelper(ZapierService zapierService)
14+
{
15+
_zapierService = zapierService;
16+
}
17+
18+
public string Execute(string hookUrl, string contentId, string contentName)
19+
{
20+
var content = new Dictionary<string, string>
21+
{
22+
{Constants.Content.Id, contentId},
23+
{Constants.Content.Name, contentName},
24+
{Constants.Content.PublishDate, DateTime.UtcNow.ToString()}
25+
};
26+
27+
var t = Task.Run(
28+
async () => await _zapierService.TriggerAsync(hookUrl, content));
29+
30+
return t.Result;
31+
}
32+
}
33+
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Services/ZapierSubscriptionHookService.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,21 @@ public string Delete(string contentTypeAlias, string hookUrl)
121121
}
122122
}
123123

124-
public bool TryGetByAlias(string contentTypeAlias, out ContentConfigDto dto)
124+
public bool TryGetByAlias(string contentTypeAlias, out IEnumerable<ContentConfigDto> dto)
125125
{
126126
using (var scope = _scopeProvider.CreateScope())
127127
{
128-
var entity =
128+
var entities =
129129
scope.Database
130-
.FirstOrDefault<ZapierSubscriptionHookTable.ZapierSubscriptionHook>("where ContentTypeAlias = @0", contentTypeAlias);
130+
.Query<ZapierSubscriptionHookTable.ZapierSubscriptionHook>()
131+
.Where(p => p.ContentTypeAlias == contentTypeAlias)
132+
.ToList();
131133

132-
dto = entity != null
133-
? new ContentConfigDto {HookUrl = entity.HookUrl}
134+
dto = entities.Any()
135+
? entities.Select(p => new ContentConfigDto { HookUrl = p.HookUrl })
134136
: null;
135137

136-
return entity != null;
138+
return entities.Any();
137139
}
138140
}
139141
}

0 commit comments

Comments
 (0)