2121import java .util .Comparator ;
2222import java .util .List ;
2323import java .util .Objects ;
24+ import java .util .Set ;
2425import java .util .regex .Pattern ;
2526import java .util .stream .Collectors ;
2627import javax .imageio .ImageIO ;
@@ -44,49 +45,57 @@ public void setAdminId(long adminId) {
4445
4546 public void run () {
4647 bot .setUpdatesListener (updates -> {
47- processAdminCommands (updates );
48- processUpdates (updates );
48+ final var removedPosts = processAdminCommands (updates );
49+ processUpdates (updates , removedPosts );
4950 return UpdatesListener .CONFIRMED_UPDATES_ALL ;
5051 });
5152 }
5253
5354 public void runOnce () {
5455 final var updates = bot .execute (new GetUpdates ()).updates ();
55- processAdminCommands (updates );
56- processUpdates (updates );
56+ final var removedPosts = processAdminCommands (updates );
57+ processUpdates (updates , removedPosts );
5758 }
5859
59- private void processAdminCommands (List <Update > updates ) {
60+ private Set < Post > processAdminCommands (List <Update > updates ) {
6061 final var delPattern = Pattern .compile ("/del(\\ d+)m(\\ d+)" );
61- updates .stream ()
62+ return updates .stream ()
6263 .map (Update ::message )
6364 .filter (Objects ::nonNull )
6465 .filter (msg -> msg .chat ().id () == adminId )
6566 .map (Message ::text )
6667 .filter (Objects ::nonNull )
67- .forEach (command -> {
68+ .map (command -> {
6869 final var m = delPattern .matcher (command );
6970 if (m .find ()) {
7071 final var channelId = Long .parseLong ("-100" + m .group (1 ));
7172 final var messageId = Integer .parseInt (m .group (2 ));
7273 bot .execute (new DeleteMessage (channelId , messageId ));
7374 try {
7475 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 );
7680 }
77- });
81+ return null ;
82+ })
83+ .filter (Objects ::nonNull )
84+ .collect (Collectors .toSet ());
7885 }
7986
80- private void processUpdates (List <Update > updates ) {
87+ private void processUpdates (List <Update > updates , Set < Post > ignoredPosts ) {
8188 final List <Message > channelPosts = getChannelPostsWithPhotos (updates );
8289 final var similarImagesInfos = new ArrayList <SimilarImagesInfo >();
8390 for (var post : channelPosts ) {
91+ final var originalPost = new Post (post .chat ().id (), post .messageId ());
92+ if (ignoredPosts .contains (originalPost )) continue ;
93+
8494 final PhotoSize photo = getSmallestPhoto (post .photo ());
8595 try {
8696 final var tgFile = bot .execute (new GetFile (photo .fileId ())).file ();
8797 final var url = new URL (bot .getFullFilePath (tgFile ));
8898 final BufferedImage image = ImageIO .read (url );
89- final var originalPost = new Post (post .chat ().id (), post .messageId ());
9099 final SimilarImagesInfo info = indexer .processImage (originalPost , image );
91100 if (info .hasResults ()) {
92101 similarImagesInfos .add (info );
0 commit comments