Skip to content

Commit

Permalink
migrated PhotoGroupPost#photoUrlsPages to LinkedList<Queue<String>>
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Oct 29, 2023
1 parent 3e71e5c commit 79a9fce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.github.yvasyliev.model.dto.post;

import java.util.List;
import java.util.LinkedList;
import java.util.Queue;

public class PhotoGroupPost extends SpoilerablePost {
private List<List<String>> photoUrlsPages;
private LinkedList<Queue<String>> photoUrlsPages; // TODO: 10/29/2023 Migrate to JDK 21 and replace with List<List<String>>

public List<List<String>> getPhotoUrlsPages() {
public LinkedList<Queue<String>> getPhotoUrlsPages() {
return photoUrlsPages;
}

public void setPhotoUrlsPages(List<List<String>> photoUrlsPages) {
public void setPhotoUrlsPages(LinkedList<Queue<String>> photoUrlsPages) {
this.photoUrlsPages = photoUrlsPages;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -33,18 +36,16 @@ public Optional<Post> applyWithException(JsonNode jsonPost) {
return Optional.empty();
}

private List<List<String>> extractPhotoUrlsPages(JsonNode post) {
private LinkedList<Queue<String>> extractPhotoUrlsPages(JsonNode post) {
var photoUrls = extractPhotoUrls(post);
return IntStream
return new LinkedList<>(IntStream
.range(0, photoUrls.size())
.boxed()
.collect(Collectors.groupingBy(
i -> i / pageSize,
Collectors.mapping(photoUrls::get, Collectors.toList())
Collectors.mapping(photoUrls::get, Collectors.toCollection(ArrayDeque::new))
))
.values()
.stream()
.toList();
.values());
}

private List<String> extractPhotoUrls(JsonNode post) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -47,7 +48,7 @@ public Optional<List<Message>> applyWithException(String chatId, PhotoGroupPost
? responseReader.applyWithException("responses/photogroup.md").formatted(text)
: text;

var sendMediaGroup = sendMediaGroup(chatId, pages.get(0), hasSpoiler, null); // TODO: 10/16/2023 replace pages.get(0)
var sendMediaGroup = sendMediaGroup(chatId, pages.getFirst(), hasSpoiler, null);
sendMediaGroup.getMedias().get(0).setCaption(caption);

var publishedPost = redTelBot.execute(sendMediaGroup);
Expand All @@ -74,17 +75,16 @@ private List<Message> sendDelayed(String chatId, int replyToMessageId, PhotoGrou
var pages = post.getPhotoUrlsPages();
var hasSpoiler = post.isHasSpoiler();
var messages = new ArrayList<Message>();
for (var i = 1; i < pages.size(); i++) {
var page = pages.get(i);
for (var page : pages.subList(1, pages.size())) {
messages.addAll(page.size() > 1
? executeDelayed(sendMediaGroup(chatId, page, hasSpoiler, replyToMessageId))
: List.of(executeDelayed(sendPhoto(chatId, page.get(0), hasSpoiler, replyToMessageId)))
: List.of(executeDelayed(sendPhoto(chatId, page.element(), hasSpoiler, replyToMessageId)))
);
}
return messages;
}

private SendMediaGroup sendMediaGroup(String chatId, List<String> page, boolean hasSpoiler, Integer replyToMessageId) {
private SendMediaGroup sendMediaGroup(String chatId, Queue<String> page, boolean hasSpoiler, Integer replyToMessageId) {
var inputMediaPhotos = page
.stream()
.map(photoUrl -> (InputMedia) InputMediaPhoto
Expand Down

0 comments on commit 79a9fce

Please sign in to comment.