Skip to content

Commit

Permalink
feat: Send Badge update each time a Web Notification is updated - MEE…
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker committed Aug 8, 2024
1 parent 6ef1823 commit 4b400bf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -168,4 +178,14 @@ public interface WebNotificationService {
*/
void resetNumberOnBadge(List<String> 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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -90,6 +109,8 @@ public void markAllRead(List<String> plugins, String username) {
markAllRead(username);
} else {
storage.markAllRead(plugins, username);
int badgeNumber = storage.getNumberOnBadge(username);
sendWebNotificationUpdate(username, badgeNumber);
}
}

Expand Down Expand Up @@ -128,18 +149,27 @@ public List<String> 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
Expand All @@ -148,7 +178,7 @@ public void resetNumberOnBadge(List<String> plugins, String username) {
resetNumberOnBadge(username);
} else {
storage.resetNumberOnBadge(plugins, username);
WebNotificationSender.sendJsonMessage(username, new MessageInfo());
sendWebNotificationUpdate(username, 0);
}
}

Expand Down Expand Up @@ -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));
}

}

0 comments on commit 4b400bf

Please sign in to comment.