21
21
import java .util .Comparator ;
22
22
import java .util .List ;
23
23
import java .util .Objects ;
24
+ import java .util .Set ;
24
25
import java .util .regex .Pattern ;
25
26
import java .util .stream .Collectors ;
26
27
import javax .imageio .ImageIO ;
@@ -44,49 +45,57 @@ public void setAdminId(long adminId) {
44
45
45
46
public void run () {
46
47
bot .setUpdatesListener (updates -> {
47
- processAdminCommands (updates );
48
- processUpdates (updates );
48
+ final var removedPosts = processAdminCommands (updates );
49
+ processUpdates (updates , removedPosts );
49
50
return UpdatesListener .CONFIRMED_UPDATES_ALL ;
50
51
});
51
52
}
52
53
53
54
public void runOnce () {
54
55
final var updates = bot .execute (new GetUpdates ()).updates ();
55
- processAdminCommands (updates );
56
- processUpdates (updates );
56
+ final var removedPosts = processAdminCommands (updates );
57
+ processUpdates (updates , removedPosts );
57
58
}
58
59
59
- private void processAdminCommands (List <Update > updates ) {
60
+ private Set < Post > processAdminCommands (List <Update > updates ) {
60
61
final var delPattern = Pattern .compile ("/del(\\ d+)m(\\ d+)" );
61
- updates .stream ()
62
+ return updates .stream ()
62
63
.map (Update ::message )
63
64
.filter (Objects ::nonNull )
64
65
.filter (msg -> msg .chat ().id () == adminId )
65
66
.map (Message ::text )
66
67
.filter (Objects ::nonNull )
67
- .forEach (command -> {
68
+ .map (command -> {
68
69
final var m = delPattern .matcher (command );
69
70
if (m .find ()) {
70
71
final var channelId = Long .parseLong ("-100" + m .group (1 ));
71
72
final var messageId = Integer .parseInt (m .group (2 ));
72
73
bot .execute (new DeleteMessage (channelId , messageId ));
73
74
try {
74
75
indexer .deleteImage (channelId , messageId );
75
- } catch (SQLException ignored ) {}
76
+ } catch (SQLException ex ) {
77
+ System .err .println ("Cannot delete image in db" );
78
+ }
79
+ return new Post (channelId , messageId );
76
80
}
77
- });
81
+ return null ;
82
+ })
83
+ .filter (Objects ::nonNull )
84
+ .collect (Collectors .toSet ());
78
85
}
79
86
80
- private void processUpdates (List <Update > updates ) {
87
+ private void processUpdates (List <Update > updates , Set < Post > ignoredPosts ) {
81
88
final List <Message > channelPosts = getChannelPostsWithPhotos (updates );
82
89
final var similarImagesInfos = new ArrayList <SimilarImagesInfo >();
83
90
for (var post : channelPosts ) {
91
+ final var originalPost = new Post (post .chat ().id (), post .messageId ());
92
+ if (ignoredPosts .contains (originalPost )) continue ;
93
+
84
94
final PhotoSize photo = getSmallestPhoto (post .photo ());
85
95
try {
86
96
final var tgFile = bot .execute (new GetFile (photo .fileId ())).file ();
87
97
final var url = new URL (bot .getFullFilePath (tgFile ));
88
98
final BufferedImage image = ImageIO .read (url );
89
- final var originalPost = new Post (post .chat ().id (), post .messageId ());
90
99
final SimilarImagesInfo info = indexer .processImage (originalPost , image );
91
100
if (info .hasResults ()) {
92
101
similarImagesInfos .add (info );
0 commit comments