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:
@@ -41,6 +41,8 @@ builder.Services.AddSingleton(TimeProvider.System);
|
||||
builder.Services.AddSingleton<ConcurrentQueue<SubscribeTask>>();
|
||||
builder.Services.AddHostedService<SubscriptionWorker>();
|
||||
|
||||
builder.Services.AddSingleton<LastPostedStreamStore>();
|
||||
|
||||
builder.Services.AddOptionsWithValidateOnStart<DiscordNotificationOptions>()
|
||||
.BindConfiguration(nameof(DiscordNotificationOptions))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
@@ -8,6 +8,7 @@ internal static class NotificationHandler
|
||||
[FromServices] IYouTubeDataApiClient youTubeDataApiClient,
|
||||
[FromServices] IDiscordRestClient discordRestClient,
|
||||
[FromServices] IOptionsMonitor<DiscordNotificationOptions> discordNotificationOptions,
|
||||
[FromServices] LastPostedStreamStore lastPostedStream,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
@@ -39,6 +40,14 @@ internal static class NotificationHandler
|
||||
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(
|
||||
"Video ID {VideoId} is a live stream. Creating discord message in channel {ChannelId}",
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user