fix: prevent duplicate stream notifications

- add store that is singleton
  to keep track of last posted
  stream and prevent
  posting twice for same stream
This commit is contained in:
Stevan Freeborn
2025-05-28 21:43:57 -05:00
parent 21adfcd3bb
commit 887b95f798
3 changed files with 22 additions and 0 deletions
+2
View File
@@ -41,6 +41,8 @@ builder.Services.AddSingleton(TimeProvider.System);
builder.Services.AddSingleton<ConcurrentQueue<SubscribeTask>>(); builder.Services.AddSingleton<ConcurrentQueue<SubscribeTask>>();
builder.Services.AddHostedService<SubscriptionWorker>(); builder.Services.AddHostedService<SubscriptionWorker>();
builder.Services.AddSingleton<LastPostedStreamStore>();
builder.Services.AddOptionsWithValidateOnStart<DiscordNotificationOptions>() builder.Services.AddOptionsWithValidateOnStart<DiscordNotificationOptions>()
.BindConfiguration(nameof(DiscordNotificationOptions)) .BindConfiguration(nameof(DiscordNotificationOptions))
.ValidateDataAnnotations(); .ValidateDataAnnotations();
@@ -8,6 +8,7 @@ internal static class NotificationHandler
[FromServices] IYouTubeDataApiClient youTubeDataApiClient, [FromServices] IYouTubeDataApiClient youTubeDataApiClient,
[FromServices] IDiscordRestClient discordRestClient, [FromServices] IDiscordRestClient discordRestClient,
[FromServices] IOptionsMonitor<DiscordNotificationOptions> discordNotificationOptions, [FromServices] IOptionsMonitor<DiscordNotificationOptions> discordNotificationOptions,
[FromServices] LastPostedStreamStore lastPostedStream,
CancellationToken cancellationToken CancellationToken cancellationToken
) )
{ {
@@ -39,6 +40,14 @@ internal static class NotificationHandler
return Results.Ok(); return Results.Ok();
} }
if (lastPostedStream.Value == videoId)
{
logger.LogInformation("Video ID {VideoId} has already been posted. Skipping notification.", videoId);
return Results.Ok();
}
lastPostedStream.SetValue(videoId);
logger.LogInformation( logger.LogInformation(
"Video ID {VideoId} is a live stream. Creating discord message in channel {ChannelId}", "Video ID {VideoId} is a live stream. Creating discord message in channel {ChannelId}",
videoId, videoId,
@@ -0,0 +1,11 @@
namespace StevesBot.Webhook.YouTube;
internal class LastPostedStreamStore
{
public string Value { get; private set; } = string.Empty;
public void SetValue(string value)
{
Value = value;
}
}