aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-05-19 19:46:31 +0300
committerGravatar Vitaly Takmazov2016-05-19 19:46:31 +0300
commitcc0a29650d3ba9b848a86cce73fbe6e3ff5d8693 (patch)
tree9383203d4ca8ec52fe33502a99e6859fac612ba8
parent0400e141715d44e6cb8f25a3709e86a2d1ac7a1a (diff)
telegram: notify subcribed users
m---------deps/com.juick.server10
-rw-r--r--src/main/java/com/juick/api/TelegramBotHook.java57
2 files changed, 44 insertions, 23 deletions
diff --git a/deps/com.juick.server b/deps/com.juick.server
-Subproject ea43e78cb8dc20c2f54a15e37dba638b963f65e
+Subproject 3fe5180ff7f2488966d3c940629ddefcfc84467
diff --git a/src/main/java/com/juick/api/TelegramBotHook.java b/src/main/java/com/juick/api/TelegramBotHook.java
index c68d829a..907898f2 100644
--- a/src/main/java/com/juick/api/TelegramBotHook.java
+++ b/src/main/java/com/juick/api/TelegramBotHook.java
@@ -1,5 +1,6 @@
package com.juick.api;
+import com.juick.User;
import com.juick.json.MessageSerializer;
import com.juick.server.UserQueries;
import com.neovisionaries.ws.client.*;
@@ -13,6 +14,7 @@ import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.response.SendResponse;
import org.json.JSONObject;
+import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.http.HttpServletRequest;
@@ -20,6 +22,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -66,25 +69,12 @@ public class TelegramBotHook {
sb.append(jmsg.getAttachmentURL());
}
String msg = sb.toString();
+ List<Long> users = getSubscribers(jmsg.getUser().getUID());
List<Long> chats = getChats();
- chats.stream().forEach(c -> {
- bot.execute(new SendMessage(c, msg).replyMarkup(
- new InlineKeyboardMarkup(
- new InlineKeyboardButton[] {
- new InlineKeyboardButton("See on Juick").url("https://juick.com/" + jmsg.getMID())
- }
- )), new Callback<SendMessage, SendResponse>() {
- @Override
- public void onResponse(SendMessage request, SendResponse response) {
- logger.info("got response: " + response.message().toString());
- }
-
- @Override
- public void onFailure(SendMessage request, IOException e) {
- logger.log(Level.WARNING, "telegram failure", e);
- }
- });
- });
+ // registered subscribed users
+ users.stream().forEach(c -> telegramNotify(c, msg, jmsg.getMID()));
+ // anonymous
+ chats.stream().filter(u -> !getUser(u).isPresent()).forEach(c -> telegramNotify(c, msg, jmsg.getMID()));
}
});
ws.connect();
@@ -93,6 +83,25 @@ public class TelegramBotHook {
}
}
+ private void telegramNotify(Long c, String msg, int mid) {
+ bot.execute(new SendMessage(c, msg).replyMarkup(
+ new InlineKeyboardMarkup(
+ new InlineKeyboardButton[] {
+ new InlineKeyboardButton("See on Juick").url("https://juick.com/" + mid)
+ }
+ )), new Callback<SendMessage, SendResponse>() {
+ @Override
+ public void onResponse(SendMessage request, SendResponse response) {
+ logger.info("got response: " + response.message().toString());
+ }
+
+ @Override
+ public void onFailure(SendMessage request, IOException e) {
+ logger.log(Level.WARNING, "telegram failure", e);
+ }
+ });
+ }
+
List<Long> getChats() {
return jdbc.queryForList("SELECT chat_id FROM telegram_chats", Long.class);
}
@@ -135,4 +144,16 @@ public class TelegramBotHook {
return jdbc.update("INSERT INTO telegram(tg_id, tg_name, loginhash) VALUES(?,?,?)",
tgID, tgName, UUID.randomUUID().toString()) > 0;
}
+ private Optional<User> getUser(long tgId) {
+ try {
+ return Optional.of(jdbc.queryForObject("SELECT id, name, banned FROM users INNER JOIN telegram" +
+ "ON telegram.user_id = users.id WHERE telegram.tg_id=?", new UserQueries.UserMapper(), tgId));
+ } catch (EmptyResultDataAccessException e) {
+ return Optional.empty();
+ }
+ }
+ private List<Long> getSubscribers(int uid) {
+ return jdbc.queryForList("SELECT tg_id FROM telegram INNER JOIN subscr_users " +
+ "ON (subscr_users.user_id=? AND telegram.tg_id=subscr_users.suser_id)", Long.class, uid, uid);
+ }
}