From 3ec635eb771d8d68b63ae5139cd33cbec4e1222c Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Mon, 12 Dec 2022 10:25:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B3=D0=B8=D0=BD=20=D0=BF=D1=80=D0=B5-=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=86=D0=B5=D1=81=D1=81=D0=B8=D0=BD=D0=B3=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=B4=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BA=D0=BE=D0=B9=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/service/SenderMapRepository.java | 8 +++--- .../quarkus/sender/SendPreProcessing.java | 13 --------- .../quarkus/sender/TelegramSender.java | 28 +++++++++---------- .../simple/sender/SendPreProcessing.java | 13 --------- .../simple/sender/TelegramSender.java | 25 ++++++++++------- 5 files changed, 32 insertions(+), 55 deletions(-) delete mode 100644 telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/SendPreProcessing.java delete mode 100644 telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/SendPreProcessing.java diff --git a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/SenderMapRepository.java b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/SenderMapRepository.java index 75956e7..f3cc993 100644 --- a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/SenderMapRepository.java +++ b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/SenderMapRepository.java @@ -11,22 +11,22 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull; public class SenderMapRepository implements SenderRepository { - private final Map lastMessageId = new HashMap<>(); + private final Map lastMessageId = new HashMap<>(); @Override - public Uni getLastSendMessage(Long telegramId) { + public Uni getLastSendMessage(String telegramId) { return Uni.createFrom().item(lastMessageId.get(telegramId)); } @Override - public Uni saveLastSendMessage(@NotNull Long telegramId, @NotNull Integer messageId) { + public Uni saveLastSendMessage(@NotNull String telegramId, @NotNull Integer messageId) { isNotNull(telegramId); lastMessageId.put(telegramId, messageId); return Uni.createFrom().voidItem(); } @Override - public Uni removeLastSendMessage(Long telegramId) { + public Uni removeLastSendMessage(String telegramId) { lastMessageId.remove(telegramId); return Uni.createFrom().voidItem(); } diff --git a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/SendPreProcessing.java b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/SendPreProcessing.java deleted file mode 100644 index c034feb..0000000 --- a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/SendPreProcessing.java +++ /dev/null @@ -1,13 +0,0 @@ -package dev.struchkov.godfather.telegram.quarkus.sender; - -/** - * // TODO: 18.09.2020 Добавить описание. - * - * @author upagge 18.09.2020 - */ -@FunctionalInterface -public interface SendPreProcessing { - - String pretreatment(String messageText); - -} diff --git a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java index 9bb0381..280ed3b 100644 --- a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java +++ b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java @@ -2,6 +2,7 @@ package dev.struchkov.godfather.telegram.quarkus.sender; import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.main.domain.SendType; +import dev.struchkov.godfather.quarkus.context.service.PreSendProcessing; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert; @@ -18,9 +19,10 @@ import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; +import java.util.List; + import static dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert.convertInlineKeyBoard; import static dev.struchkov.haiti.utils.Checker.checkNotNull; -import static dev.struchkov.haiti.utils.Inspector.isNotNull; public class TelegramSender implements TelegramSending { @@ -30,7 +32,8 @@ public class TelegramSender implements TelegramSending { private final AbsSender absSender; - private SendPreProcessing sendPreProcessing; + //TODO [09.12.2022|uPagge]: Доработать использование preSendProcessors + private List preSendProcessors; private SenderRepository senderRepository; public TelegramSender(TelegramConnect telegramConnect) { @@ -42,11 +45,7 @@ public class TelegramSender implements TelegramSending { this.senderRepository = senderRepository; } - public void setSendPreProcessing(SendPreProcessing sendPreProcessing) { - this.sendPreProcessing = sendPreProcessing; - } - - public void setSenderRepository(SenderRepository senderStorageService) { + public void setSenderRepository(SenderRepository senderRepository) { this.senderRepository = senderRepository; } @@ -55,6 +54,11 @@ public class TelegramSender implements TelegramSending { return sendBoxAnswer(telegramId, boxAnswer, true); } + @Override + public void addPreSendProcess(@NotNull PreSendProcessing processing) { + preSendProcessors.add(processing); + } + @Override public Uni sendNotSave(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer) { return sendBoxAnswer(telegramId, boxAnswer, false); @@ -64,8 +68,6 @@ public class TelegramSender implements TelegramSending { return Uni.createFrom().voidItem() .onItem().transformToUni( v -> { - isNotNull(telegramId, boxAnswer); - if (boxAnswer.isReplace() && checkNotNull(senderRepository)) { return senderRepository.getLastSendMessage(telegramId) .onItem().transformToUni( @@ -89,7 +91,7 @@ public class TelegramSender implements TelegramSending { .onItem().transformToUni( v -> { final EditMessageText editMessageText = new EditMessageText(); - editMessageText.setChatId(String.valueOf(telegramId)); + editMessageText.setChatId(telegramId); editMessageText.setMessageId(lastMessageId); editMessageText.enableMarkdown(true); editMessageText.setText(boxAnswer.getMessage()); @@ -111,11 +113,7 @@ public class TelegramSender implements TelegramSending { final SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); sendMessage.setChatId(telegramId); - sendMessage.setText( - sendPreProcessing != null - ? sendPreProcessing.pretreatment(boxAnswer.getMessage()) - : boxAnswer.getMessage() - ); + sendMessage.setText(boxAnswer.getMessage()); sendMessage.setReplyMarkup(KeyBoardConvert.convertKeyBoard(boxAnswer.getKeyBoard())); try { diff --git a/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/SendPreProcessing.java b/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/SendPreProcessing.java deleted file mode 100644 index f98d98a..0000000 --- a/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/SendPreProcessing.java +++ /dev/null @@ -1,13 +0,0 @@ -package dev.struchkov.godfather.telegram.simple.sender; - -/** - * // TODO: 18.09.2020 Добавить описание. - * - * @author upagge 18.09.2020 - */ -@FunctionalInterface -public interface SendPreProcessing { - - String pretreatment(String messageText); - -} diff --git a/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java b/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java index eb2be75..2626f6b 100644 --- a/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java +++ b/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java @@ -2,6 +2,7 @@ package dev.struchkov.godfather.telegram.simple.sender; import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.main.domain.SendType; +import dev.struchkov.godfather.simple.context.service.PreSendProcessing; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert; @@ -17,6 +18,8 @@ import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; import static dev.struchkov.haiti.utils.Checker.checkNotNull; @@ -30,7 +33,7 @@ public class TelegramSender implements TelegramSending { private final AbsSender absSender; - private SendPreProcessing sendPreProcessing; + private List preSendProcessors = new ArrayList<>(); private SenderRepository senderRepository; public TelegramSender(TelegramConnect telegramConnect) { @@ -42,10 +45,6 @@ public class TelegramSender implements TelegramSending { this.senderRepository = senderRepository; } - public void setSendPreProcessing(SendPreProcessing sendPreProcessing) { - this.sendPreProcessing = sendPreProcessing; - } - public void setSenderRepository(SenderRepository senderRepository) { this.senderRepository = senderRepository; } @@ -55,6 +54,11 @@ public class TelegramSender implements TelegramSending { sendBoxAnswer(telegramId, boxAnswer, true); } + @Override + public void addPreSendProcess(@NotNull PreSendProcessing processing) { + preSendProcessors.add(processing); + } + @Override public void sendNotSave(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer) { sendBoxAnswer(telegramId, boxAnswer, false); @@ -62,6 +66,11 @@ public class TelegramSender implements TelegramSending { private void sendBoxAnswer(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { isNotNull(telegramId, boxAnswer); + + for (PreSendProcessing preSendProcessor : preSendProcessors) { + boxAnswer = preSendProcessor.pretreatment(boxAnswer); + } + try { if (boxAnswer.isReplace() && checkNotNull(senderRepository)) { final Optional optLastId = senderRepository.getLastSendMessage(telegramId); @@ -97,11 +106,7 @@ public class TelegramSender implements TelegramSending { final SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); sendMessage.setChatId(telegramId); - sendMessage.setText( - sendPreProcessing != null - ? sendPreProcessing.pretreatment(boxAnswer.getMessage()) - : boxAnswer.getMessage() - ); + sendMessage.setText(boxAnswer.getMessage()); sendMessage.setReplyMarkup(KeyBoardConvert.convertKeyBoard(boxAnswer.getKeyBoard())); try { final Message execute = absSender.execute(sendMessage);