Skip to content

Commit fe9d33e

Browse files
committed
fix: avoid redundant session imports
1 parent ed69573 commit fe9d33e

3 files changed

Lines changed: 48 additions & 20 deletions

File tree

SgfDevs.Tests/ImportedPresenterBlockBuilderTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,19 @@ public void Build_CreatesBlockListPayloadForNonMemberPresenters()
4545
Assert.Equal("Dinesh Chugtai", secondValues[0].GetProperty("value").GetString());
4646
Assert.Equal(string.Empty, secondValues[1].GetProperty("value").GetString());
4747
}
48+
49+
[Fact]
50+
public void Build_ReturnsSamePayloadForSamePresenters()
51+
{
52+
var presenters = new[]
53+
{
54+
new ImportedPresenterPlan("speaker-1", "Bertram Gilfoyle", null, MatchedMemberKey: new Guid("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")),
55+
new ImportedPresenterPlan("speaker-2", "Dinesh Chugtai", null)
56+
};
57+
58+
var first = _builder.Build(presenters);
59+
var second = _builder.Build(presenters);
60+
61+
Assert.Equal(first, second);
62+
}
4863
}

SgfDevs/Dev/EventSync/ImportedPresenterBlockBuilder.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Security.Cryptography;
6+
using System.Text;
57
using System.Text.Json;
68
using SgfDevs.Dev.EventSync.Sessionize;
79
using Umbraco.Cms.Core;
@@ -45,7 +47,7 @@ public string Build(IReadOnlyList<ImportedPresenterPlan> presenters)
4547

4648
var blocks = presenters.Select(presenter =>
4749
{
48-
var key = Guid.NewGuid();
50+
var key = BuildBlockKey(presenter);
4951
var isMatchedMember = presenter.MatchedMemberKey.HasValue;
5052

5153
return new
@@ -114,4 +116,21 @@ public string Build(IReadOnlyList<ImportedPresenterPlan> presenters)
114116

115117
return JsonSerializer.Serialize(payload);
116118
}
119+
120+
private static Guid BuildBlockKey(ImportedPresenterPlan presenter)
121+
{
122+
if (presenter.MatchedMemberKey.HasValue)
123+
{
124+
return presenter.MatchedMemberKey.Value;
125+
}
126+
127+
if (Guid.TryParse(presenter.SessionizeSpeakerId, out var sessionizeSpeakerKey))
128+
{
129+
return sessionizeSpeakerKey;
130+
}
131+
132+
var identity = $"{presenter.SessionizeSpeakerId}\n{presenter.Name}";
133+
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(identity));
134+
return new Guid(hash.AsSpan(0, 16));
135+
}
117136
}

SgfDevs/Dev/EventSync/SessionizeEventSyncService.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ public async Task SyncAsync(CancellationToken cancellationToken = default)
112112

113113
var eventContent = GetOrCreateEvent(existingEvents, references.EventsContainerId, eventPlan);
114114
SaveEventContent(eventContent, eventPlan);
115-
_contentService.Save(eventContent, SystemUserId);
116-
var presentationsToPublish = new List<IContent>();
115+
SaveAndPublishContent(eventContent, "event", clearSchedule: true);
117116

118117
var existingPresentations = GetChildren(eventContent.Id)
119118
.Where(content => string.Equals(content.ContentType.Alias, PresentationAlias, StringComparison.Ordinal))
@@ -136,14 +135,7 @@ public async Task SyncAsync(CancellationToken cancellationToken = default)
136135

137136
var presentationContent = GetOrCreatePresentation(existingPresentations, eventContent.Id, presentationPlan);
138137
SavePresentationContent(presentationContent, references.SpringfieldDevsGroupKey, enrichedPresentationPlan, meetupMatch?.EventUrl);
139-
_contentService.Save(presentationContent, SystemUserId);
140-
presentationsToPublish.Add(presentationContent);
141-
}
142-
143-
PublishContent(eventContent, "event", clearSchedule: true);
144-
foreach (var presentationContent in presentationsToPublish)
145-
{
146-
PublishContent(presentationContent, "presentation");
138+
SaveAndPublishContent(presentationContent, "presentation");
147139
}
148140
}
149141
}
@@ -262,29 +254,31 @@ private async Task<IReadOnlyList<ImportedPresenterPlan>> ImportPresenterImagesAs
262254
return enrichedPresenters;
263255
}
264256

265-
private void PublishContent(IContent content, string contentKind, bool clearSchedule = false)
257+
private void SaveAndPublishContent(IContent content, string contentKind, bool clearSchedule = false)
266258
{
267-
if (clearSchedule)
259+
if (content.IsDirty() == false && content.IsCultureEdited(null!) == false)
260+
{
261+
return;
262+
}
263+
264+
if (clearSchedule && content.HasIdentity)
268265
{
269266
_contentService.PersistContentSchedule(content, new ContentScheduleCollection());
270267
}
271268

272-
var publishResult = _contentService.Publish(content, ["*"], SystemUserId);
269+
var publishResult = _contentService.SaveAndPublish(content, [], SystemUserId);
273270
if (publishResult.Success)
274271
{
275272
_logger.LogInformation(
276-
"Published {ContentKind} {ContentName} with result {PublishResult}.",
273+
"Saved and published {ContentKind} {ContentName} with result {PublishResult}.",
277274
contentKind,
278275
content.Name,
279276
publishResult.Result);
280277
return;
281278
}
282279

283-
_logger.LogWarning(
284-
"Failed to publish {ContentKind} {ContentName}. Publish result was {PublishResult}.",
285-
contentKind,
286-
content.Name,
287-
publishResult.Result);
280+
throw new InvalidOperationException(
281+
$"Failed to save and publish {contentKind} {content.Name}. Publish result was {publishResult.Result}.");
288282
}
289283

290284
private IReadOnlyList<IContent> GetChildren(int parentId)

0 commit comments

Comments
 (0)