aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick
diff options
context:
space:
mode:
Diffstat (limited to 'juick-api/src/main/java/com/juick')
-rw-r--r--juick-api/src/main/java/com/juick/api/TelegramBotHook.java92
1 files changed, 43 insertions, 49 deletions
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java b/juick-api/src/main/java/com/juick/api/TelegramBotHook.java
index ef78485e..d0a5b177 100644
--- a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java
+++ b/juick-api/src/main/java/com/juick/api/TelegramBotHook.java
@@ -38,7 +38,7 @@ public class TelegramBotHook {
private static final Logger logger = Logger.getLogger(TelegramBotHook.class.getName());
TelegramBot bot;
- WebSocket ws, wsReply;
+ WebSocket ws;
MessageSerializer ms = new MessageSerializer();
JdbcTemplate jdbc;
JuickProtocol protocol;
@@ -50,9 +50,7 @@ public class TelegramBotHook {
protocol = new JuickProtocol(jdbc, "https://juick.com/");
try {
bot.setWebhook("https://api.juick.com/tlgmbtwbhk");
- ws = new WebSocketFactory().createSocket("wss://ws.juick.com/_all");
- ws.addHeader("Origin", "ws.juick.com");
- ws.addHeader("Host", "ws.juick.com"); //TODO: remove from server
+ ws = new WebSocketFactory().createSocket("wss://ws.juick.com/");
ws.setPingInterval(60 * 1000);
ws.addListener(new WebSocketAdapter() {
@Override
@@ -71,61 +69,57 @@ public class TelegramBotHook {
super.onTextMessage(websocket, text);
com.juick.Message jmsg = ms.deserialize(new JSONObject(text));
logger.info("got jmsg: " + ms.serialize(jmsg).toString());
- StringBuilder sb = new StringBuilder();
- sb.append("@").append(jmsg.getUser().getUName()).append(":\n")
- .append(jmsg.getTagsString()).append("\n").append(jmsg.getText()).append("\n");
- if (jmsg.Photo != null) {
- sb.append(jmsg.Photo);
- }
- String msg = sb.toString();
- List<Long> users = getSubscribers(jmsg.getUser().getUID());
- List<Long> chats = getChats();
- // registered subscribed users
- String msgUrl = "https://juick.com/" + jmsg.getMID();
- users.stream().forEach(c -> telegramNotify(c, msg, msgUrl));
- // anonymous
- chats.stream().filter(u -> getUser(u) == 0).forEach(c -> telegramNotify(c, msg, msgUrl));
- }
- });
- ws.connect();
- wsReply = new WebSocketFactory().createSocket("wss://ws.juick.com/_replies");
- wsReply.addHeader("Origin", "ws.juick.com");
- wsReply.addHeader("Host", "ws.juick.com");
- wsReply.setPingInterval(60 * 1000);
- wsReply.addListener(new WebSocketAdapter() {
- @Override
- public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
- logger.info("ws replies disconnected");
- ws.connect();
- }
+ String msgUrl = formatUrl(jmsg);
+ if (jmsg.getRID() == 0) {
+ String msg = formatPost(jmsg);
- @Override
- public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
- logger.info("ws replies connected");
- }
+ List<Long> users = getSubscribers(jmsg.getUser().getUID());
+ List<Long> chats = getChats();
+ // registered subscribed users
- @Override
- public void onTextMessage(WebSocket websocket, String text) throws Exception {
- com.juick.Message jmsg = ms.deserialize(new JSONObject(text));
- logger.info(String.format("got jmsg: %s", ms.serialize(jmsg).toString()));
- StringBuilder sb = new StringBuilder();
- sb.append("Reply by @").append(jmsg.getUser().getUName()).append(":\n")
- .append(getReplyQuote(jmsg.getMID(), jmsg.ReplyTo)).append("\n").append(jmsg.getText());
- if (jmsg.getAttachmentURL() != null) {
- sb.append("\n").append(jmsg.getAttachmentURL());
+ users.forEach(c -> telegramNotify(c, msg, msgUrl));
+ // anonymous
+ chats.stream().filter(u -> getUser(u) == 0).forEach(c -> telegramNotify(c, msg, msgUrl));
+ } else {
+ String msg = formatReply(jmsg);
+ getSubscribersToComments(jmsg.getMID(), jmsg.getUser().getUID()).stream()
+ .forEach(c -> telegramNotify(c, msg, msgUrl));
}
- String msg = sb.toString();
- String msgUrl = String.format("https://juick.com/%d#%d", jmsg.getMID(), jmsg.getRID());
- getSubscribersToComments(jmsg.getMID(), jmsg.getUser().getUID()).stream()
- .forEach(c -> telegramNotify(c, msg, msgUrl));
}
});
- wsReply.connect();
+ ws.connect();
} catch (Exception e) {
logger.log(Level.WARNING, "couldn't initialize telegram bot", e);
}
}
+ private String formatPost(com.juick.Message jmsg) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("@").append(jmsg.getUser().getUName()).append(":\n")
+ .append(jmsg.getTagsString()).append("\n").append(jmsg.getText()).append("\n");
+ if (jmsg.Photo != null) {
+ sb.append(jmsg.Photo);
+ }
+ return sb.toString();
+ }
+ private String formatReply(com.juick.Message jmsg) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Reply by @").append(jmsg.getUser().getUName()).append(":\n")
+ .append(getReplyQuote(jmsg.getMID(), jmsg.ReplyTo)).append("\n")
+ .append(jmsg.getText()).append("\n");
+ if (jmsg.Photo != null) {
+ sb.append(jmsg.Photo);
+ }
+ return sb.toString();
+ }
+
+ private String formatUrl(com.juick.Message jmsg) {
+ if (jmsg.getRID() > 0) {
+ return String.format("https://juick.com/%d#%d", jmsg.getMID(), jmsg.getRID());
+ }
+ return "https://juick.com/" + jmsg.getMID();
+ }
+
private void telegramNotify(Long c, String msg, String msgUrl) {
SendMessage telegramMessage = new SendMessage(c, msg);
if (msgUrl != null) {