spring-boot + database
This commit is contained in:
commit
ef2b9a8646
@ -66,6 +66,10 @@ public class OfferService {
|
|||||||
findByDto(dto).ifPresent(offer -> offer.setTelegramMessageId(telegramMessageId));
|
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) {
|
private Optional<Offer> findByDto(final OfferDto dto) {
|
||||||
return repository.findById(dto.getId());
|
return repository.findById(dto.getId());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,8 +68,8 @@ public class TelegramService {
|
|||||||
if (offer.is_existing_()) {
|
if (offer.is_existing_()) {
|
||||||
if (offer.getTelegramMessageId() == null) {
|
if (offer.getTelegramMessageId() == null) {
|
||||||
send(offer);
|
send(offer);
|
||||||
}else{
|
} else {
|
||||||
updateMessage(offer.getTelegramMessageId(), offer.getTelegramMessageId().getChatId());
|
updateMessage(CHAT_ID, offer.getTelegramMessageId());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
remove(List.of(offer));
|
remove(List.of(offer));
|
||||||
@ -108,7 +108,7 @@ public class TelegramService {
|
|||||||
case HIDE -> hide(message);
|
case HIDE -> hide(message);
|
||||||
case REMEMBER -> remember(message, true);
|
case REMEMBER -> remember(message, true);
|
||||||
case UNREMEMBER -> remember(message, false);
|
case UNREMEMBER -> remember(message, false);
|
||||||
default -> updateMessage(message, message.getChatId());
|
default -> updateMessage(message.getChatId(), message.getMessageId());
|
||||||
}
|
}
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.error("Failed to read InlineDto.", e);
|
log.error("Failed to read InlineDto.", e);
|
||||||
@ -117,32 +117,42 @@ public class TelegramService {
|
|||||||
|
|
||||||
private void hide(final MaybeInaccessibleMessage message) {
|
private void hide(final MaybeInaccessibleMessage message) {
|
||||||
offerService.hideByTelegramMessageId(message.getMessageId(), true);
|
offerService.hideByTelegramMessageId(message.getMessageId(), true);
|
||||||
remove(message);
|
_remove(message.getMessageId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void remember(final MaybeInaccessibleMessage message, final boolean remember) {
|
private void remember(final MaybeInaccessibleMessage message, final boolean remember) {
|
||||||
offerService.rememberByTelegramMessageId(message.getMessageId(), remember).ifPresentOrElse(
|
offerService.rememberByTelegramMessageId(message.getMessageId(), remember).ifPresentOrElse(
|
||||||
offer -> updateMessage(offer, message.getChatId(), message.getMessageId()),
|
offer -> updateMessage(offer, message.getChatId(), message.getMessageId()),
|
||||||
() -> remove(message)
|
() -> _remove(message.getMessageId())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateMessage(final MaybeInaccessibleMessage message, final Long chatId) {
|
private void updateMessage(final long chatId, final int messageId) {
|
||||||
offerService.findByTelegramMessageId(message.getMessageId()).ifPresentOrElse(
|
offerService.findByTelegramMessageId(messageId).ifPresentOrElse(
|
||||||
offer -> updateMessage(offer, chatId, message.getMessageId()),
|
offer -> updateMessage(offer, chatId, messageId),
|
||||||
() -> remove(message)
|
() -> _remove(messageId)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateMessage(final OfferDto offer, final long chatId, final int messageId) {
|
private void updateMessage(final OfferDto offer, final long chatId, final int messageId) {
|
||||||
|
if (offer.isHide()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
final EditMessageCaption edit = new EditMessageCaption(chatId + "", messageId, null, createText(offer), createKeyboard(offer), null, null);
|
final EditMessageCaption edit = new EditMessageCaption(chatId + "", messageId, null, createText(offer), createKeyboard(offer), null, null);
|
||||||
edit.setParseMode("Markdown");
|
edit.setParseMode("Markdown");
|
||||||
bot.execute(edit);
|
bot.execute(edit);
|
||||||
} catch (TelegramApiException | JsonProcessingException e) {
|
} catch (TelegramApiException | JsonProcessingException 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);
|
log.error("Failed to edit Message to #{}.", chatId, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void send(final OfferDto offer) {
|
private void send(final OfferDto offer) {
|
||||||
try {
|
try {
|
||||||
@ -160,11 +170,12 @@ public class TelegramService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String createText(final OfferDto offer) {
|
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.getTitle().replaceAll("\\[", "(").replaceAll("]", ")"),
|
||||||
offer.getArticleURL(),
|
|
||||||
offer.combineLocation(),
|
offer.combineLocation(),
|
||||||
offer.getDescription()
|
offer.getDescription(),
|
||||||
|
offer.getArticleURL(),
|
||||||
|
offer.getVersion()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,19 +200,12 @@ public class TelegramService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void remove(final List<OfferDto> offers) {
|
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) {
|
private void _remove(final Integer... messageIds) {
|
||||||
_remove(Arrays.stream(messages).map(MaybeInaccessibleMessage::getMessageId).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void _remove(final List<Integer> messageIds) {
|
|
||||||
if (messageIds.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
bot.execute(new DeleteMessages(CHAT_ID + "", messageIds));
|
bot.execute(new DeleteMessages(CHAT_ID + "", Arrays.stream(messageIds).toList()));
|
||||||
} catch (TelegramApiException e) {
|
} catch (TelegramApiException e) {
|
||||||
log.error("Failed to remove Message to #{}.", CHAT_ID, e);
|
log.error("Failed to remove Message to #{}.", CHAT_ID, e);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user