From 3bbaae4589de56da9ab09360aa28a3cd12198471 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 9 Oct 2018 14:29:47 +0300 Subject: MessageReadEvent refactoring --- .../juick/service/component/MessageReadEvent.java | 10 +++---- .../java/com/juick/components/APNSManager.java | 28 ++++++++--------- .../com/juick/components/NotificationsManager.java | 4 ++- .../java/com/juick/server/CommandsManager.java | 8 ++--- .../main/java/com/juick/server/ServerManager.java | 35 +++++++++++----------- .../java/com/juick/server/TelegramBotManager.java | 2 +- .../main/java/com/juick/server/api/Messages.java | 4 +-- 7 files changed, 45 insertions(+), 46 deletions(-) diff --git a/juick-common/src/main/java/com/juick/service/component/MessageReadEvent.java b/juick-common/src/main/java/com/juick/service/component/MessageReadEvent.java index b314b89b..b070c8cb 100644 --- a/juick-common/src/main/java/com/juick/service/component/MessageReadEvent.java +++ b/juick-common/src/main/java/com/juick/service/component/MessageReadEvent.java @@ -7,21 +7,21 @@ import org.springframework.context.ApplicationEvent; import java.util.List; public class MessageReadEvent extends ApplicationEvent { - private List users; + private User user; private Message message; /** * Create a new ApplicationEvent. * * @param source the object on which the event initially occurred (never {@code null}) */ - public MessageReadEvent(Object source, List users, Message message) { + public MessageReadEvent(Object source, User user, Message message) { super(source); - this.users = users; + this.user = user; this.message = message; } - public List getUsers() { - return users; + public User getUser() { + return user; } public Message getMessage() { diff --git a/juick-notifications/src/main/java/com/juick/components/APNSManager.java b/juick-notifications/src/main/java/com/juick/components/APNSManager.java index 3eaf3c93..2c7cfce4 100644 --- a/juick-notifications/src/main/java/com/juick/components/APNSManager.java +++ b/juick-notifications/src/main/java/com/juick/components/APNSManager.java @@ -95,22 +95,20 @@ public class APNSManager implements NotificationListener { @Override public void processMessageReadEvent(MessageReadEvent messageReadEvent) { - List users = messageReadEvent.getUsers(); + User user = messageReadEvent.getUser(); ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); - users.forEach(user -> { - apnsPayloadBuilder.setBadgeNumber(user.getUnreadCount()); - String payload = apnsPayloadBuilder.buildWithDefaultMaximumLength(); - user.getTokens().stream().filter(t -> t.getType().equals("apns")) - .map(ExternalToken::getToken).forEach(token -> { - Future> notification = apns.sendNotification( - new SimpleApnsPushNotification(token, topic, payload)); - notification.addListener((PushNotificationResponseListener) future -> { - if (future.isSuccess()) { - processAPNSResponse(token, future.getNow()); - } else { - logger.warn("APNS error ", future.cause()); - } - }); + apnsPayloadBuilder.setBadgeNumber(user.getUnreadCount()); + String payload = apnsPayloadBuilder.buildWithDefaultMaximumLength(); + user.getTokens().stream().filter(t -> t.getType().equals("apns")) + .map(ExternalToken::getToken).forEach(token -> { + Future> notification = apns.sendNotification( + new SimpleApnsPushNotification(token, topic, payload)); + notification.addListener((PushNotificationResponseListener) future -> { + if (future.isSuccess()) { + processAPNSResponse(token, future.getNow()); + } else { + logger.warn("APNS error ", future.cause()); + } }); }); } diff --git a/juick-notifications/src/main/java/com/juick/components/NotificationsManager.java b/juick-notifications/src/main/java/com/juick/components/NotificationsManager.java index be97ea40..fce0bed7 100644 --- a/juick-notifications/src/main/java/com/juick/components/NotificationsManager.java +++ b/juick-notifications/src/main/java/com/juick/components/NotificationsManager.java @@ -103,7 +103,9 @@ public class NotificationsManager extends TextWebSocketHandler { jmsg.getUser().getUid()), HttpMethod.GET, null, new ParameterizedTypeReference>() { }).getBody(); - applicationEventPublisher.publishEvent(new MessageReadEvent(this, users, jmsg)); + users.forEach(user -> { + applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, jmsg)); + }); } public void addInvalidGCMToken(String token) { diff --git a/juick-server/src/main/java/com/juick/server/CommandsManager.java b/juick-server/src/main/java/com/juick/server/CommandsManager.java index 297e986f..597604c6 100644 --- a/juick-server/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-server/src/main/java/com/juick/server/CommandsManager.java @@ -123,7 +123,7 @@ public class CommandsManager { Message msg = messagesService.getMessage(mid); subscriptionService.subscribeMessage(msg, user); - applicationEventPublisher.publishEvent(new MessageReadEvent(this, Collections.singletonList(user), msg)); + applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, msg)); applicationEventPublisher.publishEvent(new MessageEvent(this, msg, subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg))); return CommandResult.build(msg, "New message posted.\n#" + msg.getMid() + " https://juick.com/m/" + msg.getMid(), String.format("[New message](%s) posted", PlainTextFormatter.formatUrl(msg))); } @@ -358,7 +358,7 @@ public class CommandsManager { if (subscribe) { if (subscriptionService.subscribeMessage(msg, user)) { applicationEventPublisher.publishEvent( - new MessageReadEvent(this, Collections.singletonList(user), msg)); + new MessageReadEvent(this, user, msg)); return CommandResult.fromString("Subscribed"); } } else { @@ -411,7 +411,7 @@ public class CommandsManager { if (showReplies) { List replies = messagesService.getReplies(user, mid); applicationEventPublisher.publishEvent( - new MessageReadEvent(this, Collections.singletonList(user), msg)); + new MessageReadEvent(this, user, msg)); replies.add(0, msg); return CommandResult.fromString(String.join("\n", replies.stream().map(PlainTextFormatter::formatPostSummary).collect(Collectors.toList()))); @@ -514,7 +514,7 @@ public class CommandsManager { } int newrid = messagesService.createReply(mid, rid, user, txt, attachmentType); applicationEventPublisher.publishEvent( - new MessageReadEvent(this, Collections.singletonList(user), msg)); + new MessageReadEvent(this, user, msg)); if (haveAttachment) { String fname = String.format("%d-%d.%s", mid, newrid, attachmentType); imagesService.saveImageWithPreviews(attachmentFName, fname); diff --git a/juick-server/src/main/java/com/juick/server/ServerManager.java b/juick-server/src/main/java/com/juick/server/ServerManager.java index fce92705..cbef33a5 100644 --- a/juick-server/src/main/java/com/juick/server/ServerManager.java +++ b/juick-server/src/main/java/com/juick/server/ServerManager.java @@ -215,26 +215,25 @@ public class ServerManager implements NotificationListener { @Override public void processMessageReadEvent(MessageReadEvent messageReadEvent) { - List users = messageReadEvent.getUsers(); + User user = messageReadEvent.getUser(); Message source = messageReadEvent.getMessage(); - users.forEach(userTo -> { - logger.info("Message read event from {} for {}", userTo.getName(), source.getMid()); - wsHandler.getClients().stream().filter(c -> - (!c.legacy && c.visitor.getUid() == userTo.getUid()) || c.visitor.getName().equals(serviceUser) - ).forEach(u -> { - Message serviceMessage = new Message(); - serviceMessage.setService(true); - serviceMessage.setUser(userTo); - serviceMessage.setMid(source.getMid()); - serviceMessage.setUnread(false); - try { - u.sendMessage(new TextMessage(jsonMapper.writeValueAsString(serviceMessage))); - } catch (IOException e) { - logger.error("JSON error", e); - } - }); + + logger.info("Message read event from {} for {}", user.getName(), source.getMid()); + Message serviceMessage = new Message(); + serviceMessage.setService(true); + serviceMessage.setUser(user); + serviceMessage.setMid(source.getMid()); + serviceMessage.setUnread(false); + wsHandler.getClients().stream().filter(c -> + (!c.legacy && c.visitor == user) || c.visitor.equals(serviceUser) + ).forEach(u -> { + try { + u.sendMessage(new TextMessage(jsonMapper.writeValueAsString(serviceMessage))); + } catch (IOException e) { + logger.error("JSON error", e); + } }); - sendSseEvent(source, Collections.singletonList(serviceUser)); + sendSseEvent(serviceMessage, Collections.singletonList(serviceUser)); } @Override diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java index 38134188..01dd7b66 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -313,7 +313,7 @@ public class TelegramBotManager implements NotificationListener { .orElseThrow(IllegalStateException::new), source.getMid(), source.getRid()); User user = userService.getUserByUID(userId).orElseThrow(IllegalStateException::new); applicationEventPublisher.publishEvent( - new MessageReadEvent(this, Collections.singletonList(user), source)); + new MessageReadEvent(this, user, source)); } } } diff --git a/juick-server/src/main/java/com/juick/server/api/Messages.java b/juick-server/src/main/java/com/juick/server/api/Messages.java index 6baa154b..424ea492 100644 --- a/juick-server/src/main/java/com/juick/server/api/Messages.java +++ b/juick-server/src/main/java/com/juick/server/api/Messages.java @@ -175,7 +175,7 @@ public class Messages { List replies = messagesService.getReplies(visitor, mid); if (!visitor.isAnonymous()) { applicationEventPublisher.publishEvent( - new MessageReadEvent(this, Collections.singletonList(visitor), msg)); + new MessageReadEvent(this, visitor, msg)); } replies.add(0, msg); return ResponseEntity.ok(replies); @@ -190,7 +190,7 @@ public class Messages { messagesService.setLastReadComment(visitor, mid, rid); Message msg = messagesService.getMessage(mid); applicationEventPublisher.publishEvent( - new MessageReadEvent(this, Collections.singletonList(visitor), msg)); + new MessageReadEvent(this, visitor, msg)); return IOUtils.toByteArray( Objects.requireNonNull(getClass().getClassLoader().getResource("Transparent.gif"))); } -- cgit v1.2.3