aboutsummaryrefslogtreecommitdiff
path: root/juick-crosspost/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'juick-crosspost/src/main')
-rw-r--r--juick-crosspost/src/main/java/com/juick/components/Crosspost.java66
-rw-r--r--juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java15
2 files changed, 36 insertions, 45 deletions
diff --git a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java
index 769fb892..d7c0ec14 100644
--- a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java
+++ b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java
@@ -17,6 +17,9 @@
*/
package com.juick.components;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.juick.Message;
import com.juick.Tag;
import com.juick.service.CrosspostService;
import com.juick.service.MessagesService;
@@ -24,20 +27,15 @@ import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
-import rocks.xmpp.core.XmppException;
-import rocks.xmpp.core.session.Extension;
-import rocks.xmpp.core.session.XmppSession;
-import rocks.xmpp.core.session.XmppSessionConfiguration;
-import rocks.xmpp.core.stanza.model.Message;
-import rocks.xmpp.extensions.component.accept.ExternalComponent;
-
-import javax.annotation.PostConstruct;
+import org.springframework.web.socket.TextMessage;
+import org.springframework.web.socket.WebSocketSession;
+import org.springframework.web.socket.handler.TextWebSocketHandler;
+
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.inject.Inject;
@@ -53,7 +51,7 @@ import java.util.UUID;
/**
* @author Ugnich Anton
*/
-public class Crosspost implements AutoCloseable {
+public class Crosspost extends TextWebSocketHandler {
final static String TWITTERURL = "https://api.twitter.com/1.1/statuses/update.json";
final static String FBURL = "https://graph.facebook.com/me/feed";
final static String VKURL = "https://api.vk.com/method/wall.post";
@@ -61,13 +59,10 @@ public class Crosspost implements AutoCloseable {
private static Logger logger = LoggerFactory.getLogger(Crosspost.class);
private final CrosspostService crosspostService;
- private XmppSession xmpp;
private final String twitter_consumer_key;
private final String twitter_consumer_secret;
- private final String crosspostJid;
- private final String password;
- private final int port;
+ private ObjectMapper ms;
@Inject
MessagesService messagesService;
@@ -80,9 +75,10 @@ public class Crosspost implements AutoCloseable {
twitter_consumer_key = env.getProperty("twitter_consumer_key", StringUtils.EMPTY);
twitter_consumer_secret = env.getProperty("twitter_consumer_secret", StringUtils.EMPTY);
- crosspostJid = env.getProperty("crosspost_jid", "crosspost.juick.local");
- password = env.getProperty("xmpp_password", StringUtils.EMPTY);
- port = NumberUtils.toInt(env.getProperty("xmpp_port"), 5347);
+ ms = new ObjectMapper();
+ ms.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
+ ms.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+ ms.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
}
public static String percentEncode(final String s) {
@@ -94,40 +90,20 @@ public class Crosspost implements AutoCloseable {
return ret;
}
- @PostConstruct
- public void init() {
- XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
- .extensions(Extension.of(com.juick.Message.class))
- .build();
- xmpp = ExternalComponent.create(crosspostJid, password, configuration, "localhost", port);
- xmpp.addInboundMessageListener(e -> {
- Message msg = e.getMessage();
- com.juick.Message jmsg = messagesService.getMessage(msg.getExtension(com.juick.Message.class).getMid());
- if (msg.getTo() != null && msg.getTo().getLocal() != null && jmsg != null && jmsg.getRid() == 0) {
- if (msg.getTo().getLocal().equals("twitter")) {
+ @Override
+ protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
+ Message msg = ms.readValue(message.asBytes(), Message.class);
+ if (msg.getMid() > 0 && msg.getRid() == 0) {
+ Message jmsg = messagesService.getMessage(msg.getMid());
+ if (StringUtils.isNotEmpty(crosspostService.getTwitterName(msg.getUser().getUid()))) {
+ if (jmsg.getTags().stream().noneMatch(t -> t.getName().equals("notwitter"))) {
twitterPost(jmsg);
- } else if (msg.getTo().getLocal().equals("fb")) {
- facebookPost(jmsg);
- } else if (msg.getTo().getLocal().equals("vk")) {
- vkontaktePost(jmsg);
}
}
- });
- try {
- xmpp.connect();
- } catch (XmppException e) {
- logger.warn("xmpp exception", e);
+ // TODO: approve application for facebook crosspost
}
}
- @Override
- public void close() throws Exception {
- if (xmpp != null)
- xmpp.close();
-
- logger.info("XmppSession on crosspost destroyed");
- }
-
public boolean facebookPost(final com.juick.Message jmsg) {
String token = crosspostService.getFacebookToken(jmsg.getUser().getUid()).orElse(StringUtils.EMPTY);
if (token.isEmpty()) {
diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java
index 95449589..2fcd3bfe 100644
--- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java
+++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java
@@ -6,6 +6,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
+import org.springframework.web.socket.client.WebSocketConnectionManager;
+import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import javax.inject.Inject;
@@ -24,4 +26,17 @@ public class CrosspostAppConfiguration {
public Crosspost crosspost() {
return new Crosspost(env, crosspostService);
}
+ private static final String WS_URI = "wss://ws.juick.com/";
+
+ @Bean
+ public WebSocketConnectionManager connectionManager() {
+ WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), crosspost(), WS_URI);
+ manager.setAutoStartup(true);
+ return manager;
+ }
+
+ @Bean
+ public StandardWebSocketClient client() {
+ return new StandardWebSocketClient();
+ }
}