private static void RegisterType(NotificationData data)
        {
            var type = data.GetType();

            var names = type
                        .GetProperties()
                        .Select(p => p.Name)
                        .ToArray();

            Template.RegisterSafeType(type, names);
        }
Exemple #2
0
 public Notification(Guid id, string notificationName, NotificationData data, string entityTypeName, string entityId, NotificationSeverity severity, DateTime creationTime, Guid?tenantId)
 {
     Id = id;
     NotificationName = notificationName;
     Data             = JsonSerializer.Serialize(data);
     DataTypeName     = data?.GetType().AssemblyQualifiedName;
     EntityTypeName   = entityTypeName;
     EntityId         = entityId;
     Severity         = severity;
     CreationTime     = creationTime;
     TenantId         = tenantId;
 }
Exemple #3
0
        //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(
            string notificationName,
            NotificationData data             = null,
            EntityIdentifier entityIdentifier = null,
            NotificationSeverity severity     = NotificationSeverity.Info,
            UserIdentifier[] userIds          = null,
            UserIdentifier[] excludedUserIds  = null,
            int?[] tenantIds = null)
        {
            Guid?notificationId = null;

            using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
            {
                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[] { AbpSession.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 uow.CompleteAsync(); //To get Id of the notification

                notificationId = notificationInfo.Id;
            }

            if (notificationId == null)
            {
                throw new ShaNotificationSaveFailedException(notificationName, data);
            }

            var isShaNotification = data != null && data is ShaNotificationData;

            if (isShaNotification || userIds != null && userIds.Length <= MaxUserCountToDirectlyDistributeANotification)
            {
                await _notificationDistributer.DistributeAsync(notificationId.Value);
            }
            else
            {
                //We enqueue a background job since distributing may get a long time
                await _backgroundJobManager.EnqueueAsync <NotificationDistributionJob, NotificationDistributionJobArgs>(
                    new NotificationDistributionJobArgs(
                        notificationId.Value
                        )
                    );
            }
        }