From 4b400bfd218e135dcf4fe2d0cd2b445a2747ccb4 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Mon, 22 Jul 2024 12:55:35 +0100 Subject: [PATCH] feat: Send Badge update each time a Web Notification is updated - MEED-7279 - Meeds-io/MIPs#134 --- .../service/WebNotificationService.java | 22 +++++++- .../service/WebNotificationServiceImpl.java | 50 +++++++++++++++++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/commons-api/src/main/java/org/exoplatform/commons/api/notification/service/WebNotificationService.java b/commons-api/src/main/java/org/exoplatform/commons/api/notification/service/WebNotificationService.java index de3cea7aa5..3643c28360 100644 --- a/commons-api/src/main/java/org/exoplatform/commons/api/notification/service/WebNotificationService.java +++ b/commons-api/src/main/java/org/exoplatform/commons/api/notification/service/WebNotificationService.java @@ -38,7 +38,17 @@ public interface WebNotificationService { * @since PLF 4.2 */ void save(NotificationInfo notification); - + + /** + * Update an existing notification message. + * + * @param notification the notification + * @param moveTop After updating, MUST move the notification to top of list + * @LevelAPI Platform + * @since PLF 4.2 + */ + void update(NotificationInfo notification, boolean moveTop); + /** * Get the notificationInfo for the provided id * @@ -168,4 +178,14 @@ public interface WebNotificationService { */ void resetNumberOnBadge(List plugins, String username); + /** + * Gets the notification by the given conditions + * + * @param pluginId + * @param activityId + * @param userId + * @return {@link NotificationInfo} + */ + NotificationInfo getUnreadNotification(String pluginId, String activityId, String userId); + } diff --git a/commons-component-common/src/main/java/org/exoplatform/commons/notification/impl/service/WebNotificationServiceImpl.java b/commons-component-common/src/main/java/org/exoplatform/commons/notification/impl/service/WebNotificationServiceImpl.java index a5e80e8054..ea33b1a17d 100644 --- a/commons-component-common/src/main/java/org/exoplatform/commons/notification/impl/service/WebNotificationServiceImpl.java +++ b/commons-component-common/src/main/java/org/exoplatform/commons/notification/impl/service/WebNotificationServiceImpl.java @@ -22,6 +22,7 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; + import org.exoplatform.commons.api.notification.NotificationContext; import org.exoplatform.commons.api.notification.channel.AbstractChannel; import org.exoplatform.commons.api.notification.channel.ChannelManager; @@ -67,8 +68,22 @@ public WebNotificationServiceImpl(PluginSettingService pluginSettingService, @Override public void save(NotificationInfo notification) { storage.save(notification); + String username = notification.getTo(); + sendWebNotificationUpdate(username); } - + + @Override + public void update(NotificationInfo notification, boolean moveTop) { + storage.update(notification, moveTop); + String username = notification.getTo(); + sendWebNotificationUpdate(username); + } + + @Override + public NotificationInfo getUnreadNotification(String pluginId, String activityId, String userId) { + return storage.getUnreadNotification(pluginId, activityId, userId); + } + @Override public NotificationInfo getNotificationInfo(String notificationId) { return storage.get(notificationId); @@ -77,11 +92,15 @@ public NotificationInfo getNotificationInfo(String notificationId) { @Override public void markRead(String notificationId) { storage.markRead(notificationId); + NotificationInfo notification = storage.get(notificationId); + String username = notification.getTo(); + sendWebNotificationUpdate(username); } @Override public void markAllRead(String userId) { storage.markAllRead(userId); + sendWebNotificationUpdate(userId, 0); } @Override @@ -90,6 +109,8 @@ public void markAllRead(List plugins, String username) { markAllRead(username); } else { storage.markAllRead(plugins, username); + int badgeNumber = storage.getNumberOnBadge(username); + sendWebNotificationUpdate(username, badgeNumber); } } @@ -128,18 +149,27 @@ public List get(WebNotificationFilter filter, int offset, int limit) { @Override public boolean remove(String notificationId) { - return storage.remove(notificationId); + NotificationInfo notification = storage.get(notificationId); + try { + return storage.remove(notificationId); + } finally { + String username = notification.getTo(); + sendWebNotificationUpdate(username); + } } @Override public void hidePopover(String notificationId) { storage.hidePopover(notificationId); + NotificationInfo notification = storage.get(notificationId); + String username = notification.getTo(); + sendWebNotificationUpdate(username); } @Override public void resetNumberOnBadge(String userId) { storage.resetNumberOnBadge(userId); - WebNotificationSender.sendJsonMessage(userId, new MessageInfo().setNumberOnBadge(0)); + sendWebNotificationUpdate(userId, 0); } @Override @@ -148,7 +178,7 @@ public void resetNumberOnBadge(List plugins, String username) { resetNumberOnBadge(username); } else { storage.resetNumberOnBadge(plugins, username); - WebNotificationSender.sendJsonMessage(username, new MessageInfo()); + sendWebNotificationUpdate(username, 0); } } @@ -199,4 +229,16 @@ private String getNotificationMessage(NotificationContext ctx, NotificationInfo return null; } + private void sendWebNotificationUpdate(String username) { + if (StringUtils.isNotBlank(username)) { + int badgeNumber = storage.getNumberOnBadge(username); + sendWebNotificationUpdate(username, badgeNumber); + } + } + + private void sendWebNotificationUpdate(String username, int badgeNumber) { + WebNotificationSender.sendJsonMessage(username, + new MessageInfo().setNumberOnBadge(badgeNumber)); + } + }