aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-05-10 12:50:08 +0300
committerGravatar Vitaly Takmazov2018-05-10 12:50:08 +0300
commitfb540605626e1c7bb1544a518a254e6a820afcb3 (patch)
treeb370df3f031a1a72a34b64bc197cd600b6947f5d
parentd2a1f21717c8583c9804c69319123fb7ebaac261 (diff)
server: MessageReadEvent
-rw-r--r--juick-common/src/main/java/com/juick/Message.java9
-rw-r--r--juick-common/src/main/java/com/juick/server/CommandsManager.java9
-rw-r--r--juick-common/src/main/java/com/juick/server/component/MessageReadEvent.java28
-rw-r--r--juick-server/src/main/java/com/juick/server/EmailManager.java3
-rw-r--r--juick-server/src/main/java/com/juick/server/MessengerManager.java12
-rw-r--r--juick-server/src/main/java/com/juick/server/NotificationListener.java9
-rw-r--r--juick-server/src/main/java/com/juick/server/ServerManager.java48
-rw-r--r--juick-server/src/main/java/com/juick/server/TelegramBotManager.java20
-rw-r--r--juick-server/src/main/java/com/juick/server/TwitterManager.java50
-rw-r--r--juick-server/src/main/java/com/juick/server/XMPPConnection.java15
-rw-r--r--juick-server/src/main/java/com/juick/server/api/Messages.java7
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java2
12 files changed, 162 insertions, 50 deletions
diff --git a/juick-common/src/main/java/com/juick/Message.java b/juick-common/src/main/java/com/juick/Message.java
index b462e9b2..b1c0ab79 100644
--- a/juick-common/src/main/java/com/juick/Message.java
+++ b/juick-common/src/main/java/com/juick/Message.java
@@ -77,6 +77,7 @@ public class Message implements Comparable {
private String replyQuote;
@XmlTransient
private Set<Reaction> reactions;
+ private boolean service;
public Message() {
tags = new ArrayList<>();
@@ -323,4 +324,12 @@ public class Message implements Comparable {
public int hashCode() {
return Objects.hash(mid, rid);
}
+
+ public boolean isService() {
+ return service;
+ }
+
+ public void setService(boolean service) {
+ this.service = service;
+ }
}
diff --git a/juick-common/src/main/java/com/juick/server/CommandsManager.java b/juick-common/src/main/java/com/juick/server/CommandsManager.java
index ab55bba7..918e6028 100644
--- a/juick-common/src/main/java/com/juick/server/CommandsManager.java
+++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java
@@ -21,10 +21,7 @@ import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.formatters.PlainTextFormatter;
-import com.juick.server.component.LikeEvent;
-import com.juick.server.component.MessageEvent;
-import com.juick.server.component.PingEvent;
-import com.juick.server.component.SubscribeEvent;
+import com.juick.server.component.*;
import com.juick.server.helpers.CommandResult;
import com.juick.server.helpers.TagStats;
import com.juick.server.helpers.annotation.UserCommand;
@@ -116,6 +113,7 @@ public class CommandsManager {
imagesService.saveImageWithPreviews(attachmentFName, fname);
}
com.juick.Message msg = messagesService.getMessage(mid);
+ applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, msg));
applicationEventPublisher.publishEvent(new MessageEvent(this, msg, subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg.getMid())));
return CommandResult.build(msg, "New message posted.\n#" + msg.getMid() + " https://juick.com/" + msg.getMid(), String.format("[New message](%s) posted", PlainTextFormatter.formatUrl(msg)));
}
@@ -347,6 +345,7 @@ public class CommandsManager {
if (msg != null) {
if (subscribe) {
if (subscriptionService.subscribeMessage(msg, user)) {
+ applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, msg));
return CommandResult.fromString("Subscribed");
}
} else {
@@ -398,6 +397,7 @@ public class CommandsManager {
if (msg != null) {
if (showReplies) {
List<com.juick.Message> replies = messagesService.getReplies(user, mid);
+ applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, msg));
replies.add(0, msg);
return CommandResult.fromString(String.join("\n",
replies.stream().map(PlainTextFormatter::formatPostSummary).collect(Collectors.toList())));
@@ -491,6 +491,7 @@ public class CommandsManager {
String attachmentStr = attachment.toString();
String attachmentType = StringUtils.isNotEmpty(attachmentStr) ? attachmentStr.substring(attachmentStr.length() - 3) : null;
int newrid = messagesService.createReply(mid, rid, user, txt, attachmentType);
+ applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, msg));
if (StringUtils.isNotEmpty(attachmentType)) {
String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost()
: HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost();
diff --git a/juick-common/src/main/java/com/juick/server/component/MessageReadEvent.java b/juick-common/src/main/java/com/juick/server/component/MessageReadEvent.java
new file mode 100644
index 00000000..e75d3636
--- /dev/null
+++ b/juick-common/src/main/java/com/juick/server/component/MessageReadEvent.java
@@ -0,0 +1,28 @@
+package com.juick.server.component;
+
+import com.juick.Message;
+import com.juick.User;
+import org.springframework.context.ApplicationEvent;
+
+public class MessageReadEvent extends ApplicationEvent {
+ 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, User user, Message message) {
+ super(source);
+ this.user = user;
+ this.message = message;
+ }
+
+ public User getUser() {
+ return user;
+ }
+
+ public Message getMessage() {
+ return message;
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/server/EmailManager.java b/juick-server/src/main/java/com/juick/server/EmailManager.java
index aaebaac8..66e84358 100644
--- a/juick-server/src/main/java/com/juick/server/EmailManager.java
+++ b/juick-server/src/main/java/com/juick/server/EmailManager.java
@@ -45,6 +45,9 @@ public class EmailManager implements ApplicationListener<MessageEvent> {
public void onApplicationEvent(@Nonnull MessageEvent event) {
Message msg = event.getMessage();
List<User> subscribedUsers = event.getUsers();
+ if (msg.isService()) {
+ return;
+ }
if (MessageUtils.isPM(msg)) {
String subject = String.format("Private message from %s", msg.getUser().getName());
emailService.getEmails(msg.getTo().getUid(), true).forEach(email -> {
diff --git a/juick-server/src/main/java/com/juick/server/MessengerManager.java b/juick-server/src/main/java/com/juick/server/MessengerManager.java
index 323eef7c..51bbb3e2 100644
--- a/juick-server/src/main/java/com/juick/server/MessengerManager.java
+++ b/juick-server/src/main/java/com/juick/server/MessengerManager.java
@@ -19,6 +19,7 @@ import com.juick.service.MessagesService;
import com.juick.service.MessengerService;
import com.juick.service.SubscriptionService;
import com.juick.service.UserService;
+import com.juick.util.MessageUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -111,13 +112,10 @@ public class MessengerManager implements ApplicationListener<MessageEvent> {
public void onApplicationEvent(@Nonnull MessageEvent event) {
Message msg = event.getMessage();
List<User> subscribedUsers = event.getUsers();
- if (msg.getMid() > 0 && msg.getRid() == 0) {
- String subject = formatPost(msg);
- subscribedUsers
- .forEach(user -> messengerService.getSenderId(user)
- .ifPresent(t -> messengerNotify(t, subject, formatUrl(msg))));
- } else if (msg.getRid() > 0) {
- Message op = messagesService.getMessage(msg.getMid());
+ if (msg.isService()) {
+ return;
+ }
+ if (!MessageUtils.isPM(msg)) {
String subject = formatPost(msg);
subscribedUsers
.forEach(user -> messengerService.getSenderId(user)
diff --git a/juick-server/src/main/java/com/juick/server/NotificationListener.java b/juick-server/src/main/java/com/juick/server/NotificationListener.java
index f6330570..47b350c1 100644
--- a/juick-server/src/main/java/com/juick/server/NotificationListener.java
+++ b/juick-server/src/main/java/com/juick/server/NotificationListener.java
@@ -1,9 +1,6 @@
package com.juick.server;
-import com.juick.server.component.LikeEvent;
-import com.juick.server.component.MessageEvent;
-import com.juick.server.component.PingEvent;
-import com.juick.server.component.SubscribeEvent;
+import com.juick.server.component.*;
import org.springframework.context.event.EventListener;
public interface NotificationListener {
@@ -14,5 +11,7 @@ public interface NotificationListener {
@EventListener
void processLikeEvent(LikeEvent likeEvent);
@EventListener
- void ProcessPingEvent(PingEvent pingEvent);
+ void processPingEvent(PingEvent pingEvent);
+ @EventListener
+ void processMessageReadEvent(MessageReadEvent messageReadEvent);
}
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 3db6ec90..fa266c77 100644
--- a/juick-server/src/main/java/com/juick/server/ServerManager.java
+++ b/juick-server/src/main/java/com/juick/server/ServerManager.java
@@ -20,10 +20,11 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.Message;
import com.juick.User;
-import com.juick.server.component.MessageEvent;
+import com.juick.server.component.*;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.UserService;
+import com.juick.util.MessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@@ -41,7 +42,7 @@ import java.util.stream.Collectors;
* @author Ugnich Anton
*/
@Component
-public class ServerManager implements ApplicationListener<MessageEvent> {
+public class ServerManager implements NotificationListener {
private static Logger logger = LoggerFactory.getLogger(ServerManager.class);
@Inject
@@ -151,14 +152,15 @@ public class ServerManager implements ApplicationListener<MessageEvent> {
}
@Override
- public void onApplicationEvent(@Nonnull MessageEvent event) {
+ public void processMessageEvent(MessageEvent event) {
com.juick.Message jmsg = event.getMessage();
List<User> subscribedUsers = event.getUsers();
- if (jmsg.getMid() == 0) {
- if (jmsg.getTo().getUid() > 0) {
- onJuickPM(jmsg.getTo().getUid(), jmsg);
- }
- } else if (jmsg.getRid() == 0) {
+ if (jmsg.isService()) {
+ return;
+ }
+ if (MessageUtils.isPM(jmsg)) {
+ onJuickPM(jmsg.getTo().getUid(), jmsg);
+ } else if (!MessageUtils.isReply(jmsg)) {
// to get full message with attachment, etc.
onJuickMessagePost(messagesService.getMessage(jmsg.getMid()), subscribedUsers);
} else {
@@ -171,4 +173,34 @@ public class ServerManager implements ApplicationListener<MessageEvent> {
onJuickMessageReply(reply, subscribedUsers);
}
}
+
+ @Override
+ public void processSubscribeEvent(SubscribeEvent subscribeEvent) {
+
+ }
+
+ @Override
+ public void processLikeEvent(LikeEvent likeEvent) {
+
+ }
+
+ @Override
+ public void processPingEvent(PingEvent pingEvent) {
+
+ }
+
+ @Override
+ public void processMessageReadEvent(MessageReadEvent messageReadEvent) {
+ User userTo = messageReadEvent.getUser();
+ Message source = messageReadEvent.getMessage();
+ 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);
+ });
+ }
}
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 a024bee6..a30b2bf3 100644
--- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
+++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
@@ -18,10 +18,7 @@
package com.juick.server;
import com.juick.User;
-import com.juick.server.component.LikeEvent;
-import com.juick.server.component.MessageEvent;
-import com.juick.server.component.PingEvent;
-import com.juick.server.component.SubscribeEvent;
+import com.juick.server.component.*;
import com.juick.server.helpers.CommandResult;
import com.juick.server.util.HttpUtils;
import com.juick.service.MessagesService;
@@ -46,6 +43,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -80,6 +78,8 @@ public class TelegramBotManager implements NotificationListener {
private UserService userService;
@Inject
private CommandsManager commandsManager;
+ @Inject
+ private ApplicationEventPublisher applicationEventPublisher;
@Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
private String tmpDir;
@@ -302,6 +302,8 @@ public class TelegramBotManager implements NotificationListener {
if (MessageUtils.isReply(source)) {
messagesService.setLastReadComment(userService.getUserByUID(userId)
.orElseThrow(IllegalStateException::new), source.getMid(), source.getRid());
+ User user = userService.getUserByUID(userId).orElseThrow(IllegalStateException::new);
+ applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, source));
}
}
}
@@ -327,6 +329,9 @@ public class TelegramBotManager implements NotificationListener {
public void processMessageEvent(MessageEvent messageEvent) {
com.juick.Message jmsg = messageEvent.getMessage();
List<User> subscribedUsers = messageEvent.getUsers();
+ if (jmsg.isService()) {
+ return;
+ }
String msgUrl = formatUrl(jmsg);
if (MessageUtils.isPM(jmsg)) {
telegramService.getTelegramIdentifiers(Collections.singletonList(jmsg.getTo()))
@@ -364,7 +369,12 @@ public class TelegramBotManager implements NotificationListener {
}
@Override
- public void ProcessPingEvent(PingEvent pingEvent) {
+ public void processPingEvent(PingEvent pingEvent) {
+
+ }
+
+ @Override
+ public void processMessageReadEvent(MessageReadEvent messageReadEvent) {
}
diff --git a/juick-server/src/main/java/com/juick/server/TwitterManager.java b/juick-server/src/main/java/com/juick/server/TwitterManager.java
index 35dca744..411298bc 100644
--- a/juick-server/src/main/java/com/juick/server/TwitterManager.java
+++ b/juick-server/src/main/java/com/juick/server/TwitterManager.java
@@ -17,7 +17,7 @@
package com.juick.server;
import com.juick.Message;
-import com.juick.server.component.MessageEvent;
+import com.juick.server.component.*;
import com.juick.service.CrosspostService;
import com.juick.util.MessageUtils;
import org.apache.commons.lang3.StringUtils;
@@ -37,7 +37,7 @@ import javax.inject.Inject;
* @author Ugnich Anton
*/
@Component
-public class TwitterManager {
+public class TwitterManager implements NotificationListener {
private static Logger logger = LoggerFactory.getLogger(TwitterManager.class);
@@ -49,19 +49,6 @@ public class TwitterManager {
@Value("${twitter_consumer_secret:secret}")
private String twitter_consumer_secret;
- @Async
- @EventListener
- public void onApplicationEvent(@Nonnull MessageEvent event) {
- Message msg = event.getMessage();
- if (msg.getMid() > 0 && msg.getRid() == 0) {
- if (StringUtils.isNotEmpty(crosspostService.getTwitterName(msg.getUser().getUid()))) {
- if (msg.getTags().stream().noneMatch(t -> t.getName().equals("notwitter"))) {
- twitterPost(msg);
- }
- }
- }
- }
-
void twitterPost(final com.juick.Message jmsg) {
crosspostService.getTwitterToken(jmsg.getUser().getUid()).ifPresent(t -> {
String status = MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText());
@@ -83,4 +70,37 @@ public class TwitterManager {
}
});
}
+
+ @Override
+ public void processMessageEvent(MessageEvent messageEvent) {
+ Message msg = messageEvent.getMessage();
+ if (MessageUtils.isPM(msg) || MessageUtils.isReply(msg) || msg.isService()) {
+ return;
+ }
+ if (StringUtils.isNotEmpty(crosspostService.getTwitterName(msg.getUser().getUid()))) {
+ if (msg.getTags().stream().noneMatch(t -> t.getName().equals("notwitter"))) {
+ twitterPost(msg);
+ }
+ }
+ }
+
+ @Override
+ public void processSubscribeEvent(SubscribeEvent subscribeEvent) {
+
+ }
+
+ @Override
+ public void processLikeEvent(LikeEvent likeEvent) {
+
+ }
+
+ @Override
+ public void processPingEvent(PingEvent pingEvent) {
+
+ }
+
+ @Override
+ public void processMessageReadEvent(MessageReadEvent messageReadEvent) {
+
+ }
}
diff --git a/juick-server/src/main/java/com/juick/server/XMPPConnection.java b/juick-server/src/main/java/com/juick/server/XMPPConnection.java
index 0975611a..2c21a973 100644
--- a/juick-server/src/main/java/com/juick/server/XMPPConnection.java
+++ b/juick-server/src/main/java/com/juick/server/XMPPConnection.java
@@ -18,10 +18,7 @@
package com.juick.server;
import com.juick.User;
-import com.juick.server.component.LikeEvent;
-import com.juick.server.component.MessageEvent;
-import com.juick.server.component.PingEvent;
-import com.juick.server.component.SubscribeEvent;
+import com.juick.server.component.*;
import com.juick.server.helpers.CommandResult;
import com.juick.server.helpers.UserInfo;
import com.juick.server.xmpp.iq.MessageQuery;
@@ -346,6 +343,9 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
public void processMessageEvent(MessageEvent event) {
com.juick.Message msg = event.getMessage();
List<User> subscribers = event.getUsers();
+ if (msg.isService()) {
+ return;
+ }
if (MessageUtils.isPM(msg)) {
userService.getJIDsbyUID(msg.getTo().getUid())
.forEach(userJid -> {
@@ -434,7 +434,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
@Override
- public void ProcessPingEvent(PingEvent pingEvent) {
+ public void processPingEvent(PingEvent pingEvent) {
userService.getJIDsbyUID(pingEvent.getPinger().getUid())
.forEach(userJid -> {
Presence p = new Presence(Jid.of(userJid));
@@ -444,6 +444,11 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
});
}
+ @Override
+ public void processMessageReadEvent(MessageReadEvent messageReadEvent) {
+
+ }
+
private void incomingPresence(Presence p) {
final String username = p.getTo().getLocal();
final boolean toJuick = username.equals(jid.getLocal());
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 7eb86284..666bab0a 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
@@ -20,12 +20,14 @@ package com.juick.server.api;
import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
+import com.juick.server.component.MessageReadEvent;
import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.UserUtils;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import org.apache.commons.io.IOUtils;
+import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@@ -59,6 +61,8 @@ public class Messages {
private UserService userService;
@Inject
private TagService tagService;
+ @Inject
+ private ApplicationEventPublisher applicationEventPublisher;
// TODO: serialize image urls
@@ -148,6 +152,7 @@ public class Messages {
return FORBIDDEN;
} else {
List<com.juick.Message> replies = messagesService.getReplies(visitor, mid);
+ applicationEventPublisher.publishEvent(new MessageReadEvent(this, visitor, msg));
replies.add(0, msg);
return ResponseEntity.ok(replies);
}
@@ -159,6 +164,8 @@ public class Messages {
User visitor = UserUtils.getCurrentUser();
if (visitor.getUid() > 0) {
messagesService.setLastReadComment(visitor, mid, rid);
+ Message msg = messagesService.getMessage(mid);
+ applicationEventPublisher.publishEvent(new MessageReadEvent(this, visitor, msg));
return IOUtils.toByteArray(
Objects.requireNonNull(getClass().getClassLoader().getResource("Transparent.gif")));
}
diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
index 3c12de40..d3e7b7a5 100644
--- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
@@ -909,7 +909,7 @@ public class ServerTests {
assertThat(lastRead.apply(ugnich, mid), is(freefdrid));
privacyQueriesService.blacklistUser(ugnich, freefd);
int newfreefdrid = messagesService.createReply(mid, 0, freefd, "from ban", null);
- serverManager.onApplicationEvent(new MessageEvent(this, messagesService.getReply(mid, newfreefdrid),
+ serverManager.processMessageEvent(new MessageEvent(this, messagesService.getReply(mid, newfreefdrid),
Collections.emptyList()));
assertThat(lastRead.apply(ugnich, mid), is(newfreefdrid));
privacyQueriesService.blacklistUser(ugnich, freefd);