|
|
@ -43,6 +43,7 @@ import org.thingsboard.server.common.data.notification.NotificationType; |
|
|
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; |
|
|
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; |
|
|
import org.thingsboard.server.common.data.notification.template.DeliveryMethodNotificationTemplate; |
|
|
import org.thingsboard.server.common.data.notification.template.DeliveryMethodNotificationTemplate; |
|
|
import org.thingsboard.server.common.data.notification.template.NotificationTemplate; |
|
|
import org.thingsboard.server.common.data.notification.template.NotificationTemplate; |
|
|
|
|
|
import org.thingsboard.server.common.data.notification.template.PushDeliveryMethodNotificationTemplate; |
|
|
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|
|
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|
|
import org.thingsboard.server.common.msg.queue.ServiceType; |
|
|
import org.thingsboard.server.common.msg.queue.ServiceType; |
|
|
import org.thingsboard.server.common.msg.queue.TbCallback; |
|
|
import org.thingsboard.server.common.msg.queue.TbCallback; |
|
|
@ -97,7 +98,7 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple |
|
|
NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId); |
|
|
NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId); |
|
|
NotificationTemplate notificationTemplate = notificationTemplateService.findNotificationTemplateById(tenantId, notificationRequest.getTemplateId()); |
|
|
NotificationTemplate notificationTemplate = notificationTemplateService.findNotificationTemplateById(tenantId, notificationRequest.getTemplateId()); |
|
|
|
|
|
|
|
|
notificationRequest.getDeliveryMethods().forEach(deliveryMethod -> { |
|
|
notificationTemplate.getConfiguration().getDeliveryMethodsTemplates().keySet().forEach(deliveryMethod -> { |
|
|
if (settings.getDeliveryMethodsConfigs().containsKey(deliveryMethod) && |
|
|
if (settings.getDeliveryMethodsConfigs().containsKey(deliveryMethod) && |
|
|
!settings.getDeliveryMethodsConfigs().get(deliveryMethod).isEnabled()) { |
|
|
!settings.getDeliveryMethodsConfigs().get(deliveryMethod).isEnabled()) { |
|
|
throw new IllegalArgumentException("Delivery method " + deliveryMethod + " is disabled"); |
|
|
throw new IllegalArgumentException("Delivery method " + deliveryMethod + " is disabled"); |
|
|
@ -106,10 +107,6 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple |
|
|
if (!settings.getDeliveryMethodsConfigs().containsKey(deliveryMethod)) { |
|
|
if (!settings.getDeliveryMethodsConfigs().containsKey(deliveryMethod)) { |
|
|
throw new IllegalArgumentException("Slack must be configured in the settings"); |
|
|
throw new IllegalArgumentException("Slack must be configured in the settings"); |
|
|
} |
|
|
} |
|
|
if (!notificationTemplate.getConfiguration().getTemplates().containsKey(deliveryMethod)) { |
|
|
|
|
|
throw new IllegalArgumentException("To send notification via Slack, " + |
|
|
|
|
|
"you need to configure corresponding template"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
@ -139,7 +136,7 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple |
|
|
DaoUtil.processBatches(pageLink -> { |
|
|
DaoUtil.processBatches(pageLink -> { |
|
|
return notificationTargetService.findRecipientsForNotificationTarget(tenantId, ctx.getCustomerId(), targetId, pageLink); |
|
|
return notificationTargetService.findRecipientsForNotificationTarget(tenantId, ctx.getCustomerId(), targetId, pageLink); |
|
|
}, 200, recipientsBatch -> { |
|
|
}, 200, recipientsBatch -> { |
|
|
for (NotificationDeliveryMethod deliveryMethod : savedNotificationRequest.getDeliveryMethods()) { |
|
|
for (NotificationDeliveryMethod deliveryMethod : ctx.getDeliveryMethods()) { |
|
|
List<User> recipients = recipientsBatch.getData(); |
|
|
List<User> recipients = recipientsBatch.getData(); |
|
|
log.debug("Sending {} notifications for request {} to recipients batch ({})", deliveryMethod, savedNotificationRequest.getId(), recipients.size()); |
|
|
log.debug("Sending {} notifications for request {} to recipients batch ({})", deliveryMethod, savedNotificationRequest.getId(), recipients.size()); |
|
|
NotificationChannel notificationChannel = channels.get(deliveryMethod); |
|
|
NotificationChannel notificationChannel = channels.get(deliveryMethod); |
|
|
@ -168,11 +165,11 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple |
|
|
if (senderId != null) { |
|
|
if (senderId != null) { |
|
|
if (stats.getErrors().isEmpty()) { |
|
|
if (stats.getErrors().isEmpty()) { |
|
|
int sent = stats.getSent().values().stream().mapToInt(AtomicInteger::get).sum(); |
|
|
int sent = stats.getSent().values().stream().mapToInt(AtomicInteger::get).sum(); |
|
|
sendBasicNotification(tenantId, senderId, NotificationType.COMPLETED, "Notifications sent", |
|
|
sendBasicNotification(tenantId, senderId, "Notifications sent", |
|
|
"All notifications were successfully sent (" + sent + ")"); |
|
|
"All notifications were successfully sent (" + sent + ")"); |
|
|
} else { |
|
|
} else { |
|
|
int failures = stats.getErrors().values().stream().mapToInt(Map::size).sum(); |
|
|
int failures = stats.getErrors().values().stream().mapToInt(Map::size).sum(); |
|
|
sendBasicNotification(tenantId, senderId, NotificationType.FAILURE, "Notification failure", |
|
|
sendBasicNotification(tenantId, senderId, "Notification failure", |
|
|
"Some notifications were not sent (" + failures + ")"); // TODO: 'Go to request' button
|
|
|
"Some notifications were not sent (" + failures + ")"); // TODO: 'Go to request' button
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -211,13 +208,14 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public ListenableFuture<Void> sendNotification(User recipient, String text, NotificationProcessingContext ctx) { |
|
|
public ListenableFuture<Void> sendNotification(User recipient, String text, NotificationProcessingContext ctx) { |
|
|
|
|
|
PushDeliveryMethodNotificationTemplate template = ctx.getTemplate(NotificationDeliveryMethod.PUSH); |
|
|
NotificationRequest request = ctx.getRequest(); |
|
|
NotificationRequest request = ctx.getRequest(); |
|
|
log.trace("Creating notification for recipient {} (notification request id: {})", recipient.getId(), request.getId()); |
|
|
log.trace("Creating notification for recipient {} (notification request id: {})", recipient.getId(), request.getId()); |
|
|
Notification notification = Notification.builder() |
|
|
Notification notification = Notification.builder() |
|
|
.requestId(request.getId()) |
|
|
.requestId(request.getId()) |
|
|
.recipientId(recipient.getId()) |
|
|
.recipientId(recipient.getId()) |
|
|
.type(ctx.getNotificationTemplate().getNotificationType()) |
|
|
.type(ctx.getNotificationTemplate().getNotificationType()) |
|
|
.subject(ctx.getNotificationTemplate().getNotificationSubject()) |
|
|
.subject(template.getSubject()) |
|
|
.text(text) |
|
|
.text(text) |
|
|
.info(request.getInfo()) |
|
|
.info(request.getInfo()) |
|
|
.originatorType(request.getOriginatorType()) |
|
|
.originatorType(request.getOriginatorType()) |
|
|
@ -238,10 +236,10 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void sendBasicNotification(TenantId tenantId, UserId recipientId, NotificationType type, String subject, String text) { |
|
|
public void sendBasicNotification(TenantId tenantId, UserId recipientId, String subject, String text) { |
|
|
Notification notification = Notification.builder() |
|
|
Notification notification = Notification.builder() |
|
|
.recipientId(recipientId) |
|
|
.recipientId(recipientId) |
|
|
.type(type) |
|
|
.type(NotificationType.GENERAL) |
|
|
.subject(subject) |
|
|
.subject(subject) |
|
|
.text(text) |
|
|
.text(text) |
|
|
.originatorType(NotificationOriginatorType.SYSTEM) |
|
|
.originatorType(NotificationOriginatorType.SYSTEM) |
|
|
|