Fix filtering removed posts

This commit is contained in:
Victor Melnik 2020-10-02 20:13:54 +00:00 committed by GitHub
parent d26caebd3f
commit c3868f1744
3 changed files with 30 additions and 11 deletions

5
.gitignore vendored
View File

@ -1,4 +1,9 @@
.idea .idea
.vscode
.gradle .gradle
.classpath
.project
.settings
bin
build build
*.db *.db

View File

@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@ -44,27 +45,27 @@ public class BotHandler {
public void run() { public void run() {
bot.setUpdatesListener(updates -> { bot.setUpdatesListener(updates -> {
processAdminCommands(updates); final var removedPosts = processAdminCommands(updates);
processUpdates(updates); processUpdates(updates, removedPosts);
return UpdatesListener.CONFIRMED_UPDATES_ALL; return UpdatesListener.CONFIRMED_UPDATES_ALL;
}); });
} }
public void runOnce() { public void runOnce() {
final var updates = bot.execute(new GetUpdates()).updates(); final var updates = bot.execute(new GetUpdates()).updates();
processAdminCommands(updates); final var removedPosts = processAdminCommands(updates);
processUpdates(updates); processUpdates(updates, removedPosts);
} }
private void processAdminCommands(List<Update> updates) { private Set<Post> processAdminCommands(List<Update> updates) {
final var delPattern = Pattern.compile("/del(\\d+)m(\\d+)"); final var delPattern = Pattern.compile("/del(\\d+)m(\\d+)");
updates.stream() return updates.stream()
.map(Update::message) .map(Update::message)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.filter(msg -> msg.chat().id() == adminId) .filter(msg -> msg.chat().id() == adminId)
.map(Message::text) .map(Message::text)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.forEach(command -> { .map(command -> {
final var m = delPattern.matcher(command); final var m = delPattern.matcher(command);
if (m.find()) { if (m.find()) {
final var channelId = Long.parseLong("-100" + m.group(1)); final var channelId = Long.parseLong("-100" + m.group(1));
@ -72,21 +73,29 @@ public class BotHandler {
bot.execute(new DeleteMessage(channelId, messageId)); bot.execute(new DeleteMessage(channelId, messageId));
try { try {
indexer.deleteImage(channelId, messageId); indexer.deleteImage(channelId, messageId);
} catch (SQLException ignored) {} } catch (SQLException ex) {
System.err.println("Cannot delete image in db");
}
return new Post(channelId, messageId);
} }
}); return null;
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
} }
private void processUpdates(List<Update> updates) { private void processUpdates(List<Update> updates, Set<Post> ignoredPosts) {
final List<Message> channelPosts = getChannelPostsWithPhotos(updates); final List<Message> channelPosts = getChannelPostsWithPhotos(updates);
final var similarImagesInfos = new ArrayList<SimilarImagesInfo>(); final var similarImagesInfos = new ArrayList<SimilarImagesInfo>();
for (var post : channelPosts) { for (var post : channelPosts) {
final var originalPost = new Post(post.chat().id(), post.messageId());
if (ignoredPosts.contains(originalPost)) continue;
final PhotoSize photo = getSmallestPhoto(post.photo()); final PhotoSize photo = getSmallestPhoto(post.photo());
try { try {
final var tgFile = bot.execute(new GetFile(photo.fileId())).file(); final var tgFile = bot.execute(new GetFile(photo.fileId())).file();
final var url = new URL(bot.getFullFilePath(tgFile)); final var url = new URL(bot.getFullFilePath(tgFile));
final BufferedImage image = ImageIO.read(url); final BufferedImage image = ImageIO.read(url);
final var originalPost = new Post(post.chat().id(), post.messageId());
final SimilarImagesInfo info = indexer.processImage(originalPost, image); final SimilarImagesInfo info = indexer.processImage(originalPost, image);
if (info.hasResults()) { if (info.hasResults()) {
similarImagesInfos.add(info); similarImagesInfos.add(info);

View File

@ -33,4 +33,9 @@ public class Post {
public int hashCode() { public int hashCode() {
return Objects.hash(channelId, messageId); return Objects.hash(channelId, messageId);
} }
@Override
public String toString() {
return "{" + channelId + ":" + messageId + "}";
}
} }