public virtual async Task PublishAsync( string notificationName, NotificationData data = null, EntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, UserIdentifier[] userIds = null, UserIdentifier[] excludedUserIds = null, int?[] tenantIds = null) { if (notificationName.IsNullOrEmpty()) { throw new ArgumentException("NotificationName can not be null or whitespace!", "notificationName"); } if (!tenantIds.IsNullOrEmpty() && !userIds.IsNullOrEmpty()) { throw new ArgumentException("tenantIds can be set only if userIds is not set!", "tenantIds"); } if (tenantIds.IsNullOrEmpty() && userIds.IsNullOrEmpty()) { tenantIds = new[] { CodeZeroSession.TenantId }; } var notificationInfo = new NotificationInfo(_guidGenerator.Create()) { NotificationName = notificationName, EntityTypeName = entityIdentifier == null ? null : entityIdentifier.Type.FullName, EntityTypeAssemblyQualifiedName = entityIdentifier == null ? null : entityIdentifier.Type.AssemblyQualifiedName, EntityId = entityIdentifier == null ? null : entityIdentifier.Id.ToJsonString(), Severity = severity, UserIds = userIds.IsNullOrEmpty() ? null : userIds.Select(uid => uid.ToUserIdentifierString()).JoinAsString(","), ExcludedUserIds = excludedUserIds.IsNullOrEmpty() ? null : excludedUserIds.Select(uid => uid.ToUserIdentifierString()).JoinAsString(","), TenantIds = tenantIds.IsNullOrEmpty() ? null : tenantIds.JoinAsString(","), Data = data == null ? null : data.ToJsonString(), DataTypeName = data == null ? null : data.GetType().AssemblyQualifiedName }; await _store.InsertNotificationAsync(notificationInfo); await CurrentUnitOfWork.SaveChangesAsync(); //To get Id of the notification if (userIds != null && userIds.Length <= MaxUserCountToDirectlyDistributeANotification) { //We can directly distribute the notification since there are not much receivers await _notificationDistributer.DistributeAsync(notificationInfo.Id); } else { //We enqueue a background job since distributing may get a long time await _backgroundJobManager.EnqueueAsync <NotificationDistributionJob, NotificationDistributionJobArgs>( new NotificationDistributionJobArgs( notificationInfo.Id ) ); } }
public async Task SendAsync( string notificationName, INotificationData data = null, EntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, long[] userIds = null, long[] excludedUserIds = null) { Throw.IfArgumentNull(notificationName, nameof(notificationName)); var notification = new NotificationInfo(_guidGenerator.Create()) { NotificationName = notificationName, NotificationDataTypeAssemblyQualifiedName = data == null ? typeof(NotificationData).AssemblyQualifiedName : data.GetType().AssemblyQualifiedName, EntityTypeName = entityIdentifier?.Type.FullName, EntityTypeAssemblyQualifiedName = entityIdentifier?.Type.AssemblyQualifiedName, EntityId = entityIdentifier?.Id.ToJsonString(), Severity = severity, UserIds = userIds.IsNullOrEmpty() ? null : userIds.Select(uid => uid).JoinAsString(","), ExcludedUserIds = excludedUserIds.IsNullOrEmpty() ? null : excludedUserIds.Select(uid => uid).JoinAsString(","), Data = data?.ToJsonString(), DataTypeName = data?.GetType().AssemblyQualifiedName }; await _unitOfWorkManager.PerformAsyncUow(async() => { await _notificationStore.InsertNotificationAsync(notification); // To get Id of the notification await _unitOfWorkManager.Current.SaveChangesAsync(); if (userIds != null && userIds.Length <= MaxUserCountToDirectlySendANotification) { // We can process the notification since there are not much receivers await _notificationDistributer.DistributeAsync(notification.Id); } else { await _backgroundJobManager .EnqueueAsync <NotificationDistributerJob, NotificationDistributerJobArgs>( new NotificationDistributerJobArgs(notification.Id)); } }); }
//Create EntityIdentifier includes entityType and entityId. /// <inheritdoc /> public virtual async Task PublishAsync( string notificationName, NotificationData data = null, EntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, long[] userIds = null, long[] excludedUserIds = null) { if (notificationName.IsNullOrEmpty()) { throw new ArgumentException("NotificationName can not be null or whitespace!", nameof(notificationName)); } var notificationScheme = new NotificationScheme() { NotificationName = notificationName, EntityTypeName = entityIdentifier?.Type.FullName, EntityTypeAssemblyQualifiedName = entityIdentifier?.Type.AssemblyQualifiedName, EntityId = entityIdentifier?.Id.ToJsonString(), Severity = severity, UserIds = (userIds == null || userIds.Length <= 0) ? null : string.Join(",", userIds), ExcludedUserIds = (excludedUserIds == null || excludedUserIds.Length <= 0) ? null : string.Join(",", excludedUserIds), Data = data?.ToJsonString(), DataTypeName = data?.GetType().AssemblyQualifiedName }; await _notificationRepository.InsertNotificationSchemeAsync(notificationScheme); await _notificationRepository.SaveChangesAsync(); //To get Id of the notification if (userIds != null && userIds.Length <= MaxUserCountToDirectlyDistributeANotification) { //We can directly distribute the notification since there are not much receivers await _notificationDistributer.DistributeAsync(notificationScheme.Id); } else { //We enqueue a background job since distributing may get a long time await _backgroundJobManager.EnqueueAsync(new NotificationDistributionJobArgs(notificationScheme.Id)); } }
public virtual async Task PublishAsync( [NotNull] string notificationName, NotificationData data = null, NotificationEntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, Guid[] userIds = null, Guid[] excludedUserIds = null) { var notificationInfo = new NotificationInfo( _guidGenerator.Create(), notificationName, data, entityIdentifier?.Type.FullName, entityIdentifier?.Id, severity, _clock.Now, _currentTenant.Id ); if (userIds != null && userIds.Length <= MaxUserCountToDirectlyDistributeANotification) { //We can directly distribute the notification since there are not much receivers await _notificationDistributer.DistributeAsync(notificationInfo, userIds, excludedUserIds); } else { //We enqueue a background job since distributing may get a long time await _backgroundJobManager.EnqueueAsync( new NotificationDistributionJobArgs( notificationInfo, userIds, excludedUserIds ) ); } }
public override void Execute(NotificationDistributionJobArgs args) { AsyncHelper.RunSync(() => _notificationDistributer.DistributeAsync(args.NotificationId)); }
public async Task ExecuteAsync(NotificationDistributionJobArgs args) { await _notificationDistributer.DistributeAsync(args.NotificationId); }
public async Task ExecuteAsync(NotificationDistributionJobArgs args) { await _notificationDistributer.DistributeAsync(args.Notification, args.UserIds, args.ExcludedUserIds); }