diff options
author | 2018-10-09 14:29:47 +0300 | |
---|---|---|
committer | 2018-10-09 14:29:47 +0300 | |
commit | 3bbaae4589de56da9ab09360aa28a3cd12198471 (patch) | |
tree | 436fd6436e582d0de7cbb0b3373e3d2d9fcc97ee /juick-server/src/main | |
parent | 9eb0139ed6a0e823a0863c8734278e620587627f (diff) |
MessageReadEvent refactoring
Diffstat (limited to 'juick-server/src/main')
4 files changed, 24 insertions, 25 deletions
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<com.juick.Message> 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<User> 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<com.juick.Message> 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"))); } |