aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/ServerManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/server/ServerManager.java')
-rw-r--r--src/main/java/com/juick/server/ServerManager.java71
1 files changed, 31 insertions, 40 deletions
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<User> 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<User> subscribers) {
+ List<User> 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<User> subscribers){
+ private void topEvent(Message msg, List<User> subscribers){
sendSseEvent(msg, "top", subscribers);
}
- public void readEvent(Message msg, List<User> subscribers){
+ private void readEvent(Message msg, List<User> subscribers){
sendSseEvent(msg, "read", subscribers);
}
- public void messageEvent(Message msg, List<User> subscribers) {
+ private void messageEvent(Message msg, List<User> subscribers) {
sendSseEvent(msg, "msg", subscribers);
}