diff --git a/aspnet-core/tests/LINGYUN.Abp.Notifications.Tests/LINGYUN/Abp/Notifications/FakeNotificationSender.cs b/aspnet-core/tests/LINGYUN.Abp.Notifications.Tests/LINGYUN/Abp/Notifications/FakeNotificationSender.cs index 9a73efab0..88305c841 100644 --- a/aspnet-core/tests/LINGYUN.Abp.Notifications.Tests/LINGYUN/Abp/Notifications/FakeNotificationSender.cs +++ b/aspnet-core/tests/LINGYUN.Abp.Notifications.Tests/LINGYUN/Abp/Notifications/FakeNotificationSender.cs @@ -1,219 +1,219 @@ -using Microsoft.Extensions.Localization; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Json; -using Volo.Abp.TextTemplating; - -namespace LINGYUN.Abp.Notifications; - -[Dependency(ReplaceServices = true)] -public class FakeNotificationSender : INotificationSender, ITransientDependency -{ - public ILogger Logger { get; set; } - - protected AbpNotificationsPublishOptions Options { get; } - - protected IJsonSerializer JsonSerializer { get; } - - protected ITemplateRenderer TemplateRenderer { get; } - - protected INotificationStore NotificationStore { get; } - - protected INotificationDataSerializer NotificationDataSerializer { get; } - - protected IStringLocalizerFactory StringLocalizerFactory { get; } - - protected INotificationDefinitionManager NotificationDefinitionManager { get; } - - protected INotificationSubscriptionManager NotificationSubscriptionManager { get; } - - protected INotificationPublishProviderManager NotificationPublishProviderManager { get; } - - public FakeNotificationSender( - IJsonSerializer jsonSerializer, - ITemplateRenderer templateRenderer, - IStringLocalizerFactory stringLocalizerFactory, - IOptions options, - INotificationStore notificationStore, - INotificationDataSerializer notificationDataSerializer, - INotificationDefinitionManager notificationDefinitionManager, - INotificationSubscriptionManager notificationSubscriptionManager, - INotificationPublishProviderManager notificationPublishProviderManager) - { - Options = options.Value; - JsonSerializer = jsonSerializer; - TemplateRenderer = templateRenderer; - StringLocalizerFactory = stringLocalizerFactory; - NotificationStore = notificationStore; - NotificationDataSerializer = notificationDataSerializer; - NotificationDefinitionManager = notificationDefinitionManager; - NotificationSubscriptionManager = notificationSubscriptionManager; - NotificationPublishProviderManager = notificationPublishProviderManager; - - Logger = NullLogger.Instance; - } - - public async virtual Task SendNofiterAsync( - string name, - NotificationData data, - IEnumerable users = null, - Guid? tenantId = null, - NotificationSeverity severity = NotificationSeverity.Info, - IEnumerable useProviders = null) - { - var notification = await NotificationDefinitionManager.GetOrNullAsync(name); - if (notification == null) - { - return ""; - } - - var notificationInfo = new NotificationInfo - { - Name = notification.Name, - CreationTime = DateTime.Now, - Data = data, - Severity = severity, - Lifetime = notification.NotificationLifetime, - TenantId = tenantId, - Type = notification.NotificationType - }; - notificationInfo.SetId(DateTimeOffset.Now.ToUnixTimeMilliseconds()); - - notificationInfo.Data = NotificationDataSerializer.Serialize(notificationInfo.Data); - - Logger.LogDebug($"Persistent notification {notificationInfo.Name}"); - - await NotificationStore.InsertNotificationAsync(notificationInfo); - - var providers = Enumerable.Reverse(NotificationPublishProviderManager.Providers); - - if (useProviders?.Any() == true) - { - providers = providers.Where(p => useProviders.Contains(p.Name)); - } - else if (notification.Providers.Any()) - { - providers = providers.Where(p => notification.Providers.Contains(p.Name)); - } - - await PublishFromProvidersAsync( - providers, - users ?? new List(), - notificationInfo); - - return notificationInfo.Id; - } - - public async virtual Task SendNofiterAsync( - string name, - NotificationTemplate template, - IEnumerable users = null, - Guid? tenantId = null, - NotificationSeverity severity = NotificationSeverity.Info, - IEnumerable useProviders = null) - { - var notification = await NotificationDefinitionManager.GetOrNullAsync(name); - if (notification == null) - { - return ""; - } - - var notificationInfo = new NotificationInfo - { - Name = notification.Name, - TenantId = tenantId, - Severity = severity, - Type = notification.NotificationType, - CreationTime = DateTime.Now, - Lifetime = notification.NotificationLifetime, - }; - notificationInfo.SetId(DateTimeOffset.Now.ToUnixTimeMilliseconds()); - - var title = notification.DisplayName.Localize(StringLocalizerFactory); - - var message = await TemplateRenderer.RenderAsync( - templateName: name, - model: template.ExtraProperties); - - var notificationData = new NotificationData(); - notificationData.WriteStandardData( - title: title, - message: message, - createTime: notificationInfo.CreationTime, - formUser: "Fake User"); - notificationData.ExtraProperties.AddIfNotContains(template.ExtraProperties); - - notificationInfo.Data = notificationData; - - Logger.LogDebug($"Persistent notification {notificationInfo.Name}"); - - // 持久化通知 - await NotificationStore.InsertNotificationAsync(notificationInfo); - - var providers = Enumerable.Reverse(NotificationPublishProviderManager.Providers); - - // 过滤用户指定提供者 - if (useProviders?.Any() == true) - { - providers = providers.Where(p => useProviders.Contains(p.Name)); - } - else if (notification.Providers.Any()) - { - providers = providers.Where(p => notification.Providers.Contains(p.Name)); - } - - await PublishFromProvidersAsync( - providers, - users ?? new List(), - notificationInfo); - - return notificationInfo.Id; - } - - /// - /// 指定提供者发布通知 - /// - /// 提供者列表 - /// 通知信息 - /// - protected async Task PublishFromProvidersAsync( - IEnumerable providers, - IEnumerable users, - NotificationInfo notificationInfo) - { - foreach (var provider in providers) - { - await PublishAsync(provider, notificationInfo, users); - } - } - /// - /// 发布通知 - /// - /// 通知发布者 - /// 通知信息 - /// 订阅用户列表 - /// - protected async Task PublishAsync( - INotificationPublishProvider provider, - NotificationInfo notificationInfo, - IEnumerable subscriptionUserIdentifiers) - { - Logger.LogDebug($"Sending notification with provider {provider.Name}"); - var notifacationDataMapping = Options.NotificationDataMappings - .GetMapItemOrDefault(provider.Name, notificationInfo.Name); - if (notifacationDataMapping != null) - { - notificationInfo.Data = notifacationDataMapping.MappingFunc(notificationInfo.Data); - } - // 发布 - await provider.PublishAsync(notificationInfo, subscriptionUserIdentifiers); - - Logger.LogDebug($"Send notification {notificationInfo.Name} with provider {provider.Name} was successful"); - } -} +using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Json; +using Volo.Abp.TextTemplating; + +namespace LINGYUN.Abp.Notifications; + +[Dependency(ReplaceServices = true)] +public class FakeNotificationSender : INotificationSender, ITransientDependency +{ + public ILogger Logger { get; set; } + + protected AbpNotificationsPublishOptions Options { get; } + + protected IJsonSerializer JsonSerializer { get; } + + protected ITemplateRenderer TemplateRenderer { get; } + + protected INotificationStore NotificationStore { get; } + + protected INotificationDataSerializer NotificationDataSerializer { get; } + + protected IStringLocalizerFactory StringLocalizerFactory { get; } + + protected INotificationDefinitionManager NotificationDefinitionManager { get; } + + protected INotificationSubscriptionManager NotificationSubscriptionManager { get; } + + protected INotificationPublishProviderManager NotificationPublishProviderManager { get; } + + public FakeNotificationSender( + IJsonSerializer jsonSerializer, + ITemplateRenderer templateRenderer, + IStringLocalizerFactory stringLocalizerFactory, + IOptions options, + INotificationStore notificationStore, + INotificationDataSerializer notificationDataSerializer, + INotificationDefinitionManager notificationDefinitionManager, + INotificationSubscriptionManager notificationSubscriptionManager, + INotificationPublishProviderManager notificationPublishProviderManager) + { + Options = options.Value; + JsonSerializer = jsonSerializer; + TemplateRenderer = templateRenderer; + StringLocalizerFactory = stringLocalizerFactory; + NotificationStore = notificationStore; + NotificationDataSerializer = notificationDataSerializer; + NotificationDefinitionManager = notificationDefinitionManager; + NotificationSubscriptionManager = notificationSubscriptionManager; + NotificationPublishProviderManager = notificationPublishProviderManager; + + Logger = NullLogger.Instance; + } + + public async virtual Task SendNofiterAsync( + string name, + NotificationData data, + IEnumerable users = null, + Guid? tenantId = null, + NotificationSeverity severity = NotificationSeverity.Info, + IEnumerable useProviders = null) + { + var notification = await NotificationDefinitionManager.GetOrNullAsync(name); + if (notification == null) + { + return ""; + } + + var notificationInfo = new NotificationInfo + { + Name = notification.Name, + CreationTime = DateTime.Now, + Data = data, + Severity = severity, + Lifetime = notification.NotificationLifetime, + TenantId = tenantId, + Type = notification.NotificationType + }; + notificationInfo.SetId(DateTimeOffset.Now.ToUnixTimeMilliseconds()); + + notificationInfo.Data = NotificationDataSerializer.Serialize(notificationInfo.Data); + + Logger.LogDebug($"Persistent notification {notificationInfo.Name}"); + + await NotificationStore.InsertNotificationAsync(notificationInfo); + + var providers = Enumerable.Reverse(NotificationPublishProviderManager.Providers); + + if (useProviders?.Any() == true) + { + providers = providers.Where(p => useProviders.Contains(p.Name)); + } + else if (notification.Providers.Any()) + { + providers = providers.Where(p => notification.Providers.Contains(p.Name)); + } + + await PublishFromProvidersAsync( + providers, + users ?? new List(), + notificationInfo); + + return notificationInfo.Id; + } + + public async virtual Task SendNofiterAsync( + string name, + NotificationTemplate template, + IEnumerable users = null, + Guid? tenantId = null, + NotificationSeverity severity = NotificationSeverity.Info, + IEnumerable useProviders = null) + { + var notification = await NotificationDefinitionManager.GetOrNullAsync(name); + if (notification == null) + { + return ""; + } + + var notificationInfo = new NotificationInfo + { + Name = notification.Name, + TenantId = tenantId, + Severity = severity, + Type = notification.NotificationType, + CreationTime = DateTime.Now, + Lifetime = notification.NotificationLifetime, + }; + notificationInfo.SetId(DateTimeOffset.Now.ToUnixTimeMilliseconds()); + + var title = notification.DisplayName.Localize(StringLocalizerFactory); + + var message = await TemplateRenderer.RenderAsync( + templateName: name, + model: template.ExtraProperties); + + var notificationData = new NotificationData(); + notificationData.WriteStandardData( + title: title, + message: message, + createTime: notificationInfo.CreationTime, + formUser: "Fake User"); + notificationData.ExtraProperties.AddIfNotContains(template.ExtraProperties); + + notificationInfo.Data = notificationData; + + Logger.LogDebug($"Persistent notification {notificationInfo.Name}"); + + // 持久化通知 + await NotificationStore.InsertNotificationAsync(notificationInfo); + + var providers = Enumerable.Reverse(NotificationPublishProviderManager.Providers); + + // 过滤用户指定提供者 + if (useProviders?.Any() == true) + { + providers = providers.Where(p => useProviders.Contains(p.Name)); + } + else if (notification.Providers.Any()) + { + providers = providers.Where(p => notification.Providers.Contains(p.Name)); + } + + await PublishFromProvidersAsync( + providers, + users ?? new List(), + notificationInfo); + + return notificationInfo.Id; + } + + /// + /// 指定提供者发布通知 + /// + /// 提供者列表 + /// 通知信息 + /// + protected async Task PublishFromProvidersAsync( + IEnumerable providers, + IEnumerable users, + NotificationInfo notificationInfo) + { + foreach (var provider in providers) + { + await PublishAsync(provider, notificationInfo, users); + } + } + /// + /// 发布通知 + /// + /// 通知发布者 + /// 通知信息 + /// 订阅用户列表 + /// + protected async Task PublishAsync( + INotificationPublishProvider provider, + NotificationInfo notificationInfo, + IEnumerable subscriptionUserIdentifiers) + { + Logger.LogDebug($"Sending notification with provider {provider.Name}"); + // var notifacationDataMapping = Options.NotificationDataMappings + // .GetMapItemOrDefault(provider.Name, notificationInfo.Name); + // if (notifacationDataMapping != null) + // { + // notificationInfo.Data = notifacationDataMapping.MappingFunc(notificationInfo.Data); + // } + // 发布 + await provider.PublishAsync(notificationInfo, subscriptionUserIdentifiers); + + Logger.LogDebug($"Send notification {notificationInfo.Name} with provider {provider.Name} was successful"); + } +}