From f641853214daace2a73503f3a78e1f9ba29fd386 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 17 Jan 2018 13:43:53 +0300 Subject: api: delete telegram id when got 403 --- .../java/com/juick/api/TelegramBotManager.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'juick-api/src/main/java/com') 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 { 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 { @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()); + } } } -- cgit v1.2.3