public virtual async Task InsertUserNotificationsAsync(
            NotificationInfo notification,
            IEnumerable <Guid> userIds,
            CancellationToken cancellationToken = default)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
                using (_currentTenant.Change(notification.TenantId))
                {
                    var userNofitications = new List <UserNotification>();
                    foreach (var userId in userIds)
                    {
                        // 重复检查
                        // TODO:如果存在很多个订阅用户,这是个隐患
                        if (!await _userNotificationRepository.AnyAsync(userId, notification.GetId(), cancellationToken))
                        {
                            var userNofitication = new UserNotification(notification.GetId(), userId, notification.TenantId);
                            userNofitications.Add(userNofitication);
                        }
                    }
                    await _userNotificationRepository
                    .InsertUserNotificationsAsync(userNofitications, cancellationToken);

                    await unitOfWork.SaveChangesAsync(cancellationToken);
                }
        }
Exemple #2
0
        /// <summary>
        /// 发布通知
        /// </summary>
        /// <param name="provider">通知发布者</param>
        /// <param name="notificationInfo">通知信息</param>
        /// <param name="subscriptionUserIdentifiers">订阅用户列表</param>
        /// <returns></returns>
        protected async Task PublishAsync(INotificationPublishProvider provider, NotificationInfo notificationInfo,
                                          IEnumerable <UserIdentifier> subscriptionUserIdentifiers)
        {
            try
            {
                Logger.LogDebug($"Sending notification with provider {provider.Name}");
                var notifacationDataMapping = Options.NotificationDataMappings
                                              .GetMapItemOrNull(provider.Name, notificationInfo.CateGory);
                if (notifacationDataMapping != null)
                {
                    notificationInfo.Data = notifacationDataMapping.MappingFunc(notificationInfo.Data);
                }
                // 发布
                await provider.PublishAsync(notificationInfo, subscriptionUserIdentifiers);

                Logger.LogDebug($"Send notification {notificationInfo.Name} with provider {provider.Name} was successful");
            }
            catch (Exception ex)
            {
                Logger.LogWarning($"Send notification error with provider {provider.Name}");
                Logger.LogWarning($"Error message:{ex.Message}");

                Logger.LogTrace(ex, $"Send notification error with provider { provider.Name}");

                Logger.LogDebug($"Send notification error, notification {notificationInfo.Name} entry queue");
                // 发送失败的消息进入后台队列
                await BackgroundJobManager.EnqueueAsync(
                    new NotificationPublishJobArgs(notificationInfo.GetId(),
                                                   provider.GetType().AssemblyQualifiedName,
                                                   subscriptionUserIdentifiers.ToList(),
                                                   notificationInfo.TenantId));
            }
        }
        public virtual async Task InsertNotificationAsync(
            NotificationInfo notification,
            CancellationToken cancellationToken = default)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
                using (_currentTenant.Change(notification.TenantId))
                {
                    var notify = new Notification(
                        notification.GetId(),
                        notification.Name,
                        notification.Data.GetType().AssemblyQualifiedName,
                        _jsonSerializer.Serialize(notification.Data),
                        notification.Severity, notification.TenantId)
                    {
                        CreationTime = _clock.Now,
                        Type         = notification.Type,
                        // TODO: 通知过期时间应该可以配置
                        ExpirationTime = _clock.Now.AddDays(60)
                    };

                    await _notificationRepository.InsertAsync(notify, cancellationToken : cancellationToken);

                    notification.Id = notify.NotificationId.ToString();

                    await unitOfWork.SaveChangesAsync(cancellationToken);
                }
        }
Exemple #4
0
        public async Task InsertUserNotificationAsync(NotificationInfo notification, Guid userId)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
                using (CurrentTenant.Change(notification.TenantId))
                {
                    var userNotification = new UserNotification(notification.GetId(), userId, notification.TenantId);
                    await UserNotificationRepository.InsertAsync(userNotification);

                    await unitOfWork.SaveChangesAsync();
                }
        }
Exemple #5
0
        public async Task DeleteNotificationAsync(NotificationInfo notification)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
                using (CurrentTenant.Change(notification.TenantId))
                {
                    var notify = await NotificationRepository.GetByIdAsync(notification.GetId());

                    await NotificationRepository.DeleteAsync(notify.Id);

                    await unitOfWork.SaveChangesAsync();
                }
        }
Exemple #6
0
        public async Task InsertUserNotificationsAsync(NotificationInfo notification, IEnumerable <Guid> userIds)
        {
            // 添加工作单元
            using (var unitOfWork = _unitOfWorkManager.Begin())
                using (CurrentTenant.Change(notification.TenantId))
                {
                    var userNofitications = new List <UserNotification>();
                    foreach (var userId in userIds)
                    {
                        // 重复检查
                        // TODO:如果存在很多个订阅用户,这是个隐患
                        if (!await UserNotificationRepository.AnyAsync(userId, notification.GetId()))
                        {
                            var userNofitication = new UserNotification(notification.GetId(), userId, notification.TenantId);
                            userNofitications.Add(userNofitication);
                        }
                    }
                    await UserNotificationRepository.InsertUserNotificationsAsync(userNofitications);

                    await unitOfWork.SaveChangesAsync();
                }
        }
        public virtual async Task DeleteNotificationAsync(
            NotificationInfo notification,
            CancellationToken cancellationToken = default)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
                using (_currentTenant.Change(notification.TenantId))
                {
                    var notify = await _notificationRepository.GetByIdAsync(notification.GetId(), cancellationToken);

                    await _notificationRepository.DeleteAsync(notify.Id, cancellationToken : cancellationToken);

                    await unitOfWork.SaveChangesAsync(cancellationToken);
                }
        }
        public virtual async Task InsertUserNotificationAsync(
            NotificationInfo notification,
            Guid userId,
            CancellationToken cancellationToken = default)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
                using (_currentTenant.Change(notification.TenantId))
                {
                    var userNotification = new UserNotification(notification.GetId(), userId, notification.TenantId);
                    await _userNotificationRepository
                    .InsertAsync(userNotification, cancellationToken : cancellationToken);

                    await unitOfWork.SaveChangesAsync(cancellationToken);
                }
        }
Exemple #9
0
        private async Task AddNotificationsAsync()
        {
            var notification1 = new NotificationInfo
            {
                Name         = NotificationsTestsNames.Test1,
                Severity     = NotificationSeverity.Success,
                CreationTime = DateTime.Now,
                Lifetime     = NotificationLifetime.OnlyOne,
                Type         = NotificationType.Application
            };

            var notification2 = new NotificationInfo
            {
                Name         = NotificationsTestsNames.Test2,
                Severity     = NotificationSeverity.Success,
                CreationTime = DateTime.Now,
                Lifetime     = NotificationLifetime.Persistent,
                Type         = NotificationType.Application
            };

            var notification3 = new NotificationInfo
            {
                Name         = NotificationsTestsNames.Test3,
                Severity     = NotificationSeverity.Success,
                CreationTime = DateTime.Now,
                Lifetime     = NotificationLifetime.OnlyOne,
                Type         = NotificationType.User
            };

            await _notificationStore.InsertNotificationAsync(notification1);

            await _notificationStore.InsertNotificationAsync(notification2);

            await _notificationStore.InsertNotificationAsync(notification3);

            NotificationsTestConsts.NotificationId1 = notification1.GetId();
            NotificationsTestConsts.NotificationId2 = notification2.GetId();
            NotificationsTestConsts.NotificationId3 = notification3.GetId();

            await AddUserNotificationsAsync(notification2, new Guid[] { NotificationsTestConsts.User1Id, NotificationsTestConsts.User2Id });
            await AddUserNotificationsAsync(notification3, new Guid[] { NotificationsTestConsts.User1Id });

            await _notificationStore.ChangeUserNotificationReadStateAsync(null, NotificationsTestConsts.User1Id, NotificationsTestConsts.NotificationId3, NotificationReadState.Read);
        }