Skip to content

Commit

Permalink
Fix filtering removed posts
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON authored Oct 2, 2020
1 parent d26caeb commit c3868f1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
.idea
.vscode
.gradle
.classpath
.project
.settings
bin
build
*.db
31 changes: 20 additions & 11 deletions src/main/java/com/annimon/similarimagesbot/BotHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
Expand All @@ -44,49 +45,57 @@ public void setAdminId(long adminId) {

public void run() {
bot.setUpdatesListener(updates -> {
processAdminCommands(updates);
processUpdates(updates);
final var removedPosts = processAdminCommands(updates);
processUpdates(updates, removedPosts);
return UpdatesListener.CONFIRMED_UPDATES_ALL;
});
}

public void runOnce() {
final var updates = bot.execute(new GetUpdates()).updates();
processAdminCommands(updates);
processUpdates(updates);
final var removedPosts = processAdminCommands(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+)");
updates.stream()
return updates.stream()
.map(Update::message)
.filter(Objects::nonNull)
.filter(msg -> msg.chat().id() == adminId)
.map(Message::text)
.filter(Objects::nonNull)
.forEach(command -> {
.map(command -> {
final var m = delPattern.matcher(command);
if (m.find()) {
final var channelId = Long.parseLong("-100" + m.group(1));
final var messageId = Integer.parseInt(m.group(2));
bot.execute(new DeleteMessage(channelId, messageId));
try {
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 var similarImagesInfos = new ArrayList<SimilarImagesInfo>();
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());
try {
final var tgFile = bot.execute(new GetFile(photo.fileId())).file();
final var url = new URL(bot.getFullFilePath(tgFile));
final BufferedImage image = ImageIO.read(url);
final var originalPost = new Post(post.chat().id(), post.messageId());
final SimilarImagesInfo info = indexer.processImage(originalPost, image);
if (info.hasResults()) {
similarImagesInfos.add(info);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/annimon/similarimagesbot/data/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(channelId, messageId);
}

@Override
public String toString() {
return "{" + channelId + ":" + messageId + "}";
}
}

0 comments on commit c3868f1

Please sign in to comment.