Browse Source

Merge pull request #1369 from colinin/fix-webhook-notification

fix(notifications): Fix webhook notifications null object references
pull/1377/head
yx lin 3 months ago
committed by GitHub
parent
commit
07a300fcc6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 3
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationContext.cs
  2. 42
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationPublishProvider.cs

3
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationContext.cs

@ -7,8 +7,9 @@ public class WebhookNotificationContext : IWebhookNotificationContext
public NotificationInfo Notification { get; }
public WebhookNotificationData Webhook { get; set; }
public bool Handled { get; set; }
public WebhookNotificationContext(IServiceProvider ServiceProvider, NotificationInfo notification)
public WebhookNotificationContext(IServiceProvider serviceProvider, NotificationInfo notification)
{
ServiceProvider = serviceProvider;
Notification = notification;
}
public bool HasResolved()

42
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Webhook/LINGYUN/Abp/Notifications/Webhook/WebhookNotificationPublishProvider.cs

@ -1,9 +1,11 @@
using LINGYUN.Abp.Webhooks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Features;
namespace LINGYUN.Abp.Notifications.Webhook;
public class WebhookNotificationPublishProvider : NotificationPublishProvider
@ -11,38 +13,48 @@ public class WebhookNotificationPublishProvider : NotificationPublishProvider
public const string ProviderName = "Webhook";
public override string Name => ProviderName;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly AbpNotificationsWebhookOptions _options;
private readonly IWebhookPublisher _webhookPublisher;
protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService<IFeatureChecker>();
protected IWebhookPublisher WebhookPublisher => ServiceProvider.LazyGetRequiredService<IWebhookPublisher>();
protected IServiceScopeFactory ServiceScopeFactory => ServiceProvider.LazyGetRequiredService<IServiceScopeFactory>();
protected IOptions<AbpNotificationsWebhookOptions> Options => ServiceProvider.LazyGetRequiredService<IOptions<AbpNotificationsWebhookOptions>>();
public WebhookNotificationPublishProvider(
IServiceScopeFactory serviceScopeFactory,
IWebhookPublisher webhookPublisher,
IOptions<AbpNotificationsWebhookOptions> options)
protected override Task<bool> CanPublishAsync(NotificationInfo notification, CancellationToken cancellationToken = default)
{
_serviceScopeFactory = serviceScopeFactory;
_webhookPublisher = webhookPublisher;
_options = options.Value;
if (Options.Value.Contributors.Count == 0)
{
Logger.LogWarning("The Webhook notification publishing contributor is empty, and the Webhook notification cannot be sent!");
return Task.FromResult(false);
}
return base.CanPublishAsync(notification, cancellationToken);
}
protected override async Task PublishAsync(NotificationInfo notification, IEnumerable<UserIdentifier> identifiers, CancellationToken cancellationToken = default)
protected async override Task PublishAsync(NotificationInfo notification, IEnumerable<UserIdentifier> identifiers, CancellationToken cancellationToken = default)
{
using var scope = _serviceScopeFactory.CreateScope();
using var scope = ServiceScopeFactory.CreateScope();
foreach (var contributor in _options.Contributors)
foreach (var contributor in Options.Value.Contributors)
{
var context = new WebhookNotificationContext(scope.ServiceProvider, notification);
await contributor.ContributeAsync(context);
if (context.HasResolved())
if (!context.HasResolved())
{
await _webhookPublisher.PublishAsync(
Logger.LogWarning("The Webhook notifies the contributor: {0} that the Webhook data for the given notification: {1} cannot be parsed. Skip it.",
contributor.Name, notification.Name);
continue;
}
else
{
await WebhookPublisher.PublishAsync(
context.Webhook.WebhookName,
context.Webhook.Data,
notification.TenantId,
context.Webhook.SendExactSameData,
context.Webhook.Headers);
Logger.LogDebug("The webhook: {webhookName} with contributor: {name} has successfully published!", context.Webhook.WebhookName, Name);
}
}
}

Loading…
Cancel
Save