Browse Source

refactor(notifications): 注释掉不再使用的通知数据映射

pull/1048/head
feijie 1 year ago
parent
commit
10bcb1b4b6
  1. 438
      aspnet-core/tests/LINGYUN.Abp.Notifications.Tests/LINGYUN/Abp/Notifications/FakeNotificationSender.cs

438
aspnet-core/tests/LINGYUN.Abp.Notifications.Tests/LINGYUN/Abp/Notifications/FakeNotificationSender.cs

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

Loading…
Cancel
Save