feat: implement verification of subscription request

This commit is contained in:
Stevan Freeborn
2025-05-26 12:38:04 -05:00
parent a1ef35d0a7
commit 0b42739cbb
3 changed files with 36 additions and 3 deletions
+34 -1
View File
@@ -1,3 +1,5 @@
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services builder.Services
@@ -6,7 +8,9 @@ builder.Services
.ValidateDataAnnotations(); .ValidateDataAnnotations();
builder.Services builder.Services
.AddHttpClient<IPubSubClient, PubSubClient>() .AddHttpClient<IPubSubClient, PubSubClient>(
static c => c.BaseAddress = new("https://pubsubhubbub.appspot.com")
)
.AddStandardResilienceHandler(); .AddStandardResilienceHandler();
builder.Services.AddOpenApi(); builder.Services.AddOpenApi();
@@ -22,4 +26,33 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection(); app.UseHttpsRedirection();
const string ytCallback = "yt-callback";
app.MapGet(
ytCallback,
static (
[FromQuery(Name = "hub.mode")] string mode,
[FromQuery(Name = "hub.topic")] string topic,
[FromQuery(Name = "hub.challenge")] string challenge,
[FromServices] IOptions<SubscriptionOptions> subOptions,
[FromServices] ILogger<Program> logger
) =>
{
if (topic != subOptions.Value.TopicUrl)
{
logger.LogInformation("Received verification request for wrong topic: {Topic}", topic);
return Results.NotFound();
}
return Results.Text(challenge);
}
);
// TODO: Implement logic to do the following:
// - extract video id from notification
// - identify video as a stream or not
// - if is stream create discord message
// - if not then just log a message
app.MapPost(ytCallback, static () => "hello");
app.Run(); app.Run();
@@ -25,7 +25,7 @@ internal sealed class SubscriptionWorker(
_logger.LogInformation("Subscribing to notifications"); _logger.LogInformation("Subscribing to notifications");
await _pubSubClient.SubscribeAsync( await _pubSubClient.SubscribeAsync(
_options.CallbackBaseUrl, _options.CallbackUrl,
_options.TopicUrl, _options.TopicUrl,
cancellationToken cancellationToken
); );
@@ -3,7 +3,7 @@ namespace StevesBot.Webhook.YouTube;
internal sealed record SubscriptionOptions internal sealed record SubscriptionOptions
{ {
[Required] [Required]
public string CallbackBaseUrl { get; init; } = string.Empty; public string CallbackUrl { get; init; } = string.Empty;
[Required] [Required]
public string TopicUrl { get; init; } = string.Empty; public string TopicUrl { get; init; } = string.Empty;