From 3bbaae4589de56da9ab09360aa28a3cd12198471 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 9 Oct 2018 14:29:47 +0300 Subject: MessageReadEvent refactoring --- .../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 +-- 4 files changed, 24 insertions(+), 25 deletions(-) (limited to 'juick-server/src/main/java/com') 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