From a7248d3d5c380eae40f4f4ebc2186056a678f5ec Mon Sep 17 00:00:00 2001 From: charconstpointer Date: Tue, 2 Feb 2021 07:54:55 +0100 Subject: [PATCH] webhook ef core config, WebhooksService.cs invoke --- .../BackgroundServices/WebhooksService.cs | 13 ++++++++- .../Consumers/ServiceStatusChangedConsumer.cs | 27 ++++++++++++++++--- .../Controllers/WebhooksController.cs | 4 ++- .../Persistence/NotificationsContext.cs | 4 +++ Gadget.Notifications/requests.http | 7 ++--- 5 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Gadget.Notifications/BackgroundServices/WebhooksService.cs b/Gadget.Notifications/BackgroundServices/WebhooksService.cs index a762d1c..14f27cf 100644 --- a/Gadget.Notifications/BackgroundServices/WebhooksService.cs +++ b/Gadget.Notifications/BackgroundServices/WebhooksService.cs @@ -1,4 +1,6 @@ using System.Net.Http; +using System.Net.Http.Json; +using System.Text.Json.Serialization; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; @@ -44,8 +46,17 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) _logger.LogInformation( $"Received new webhook notification request for service {notification.ServiceName}"); - await _client.PostAsync(notification.Webhook, null!, stoppingToken); + await _client.PostAsJsonAsync(notification.Webhook, new InvokeWebhook + { + Content = + $"Service : {notification.ServiceName} Agent : {notification.Agent} Status : {notification.Status}" + }, cancellationToken: stoppingToken); } } + + public class InvokeWebhook + { + [JsonPropertyName("content")] public string Content { get; set; } + } } } \ No newline at end of file diff --git a/Gadget.Notifications/Consumers/ServiceStatusChangedConsumer.cs b/Gadget.Notifications/Consumers/ServiceStatusChangedConsumer.cs index 9d4c4d7..83f57e1 100644 --- a/Gadget.Notifications/Consumers/ServiceStatusChangedConsumer.cs +++ b/Gadget.Notifications/Consumers/ServiceStatusChangedConsumer.cs @@ -5,8 +5,10 @@ using Gadget.Messaging.SignalR.v1; using Gadget.Notifications.Domain.ValueObjects; using Gadget.Notifications.Hubs; +using Gadget.Notifications.Persistence; using MassTransit; using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace Gadget.Notifications.Consumers @@ -16,12 +18,14 @@ public class ServiceStatusChangedConsumer : IConsumer private readonly ILogger _logger; private readonly IHubContext _hub; private readonly ChannelWriter _channel; + private readonly NotificationsContext _notificationsContext; public ServiceStatusChangedConsumer(ILogger logger, - IHubContext hub, Channel channel) + IHubContext hub, Channel channel, NotificationsContext notificationsContext) { _logger = logger; _hub = hub; + _notificationsContext = notificationsContext; _channel = channel.Writer; } @@ -38,9 +42,24 @@ public async Task Consume(ConsumeContext context) Status = context.Message.Status }, context.CancellationToken); _logger.LogInformation("Trying to enqueue webhook notification"); - await _channel.WriteAsync(new Notification(context.Message.Agent, context.Message.Name, - context.Message.Status, new Uri("http://localhost:5000/webhooks"))); - _logger.LogInformation("Enqueued webhook notification"); + + var agent = await _notificationsContext.Services + .Include(s=>s.Webhooks) + .FirstOrDefaultAsync(s => + s.Agent == context.Message.Agent && s.Name == context.Message.Name); + + if (agent is null) + { + _logger.LogInformation("There are not webhooks registered for this event, skipping"); + return; + } + + foreach (var agentWebhook in agent.Webhooks) + { + await _channel.WriteAsync(new Notification(context.Message.Agent, context.Message.Name, + context.Message.Status, agentWebhook.Uri)); + _logger.LogInformation("Enqueued webhook notification"); + } } catch (Exception e) { diff --git a/Gadget.Notifications/Controllers/WebhooksController.cs b/Gadget.Notifications/Controllers/WebhooksController.cs index 9631db2..ce205af 100644 --- a/Gadget.Notifications/Controllers/WebhooksController.cs +++ b/Gadget.Notifications/Controllers/WebhooksController.cs @@ -34,7 +34,9 @@ public async Task TestWebhook() public async Task CreateNewWebhook(string agentName, string serviceName, CreateWebhook createWebhook) { - var agent = await _context.Services.FirstOrDefaultAsync(s => s.Agent == agentName); + var agent = await _context.Services + .Include(s=>s.Webhooks) + .FirstOrDefaultAsync(s => s.Agent == agentName); if (agent is null) { agent = new Service(agentName, serviceName); diff --git a/Gadget.Notifications/Persistence/NotificationsContext.cs b/Gadget.Notifications/Persistence/NotificationsContext.cs index 4e39227..cc65b0a 100644 --- a/Gadget.Notifications/Persistence/NotificationsContext.cs +++ b/Gadget.Notifications/Persistence/NotificationsContext.cs @@ -1,5 +1,6 @@ using System; using Gadget.Notifications.Domain.Entities; +using Gadget.Notifications.Domain.ValueObjects; using Microsoft.EntityFrameworkCore; namespace Gadget.Notifications.Persistence @@ -20,8 +21,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { w.WithOwner().HasForeignKey("OwnerId"); w.Property("Id"); + w.Property(wh => wh.Uri); + w.Property(wh => wh.CreatedAt); w.HasKey("Id"); })); + } public DbSet Services { get; set; } diff --git a/Gadget.Notifications/requests.http b/Gadget.Notifications/requests.http index d932597..d3c4282 100644 --- a/Gadget.Notifications/requests.http +++ b/Gadget.Notifications/requests.http @@ -1,8 +1,9 @@ POST http://localhost:5000/webhooks/DESKTOP-6PDIBOR/BTAGService Content-Type: application/json - +//hackme { -"Uri":"http://localhost:5000/webhooks" +"Uri":"https://discord.com/api/webhooks/806050278683049984/EJSocoVKoFRZ1Z9bLqK-QEYJCj3ZlphONoiGlAs7vKJj2VV8m1tAdwUONq9h1BCK11cC" } -### \ No newline at end of file +### +