Beispiel #1
0
        public static void Enqueue(Notification notification, CallbackContext context)
        {
            QueuedNotification qn = new QueuedNotification(notification, context);

            queue.Enqueue(qn);
            timer.Start();
        }
 public void Create(QueuedNotification notification)
 {
     if (notification != null)
     {
         Repository.Insert(notification);
     }
 }
Beispiel #3
0
        public virtual void NotifyObservers <T>(T message)
        {
            QueuedNotification template = CastToMessageTemplate(message);

            if (template == null)
            {
                return;
            }

            Observers.ToList().ForEach(e => e.Notify(template));
        }
        /// <summary>
        /// Deleted a queued notification
        /// </summary>
        /// <param name="queuedNotification">Queued notification</param>
        public virtual void DeleteQueuedNotification(QueuedNotification queuedNotification)
        {
            if (queuedNotification == null)
            {
                throw new ArgumentNullException(nameof(queuedNotification));
            }

            _queuedNotificationRepository.Delete(queuedNotification);

            //event notification
            _eventPublisher.EntityDeleted(queuedNotification);
        }
        private async Task SendNotificationAsync(QueuedNotification notification)
        {
            logger.Debug(() => $"SendNotificationAsync");

            try
            {
                var poolConfig = !string.IsNullOrEmpty(notification.PoolId) ? poolConfigs[notification.PoolId] : null;

                switch (notification.Category)
                {
                case NotificationCategory.Admin:
                    if (clusterConfig.Notifications?.Admin?.Enabled == true)
                    {
                        await SendEmailAsync(adminEmail, notification.Subject, notification.Msg);
                    }
                    break;

                case NotificationCategory.Block:
                    // notify admin
                    if (clusterConfig.Notifications?.Admin?.Enabled == true &&
                        clusterConfig.Notifications?.Admin?.NotifyBlockFound == true)
                    {
                        await SendEmailAsync(adminEmail, notification.Subject, notification.Msg);
                    }

                    break;

                case NotificationCategory.PaymentSuccess:
                    // notify admin
                    if (clusterConfig.Notifications?.Admin?.Enabled == true &&
                        clusterConfig.Notifications?.Admin?.NotifyPaymentSuccess == true)
                    {
                        await SendEmailAsync(adminEmail, notification.Subject, notification.Msg);
                    }

                    break;

                case NotificationCategory.PaymentFailure:
                    // notify admin
                    if (clusterConfig.Notifications?.Admin?.Enabled == true &&
                        clusterConfig.Notifications?.Admin?.NotifyPaymentSuccess == true)
                    {
                        await SendEmailAsync(adminEmail, notification.Subject, notification.Msg);
                    }
                    break;
                }
            }

            catch (Exception ex)
            {
                logger.Error(ex, $"Error sending notification");
            }
        }
 public static SMSMessage BuildMessageFromNotification(QueuedNotification notification)
 {
     SMSMessage message = null;
     if (notification != null)
     {
         message = new SMSMessage
         {
             Body = notification.Message,
             Recipient = notification.RecipientAddress
         };
     }
     return message;
 }
 public static Notification Convert(QueuedNotification notification)
 {
     var notification2 = new Notification();
     if (notification != null)
     {
         notification2.DateSent = DateTime.Now;
         notification2.IsSent = true;
         notification2.Message = notification.Message;
         notification2.RecipientAddress = notification.RecipientAddress;
         notification2.NotificationCateoryCategoryId = notification.NotificationCateoryCategoryId;
     }
     return notification2;
 }
Beispiel #8
0
        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (queue.Count > 0)
            {
                QueuedNotification qn = queue.Dequeue();
                growl.Notify(qn.Notification, qn.Context);
            }

            // if the queue has been emptied, stop the timer (it will get restarted when another item is enqueued)
            if (queue.Count == 0)
            {
                timer.Stop();
            }
        }
        public virtual int SendNotification(string userIds, MessageTemplate messageTemplate,
                                            EmailAccount emailAccount, int languageId, IEnumerable <Token> tokens,
                                            string toEmailAddress, string toName,
                                            string attachmentFilePath  = null, string attachmentFileName = null,
                                            string replyToEmailAddress = null, string replyToName        = null,
                                            string fromEmail           = null, string fromName = null, string subject = null)
        {
            if (messageTemplate == null)
            {
                throw new ArgumentNullException(nameof(messageTemplate));
            }

            if (emailAccount == null)
            {
                throw new ArgumentNullException(nameof(emailAccount));
            }

            SendNotification(messageTemplate, emailAccount, languageId, tokens, toEmailAddress, toName,
                             attachmentFilePath, attachmentFileName, replyToEmailAddress, replyToName, fromEmail, fromName, subject);

            //retrieve localized message template data
            if (string.IsNullOrEmpty(subject))
            {
                subject = _localizationService.GetLocalized(messageTemplate, mt => mt.Subject, languageId);
            }
            var body = _localizationService.GetLocalized(messageTemplate, mt => mt.Body, languageId);

            //Replace subject and body tokens
            var subjectReplaced = _tokenizer.Replace(subject, tokens, false);
            var bodyReplaced    = _tokenizer.Replace(body, tokens, true);

            var notification = new QueuedNotification
            {
                Priority              = QueuedNotificationPriority.High,
                UserIds               = userIds,
                Subject               = subjectReplaced,
                Body                  = bodyReplaced,
                AttachmentFilePath    = attachmentFilePath,
                AttachmentFileName    = attachmentFileName,
                AttachedDownloadId    = messageTemplate.AttachedDownloadId,
                CreatedOnUtc          = DateTime.UtcNow,
                DontSendBeforeDateUtc = !messageTemplate.DelayBeforeSend.HasValue ? null
                    : (DateTime?)(DateTime.UtcNow + TimeSpan.FromHours(messageTemplate.DelayPeriod.ToHours(messageTemplate.DelayBeforeSend.Value)))
            };

            _queuedNotificationService.InsertQueuedNotification(notification);
            return(notification.Id);
        }
        private async Task SendNotificationAsync(QueuedNotification notification)
        {
            logger.Debug(() => $"SendNotificationAsync");

            foreach (var sender in notificationSenders)
            {
                try
                {
                    string recipient = null;

                    // assign recipient if necessary
                    if (notification.Category != NotificationCategory.Admin)
                    {
                        recipient = notification.Recipient;
                    }
                    else
                    {
                        switch (sender.Metadata.NotificationType)
                        {
                        case NotificationType.Email:
                            recipient = adminEmail;
                            break;

                        case NotificationType.Sms:
                            recipient = adminPhone;
                            break;
                        }
                    }

                    if (string.IsNullOrEmpty(recipient))
                    {
                        logger.Warn(() => $"No recipient for {notification.Category.ToString().ToLower()}");
                        continue;
                    }

                    // send it
                    await sender.Value.NotifyAsync(recipient, notification.Subject, notification.Msg);
                }

                catch (Exception ex)
                {
                    logger.Error(ex, $"Error sending notification using {sender.GetType().Name}");
                }
            }
        }
        public virtual int SendNotification(string userIds, string subject, string body,
                                            string attachmentFilePath = null, string attachmentFileName = null)
        {
            MessageTemplate messageTemplate = _messageTemplateService.GetMessageTemplatesByName("").FirstOrDefault();

            var notification = new QueuedNotification
            {
                Priority              = QueuedNotificationPriority.High,
                UserIds               = userIds,
                Subject               = subject,
                Body                  = body,
                AttachmentFilePath    = attachmentFilePath,
                AttachmentFileName    = attachmentFileName,
                AttachedDownloadId    = messageTemplate?.AttachedDownloadId ?? 0,
                CreatedOnUtc          = DateTime.UtcNow,
                DontSendBeforeDateUtc = (!messageTemplate?.DelayBeforeSend.HasValue ?? true) ? null
                                         : (DateTime?)(DateTime.UtcNow + TimeSpan.FromHours(messageTemplate.DelayPeriod.ToHours(messageTemplate.DelayBeforeSend.Value)))
            };

            _queuedNotificationService.InsertQueuedNotification(notification);
            return(notification.Id);
        }
Beispiel #12
0
        private async Task SendNotificationAsync(QueuedNotification notification)
        {
            _logger.Debug("SendNotificationAsync");

            try
            {
                if (notification.Category == NotificationCategory.PaymentFailure)
                {
                    if (_contextHolder.Config.Notifications?.DiscordNotifications?.Enabled == true)
                    {
                        DiscordNotifications discordNotifications = _contextHolder.Config.Notifications.DiscordNotifications;
                        await SendDiscordNotificationAsync(discordNotifications.WebHookUrl, notification.Msg,
                                                           discordNotifications.Username,
                                                           discordNotifications.AvatarUrl);
                    }
                }
            }

            catch (Exception ex)
            {
                _logger.Error(ex);
            }
        }
 public QueuedNotificationEventArgs(QueuedNotification q)
 {
     this._notification = q;
 }
 public static void Enqueue(Notification notification, CallbackContext context)
 {
     QueuedNotification qn = new QueuedNotification(notification, context);
     queue.Enqueue(qn);
     timer.Start();
 }