aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-04-11 09:33:12 +0300
committerGravatar Vitaly Takmazov2018-04-11 09:33:12 +0300
commit20e21d61e25b680c086f40ecf5416ed36298c3c3 (patch)
tree09cf55368eae7d065c2ed365e6c0648dd99b2a2b /juick-server/src
parent3a19c7229c07e4de07453d8a23775f6dee1a687f (diff)
server: try to crosspost async
Diffstat (limited to 'juick-server/src')
-rw-r--r--juick-server/src/main/java/com/juick/server/CrosspostManager.java97
1 files changed, 5 insertions, 92 deletions
diff --git a/juick-server/src/main/java/com/juick/server/CrosspostManager.java b/juick-server/src/main/java/com/juick/server/CrosspostManager.java
index 631e98ae..f518c111 100644
--- a/juick-server/src/main/java/com/juick/server/CrosspostManager.java
+++ b/juick-server/src/main/java/com/juick/server/CrosspostManager.java
@@ -20,31 +20,24 @@ import com.juick.Message;
import com.juick.server.component.MessageEvent;
import com.juick.service.CrosspostService;
import com.juick.util.MessageUtils;
-import org.apache.commons.codec.CharEncoding;
-import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Nonnull;
import javax.inject.Inject;
-import javax.net.ssl.HttpsURLConnection;
-import java.io.OutputStreamWriter;
-import java.net.URL;
-import java.net.URLEncoder;
-import java.nio.charset.StandardCharsets;
/**
* @author Ugnich Anton
*/
@Component
-public class CrosspostManager implements ApplicationListener<MessageEvent> {
+public class CrosspostManager {
private final static String FBURL = "https://graph.facebook.com/me/feed";
private final static String VKURL = "https://api.vk.com/method/wall.post";
@@ -58,7 +51,8 @@ public class CrosspostManager implements ApplicationListener<MessageEvent> {
@Value("${twitter_consumer_secret:secret}")
private String twitter_consumer_secret;
- @Override
+ @Async
+ @EventListener
public void onApplicationEvent(@Nonnull MessageEvent event) {
Message msg = event.getMessage();
if (msg.getMid() > 0 && msg.getRid() == 0) {
@@ -67,90 +61,9 @@ public class CrosspostManager implements ApplicationListener<MessageEvent> {
twitterPost(msg);
}
}
- // TODO: approve application for facebook crosspost
}
}
- private boolean facebookPost(final com.juick.Message jmsg) {
- String token = crosspostService.getFacebookTokens(jmsg.getUser().getUid())
- .orElse(Pair.of(StringUtils.EMPTY, StringUtils.EMPTY)).getRight();
- if (token.isEmpty()) {
- return false;
- }
-
- logger.info("FB: #{}", jmsg.getMid());
-
- String status = MessageUtils.getMessageHashTags(jmsg) + "\n" + jmsg.getText();
-
- boolean ret = false;
- try {
- String body = "access_token="
- + URLEncoder.encode(token, CharEncoding.UTF_8)
- + "&message="
- + URLEncoder.encode(status, CharEncoding.UTF_8)
- + "&link=http%3A%2F%2Fjuick.com%2F"
- + jmsg.getMid();
-
- HttpsURLConnection conn = (HttpsURLConnection) new URL(FBURL).openConnection();
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- conn.setRequestProperty("User-Agent", "Juick");
- conn.setRequestProperty("Content-Length", Integer.toString(body.length()));
- conn.setUseCaches(false);
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setRequestMethod("POST");
- conn.connect();
-
- OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
- wr.write(body);
- wr.close();
-
- ret = StringUtils.isNotEmpty(IOUtils.toString(conn.getInputStream(), StandardCharsets.UTF_8));
-
- conn.disconnect();
- } catch (Exception e) {
- logger.error("fbPost exception", e);
- }
- return ret;
- }
-
- private boolean vkontaktePost(final com.juick.Message jmsg) {
- Pair<String, String> tokens = crosspostService.getVkTokens(jmsg.getUser().getUid()).orElse(Pair.of(StringUtils.EMPTY, StringUtils.EMPTY));
- if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) {
- return false;
- }
-
- logger.info("VK: #", jmsg.getMid());
-
- String status = MessageUtils.getMessageHashTags(jmsg) + "\n" + jmsg.getText() + "\nhttp://juick.com/" + jmsg.getMid();
-
- boolean ret = false;
- try {
- String body = "owner_id=" + tokens.getLeft() + "&access_token=" + URLEncoder.encode(tokens.getRight(), CharEncoding.UTF_8) + "&from_group=1&message=" + URLEncoder.encode(status, CharEncoding.UTF_8);
-
- HttpsURLConnection conn = (HttpsURLConnection) new URL(VKURL).openConnection();
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- conn.setRequestProperty("User-Agent", "Juick");
- conn.setRequestProperty("Content-Length", Integer.toString(body.length()));
- conn.setUseCaches(false);
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setRequestMethod("POST");
- conn.connect();
-
- OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
- wr.write(body);
- wr.close();
-
- ret = StringUtils.isNotEmpty(IOUtils.toString(conn.getInputStream(), StandardCharsets.UTF_8));
-
- conn.disconnect();
- } catch (Exception e) {
- logger.error("vkPost exception", e);
- }
- return ret;
- }
-
void twitterPost(final com.juick.Message jmsg) {
crosspostService.getTwitterToken(jmsg.getUser().getUid()).ifPresent(t -> {
String status = MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText());