spring-boot + database

This commit is contained in:
Patrick Haßel 2024-06-10 09:20:50 +02:00
commit ef2b9a8646
2 changed files with 32 additions and 24 deletions

View File

@ -66,6 +66,10 @@ public class OfferService {
findByDto(dto).ifPresent(offer -> offer.setTelegramMessageId(telegramMessageId));
}
public void setHide(final OfferDto dto, final boolean hide) {
findByDto(dto).ifPresent(offer -> offer.setHide(hide));
}
private Optional<Offer> findByDto(final OfferDto dto) {
return repository.findById(dto.getId());
}

View File

@ -67,9 +67,9 @@ public class TelegramService {
public void onOfferChanged(final OfferDto offer) {
if (offer.is_existing_()) {
if (offer.getTelegramMessageId() == null) {
send(offer);
}else{
updateMessage(offer.getTelegramMessageId(), offer.getTelegramMessageId().getChatId());
send(offer);
} else {
updateMessage(CHAT_ID, offer.getTelegramMessageId());
}
} else {
remove(List.of(offer));
@ -108,7 +108,7 @@ public class TelegramService {
case HIDE -> hide(message);
case REMEMBER -> remember(message, true);
case UNREMEMBER -> remember(message, false);
default -> updateMessage(message, message.getChatId());
default -> updateMessage(message.getChatId(), message.getMessageId());
}
} catch (JsonProcessingException e) {
log.error("Failed to read InlineDto.", e);
@ -117,30 +117,40 @@ public class TelegramService {
private void hide(final MaybeInaccessibleMessage message) {
offerService.hideByTelegramMessageId(message.getMessageId(), true);
remove(message);
_remove(message.getMessageId());
}
private void remember(final MaybeInaccessibleMessage message, final boolean remember) {
offerService.rememberByTelegramMessageId(message.getMessageId(), remember).ifPresentOrElse(
offer -> updateMessage(offer, message.getChatId(), message.getMessageId()),
() -> remove(message)
() -> _remove(message.getMessageId())
);
}
private void updateMessage(final MaybeInaccessibleMessage message, final Long chatId) {
offerService.findByTelegramMessageId(message.getMessageId()).ifPresentOrElse(
offer -> updateMessage(offer, chatId, message.getMessageId()),
() -> remove(message)
private void updateMessage(final long chatId, final int messageId) {
offerService.findByTelegramMessageId(messageId).ifPresentOrElse(
offer -> updateMessage(offer, chatId, messageId),
() -> _remove(messageId)
);
}
private void updateMessage(final OfferDto offer, final long chatId, final int messageId) {
if (offer.isHide()) {
return;
}
try {
final EditMessageCaption edit = new EditMessageCaption(chatId + "", messageId, null, createText(offer), createKeyboard(offer), null, null);
edit.setParseMode("Markdown");
bot.execute(edit);
} catch (TelegramApiException | JsonProcessingException e) {
log.error("Failed to edit Message to #{}.", chatId, e);
if (e.toString().endsWith("Bad Request: message to edit not found")) {
log.info("Message has been deleted by User. Marking has hidden: {}", offer);
offerService.setHide(offer, true);
} else if (e.toString().endsWith("Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message")) {
log.debug("Ignoring complaint from telegram-bot-api about unmodified message: {}", offer);
} else {
log.error("Failed to edit Message to #{}.", chatId, e);
}
}
}
@ -160,11 +170,12 @@ public class TelegramService {
}
private String createText(final OfferDto offer) {
return "[%s](%s)\n%s\n%s".formatted(
return "%s\n%s\n%s\n%s\nv%d".formatted(
offer.getTitle().replaceAll("\\[", "(").replaceAll("]", ")"),
offer.getArticleURL(),
offer.combineLocation(),
offer.getDescription()
offer.getDescription(),
offer.getArticleURL(),
offer.getVersion()
);
}
@ -189,19 +200,12 @@ public class TelegramService {
}
private void remove(final List<OfferDto> offers) {
_remove(offers.stream().map(OfferDto::getTelegramMessageId).filter(Objects::nonNull).toList());
_remove(offers.stream().map(OfferDto::getTelegramMessageId).filter(Objects::nonNull).toArray(Integer[]::new));
}
private void remove(final MaybeInaccessibleMessage... messages) {
_remove(Arrays.stream(messages).map(MaybeInaccessibleMessage::getMessageId).toList());
}
private void _remove(final List<Integer> messageIds) {
if (messageIds.isEmpty()) {
return;
}
private void _remove(final Integer... messageIds) {
try {
bot.execute(new DeleteMessages(CHAT_ID + "", messageIds));
bot.execute(new DeleteMessages(CHAT_ID + "", Arrays.stream(messageIds).toList()));
} catch (TelegramApiException e) {
log.error("Failed to remove Message to #{}.", CHAT_ID, e);
}