Browse Source

Merge pull request #595 from colinin/enhanced-notification-module

优化通知模块.增加内置通知
pull/645/head
yx lin 4 years ago
committed by GitHub
parent
commit
d63bac01d8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN.Abp.Notifications.csproj
  2. 20
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationModule.cs
  3. 18
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotifications.cs
  4. 44
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/DefaultNotificationDefinitionProvider.cs
  5. 8
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Localization/NotificationsResource.cs
  6. 12
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Localization/Resources/en.json
  7. 12
      aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Localization/Resources/zh-Hans.json
  8. 15
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs
  9. 73
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/TenantSynchronizer.cs
  10. 38
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Local/UserCreateSendWelcomeEventHandler.cs

8
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN.Abp.Notifications.csproj

@ -8,6 +8,14 @@
<RootNamespace /> <RootNamespace />
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Remove="LINGYUN\Abp\Notifications\Localization\Resources\*.json" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="LINGYUN\Abp\Notifications\Localization\Resources\*.json" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Volo.Abp.EventBus" Version="$(VoloAbpPackageVersion)" /> <PackageReference Include="Volo.Abp.EventBus" Version="$(VoloAbpPackageVersion)" />
<PackageReference Include="Volo.Abp.Json" Version="$(VoloAbpPackageVersion)" /> <PackageReference Include="Volo.Abp.Json" Version="$(VoloAbpPackageVersion)" />

20
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/AbpNotificationModule.cs

@ -1,4 +1,5 @@
using LINGYUN.Abp.IdGenerator; using LINGYUN.Abp.IdGenerator;
using LINGYUN.Abp.Notifications.Localization;
using LINGYUN.Abp.RealTime; using LINGYUN.Abp.RealTime;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
@ -6,7 +7,9 @@ using System.Collections.Generic;
using Volo.Abp.BackgroundJobs; using Volo.Abp.BackgroundJobs;
using Volo.Abp.BackgroundWorkers; using Volo.Abp.BackgroundWorkers;
using Volo.Abp.Json; using Volo.Abp.Json;
using Volo.Abp.Localization;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;
namespace LINGYUN.Abp.Notifications namespace LINGYUN.Abp.Notifications
{ {
@ -16,15 +19,30 @@ namespace LINGYUN.Abp.Notifications
typeof(AbpBackgroundJobsAbstractionsModule), typeof(AbpBackgroundJobsAbstractionsModule),
typeof(AbpIdGeneratorModule), typeof(AbpIdGeneratorModule),
typeof(AbpJsonModule), typeof(AbpJsonModule),
typeof(AbpLocalizationModule),
typeof(AbpRealTimeModule))] typeof(AbpRealTimeModule))]
public class AbpNotificationModule : AbpModule public class AbpNotificationModule : AbpModule
{ {
public override void PreConfigureServices(ServiceConfigurationContext context) public override void PreConfigureServices(ServiceConfigurationContext context)
{ {
AutoAddDefinitionProviders(context.Services); AutoAddDefinitionProviders(context.Services);
} }
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<AbpNotificationModule>();
});
Configure<AbpLocalizationOptions>(options =>
{
options.Resources
.Add<NotificationsResource>("en")
.AddVirtualJson("/LINGYUN/Abp/Notifications/Localization/Resources");
});
}
private void AutoAddDefinitionProviders(IServiceCollection services) private void AutoAddDefinitionProviders(IServiceCollection services)
{ {
var definitionProviders = new List<Type>(); var definitionProviders = new List<Type>();

18
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotifications.cs

@ -0,0 +1,18 @@
namespace LINGYUN.Abp.Notifications;
public static class DefaultNotifications
{
public const string GroupName = "Abp.Notifications";
/// <summary>
/// 站内通知
/// </summary>
public const string OnsideNotice = GroupName + ".OnsideNotice";
/// <summary>
/// 活动通知
/// </summary>
public const string ActivityNotice = GroupName + ".ActivityNotice";
/// <summary>
/// 系统通知
/// </summary>
public const string SystemNotice = GroupName + ".SystemNotice";
}

44
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/DefaultNotificationDefinitionProvider.cs

@ -0,0 +1,44 @@
using LINGYUN.Abp.Notifications.Localization;
using Volo.Abp.Localization;
namespace LINGYUN.Abp.Notifications.Internal;
internal class DefaultNotificationDefinitionProvider : NotificationDefinitionProvider
{
public override void Define(INotificationDefinitionContext context)
{
var internalGroup = context.AddGroup(
DefaultNotifications.GroupName,
L("Notifications:Internal"));
internalGroup.AddNotification(
DefaultNotifications.OnsideNotice,
L("Notifications:OnsideNotice"),
L("Notifications:OnsideNoticeDesc"),
notificationType: NotificationType.Application,
lifetime: NotificationLifetime.Persistent,
allowSubscriptionToClients: true)
.WithProviders(NotificationProviderNames.SignalR);
internalGroup.AddNotification(
DefaultNotifications.ActivityNotice,
L("Notifications:ActivityNotice"),
L("Notifications:ActivityNoticeDesc"),
notificationType: NotificationType.Application,
lifetime: NotificationLifetime.Persistent,
allowSubscriptionToClients: true)
.WithProviders(NotificationProviderNames.SignalR);
internalGroup.AddNotification(
DefaultNotifications.SystemNotice,
L("Notifications:SystemNotice"),
L("Notifications:SystemNoticeDesc"),
notificationType: NotificationType.System,
lifetime: NotificationLifetime.Persistent,
allowSubscriptionToClients: true)
.WithProviders(NotificationProviderNames.SignalR);
}
protected LocalizableString L(string name)
{
return LocalizableString.Create<NotificationsResource>(name);
}
}

8
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Localization/NotificationsResource.cs

@ -0,0 +1,8 @@
using Volo.Abp.Localization;
namespace LINGYUN.Abp.Notifications.Localization;
[LocalizationResourceName("Notifications")]
public class NotificationsResource
{
}

12
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Localization/Resources/en.json

@ -0,0 +1,12 @@
{
"culture": "en",
"texts": {
"Notifications:Internal": "Internal",
"Notifications:OnsideNotice": "Onside",
"Notifications:OnsideNoticeDesc": "In-app notification push",
"Notifications:ActivityNotice": "Activity",
"Notifications:ActivityNoticeDesc": "The administrator delivers an event notification push",
"Notifications:SystemNotice": "System",
"Notifications:SystemNoticeDesc": "System global notification push"
}
}

12
aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Localization/Resources/zh-Hans.json

@ -0,0 +1,12 @@
{
"culture": "zh-Hans",
"texts": {
"Notifications:Internal": "内置通知",
"Notifications:OnsideNotice": "站内通知",
"Notifications:OnsideNoticeDesc": "应用内部通知推送",
"Notifications:ActivityNotice": "活动通知",
"Notifications:ActivityNoticeDesc": "管理员下发活动通知推送",
"Notifications:SystemNotice": "系统通知",
"Notifications:SystemNoticeDesc": "系统全局消息推送"
}
}

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

@ -145,19 +145,10 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{ {
// 检查是够已订阅消息 // 检查是够已订阅消息
Logger.LogDebug($"Gets a list of user subscriptions {notificationInfo.Name}"); Logger.LogDebug($"Gets a list of user subscriptions {notificationInfo.Name}");
List<NotificationSubscriptionInfo> userSubscriptions;
if (users == null) // 获取用户订阅列表
{ var userSubscriptions = await NotificationSubscriptionManager
// 获取用户订阅列表
userSubscriptions = await NotificationSubscriptionManager
.GetUserSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name);
}
else
{
// 过滤未订阅的用户
userSubscriptions = await NotificationSubscriptionManager
.GetUsersSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name, users); .GetUsersSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name, users);
}
users = userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName)); users = userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName));

73
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/TenantSynchronizer.cs

@ -70,44 +70,51 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
private async Task SendNotificationAsync(CreateEventData eventData) private async Task SendNotificationAsync(CreateEventData eventData)
{ {
var tenantAdminUserIdentifier = new UserIdentifier(eventData.AdminUserId, eventData.AdminEmailAddress); try
{
var tenantAdminUserIdentifier = new UserIdentifier(eventData.AdminUserId, eventData.AdminEmailAddress);
// 租户管理员订阅事件 // 租户管理员订阅事件
await NotificationSubscriptionManager await NotificationSubscriptionManager
.SubscribeAsync( .SubscribeAsync(
eventData.Id, eventData.Id,
tenantAdminUserIdentifier, tenantAdminUserIdentifier,
TenantNotificationNames.NewTenantRegistered); TenantNotificationNames.NewTenantRegistered);
var notificationData = new NotificationData(); var notificationData = new NotificationData();
notificationData.TrySetData("name", eventData.Name); notificationData.TrySetData("name", eventData.Name);
notificationData.WriteLocalizedData( notificationData.WriteLocalizedData(
new LocalizableStringInfo( new LocalizableStringInfo(
LocalizationResourceNameAttribute.GetName(typeof(MessageServiceResource)), LocalizationResourceNameAttribute.GetName(typeof(MessageServiceResource)),
"NewTenantRegisteredNotificationTitle", "NewTenantRegisteredNotificationTitle",
new Dictionary<object, object> new Dictionary<object, object>
{ {
{ "Name", eventData.Name }, { "Name", eventData.Name },
}), }),
new LocalizableStringInfo( new LocalizableStringInfo(
LocalizationResourceNameAttribute.GetName(typeof(MessageServiceResource)), LocalizationResourceNameAttribute.GetName(typeof(MessageServiceResource)),
"NewTenantRegisteredNotificationMessage", "NewTenantRegisteredNotificationMessage",
new Dictionary<object, object> new Dictionary<object, object>
{ {
{ "Name", eventData.Name} { "Name", eventData.Name}
}), }),
DateTime.Now, eventData.AdminEmailAddress); DateTime.Now, eventData.AdminEmailAddress);
Logger.LogInformation("publish new tenant notification.."); Logger.LogInformation("publish new tenant notification..");
// 发布租户创建通知 // 发布租户创建通知
await NotificationSender await NotificationSender
.SendNofiterAsync( .SendNofiterAsync(
TenantNotificationNames.NewTenantRegistered, TenantNotificationNames.NewTenantRegistered,
notificationData, notificationData,
tenantAdminUserIdentifier, tenantAdminUserIdentifier,
eventData.Id, eventData.Id,
NotificationSeverity.Success); NotificationSeverity.Success);
Logger.LogInformation("tenant administrator subscribes to new tenant events.."); Logger.LogInformation("tenant administrator subscribes to new tenant events..");
}
catch(Exception ex)
{
Logger.LogWarning(ex, "Failed to send the tenant initialization notification.");
}
} }
} }
} }

38
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Local/UserCreateSendWelcomeEventHandler.cs

@ -29,19 +29,10 @@ namespace LY.MicroService.RealtimeMessage.EventBus
{ {
var userIdentifer = new UserIdentifier(eventData.Entity.Id, eventData.Entity.UserName); var userIdentifer = new UserIdentifier(eventData.Entity.Id, eventData.Entity.UserName);
// 订阅用户欢迎消息 // 订阅用户欢迎消息
await _notificationSubscriptionManager await SubscribeInternalNotifers(userIdentifer, eventData.Entity.TenantId);
.SubscribeAsync(
eventData.Entity.TenantId,
userIdentifer,
UserNotificationNames.WelcomeToApplication);
var userWelcomeNotifictionData = new NotificationData(); var userWelcomeNotifictionData = new NotificationData();
//userWelcomeNotifictionData.WriteStandardData(
// L("WelcomeToApplicationFormUser", eventData.Entity.Name ?? eventData.Entity.UserName),
// L("WelcomeToApplicationFormUser", eventData.Entity.Name ?? eventData.Entity.UserName),
// DateTime.Now, eventData.Entity.UserName);
userWelcomeNotifictionData.TrySetData("user", eventData.Entity.UserName); userWelcomeNotifictionData.TrySetData("user", eventData.Entity.UserName);
userWelcomeNotifictionData userWelcomeNotifictionData
.WriteLocalizedData( .WriteLocalizedData(
@ -70,5 +61,32 @@ namespace LY.MicroService.RealtimeMessage.EventBus
NotificationSeverity.Info NotificationSeverity.Info
); );
} }
private async Task SubscribeInternalNotifers(UserIdentifier userIdentifer, Guid? tenantId = null)
{
// 订阅内置通知
await _notificationSubscriptionManager
.SubscribeAsync(
tenantId,
userIdentifer,
DefaultNotifications.SystemNotice);
await _notificationSubscriptionManager
.SubscribeAsync(
tenantId,
userIdentifer,
DefaultNotifications.OnsideNotice);
await _notificationSubscriptionManager
.SubscribeAsync(
tenantId,
userIdentifer,
DefaultNotifications.ActivityNotice);
// 订阅用户欢迎消息
await _notificationSubscriptionManager
.SubscribeAsync(
tenantId,
userIdentifer,
UserNotificationNames.WelcomeToApplication);
}
} }
} }

Loading…
Cancel
Save