Skip to content

Commit 2f81307

Browse files
committed
Ability to remove image from index and post from channel
1 parent 16e5f38 commit 2f81307

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/main/java/com/annimon/similarimagesbot/BotHandler.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.pengrad.telegrambot.model.PhotoSize;
99
import com.pengrad.telegrambot.model.Update;
1010
import com.pengrad.telegrambot.model.request.ParseMode;
11+
import com.pengrad.telegrambot.request.DeleteMessage;
1112
import com.pengrad.telegrambot.request.GetFile;
1213
import com.pengrad.telegrambot.request.GetUpdates;
1314
import com.pengrad.telegrambot.request.SendMessage;
@@ -20,6 +21,7 @@
2021
import java.util.Comparator;
2122
import java.util.List;
2223
import java.util.Objects;
24+
import java.util.regex.Pattern;
2325
import java.util.stream.Collectors;
2426
import javax.imageio.ImageIO;
2527

@@ -42,13 +44,37 @@ public void setAdminId(long adminId) {
4244

4345
public void run() {
4446
bot.setUpdatesListener(updates -> {
47+
processAdminCommands(updates);
4548
processUpdates(updates);
4649
return UpdatesListener.CONFIRMED_UPDATES_ALL;
4750
});
4851
}
4952

5053
public void runOnce() {
51-
processUpdates(bot.execute(new GetUpdates()).updates());
54+
final var updates = bot.execute(new GetUpdates()).updates();
55+
processAdminCommands(updates);
56+
processUpdates(updates);
57+
}
58+
59+
private void processAdminCommands(List<Update> updates) {
60+
final var delPattern = Pattern.compile("/del(\\d+)m(\\d+)");
61+
updates.stream()
62+
.map(Update::message)
63+
.filter(Objects::nonNull)
64+
.filter(msg -> msg.chat().id() == adminId)
65+
.map(Message::text)
66+
.filter(Objects::nonNull)
67+
.forEach(command -> {
68+
final var m = delPattern.matcher(command);
69+
if (m.find()) {
70+
final var channelId = Long.parseLong("-100" + m.group(1));
71+
final var messageId = Integer.parseInt(m.group(2));
72+
bot.execute(new DeleteMessage(channelId, messageId));
73+
try {
74+
indexer.deleteImage(channelId, messageId);
75+
} catch (SQLException ignored) {}
76+
}
77+
});
5278
}
5379

5480
private void processUpdates(List<Update> updates) {
@@ -84,10 +110,14 @@ private List<Message> getChannelPostsWithPhotos(List<Update> updates) {
84110

85111
private void sendReport(List<SimilarImagesInfo> infos) {
86112
String report = infos.stream().map(info -> {
87-
String text = "For post " + formatPostLink(info.getOriginalPost()) + " found:\n";
113+
final var post = info.getOriginalPost();
114+
String text = "For post " + formatPostLink(post) + " found:\n";
88115
text += info.getResults().stream()
89116
.map(r -> String.format(" %s, dst: %.2f", formatPostLink(r.getPost()), r.getDistance()))
90117
.collect(Collectors.joining("\n"));
118+
text += String.format("%n/del%sm%d",
119+
post.getChannelId().toString().replace("-100", ""),
120+
post.getMessageId());
91121
return text;
92122
}).collect(Collectors.joining("\n\n"));
93123

src/main/java/com/annimon/similarimagesbot/ImageIndexer.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.annimon.similarimagesbot.data.SimilarImagesInfo;
66
import com.github.kilianB.hashAlgorithms.DifferenceHash;
77
import com.github.kilianB.hashAlgorithms.PerceptiveHash;
8-
import com.github.kilianB.matcher.persistent.database.H2DatabaseImageMatcher;
98
import java.awt.image.BufferedImage;
109
import java.sql.DriverManager;
1110
import java.sql.SQLException;
@@ -17,7 +16,7 @@
1716

1817
public class ImageIndexer {
1918

20-
private final Map<Long, H2DatabaseImageMatcher> databases = new HashMap<>(5);
19+
private final Map<Long, SimilarImagesH2DatabaseMatcher> databases = new HashMap<>(5);
2120
private final DifferenceHash differenceHash = new DifferenceHash(32, Precision.Double);
2221
private final PerceptiveHash perceptiveHash = new PerceptiveHash(32);
2322

@@ -41,14 +40,21 @@ public SimilarImagesInfo processImage(Post originalPost, BufferedImage image)
4140
return new SimilarImagesInfo(originalPost, results);
4241
}
4342

44-
private H2DatabaseImageMatcher getDatabaseForChannel(Long channelId) throws SQLException {
43+
44+
public void deleteImage(Long channelId, Integer messageId) throws SQLException {
45+
final String uniqueId = messageId.toString();
46+
final var db = getDatabaseForChannel(channelId);
47+
db.removeImage(uniqueId);
48+
}
49+
50+
private SimilarImagesH2DatabaseMatcher getDatabaseForChannel(Long channelId) throws SQLException {
4551
var db = databases.get(channelId);
4652
if (db != null) {
4753
return db;
4854
}
4955
var jdbcUrl = "jdbc:h2:./imagesdb_" + channelId;
5056
var conn = DriverManager.getConnection(jdbcUrl, "root", "");
51-
db = new H2DatabaseImageMatcher(conn);
57+
db = new SimilarImagesH2DatabaseMatcher(conn);
5258
db.addHashingAlgorithm(differenceHash, 0.4);
5359
db.addHashingAlgorithm(perceptiveHash, 0.2);
5460
databases.put(channelId, db);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.annimon.similarimagesbot;
2+
3+
import com.github.kilianB.matcher.persistent.database.H2DatabaseImageMatcher;
4+
import java.sql.Connection;
5+
import java.sql.SQLException;
6+
7+
public class SimilarImagesH2DatabaseMatcher extends H2DatabaseImageMatcher {
8+
9+
public SimilarImagesH2DatabaseMatcher(Connection dbConnection) throws SQLException {
10+
super(dbConnection);
11+
}
12+
13+
public void removeImage(String uniqueId) throws SQLException {
14+
for (var algo : steps.keySet()) {
15+
try (var stmt = conn
16+
.prepareStatement("DELETE FROM " + resolveTableName(algo) + " WHERE URL = ?")) {
17+
stmt.setString(1, uniqueId);
18+
stmt.execute();
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)