aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/api
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-09-24 11:54:21 +0300
committerGravatar Vitaly Takmazov2019-09-24 11:54:21 +0300
commit2f2e9aab6194037259625200091949d8648e3409 (patch)
tree4669e3bf5389bf68581094ab01dee4155a91e24f /src/main/java/com/juick/server/api
parent289afd5a0b510e4501cf421dd9a05bb53bc424c3 (diff)
Use single event model for all application events
Diffstat (limited to 'src/main/java/com/juick/server/api')
-rw-r--r--src/main/java/com/juick/server/api/Messages.java6
-rw-r--r--src/main/java/com/juick/server/api/PM.java6
-rw-r--r--src/main/java/com/juick/server/api/SystemActivity.java88
3 files changed, 95 insertions, 5 deletions
diff --git a/src/main/java/com/juick/server/api/Messages.java b/src/main/java/com/juick/server/api/Messages.java
index 3ac272f2..1c9e37ad 100644
--- a/src/main/java/com/juick/server/api/Messages.java
+++ b/src/main/java/com/juick/server/api/Messages.java
@@ -22,12 +22,12 @@ import com.juick.Tag;
import com.juick.User;
import com.juick.server.Utils;
import com.juick.server.www.WebApp;
-import com.juick.service.component.MessageReadEvent;
import com.juick.model.CommandResult;
import com.juick.server.util.HttpBadRequestException;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
+import com.juick.service.component.SystemEvent;
import com.juick.service.security.annotation.Visitor;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
@@ -192,7 +192,7 @@ public class Messages {
if (!visitor.isAnonymous()) {
userService.updateLastSeen(visitor);
applicationEventPublisher.publishEvent(
- new MessageReadEvent(this, visitor, msg));
+ new SystemEvent(this, SystemActivity.read(visitor, msg)));
}
replies.add(0, msg);
return ResponseEntity.ok(replies);
@@ -209,7 +209,7 @@ public class Messages {
Message msg = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new);
userService.updateLastSeen(visitor);
applicationEventPublisher.publishEvent(
- new MessageReadEvent(this, visitor, msg));
+ new SystemEvent(this, SystemActivity.read(visitor, msg)));
return IOUtils.toByteArray(invisiblePixel.getInputStream());
}
throw new HttpBadRequestException();
diff --git a/src/main/java/com/juick/server/api/PM.java b/src/main/java/com/juick/server/api/PM.java
index b65841c0..4a408dce 100644
--- a/src/main/java/com/juick/server/api/PM.java
+++ b/src/main/java/com/juick/server/api/PM.java
@@ -28,7 +28,7 @@ import com.juick.server.util.WebUtils;
import com.juick.server.www.WebApp;
import com.juick.service.PMQueriesService;
import com.juick.service.UserService;
-import com.juick.service.component.MessageEvent;
+import com.juick.service.component.SystemEvent;
import com.juick.service.security.annotation.Visitor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.MediaType;
@@ -97,7 +97,9 @@ public class PM {
jmsg.setText(body);
jmsg.setTo(userTo);
jmsg.getUser().setAvatar(webApp.getAvatarUrl(jmsg.getUser()));
- applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg, Collections.singletonList(jmsg.getTo())));
+ applicationEventPublisher.publishEvent(
+ new SystemEvent(this,
+ SystemActivity.message(jmsg.getUser(), jmsg, Collections.singletonList(jmsg.getTo()))));
return jmsg;
}
diff --git a/src/main/java/com/juick/server/api/SystemActivity.java b/src/main/java/com/juick/server/api/SystemActivity.java
new file mode 100644
index 00000000..162374d1
--- /dev/null
+++ b/src/main/java/com/juick/server/api/SystemActivity.java
@@ -0,0 +1,88 @@
+package com.juick.server.api;
+
+import com.juick.Message;
+import com.juick.User;
+
+import java.util.List;
+
+public class SystemActivity {
+ public static SystemActivity like(User from, Message message, List<User> subscribers) {
+ var like = new SystemActivity();
+ like.setType(ActivityType.like);
+ like.setFrom(from);
+ like.setMessage(message);
+ like.setTo(subscribers);
+ return like;
+ }
+
+ public static SystemActivity message(User from, Message message, List<User> subscribers) {
+ var msg = new SystemActivity();
+ msg.setType(ActivityType.message);
+ msg.setFrom(from);
+ msg.setMessage(message);
+ msg.setTo(subscribers);
+ return msg;
+ }
+
+ public static SystemActivity read(User from, Message message) {
+ var read = new SystemActivity();
+ read.setType(ActivityType.message);
+ read.setFrom(from);
+ var msg = new Message();
+ msg.setMid(message.getMid());
+ msg.setService(true);
+ msg.setUnread(false);
+ read.setMessage(msg);
+ return read;
+ }
+
+ public static SystemActivity follow(User from, List<User> to) {
+ var follow = new SystemActivity();
+ follow.setFrom(from);
+ follow.setTo(to);
+ return follow;
+ }
+
+ public enum ActivityType {
+ message,
+ like,
+ follow
+ }
+
+ private ActivityType type;
+ private User from;
+ private List<User> to;
+ private Message message;
+
+ public ActivityType getType() {
+ return type;
+ }
+
+ public void setType(ActivityType type) {
+ this.type = type;
+ }
+
+ public User getFrom() {
+ return from;
+ }
+
+ public void setFrom(User from) {
+ this.from = from;
+ }
+
+ public List<User> getTo() {
+ return to;
+ }
+
+ public void setTo(List<User> to) {
+ this.to = to;
+ }
+
+ public Message getMessage() {
+ return message;
+ }
+
+ public void setMessage(Message message) {
+ this.message = message;
+ }
+}