From fb540605626e1c7bb1544a518a254e6a820afcb3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 10 May 2018 12:50:08 +0300 Subject: server: MessageReadEvent --- .../main/java/com/juick/server/EmailManager.java | 3 ++ .../java/com/juick/server/MessengerManager.java | 12 +++--- .../com/juick/server/NotificationListener.java | 9 ++-- .../main/java/com/juick/server/ServerManager.java | 48 +++++++++++++++++---- .../java/com/juick/server/TelegramBotManager.java | 20 ++++++--- .../main/java/com/juick/server/TwitterManager.java | 50 +++++++++++++++------- .../main/java/com/juick/server/XMPPConnection.java | 15 ++++--- .../main/java/com/juick/server/api/Messages.java | 7 +++ .../java/com/juick/server/tests/ServerTests.java | 2 +- 9 files changed, 120 insertions(+), 46 deletions(-) (limited to 'juick-server/src') 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 { public void onApplicationEvent(@Nonnull MessageEvent event) { Message msg = event.getMessage(); List 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 { public void onApplicationEvent(@Nonnull MessageEvent event) { Message msg = event.getMessage(); List 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 { +public class ServerManager implements NotificationListener { private static Logger logger = LoggerFactory.getLogger(ServerManager.class); @Inject @@ -151,14 +152,15 @@ public class ServerManager implements ApplicationListener { } @Override - public void onApplicationEvent(@Nonnull MessageEvent event) { + public void processMessageEvent(MessageEvent event) { com.juick.Message jmsg = event.getMessage(); List 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 { 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 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 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 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); -- cgit v1.2.3