From 2f2e9aab6194037259625200091949d8648e3409 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 24 Sep 2019 11:54:21 +0300 Subject: Use single event model for all application events --- src/main/java/com/juick/server/ServerManager.java | 71 ++++++++++------------- 1 file changed, 31 insertions(+), 40 deletions(-) (limited to 'src/main/java/com/juick/server/ServerManager.java') diff --git a/src/main/java/com/juick/server/ServerManager.java b/src/main/java/com/juick/server/ServerManager.java index 5b859125..ca69f7d8 100644 --- a/src/main/java/com/juick/server/ServerManager.java +++ b/src/main/java/com/juick/server/ServerManager.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Message; import com.juick.User; import com.juick.model.AnonymousUser; +import com.juick.server.api.SystemActivity; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.UserService; @@ -34,6 +35,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; +import javax.annotation.Nonnull; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.ArrayList; @@ -91,10 +93,30 @@ public class ServerManager implements NotificationListener { } @Override - public void processMessageEvent(MessageEvent event) { - com.juick.Message jmsg = event.getMessage(); - List subscribedUsers = ListUtils.union(event.getUsers(), Collections.singletonList(jmsg.getUser())); + public void processSystemEvent(@Nonnull SystemEvent systemEvent) { + var activity = systemEvent.getActivity(); + var from = activity.getFrom(); + var message = activity.getMessage(); + var subscribers = activity.getTo(); + if (activity.getType().equals(SystemActivity.ActivityType.message)) { + processMessage(from, message, subscribers); + } else if (activity.getType().equals(SystemActivity.ActivityType.like)) { + if (from.equals(serviceUser)) { + processTop(message); + } + } + try { + var eventHeader = Collections.singletonMap("X-Event-Version", "1.0"); + emailManager.sendEmail("noreply@juick.com", serviceEmail, "New system event", + jsonMapper.writeValueAsString(systemEvent), null, eventHeader); + } catch (JsonProcessingException e) { + logger.warn("JSON exception", e); + } + } + private void processMessage(User from, com.juick.Message jmsg, List subscribers) { + List subscribedUsers = ListUtils.union(subscribers, Collections.singletonList(jmsg.getUser())); if (jmsg.isService()) { + logger.info("Message read event from {} for {}", from.getUid(), jmsg.getMid()); readEvent(jmsg, Collections.singletonList(serviceUser)); return; } @@ -111,21 +133,6 @@ public class ServerManager implements NotificationListener { onJuickMessageReply(jmsg, subscribedUsers); } messageEvent(jmsg, Collections.singletonList(serviceUser)); - try { - emailManager.sendEmail("noreply@juick.com", serviceEmail, "New message", jsonMapper.writeValueAsString(jmsg), null, Collections.emptyMap()); - } catch (JsonProcessingException e) { - logger.warn("JSON exception", e); - } - } - - @Override - public void processSubscribeEvent(SubscribeEvent subscribeEvent) { - - } - - @Override - public void processLikeEvent(LikeEvent likeEvent) { - } @Override @@ -133,35 +140,19 @@ public class ServerManager implements NotificationListener { } - @Override - public void processMessageReadEvent(MessageReadEvent messageReadEvent) { - User user = messageReadEvent.getUser(); - Message source = messageReadEvent.getMessage(); - - logger.info("Message read event from {} for {}", user.getUid(), source.getMid()); - Message serviceMessage = new Message(); - serviceMessage.setService(true); - serviceMessage.setUser(user); - serviceMessage.setMid(source.getMid()); - serviceMessage.setUnread(false); - readEvent(serviceMessage, Arrays.asList(serviceUser, user)); - } - - @Override - public void processTopEvent(TopEvent topEvent) { - User topUser = topEvent.getMessage().getUser(); - topEvent(topEvent.getMessage(), Arrays.asList(topUser, serviceUser)); + private void processTop(Message msg) { + User topUser = msg.getUser(); + topEvent(msg, Arrays.asList(topUser, serviceUser)); } - - public void topEvent(Message msg, List subscribers){ + private void topEvent(Message msg, List subscribers){ sendSseEvent(msg, "top", subscribers); } - public void readEvent(Message msg, List subscribers){ + private void readEvent(Message msg, List subscribers){ sendSseEvent(msg, "read", subscribers); } - public void messageEvent(Message msg, List subscribers) { + private void messageEvent(Message msg, List subscribers) { sendSseEvent(msg, "msg", subscribers); } -- cgit v1.2.3