diff options
author | Vitaly Takmazov | 2018-01-17 13:43:53 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-01-17 13:43:53 +0300 |
commit | f641853214daace2a73503f3a78e1f9ba29fd386 (patch) | |
tree | 0eeeab99a0512d98198289714fa06edf9e5cef9a | |
parent | 62df283157ff32dded77e833fbd0f4e3007acbbe (diff) |
api: delete telegram id when got 403
3 files changed, 28 insertions, 2 deletions
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java index 098d0876..757dd347 100644 --- a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java +++ b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java @@ -21,6 +21,7 @@ import com.juick.server.component.MessageEvent; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.TelegramService; +import com.juick.service.UserService; import com.pengrad.telegrambot.Callback; import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.model.request.ParseMode; @@ -60,6 +61,8 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> { private MessagesService messagesService; @Inject private SubscriptionService subscriptionService; + @Inject + private UserService userService; private static final String MSG_LINK = "🔗"; @@ -120,8 +123,23 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> { @Override public void onResponse(SendMessage request, SendResponse response) { if (!response.isOk()) { - logger.warn("null response, isOk: {}, errorCode: {}, description: {}", - response.isOk(), response.errorCode(), response.description()); + if (response.errorCode() == 403) { + // remove from anonymous chat + telegramService.getChats().stream().filter(c -> c.equals(chatId)).findFirst().ifPresent( + d -> { + telegramService.deleteChat(d); + logger.info("deleted {} chat", d); + } + ); + int userId = telegramService.getUser(chatId); + if (userId > 0) { + telegramService.deleteTelegramUser(chatId.intValue()); + logger.info("deleted telegram id of @{}", userService.getUserByUID(userId)); + } + } else { + logger.warn("error response, isOk: {}, errorCode: {}, description: {}", + response.isOk(), response.errorCode(), response.description()); + } } } diff --git a/juick-server-core/src/main/java/com/juick/service/TelegramService.java b/juick-server-core/src/main/java/com/juick/service/TelegramService.java index 4c8ebc4b..7786ca9f 100644 --- a/juick-server-core/src/main/java/com/juick/service/TelegramService.java +++ b/juick-server-core/src/main/java/com/juick/service/TelegramService.java @@ -27,6 +27,8 @@ import java.util.List; public interface TelegramService { boolean addChat(Long id); + boolean deleteChat(Long id); + List<Long> getChats(); int getUser(long tgId); diff --git a/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java index 325396e4..8e941531 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java @@ -46,6 +46,12 @@ public class TelegramServiceImpl extends BaseJdbcService implements TelegramServ return getJdbcTemplate().update("INSERT IGNORE INTO telegram_chats(chat_id) VALUES(?)", id) > 0; } + @Transactional + @Override + public boolean deleteChat(Long id) { + return getJdbcTemplate().update("DELETE FROM telegram_chats WHERE chat_id=?", id) > 0; + } + @Transactional(readOnly = true) @Override public List<Long> getChats() { |