Skip to content

Commit

Permalink
Fixing bug <- Sending updates to all
Browse files Browse the repository at this point in the history
  • Loading branch information
patbaumgartner committed Apr 30, 2022
1 parent 4f0dbed commit 468792e
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -40,17 +42,20 @@ public class LoveboxBot extends TelegramLongPollingBot {

private final Set<Long> chatIds = new TreeSet<>();
private final ConcurrentHashMap<String, String> loveboxMessageStore = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Message> telegramMessageStore = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Collection<Pair<Long, Message>>> telegramMessageStore = new ConcurrentHashMap<>();

@Scheduled(fixedRate = 10_000)
public void readMessageBox() {
List<Pair<String, String>> messages = loveboxService.getMessagesByBox();
messages.forEach(p -> {
loveboxMessageStore.computeIfPresent(p.left(), (key, value) -> {
if (!value.equals(p.right())) {
Message message = telegramMessageStore.get(p.left());
if (message != null) {
updatePhotoMessageCaption(message, p.right());
Collection<Pair<Long, Message>> pairs = telegramMessageStore.get(p.left());
for (Pair<Long, Message> pair : pairs) {
Message message = pair.right();
if (message != null) {
updatePhotoMessageCaption(message, p.right());
}
}
}
return value;
Expand Down Expand Up @@ -109,7 +114,8 @@ public void onUpdateReceived(Update update) {
// Send/respond Message
for (long chatId : chatIds) {
Message sentMessage = sendPhotoMessage(chatId, text, imagePair, statusTripple);
telegramMessageStore.putIfAbsent(statusTripple.left(), sentMessage);
telegramMessageStore.compute(statusTripple.left(), (key, value) -> (value == null) ? new ArrayList<>() : value)
.add(new Pair<>(chatId, sentMessage));
}
}
}
Expand Down

0 comments on commit 468792e

Please sign in to comment.