Browse Source

added global notification handling

pull/681/head
cKey 3 years ago
parent
commit
eadbdc4dac
  1. 6
      aspnet-core/modules/common/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationType.cs
  2. 69
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs
  3. 10
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/ITenantConfigurationCache.cs
  4. 49
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/TenantConfigurationCache.cs
  5. 20
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/TenantConfigurationCacheItem.cs

6
aspnet-core/modules/common/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationType.cs

@ -6,15 +6,15 @@
public enum NotificationType public enum NotificationType
{ {
/// <summary> /// <summary>
/// 应用(对应租户) /// 应用(仅对当前租户)
/// </summary> /// </summary>
Application = 0, Application = 0,
/// <summary> /// <summary>
/// 系统(对应宿主 /// 系统通知(全局发布
/// </summary> /// </summary>
System = 10, System = 10,
/// <summary> /// <summary>
/// 用户(对应用户) /// 用户(对应用户,受租户控制
/// </summary> /// </summary>
User = 20 User = 20
} }

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

@ -1,5 +1,6 @@
using LINGYUN.Abp.Notifications; using LINGYUN.Abp.Notifications;
using LY.MicroService.RealtimeMessage.BackgroundJobs; using LY.MicroService.RealtimeMessage.BackgroundJobs;
using LY.MicroService.RealtimeMessage.MultiTenancy;
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;
@ -43,6 +44,10 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
/// </summary> /// </summary>
protected ICurrentTenant CurrentTenant { get; } protected ICurrentTenant CurrentTenant { get; }
/// <summary> /// <summary>
/// Reference to <see cref="ITenantConfigurationCache"/>.
/// </summary>
protected ITenantConfigurationCache TenantConfigurationCache { get; }
/// <summary>
/// Reference to <see cref="IJsonSerializer"/>. /// Reference to <see cref="IJsonSerializer"/>.
/// </summary> /// </summary>
protected IJsonSerializer JsonSerializer { get; } protected IJsonSerializer JsonSerializer { get; }
@ -80,6 +85,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
/// </summary> /// </summary>
public NotificationEventHandler( public NotificationEventHandler(
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
ITenantConfigurationCache tenantConfigurationCache,
IJsonSerializer jsonSerializer, IJsonSerializer jsonSerializer,
ITemplateRenderer templateRenderer, ITemplateRenderer templateRenderer,
IBackgroundJobManager backgroundJobManager, IBackgroundJobManager backgroundJobManager,
@ -91,6 +97,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
INotificationPublishProviderManager notificationPublishProviderManager) INotificationPublishProviderManager notificationPublishProviderManager)
{ {
Options = options.Value; Options = options.Value;
TenantConfigurationCache = tenantConfigurationCache;
CurrentTenant = currentTenant; CurrentTenant = currentTenant;
JsonSerializer = jsonSerializer; JsonSerializer = jsonSerializer;
TemplateRenderer = templateRenderer; TemplateRenderer = templateRenderer;
@ -107,18 +114,38 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
[UnitOfWork] [UnitOfWork]
public async virtual Task HandleEventAsync(NotificationEto<NotificationTemplate> eventData) public async virtual Task HandleEventAsync(NotificationEto<NotificationTemplate> eventData)
{ {
using (CurrentTenant.Change(eventData.TenantId)) var notification = await NotificationDefinitionManager.GetOrNullAsync(eventData.Name);
if (notification == null)
{
return;
}
if (notification.NotificationType == NotificationType.System)
{ {
var notification = await NotificationDefinitionManager.GetOrNullAsync(eventData.Name); var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
if (notification == null)
foreach (var activeTenant in allActiveTenants)
{ {
return; await SendToTenantAsync(activeTenant.Id, notification, eventData);
} }
}
else
{
await SendToTenantAsync(eventData.TenantId, notification, eventData);
}
}
protected async virtual Task SendToTenantAsync(
Guid? tenantId,
NotificationDefinition notification,
NotificationEto<NotificationTemplate> eventData)
{
using (CurrentTenant.Change(tenantId))
{
var notificationInfo = new NotificationInfo var notificationInfo = new NotificationInfo
{ {
Name = notification.Name, Name = notification.Name,
TenantId = eventData.TenantId, TenantId = tenantId,
Severity = eventData.Severity, Severity = eventData.Severity,
Type = notification.NotificationType, Type = notification.NotificationType,
CreationTime = eventData.CreationTime, CreationTime = eventData.CreationTime,
@ -144,7 +171,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
var notificationData = new NotificationData(); var notificationData = new NotificationData();
notificationData.WriteStandardData( notificationData.WriteStandardData(
title: title, title: title,
message: message, message: message,
createTime: eventData.CreationTime, createTime: eventData.CreationTime,
formUser: eventData.Data.FormUser); formUser: eventData.Data.FormUser);
notificationData.ExtraProperties.AddIfNotContains(eventData.Data.ExtraProperties); notificationData.ExtraProperties.AddIfNotContains(eventData.Data.ExtraProperties);
@ -175,14 +202,34 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
[UnitOfWork] [UnitOfWork]
public async virtual Task HandleEventAsync(NotificationEto<NotificationData> eventData) public async virtual Task HandleEventAsync(NotificationEto<NotificationData> eventData)
{ {
using (CurrentTenant.Change(eventData.TenantId)) var notification = await NotificationDefinitionManager.GetOrNullAsync(eventData.Name);
if (notification == null)
{
return;
}
if (notification.NotificationType == NotificationType.System)
{ {
var notification = await NotificationDefinitionManager.GetOrNullAsync(eventData.Name); var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
if (notification == null)
foreach (var activeTenant in allActiveTenants)
{ {
return; await SendToTenantAsync(activeTenant.Id, notification, eventData);
} }
}
else
{
await SendToTenantAsync(eventData.TenantId, notification, eventData);
}
}
protected async virtual Task SendToTenantAsync(
Guid? tenantId,
NotificationDefinition notification,
NotificationEto<NotificationData> eventData)
{
using (CurrentTenant.Change(tenantId))
{
var notificationInfo = new NotificationInfo var notificationInfo = new NotificationInfo
{ {
Name = notification.Name, Name = notification.Name,
@ -190,7 +237,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
Data = eventData.Data, Data = eventData.Data,
Severity = eventData.Severity, Severity = eventData.Severity,
Lifetime = notification.NotificationLifetime, Lifetime = notification.NotificationLifetime,
TenantId = eventData.TenantId, TenantId = tenantId,
Type = notification.NotificationType Type = notification.NotificationType
}; };
notificationInfo.SetId(eventData.Id); notificationInfo.SetId(eventData.Id);

10
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/ITenantConfigurationCache.cs

@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.RealtimeMessage.MultiTenancy;
public interface ITenantConfigurationCache
{
Task<List<TenantConfiguration>> GetTenantsAsync();
}

49
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/TenantConfigurationCache.cs

@ -0,0 +1,49 @@
using LINGYUN.Abp.Saas.Tenants;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.RealtimeMessage.MultiTenancy;
public class TenantConfigurationCache : ITenantConfigurationCache, ITransientDependency
{
protected ITenantRepository TenantRepository { get; }
protected IDistributedCache<TenantConfigurationCacheItem> TenantCache { get; }
public TenantConfigurationCache(
ITenantRepository tenantRepository,
IDistributedCache<TenantConfigurationCacheItem> tenantCache)
{
TenantRepository = tenantRepository;
TenantCache = tenantCache;
}
public async virtual Task<List<TenantConfiguration>> GetTenantsAsync()
{
return (await GetForCacheItemAsync()).Tenants;
}
protected async virtual Task<TenantConfigurationCacheItem> GetForCacheItemAsync()
{
var cacheKey = "_Abp_Tenant_Configuration";
var cacheItem = await TenantCache.GetAsync(cacheKey);
if (cacheItem == null)
{
var allActiveTenants = await TenantRepository.GetListAsync();
cacheItem = new TenantConfigurationCacheItem(
allActiveTenants.Select(t =>
new TenantConfiguration(t.Id, t.Name)
{
IsActive = t.IsActive,
}).ToList());
await TenantCache.SetAsync(cacheKey, cacheItem);
}
return cacheItem;
}
}

20
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/TenantConfigurationCacheItem.cs

@ -0,0 +1,20 @@
using System.Collections.Generic;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.RealtimeMessage.MultiTenancy;
[IgnoreMultiTenancy]
public class TenantConfigurationCacheItem
{
public List<TenantConfiguration> Tenants { get; set; }
public TenantConfigurationCacheItem()
{
Tenants = new List<TenantConfiguration>();
}
public TenantConfigurationCacheItem(List<TenantConfiguration> tenants)
{
Tenants = tenants;
}
}
Loading…
Cancel
Save