feat: implement verification of subscription request
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services
|
||||
@@ -6,7 +8,9 @@ builder.Services
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
builder.Services
|
||||
.AddHttpClient<IPubSubClient, PubSubClient>()
|
||||
.AddHttpClient<IPubSubClient, PubSubClient>(
|
||||
static c => c.BaseAddress = new("https://pubsubhubbub.appspot.com")
|
||||
)
|
||||
.AddStandardResilienceHandler();
|
||||
|
||||
builder.Services.AddOpenApi();
|
||||
@@ -22,4 +26,33 @@ if (app.Environment.IsDevelopment())
|
||||
|
||||
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();
|
||||
@@ -25,7 +25,7 @@ internal sealed class SubscriptionWorker(
|
||||
_logger.LogInformation("Subscribing to notifications");
|
||||
|
||||
await _pubSubClient.SubscribeAsync(
|
||||
_options.CallbackBaseUrl,
|
||||
_options.CallbackUrl,
|
||||
_options.TopicUrl,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
@@ -3,7 +3,7 @@ namespace StevesBot.Webhook.YouTube;
|
||||
internal sealed record SubscriptionOptions
|
||||
{
|
||||
[Required]
|
||||
public string CallbackBaseUrl { get; init; } = string.Empty;
|
||||
public string CallbackUrl { get; init; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string TopicUrl { get; init; } = string.Empty;
|
||||
|
||||
Reference in New Issue
Block a user