messageService.create REQUIRES_NEW
This commit is contained in:
parent
6d6d70caf0
commit
0a8bf5c4fa
@ -15,7 +15,7 @@ import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class Offer {
|
||||
|
||||
@ -70,6 +70,7 @@ public class Offer {
|
||||
@Nullable
|
||||
private String imageURL = null;
|
||||
|
||||
@ToString.Exclude
|
||||
@OneToMany(mappedBy = "offer")
|
||||
private List<Message> messages = new ArrayList<>();
|
||||
|
||||
|
||||
@ -20,7 +20,6 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
public TelegramBot(final String token, final Consumer<Update> onUpdate) throws IOException, TelegramApiException {
|
||||
super(token);
|
||||
this.onUpdate = onUpdate;
|
||||
|
||||
log.info("Starting telegram bot...");
|
||||
final TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class);
|
||||
session = (DefaultBotSession) api.registerBot(this);
|
||||
|
||||
@ -31,6 +31,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@ -67,12 +68,17 @@ public class TelegramService {
|
||||
|
||||
@TransactionalEventListener(OfferDto.class)
|
||||
public void onOffer(final OfferDto offer) {
|
||||
final List<MessageDto> existing = messageService.findAllDtoByOfferDto(offer);
|
||||
for (ChatDto chat : chatService.findAllEnabled()) {
|
||||
existing.stream()
|
||||
.filter(m -> m.getChat().getId() == chat.getId())
|
||||
.findFirst()
|
||||
.ifPresentOrElse(this::update, () -> send(offer, chat));
|
||||
final List<MessageDto> existingMessages = messageService.findAllDtoByOfferDto(offer);
|
||||
final List<ChatDto> chats = chatService.findAllEnabled();
|
||||
for (final ChatDto chat : chats) {
|
||||
final Optional<MessageDto> existing = existingMessages.stream().filter(m -> m.getChat().getId() == chat.getId()).findFirst();
|
||||
if (existing.isPresent()) {
|
||||
log.info("Found existing message: {}", existing);
|
||||
update(existing.get());
|
||||
} else {
|
||||
log.info("Creating new message for chat={}", chat.getId());
|
||||
send(offer, chat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,21 +145,19 @@ public class TelegramService {
|
||||
}
|
||||
|
||||
private void send(final OfferDto offerDto, final ChatDto chatDto) {
|
||||
chatService.findAllEnabled().forEach(chat -> {
|
||||
try {
|
||||
final InputFile inputFile = offerDto.getImageURL() == null ? new InputFile(new ByteArrayInputStream(NO_IMAGE), "[Kein Bild]") : new InputFile(offerDto.getImageURL());
|
||||
final SendPhoto send = new SendPhoto(chat.getId() + "", inputFile);
|
||||
send.setCaption(createText(offerDto));
|
||||
send.setReplyMarkup(createKeyboard(false));
|
||||
try {
|
||||
final InputFile inputFile = offerDto.getImageURL() == null ? new InputFile(new ByteArrayInputStream(NO_IMAGE), "[Kein Bild]") : new InputFile(offerDto.getImageURL());
|
||||
final SendPhoto send = new SendPhoto(chatDto.getId() + "", inputFile);
|
||||
send.setCaption(createText(offerDto));
|
||||
send.setReplyMarkup(createKeyboard(false));
|
||||
|
||||
log.info("Sending Offer: {}", offerDto);
|
||||
final Message message = bot.execute(send);
|
||||
log.info("Sending: chat={}, offer={}", chatDto, offerDto);
|
||||
final Message tlgMessage = bot.execute(send);
|
||||
|
||||
messageService.create(offerDto, chatDto, message);
|
||||
} catch (TelegramApiException | JsonProcessingException e) {
|
||||
log.error("Failed to send Message to #{}.", chat.getId(), e);
|
||||
}
|
||||
});
|
||||
messageService.create(offerDto, chatDto, tlgMessage);
|
||||
} catch (TelegramApiException | JsonProcessingException e) {
|
||||
log.error("Failed to send: chat={}, offer={}: {}", chatDto, offerDto, e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void update(final MessageDto messageDto) {
|
||||
|
||||
@ -42,10 +42,10 @@ public class Message {
|
||||
@Column
|
||||
private boolean remember = false;
|
||||
|
||||
public Message(@NonNull final Offer offer, @NonNull final Chat chat, final org.telegram.telegrambots.meta.api.objects.Message message) {
|
||||
public Message(@NonNull final Offer offer, @NonNull final Chat chat, @NonNull final org.telegram.telegrambots.meta.api.objects.Message tlgMessage) {
|
||||
this.chat = chat;
|
||||
this.offer = offer;
|
||||
this.telegramMessageId = message.getMessageId();
|
||||
this.telegramMessageId = tlgMessage.getMessageId();
|
||||
}
|
||||
|
||||
public void setRemember(final boolean newRemember) {
|
||||
|
||||
@ -10,7 +10,7 @@ public interface MessageRepository extends ListCrudRepository<Message, Long> {
|
||||
|
||||
Optional<Message> findByChat_IdAndTelegramMessageId(long chatId, int messageId);
|
||||
|
||||
List<Message> findAllByOffer_id(long id);
|
||||
List<Message> findAllByOffer_Id(long id);
|
||||
|
||||
List<Message> findAllByExpiryBefore(ZonedDateTime deadline);
|
||||
|
||||
|
||||
@ -13,6 +13,7 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.telegram.telegrambots.meta.api.objects.MaybeInaccessibleMessage;
|
||||
|
||||
@ -52,10 +53,11 @@ public class MessageService {
|
||||
}
|
||||
}
|
||||
|
||||
public void create(final OfferDto offerDto, final ChatDto chatDto, final org.telegram.telegrambots.meta.api.objects.Message message) {
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void create(final OfferDto offerDto, final ChatDto chatDto, final org.telegram.telegrambots.meta.api.objects.Message tlgMessage) {
|
||||
final Offer offer = offerService.getByDto(offerDto);
|
||||
final Chat chat = chatService.getByDto(chatDto);
|
||||
messageRepository.save(new Message(offer, chat, message));
|
||||
messageRepository.save(new Message(offer, chat, tlgMessage));
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
@ -88,7 +90,7 @@ public class MessageService {
|
||||
}
|
||||
|
||||
public List<MessageDto> findAllDtoByOfferDto(final OfferDto offer) {
|
||||
return messageRepository.findAllByOffer_id(offer.getId()).stream().map(this::toDto).toList();
|
||||
return messageRepository.findAllByOffer_Id(offer.getId()).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
private MessageDto toDto(final Message message) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user