aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-10-09 14:29:47 +0300
committerGravatar Vitaly Takmazov2018-10-09 14:29:47 +0300
commit3bbaae4589de56da9ab09360aa28a3cd12198471 (patch)
tree436fd6436e582d0de7cbb0b3373e3d2d9fcc97ee
parent9eb0139ed6a0e823a0863c8734278e620587627f (diff)
MessageReadEvent refactoring
-rw-r--r--juick-common/src/main/java/com/juick/service/component/MessageReadEvent.java10
-rw-r--r--juick-notifications/src/main/java/com/juick/components/APNSManager.java28
-rw-r--r--juick-notifications/src/main/java/com/juick/components/NotificationsManager.java4
-rw-r--r--juick-server/src/main/java/com/juick/server/CommandsManager.java8
-rw-r--r--juick-server/src/main/java/com/juick/server/ServerManager.java35
-rw-r--r--juick-server/src/main/java/com/juick/server/TelegramBotManager.java2
-rw-r--r--juick-server/src/main/java/com/juick/server/api/Messages.java4
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<User> 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<User> users, Message message) {
+ public MessageReadEvent(Object source, User user, Message message) {
super(source);
- this.users = users;
+ this.user = user;
this.message = message;
}
- public List<User> 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<User> 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<PushNotificationResponse<SimpleApnsPushNotification>> notification = apns.sendNotification(
- new SimpleApnsPushNotification(token, topic, payload));
- notification.addListener((PushNotificationResponseListener<SimpleApnsPushNotification>) 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<PushNotificationResponse<SimpleApnsPushNotification>> notification = apns.sendNotification(
+ new SimpleApnsPushNotification(token, topic, payload));
+ notification.addListener((PushNotificationResponseListener<SimpleApnsPushNotification>) 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<List<User>>() {
}).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<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")));
}