|
|
|
@ -16,6 +16,7 @@ |
|
|
|
package org.thingsboard.server.service.ws.notification; |
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
@ -53,6 +54,7 @@ import java.util.stream.Collectors; |
|
|
|
@Service |
|
|
|
@TbCoreComponent |
|
|
|
@RequiredArgsConstructor |
|
|
|
@Slf4j |
|
|
|
public class DefaultNotificationCommandsHandler implements NotificationCommandsHandler { |
|
|
|
|
|
|
|
private final NotificationService notificationService; |
|
|
|
@ -64,6 +66,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
|
|
|
|
@Override |
|
|
|
public void handleUnreadNotificationsSubCmd(WebSocketSessionRef sessionRef, NotificationsSubCmd cmd) { |
|
|
|
log.debug("[{}] Handling unread notifications subscription cmd (cmdId: {})", sessionRef.getSessionId(), cmd.getCmdId()); |
|
|
|
SecurityUser user = sessionRef.getSecurityCtx(); |
|
|
|
NotificationsSubscription subscription = NotificationsSubscription.builder() |
|
|
|
.serviceId(serviceInfoProvider.getServiceId()) |
|
|
|
@ -82,6 +85,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
|
|
|
|
@Override |
|
|
|
public void handleUnreadNotificationsCountSubCmd(WebSocketSessionRef sessionRef, NotificationsCountSubCmd cmd) { |
|
|
|
log.debug("[{}] Handling unread notifications count subscription cmd (cmdId: {})", sessionRef.getSessionId(), cmd.getCmdId()); |
|
|
|
SecurityUser user = sessionRef.getSecurityCtx(); |
|
|
|
NotificationsCountSubscription subscription = NotificationsCountSubscription.builder() |
|
|
|
.serviceId(serviceInfoProvider.getServiceId()) |
|
|
|
@ -98,6 +102,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
} |
|
|
|
|
|
|
|
private void fetchUnreadNotifications(NotificationsSubscription subscription) { |
|
|
|
log.trace("[{}, subId: {}] Fetching unread notifications from DB", subscription.getSessionId(), subscription.getSubscriptionId()); |
|
|
|
PageData<Notification> notifications = notificationService.findLatestUnreadNotificationsByUserId(subscription.getTenantId(), |
|
|
|
(UserId) subscription.getEntityId(), subscription.getLimit()); |
|
|
|
subscription.getUnreadNotifications().clear(); |
|
|
|
@ -106,6 +111,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
} |
|
|
|
|
|
|
|
private void fetchUnreadNotificationsCount(NotificationsCountSubscription subscription) { |
|
|
|
log.trace("[{}, subId: {}] Fetching unread notifications count from DB", subscription.getSessionId(), subscription.getSubscriptionId()); |
|
|
|
int unreadCount = notificationService.countUnreadNotificationsByUserId(subscription.getTenantId(), (UserId) subscription.getEntityId()); |
|
|
|
subscription.getUnreadCounter().set(unreadCount); |
|
|
|
} |
|
|
|
@ -121,6 +127,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
} |
|
|
|
|
|
|
|
private void handleNotificationUpdate(NotificationsSubscription subscription, NotificationUpdate update) { |
|
|
|
log.trace("[{}, subId: {}] Handling notification update: {}", subscription.getSessionId(), subscription.getSubscriptionId(), update); |
|
|
|
Notification notification = update.getNotification(); |
|
|
|
if (notification.getStatus() == NotificationStatus.READ) { |
|
|
|
fetchUnreadNotifications(subscription); |
|
|
|
@ -139,6 +146,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
} |
|
|
|
|
|
|
|
private void handleNotificationRequestUpdate(NotificationsSubscription subscription, NotificationRequestUpdate update) { |
|
|
|
log.trace("[{}, subId: {}] Handling notification request update: {}", subscription.getSessionId(), subscription.getSubscriptionId(), update); |
|
|
|
NotificationRequestId notificationRequestId = update.getNotificationRequestId(); |
|
|
|
if (update.isDeleted()) { |
|
|
|
if (subscription.getUnreadNotifications().values().stream() |
|
|
|
@ -165,10 +173,10 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
} else if (subscriptionUpdate.getNotificationRequestUpdate() != null) { |
|
|
|
handleNotificationRequestUpdate(subscription, subscriptionUpdate.getNotificationRequestUpdate()); |
|
|
|
} |
|
|
|
sendUpdate(subscription.getSessionId(), subscription.createUpdate()); |
|
|
|
} |
|
|
|
|
|
|
|
private void handleNotificationUpdate(NotificationsCountSubscription subscription, NotificationUpdate update) { |
|
|
|
log.trace("[{}, subId: {}] Handling notification update for count sub: {}", subscription.getSessionId(), subscription.getSubscriptionId(), update); |
|
|
|
Notification notification = update.getNotification(); |
|
|
|
if (update.isNew()) { |
|
|
|
subscription.getUnreadCounter().incrementAndGet(); |
|
|
|
@ -180,6 +188,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
} |
|
|
|
|
|
|
|
private void handleNotificationRequestUpdate(NotificationsCountSubscription subscription, NotificationRequestUpdate update) { |
|
|
|
log.trace("[{}, subId: {}] Handling notification request update for count sub: {}", subscription.getSessionId(), subscription.getSubscriptionId(), update); |
|
|
|
if (update.isDeleted()) { |
|
|
|
fetchUnreadNotificationsCount(subscription); |
|
|
|
sendUpdate(subscription.getSessionId(), subscription.createUpdate()); |
|
|
|
@ -188,6 +197,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH |
|
|
|
|
|
|
|
|
|
|
|
private void sendUpdate(String sessionId, CmdUpdate update) { |
|
|
|
log.trace("[{}] Sending WS update: {}", sessionId, update); |
|
|
|
wsService.sendWsMsg(sessionId, update); |
|
|
|
} |
|
|
|
|
|
|
|
|