public async Task <List <NotificationSubscriptionInfo> > GetSubscriptionsAsync(string notificationName, NotificationEntityIdentifier entityIdentifier = null) { var notificationSubscriptionInfos = await _store.GetSubscriptionsAsync( notificationName, entityIdentifier == null?null : entityIdentifier.Type.FullName, entityIdentifier == null?null : entityIdentifier.Id ); return(notificationSubscriptionInfos); }
// TODO: Can work only for single database approach! public async Task <List <NotificationSubscription> > GetSubscriptionsAsync(string notificationName, EntityIdentifier entityIdentifier = null) { var notificationSubscriptionInfos = await _store.GetSubscriptionsAsync( notificationName, entityIdentifier == null?null : entityIdentifier.Type.FullName, entityIdentifier == null?null : entityIdentifier.Id.ToJsonString() ); return(notificationSubscriptionInfos .Select(nsi => nsi.ToNotificationSubscription()) .ToList()); }
/// <summary> /// 指定提供者发布通知 /// </summary> /// <param name="providers">提供者列表</param> /// <param name="notificationInfo">通知信息</param> /// <returns></returns> protected async Task PublishFromProvidersAsync(IEnumerable <INotificationPublishProvider> providers, NotificationInfo notificationInfo) { Logger.LogDebug($"Persistent notification {notificationInfo.Name}"); // 持久化通知 await _notificationStore.InsertNotificationAsync(notificationInfo); Logger.LogDebug($"Gets a list of user subscriptions {notificationInfo.Name}"); // 获取用户订阅列表 var userSubscriptions = await _notificationStore.GetSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name); Logger.LogDebug($"Persistent user notifications {notificationInfo.Name}"); // 持久化用户通知 var subscriptionUserIdentifiers = userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName)); await _notificationStore.InsertUserNotificationsAsync(notificationInfo, subscriptionUserIdentifiers.Select(u => u.UserId)); // 发布通知 foreach (var provider in providers) { await PublishAsync(provider, notificationInfo, subscriptionUserIdentifiers); } // TODO: 需要计算队列大小,根据情况是否需要并行发布消息 //Parallel.ForEach(providers, async (provider) => //{ // await PublishAsync(provider, notificationInfo, subscriptionUserIdentifiers); //}); }
private async Task ExecuteAsync(NotificationDistributionJobArgs args) { var notificationInfo = await _notificationStore.GetNotificationOrNullAsync(args.NotificationId); if (notificationInfo == null) { Logger.Warn("NotificationDistributionJob can not continue since could not found notification by id: " + args.NotificationId); return; } Notification notification; try { notification = notificationInfo.ToNotification(); } catch (Exception) { Logger.Warn("NotificationDistributionJob can not continue since could not convert NotificationInfo to Notification for NotificationId: " + args.NotificationId); return; } long[] userIds; if (notificationInfo.UserIds.IsNullOrEmpty()) { userIds = (await _notificationStore.GetSubscriptionsAsync( notificationInfo.NotificationName, notificationInfo.EntityTypeName, notificationInfo.EntityId )) .Select(s => s.UserId) .ToArray(); } else { userIds = notificationInfo.UserIds.Split(",").Select(uidAsStr => Convert.ToInt64(uidAsStr)).ToArray(); } var userNotificationInfos = userIds.Select(userId => new UserNotificationInfo(userId, notificationInfo.Id)).ToList(); await SaveUserNotifications(userNotificationInfos); try { var userNotifications = userNotificationInfos.Select(uni => uni.ToUserNotification(notification)).ToArray(); await RealTimeNotifier.SendNotificationsAsync(userNotifications); } catch (Exception ex) { Logger.Warn(ex.ToString(), ex); } }
public async Task DispatcheAsync(NotificationInfo notification) { // 持久化通知 await _notificationStore.InsertNotificationAsync(notification); // 获取用户订阅列表 var userSubscriptions = await _notificationStore.GetSubscriptionsAsync(notification.TenantId, notification.Name); // 持久化用户通知 var subscriptionUserIds = userSubscriptions.Select(us => us.UserId); await _notificationStore.InsertUserNotificationsAsync(notification, subscriptionUserIds); // 发布用户通知 await _notificationPublisher.PublishAsync(notification, subscriptionUserIds); }
protected virtual async Task <long[]> GetUsers(NotificationInfo notificationInfo) { List <long> userIds; if (!notificationInfo.UserIds.IsNullOrEmpty()) { userIds = notificationInfo .UserIds .Split(',') .Select(long.Parse) .ToList(); } else { //Get subscribed users var subscriptions = await _notificationStore.GetSubscriptionsAsync( notificationInfo.NotificationName, notificationInfo.EntityTypeName, notificationInfo.EntityId); //Get user ids userIds = subscriptions .Select(s => s.UserId) .ToList(); } if (!notificationInfo.ExcludedUserIds.IsNullOrEmpty()) { var excludedUserIds = notificationInfo .ExcludedUserIds .Split(',') .Select(long.Parse) .ToList(); userIds.RemoveAll(uid => excludedUserIds.Any(euid => euid.Equals(uid))); } return(userIds.ToArray()); }
protected virtual async Task <UserIdentifier[]> GetUsers(NotificationInfo notificationInfo) { List <UserIdentifier> userIds; if (!notificationInfo.UserIds.IsNullOrEmpty()) { //Directly get from UserIds userIds = notificationInfo .UserIds .Split(",") .Select(uidAsStr => UserIdentifier.Parse(uidAsStr)) .Where(uid => SettingManager.GetSettingValueForUser <bool>(NotificationSettingNames.ReceiveNotifications, uid.TenantId, uid.UserId)) .ToList(); } else { //Get subscribed users var tenantIds = GetTenantIds(notificationInfo); List <NotificationSubscriptionInfo> subscriptions; if (tenantIds.IsNullOrEmpty() || (tenantIds.Length == 1 && tenantIds[0] == NotificationInfo.AllTenantIds.To <int>())) { //Get all subscribed users of all tenants subscriptions = await _notificationStore.GetSubscriptionsAsync( notificationInfo.NotificationName, notificationInfo.EntityTypeName, notificationInfo.EntityId ); } else { //Get all subscribed users of specified tenant(s) subscriptions = await _notificationStore.GetSubscriptionsAsync( tenantIds, notificationInfo.NotificationName, notificationInfo.EntityTypeName, notificationInfo.EntityId ); } //Remove invalid subscriptions var invalidSubscriptions = new Dictionary <Guid, NotificationSubscriptionInfo>(); //TODO: Group subscriptions per tenant for potential performance improvement foreach (var subscription in subscriptions) { using (CurrentUnitOfWork.SetTenantId(subscription.TenantId)) { if (!await _notificationDefinitionManager.IsAvailableAsync(notificationInfo.NotificationName, new UserIdentifier(subscription.TenantId, subscription.UserId)) || !SettingManager.GetSettingValueForUser <bool>(NotificationSettingNames.ReceiveNotifications, subscription.TenantId, subscription.UserId)) { invalidSubscriptions[subscription.Id] = subscription; } } } subscriptions.RemoveAll(s => invalidSubscriptions.ContainsKey(s.Id)); //Get user ids userIds = subscriptions .Select(s => new UserIdentifier(s.TenantId, s.UserId)) .ToList(); } if (!notificationInfo.ExcludedUserIds.IsNullOrEmpty()) { //Exclude specified users. var excludedUserIds = notificationInfo .ExcludedUserIds .Split(",") .Select(uidAsStr => UserIdentifier.Parse(uidAsStr)) .ToList(); userIds.RemoveAll(uid => excludedUserIds.Any(euid => euid.Equals(uid))); } return(userIds.ToArray()); }
public virtual async Task <List <NotificationSubscriptionInfo> > GetSubscriptionsAsync(Guid?tenantId, string notificationName) { return(await _store.GetSubscriptionsAsync(tenantId, notificationName)); }
protected virtual async Task <long[]> GetUserIds(NotificationInfo notificationInfo) { List <long> userIds; if (!notificationInfo.UserIds.IsNullOrEmpty()) { //Directly get from UserIds userIds = notificationInfo .UserIds .Split(",") .Select(uidAsStr => uidAsStr.To <long>()) .Where(uid => SettingManager.GetSettingValueForUser <bool>(NotificationSettingNames.ReceiveNotifications, null, uid)) .ToList(); } else { //Get subscribed users var tenantIds = GetTenantIds(notificationInfo); List <NotificationSubscriptionInfo> subscriptions; using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant)) { if (tenantIds.IsNullOrEmpty()) { //Get all subscribed users of all tenants subscriptions = await _notificationStore.GetSubscriptionsAsync( notificationInfo.NotificationName, notificationInfo.EntityTypeName, notificationInfo.EntityId ); } else { //Get all subscribed users of specified tenant(s) subscriptions = await _notificationStore.GetSubscriptionsAsync( tenantIds, notificationInfo.NotificationName, notificationInfo.EntityTypeName, notificationInfo.EntityId ); } //Remove invalid subscriptions var invalidSubscriptions = new Dictionary <Guid, NotificationSubscriptionInfo>(); foreach (var subscription in subscriptions) { if (!await _notificationDefinitionManager.IsAvailableAsync(notificationInfo.NotificationName, subscription.TenantId, subscription.UserId) || !SettingManager.GetSettingValueForUser <bool>(NotificationSettingNames.ReceiveNotifications, subscription.TenantId, subscription.UserId)) { invalidSubscriptions[subscription.Id] = subscription; } } subscriptions.RemoveAll(s => invalidSubscriptions.ContainsKey(s.Id)); } //Get user ids userIds = subscriptions .Select(s => s.UserId) .ToList(); } if (!notificationInfo.ExcludedUserIds.IsNullOrEmpty()) { //Exclude specified users. var excludedUserIds = notificationInfo .ExcludedUserIds .Split(",") .Select(uidAsStr => uidAsStr.To <long>()) .ToList(); userIds.RemoveAll(uid => excludedUserIds.Contains(uid)); } return(userIds.ToArray()); }