From 20e21d61e25b680c086f40ecf5416ed36298c3c3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 11 Apr 2018 09:33:12 +0300 Subject: server: try to crosspost async --- .../java/com/juick/server/CrosspostManager.java | 97 ++-------------------- 1 file changed, 5 insertions(+), 92 deletions(-) (limited to 'juick-server/src/main/java') 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 { +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 { @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 { 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 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()); -- cgit v1.2.3