Рефакторгин пре-процессинга перед отправкой сообщения

This commit is contained in:
Struchkov Mark 2022-12-12 10:25:13 +03:00
parent 78cc771852
commit 3ec635eb77
5 changed files with 32 additions and 55 deletions

View File

@ -11,22 +11,22 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull;
public class SenderMapRepository implements SenderRepository {
private final Map<Long, Integer> lastMessageId = new HashMap<>();
private final Map<String, Integer> lastMessageId = new HashMap<>();
@Override
public Uni<Integer> getLastSendMessage(Long telegramId) {
public Uni<Integer> getLastSendMessage(String telegramId) {
return Uni.createFrom().item(lastMessageId.get(telegramId));
}
@Override
public Uni<Void> saveLastSendMessage(@NotNull Long telegramId, @NotNull Integer messageId) {
public Uni<Void> saveLastSendMessage(@NotNull String telegramId, @NotNull Integer messageId) {
isNotNull(telegramId);
lastMessageId.put(telegramId, messageId);
return Uni.createFrom().voidItem();
}
@Override
public Uni<Void> removeLastSendMessage(Long telegramId) {
public Uni<Void> removeLastSendMessage(String telegramId) {
lastMessageId.remove(telegramId);
return Uni.createFrom().voidItem();
}

View File

@ -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);
}

View File

@ -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<PreSendProcessing> 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<Void> 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 {

View File

@ -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);
}

View File

@ -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<PreSendProcessing> 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<Integer> 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);