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