Browse Source

fix(notifications): 使用接口序列化通知数据.

pull/783/head
cKey 3 years ago
parent
commit
c654b091ab
  1. 14
      aspnet-core/modules/common/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationData.cs
  2. 41
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs
  3. 5
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs
  4. 39
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataConverter.cs
  5. 9
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs

14
aspnet-core/modules/common/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationData.cs

@ -1,5 +1,6 @@
using LINGYUN.Abp.RealTime.Localization; using LINGYUN.Abp.RealTime.Localization;
using System; using System;
using System.Globalization;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.EventBus; using Volo.Abp.EventBus;
@ -20,6 +21,10 @@ namespace LINGYUN.Abp.Notifications
/// 用来标识是否需要本地化的信息 /// 用来标识是否需要本地化的信息
/// </summary> /// </summary>
public const string LocalizerKey = "L"; public const string LocalizerKey = "L";
/// <summary>
/// 用于本地化文化
/// </summary>
public const string CultureKey = "C";
public virtual string Type => GetType().FullName; public virtual string Type => GetType().FullName;
@ -58,13 +63,15 @@ namespace LINGYUN.Abp.Notifications
LocalizableStringInfo message, LocalizableStringInfo message,
DateTime createTime, DateTime createTime,
string formUser, string formUser,
LocalizableStringInfo description = null) LocalizableStringInfo description = null,
string culture = null)
{ {
TrySetData("title", title); TrySetData("title", title);
TrySetData("message", message); TrySetData("message", message);
TrySetData("formUser", formUser); TrySetData("formUser", formUser);
TrySetData("createTime", createTime); TrySetData("createTime", createTime);
TrySetData(LocalizerKey, true); TrySetData(LocalizerKey, true);
TrySetData(CultureKey, culture ?? "en");
if (description != null) if (description != null)
{ {
TrySetData("description", description); TrySetData("description", description);
@ -155,9 +162,10 @@ namespace LINGYUN.Abp.Notifications
public bool NeedLocalizer() public bool NeedLocalizer()
{ {
var localizer = TryGetData(LocalizerKey); var localizer = TryGetData(LocalizerKey);
if (localizer != null && localizer is bool needLocalizer) if (localizer != null &&
bool.TryParse(localizer.ToString(), out var isLocalizer))
{ {
return needLocalizer; return isLocalizer;
} }
return false; return false;
} }

41
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs

@ -0,0 +1,41 @@
using LINGYUN.Abp.RealTime.Localization;
using Newtonsoft.Json;
using Volo.Abp.DependencyInjection;
namespace LINGYUN.Abp.Notifications;
[Dependency(TryRegister = true)]
public class DefaultNotificationDataSerializer : INotificationDataSerializer, ISingletonDependency
{
public NotificationData Serialize(NotificationData source)
{
if (source != null)
{
if (source.NeedLocalizer())
{
// 潜在的空对象引用修复
if (source.ExtraProperties.TryGetValue("title", out var title) && title != null)
{
var titleObj = JsonConvert.DeserializeObject<LocalizableStringInfo>(title.ToString());
source.TrySetData("title", titleObj);
}
if (source.ExtraProperties.TryGetValue("message", out var message) && message != null)
{
var messageObj = JsonConvert.DeserializeObject<LocalizableStringInfo>(message.ToString());
source.TrySetData("message", messageObj);
}
if (source.ExtraProperties.TryGetValue("description", out var description) && description != null)
{
var descriptionObj = JsonConvert.DeserializeObject<LocalizableStringInfo>(description.ToString());
source.TrySetData("description", descriptionObj);
}
}
}
else
{
source = new NotificationData();
}
return source;
}
}

5
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs

@ -0,0 +1,5 @@
namespace LINGYUN.Abp.Notifications;
public interface INotificationDataSerializer
{
NotificationData Serialize(NotificationData source);
}

39
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationDataConverter.cs

@ -1,39 +0,0 @@
using LINGYUN.Abp.RealTime.Localization;
using Newtonsoft.Json;
namespace LINGYUN.Abp.Notifications
{
public class NotificationDataConverter
{
public static NotificationData Convert(NotificationData notificationData)
{
if (notificationData != null)
{
if (notificationData.NeedLocalizer())
{
// 潜在的空对象引用修复
if (notificationData.ExtraProperties.TryGetValue("title", out object title) && title != null)
{
var titleObj = JsonConvert.DeserializeObject<LocalizableStringInfo>(title.ToString());
notificationData.TrySetData("title", titleObj);
}
if (notificationData.ExtraProperties.TryGetValue("message", out object message) && message != null)
{
var messageObj = JsonConvert.DeserializeObject<LocalizableStringInfo>(message.ToString());
notificationData.TrySetData("message", messageObj);
}
if (notificationData.ExtraProperties.TryGetValue("description", out object description) && description != null)
{
notificationData.TrySetData("description", JsonConvert.DeserializeObject<LocalizableStringInfo>(description.ToString()));
}
}
}
else
{
notificationData = new NotificationData();
}
return notificationData;
}
}
}

9
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs

@ -68,6 +68,10 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
/// </summary> /// </summary>
protected IStringLocalizerFactory StringLocalizerFactory { get; } protected IStringLocalizerFactory StringLocalizerFactory { get; }
/// <summary> /// <summary>
/// Reference to <see cref="INotificationDataSerializer"/>.
/// </summary>
protected INotificationDataSerializer NotificationDataSerializer { get; }
/// <summary>
/// Reference to <see cref="INotificationDefinitionManager"/>. /// Reference to <see cref="INotificationDefinitionManager"/>.
/// </summary> /// </summary>
protected INotificationDefinitionManager NotificationDefinitionManager { get; } protected INotificationDefinitionManager NotificationDefinitionManager { get; }
@ -92,6 +96,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
IStringLocalizerFactory stringLocalizerFactory, IStringLocalizerFactory stringLocalizerFactory,
IOptions<AbpNotificationsPublishOptions> options, IOptions<AbpNotificationsPublishOptions> options,
INotificationStore notificationStore, INotificationStore notificationStore,
INotificationDataSerializer notificationDataSerializer,
INotificationDefinitionManager notificationDefinitionManager, INotificationDefinitionManager notificationDefinitionManager,
INotificationSubscriptionManager notificationSubscriptionManager, INotificationSubscriptionManager notificationSubscriptionManager,
INotificationPublishProviderManager notificationPublishProviderManager) INotificationPublishProviderManager notificationPublishProviderManager)
@ -104,6 +109,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
BackgroundJobManager = backgroundJobManager; BackgroundJobManager = backgroundJobManager;
StringLocalizerFactory = stringLocalizerFactory; StringLocalizerFactory = stringLocalizerFactory;
NotificationStore = notificationStore; NotificationStore = notificationStore;
NotificationDataSerializer = notificationDataSerializer;
NotificationDefinitionManager = notificationDefinitionManager; NotificationDefinitionManager = notificationDefinitionManager;
NotificationSubscriptionManager = notificationSubscriptionManager; NotificationSubscriptionManager = notificationSubscriptionManager;
NotificationPublishProviderManager = notificationPublishProviderManager; NotificationPublishProviderManager = notificationPublishProviderManager;
@ -289,8 +295,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
}; };
notificationInfo.SetId(eventData.Id); notificationInfo.SetId(eventData.Id);
// TODO: 可以做成一个接口来序列化消息 notificationInfo.Data = NotificationDataSerializer.Serialize(notificationInfo.Data);
notificationInfo.Data = NotificationDataConverter.Convert(notificationInfo.Data);
// 获取用户订阅 // 获取用户订阅
var subscriptionUsers = await GerSubscriptionUsersAsync( var subscriptionUsers = await GerSubscriptionUsersAsync(

Loading…
Cancel
Save