diff options
author | Vitaly Takmazov | 2018-03-21 12:20:56 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-03-21 13:52:55 +0300 |
commit | 403fab0c39e99ffd0b386eb612f3574938ae2125 (patch) | |
tree | 47b99c53134468da50c6f766ebb16043fa984cfa /juick-server/src/main/java/com/juick | |
parent | f066f48e4cabd0d7649af2e8c1f4b6a7dea56673 (diff) |
add some events
Diffstat (limited to 'juick-server/src/main/java/com/juick')
5 files changed, 60 insertions, 30 deletions
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 2793f906..9e056476 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -18,7 +18,9 @@ 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.SubscribeEvent; import com.juick.server.util.HttpUtils; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; @@ -53,10 +55,7 @@ import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; import java.net.URL; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.Optional; +import java.util.*; import static com.juick.formatters.PlainTextFormatter.formatPost; import static com.juick.formatters.PlainTextFormatter.formatUrl; @@ -65,7 +64,7 @@ import static com.juick.formatters.PlainTextFormatter.formatUrl; * Created by vt on 12/05/16. */ @Component -public class TelegramBotManager implements ApplicationListener<MessageEvent> { +public class TelegramBotManager { private static final Logger logger = LoggerFactory.getLogger(TelegramBotManager.class); private TelegramBot bot; @@ -87,6 +86,10 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> { private static final String MSG_LINK = "🔗"; + private ApplicationListener<LikeEvent> onLike; + private ApplicationListener<SubscribeEvent> onSubscribe; + private ApplicationListener<MessageEvent> onMessage; + @PostConstruct public void init() { if (StringUtils.isBlank(telegramToken)) { @@ -102,6 +105,41 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> { } catch (Exception e) { logger.warn("couldn't initialize telegram bot", e); } + onMessage = event -> { + com.juick.Message jmsg = event.getMessage(); + String msgUrl = formatUrl(jmsg); + if (jmsg.getMid() > 0 && jmsg.getRid() == 0) { + String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true)); + + List<Long> users = telegramService.getTelegramIdentifiers(subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid())); + List<Long> chats = telegramService.getChats(); + // registered subscribed users + + users.forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); + // anonymous + chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); + } else if (jmsg.getRid() > 0) { + com.juick.Message op = messagesService.getMessage(jmsg.getMid()); + String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true)); + telegramService.getTelegramIdentifiers( + subscriptionService.getUsersSubscribedToComments(op, jmsg) + ).forEach(c -> telegramNotify(c, fmsg, MessageUtils.attachmentUrl(jmsg))); + } + }; + onLike = event -> { + User liker = event.getUser(); + com.juick.Message message = event.getMessage(); + telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) + .forEach(c -> telegramNotify(c, String.format("%s recommends your post", + MessageUtils.getMarkdownUser(liker)),null)); + }; + onSubscribe = event -> { + User subscriber = event.getUser(); + User target = event.getToUser(); + telegramService.getTelegramIdentifiers(Collections.singletonList(target)) + .forEach(c -> telegramNotify(c, String.format("%s subscribed to your blog", + MessageUtils.getMarkdownUser(subscriber)), null)); + }; } public void processUpdate(String data) throws Exception { @@ -215,29 +253,6 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> { return e.offset() == 0 && e.type().equals(MessageEntity.Type.text_link) && e.length() == 2; } - @Override - public void onApplicationEvent(@Nonnull MessageEvent event) { - com.juick.Message jmsg = event.getMessage(); - String msgUrl = formatUrl(jmsg); - if (jmsg.getMid() > 0 && jmsg.getRid() == 0) { - String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true)); - - List<Long> users = telegramService.getTelegramIdentifiers(subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid())); - List<Long> chats = telegramService.getChats(); - // registered subscribed users - - users.forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); - // anonymous - chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); - } else if (jmsg.getRid() > 0) { - com.juick.Message op = messagesService.getMessage(jmsg.getMid()); - String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true)); - telegramService.getTelegramIdentifiers( - subscriptionService.getUsersSubscribedToComments(op, jmsg) - ).forEach(c -> telegramNotify(c, fmsg, MessageUtils.attachmentUrl(jmsg))); - } - } - public void telegramNotify(Long chatId, String msg, String attachment) { telegramNotify(chatId, msg, 0, attachment); } diff --git a/juick-server/src/main/java/com/juick/server/XMPPBot.java b/juick-server/src/main/java/com/juick/server/XMPPBot.java index ac1c4121..9b6b5fe4 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPBot.java +++ b/juick-server/src/main/java/com/juick/server/XMPPBot.java @@ -19,6 +19,8 @@ package com.juick.server; import com.juick.Tag; import com.juick.User; +import com.juick.server.component.LikeEvent; +import com.juick.server.component.SubscribeEvent; import com.juick.server.xmpp.s2s.StanzaListener; import com.juick.formatters.PlainTextFormatter; import com.juick.server.helpers.TagStats; @@ -31,6 +33,7 @@ import org.ocpsoft.prettytime.PrettyTime; 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 rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.*; @@ -79,6 +82,8 @@ public class XMPPBot implements StanzaListener, AutoCloseable { private PrivacyQueriesService privacyQueriesService; @Inject private SubscriptionService subscriptionService; + @Inject + private ApplicationEventPublisher applicationEventPublisher; @PostConstruct public void init() { @@ -529,6 +534,7 @@ public class XMPPBot implements StanzaListener, AutoCloseable { MessagesService.RecommendStatus status = messagesService.recommendMessage(mid, user.getUid()); switch (status) { case Added: + applicationEventPublisher.publishEvent(new LikeEvent(this, user, msg)); return "Message is added to your recommendations"; case Deleted: return "Message deleted from your recommendations."; @@ -546,6 +552,7 @@ public class XMPPBot implements StanzaListener, AutoCloseable { User toUser = userService.getUserByName(args[1]); if (subscribe) { if (subscriptionService.subscribeUser(user, toUser)) { + applicationEventPublisher.publishEvent(new SubscribeEvent(this, user, toUser)); return "Subscribed to @" + toUser.getName(); } } else { diff --git a/juick-server/src/main/java/com/juick/server/XMPPServer.java b/juick-server/src/main/java/com/juick/server/XMPPServer.java index 9f0a8183..a0c3ac92 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPServer.java +++ b/juick-server/src/main/java/com/juick/server/XMPPServer.java @@ -95,6 +95,7 @@ public class XMPPServer implements ConnectionListener, AutoCloseable { } } }; + private boolean tlsConfigured = false; private ServerSocket listener; @@ -115,8 +116,9 @@ public class XMPPServer implements ConnectionListener, AutoCloseable { kmf.init(ks, keystorePassword.toCharArray()); sc = SSLContext.getInstance("TLSv1.2"); sc.init(kmf.getKeyManagers(), trustAllCerts, new SecureRandom()); + tlsConfigured = true; } catch (Exception e) { - logger.warn("tls unavailable"); + logger.warn("tls unavailable", e); } service.submit(() -> { try { @@ -304,6 +306,11 @@ public class XMPPServer implements ConnectionListener, AutoCloseable { } @Override + public boolean isTlsAvailable() { + return tlsConfigured; + } + + @Override public void starttls(ConnectionIn connection) { logger.debug("stream {} securing", connection.streamID); connection.sendStanza("<proceed xmlns=\"" + Connection.NS_TLS + "\" />"); diff --git a/juick-server/src/main/java/com/juick/server/xmpp/s2s/ConnectionIn.java b/juick-server/src/main/java/com/juick/server/xmpp/s2s/ConnectionIn.java index dc7b3af9..01cc78ab 100644 --- a/juick-server/src/main/java/com/juick/server/xmpp/s2s/ConnectionIn.java +++ b/juick-server/src/main/java/com/juick/server/xmpp/s2s/ConnectionIn.java @@ -178,7 +178,7 @@ public class ConnectionIn extends Connection implements Runnable { xmpp.getJid().toEscapedString() + "' id='" + streamID + "' version='1.0'>"; if (xmppversionnew) { openStream += "<stream:features>"; - if (listener != null && !isSecured() && !Arrays.asList(xmpp.brokenSSLhosts).contains(from)) { + if (listener != null && listener.isTlsAvailable() && !isSecured() && !Arrays.asList(xmpp.brokenSSLhosts).contains(from)) { openStream += "<starttls xmlns=\"" + NS_TLS + "\"><optional/></starttls>"; } openStream += "</stream:features>"; diff --git a/juick-server/src/main/java/com/juick/server/xmpp/s2s/ConnectionListener.java b/juick-server/src/main/java/com/juick/server/xmpp/s2s/ConnectionListener.java index efac8732..fde7a0e7 100644 --- a/juick-server/src/main/java/com/juick/server/xmpp/s2s/ConnectionListener.java +++ b/juick-server/src/main/java/com/juick/server/xmpp/s2s/ConnectionListener.java @@ -3,6 +3,7 @@ package com.juick.server.xmpp.s2s; import com.juick.xmpp.extensions.StreamError; public interface ConnectionListener { + boolean isTlsAvailable(); void starttls(ConnectionIn connection); void proceed(ConnectionOut connection); void verify(ConnectionOut connection, String from, String type, String sid); |