aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-01-13 17:03:26 +0300
committerGravatar Vitaly Takmazov2017-01-13 17:03:26 +0300
commitacef82fecd92aec828a05fabc1fb29da7a645d33 (patch)
tree691caa72b6d999422cceb5972992a2a695670ba9
parent98658ebab12286a56a7a018ba6a92026533dde7c (diff)
juick-crosspost: xmpp -> ws
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/Messages.java36
-rw-r--r--juick-crosspost/build.gradle1
-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
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/controllers/PostController.java5
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/NewMessage.java5
6 files changed, 40 insertions, 88 deletions
diff --git a/juick-api/src/main/java/com/juick/api/controllers/Messages.java b/juick-api/src/main/java/com/juick/api/controllers/Messages.java
index d50620ff..584ff3cf 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/Messages.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/Messages.java
@@ -1,9 +1,9 @@
package com.juick.api.controllers;
+import com.juick.Status;
import com.juick.Tag;
import com.juick.User;
import com.juick.api.ApiServer;
-import com.juick.Status;
import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpForbiddenException;
import com.juick.service.MessagesService;
@@ -16,13 +16,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.RestController;
-import rocks.xmpp.addr.Jid;
-import rocks.xmpp.core.stanza.model.Message;
+import org.springframework.web.bind.annotation.*;
import javax.inject.Inject;
import java.util.Collections;
@@ -178,31 +172,7 @@ public class Messages {
@RequestParam(defaultValue = "0") int mid,
@RequestParam(defaultValue = "0") int popular) {
if (mid > 0) {
- boolean ret = messagesService.setMessagePopular(mid, popular);
-
- if (ret && popular == 2) {
- try {
- com.juick.Message m = messagesService.getMessage(mid);
- if (m != null) {
- Message msg = new Message();
- msg.setFrom(Jid.of("juick@juick.com"));
- msg.setTo(Jid.of("crosspost.juick.com"));
- m.setUser(userService.getUserByUID(11574).get());
- msg.addExtension(m);
-
- msg.setTo(Jid.of("twitter@crosspost.juick.com"));
- apiServer.getXmpp().send(msg);
- msg.setTo(Jid.of("fb@crosspost.juick.com"));
- apiServer.getXmpp().send(msg);
- msg.setTo(Jid.of("vk@crosspost.juick.com"));
- apiServer.getXmpp().send(msg);
- } else {
- throw new Exception("Message not found");
- }
- } catch (Exception e) {
- logger.error("SETPOPULAR ERROR", e);
- }
- }
+ messagesService.setMessagePopular(mid, popular);
return Status.OK;
}
throw new HttpBadRequestException();
diff --git a/juick-crosspost/build.gradle b/juick-crosspost/build.gradle
index 7b1c3731..ff26252b 100644
--- a/juick-crosspost/build.gradle
+++ b/juick-crosspost/build.gradle
@@ -6,6 +6,7 @@ apply plugin: 'com.github.ben-manes.versions'
dependencies {
compile project(':juick-server')
compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}"
+ compile "org.springframework:spring-websocket:${rootProject.springFrameworkVersion}"
providedRuntime 'mysql:mysql-connector-java:5.1.40'
}
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();
+ }
}
diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/PostController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/PostController.java
index 01f011be..95404732 100644
--- a/juick-spring-www/src/main/java/com/juick/www/controllers/PostController.java
+++ b/juick-spring-www/src/main/java/com/juick/www/controllers/PostController.java
@@ -133,11 +133,6 @@ public class PostController {
xmsg.setTo(Jid.of("juick@push.juick.com"));
webApp.getXmpp().send(xmsg);
- xmsg.setTo(Jid.of("twitter@crosspost.juick.com"));
- webApp.getXmpp().send(xmsg);
- xmsg.setTo(Jid.of("fb@crosspost.juick.com"));
- webApp.getXmpp().send(xmsg);
-
xmsg.setTo(Jid.of("jubo@nologin.ru"));
webApp.getXmpp().send(xmsg);
} else {
diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java
index dacd54a3..b340546f 100644
--- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java
+++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java
@@ -248,11 +248,6 @@ public class NewMessage {
xmsg.setTo(Jid.of("juick@push.juick.com"));
webApp.getXmpp().send(xmsg);
- xmsg.setTo(Jid.of("twitter@crosspost.juick.com"));
- webApp.getXmpp().send(xmsg);
- xmsg.setTo(Jid.of("fb@crosspost.juick.com"));
- webApp.getXmpp().send(xmsg);
-
xmsg.setTo(Jid.of("jubo@nologin.ru"));
webApp.getXmpp().send(xmsg);
} else {