Use Sone change detector in core to send notifications.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 19 Sep 2014 05:47:41 +0000 (07:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 19 Sep 2014 05:56:21 +0000 (07:56 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java

index 7d38ed9..5fa330b 100644 (file)
@@ -49,6 +49,8 @@ import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFou
 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
 import net.pterodactylus.sone.core.Options.DefaultOption;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor;
 import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay;
 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
@@ -946,7 +948,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            {@code true} if the stored Sone should be updated regardless
         *            of the age of the given Sone
         */
-       public void updateSone(Sone sone, boolean soneRescueMode) {
+       public void updateSone(final Sone sone, boolean soneRescueMode) {
                Optional<Sone> storedSone = getSone(sone.getId());
                if (storedSone.isPresent()) {
                        if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) {
@@ -954,47 +956,42 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                return;
                        }
                        /* find removed posts. */
-                       Collection<Post> removedPosts = new ArrayList<Post>();
-                       Collection<Post> newPosts = new ArrayList<Post>();
-                       Collection<Post> existingPosts = database.getPosts(sone.getId());
-                       for (Post oldPost : existingPosts) {
-                               if (!sone.getPosts().contains(oldPost)) {
-                                       removedPosts.add(oldPost);
-                               }
-                       }
-                       /* find new posts. */
-                       for (Post newPost : sone.getPosts()) {
-                               if (existingPosts.contains(newPost)) {
-                                       continue;
+                       SoneChangeDetector soneChangeDetector = new SoneChangeDetector(storedSone.get());
+                       soneChangeDetector.onNewPosts(new PostProcessor() {
+                               @Override
+                               public void processPost(Post post) {
+                                       if (post.getTime() < getSoneFollowingTime(sone)) {
+                                               post.setKnown(true);
+                                       } else if (!post.isKnown()) {
+                                               eventBus.post(new NewPostFoundEvent(post));
+                                       }
                                }
-                               if (newPost.getTime() < getSoneFollowingTime(sone)) {
-                                       newPost.setKnown(true);
-                               } else if (!newPost.isKnown()) {
-                                       newPosts.add(newPost);
+                       });
+                       soneChangeDetector.onRemovedPosts(new PostProcessor() {
+                               @Override
+                               public void processPost(Post post) {
+                                       eventBus.post(new PostRemovedEvent(post));
                                }
-                       }
-                       /* store posts. */
-                       database.storePosts(sone, sone.getPosts());
-                       Collection<PostReply> newPostReplies = new ArrayList<PostReply>();
-                       Collection<PostReply> removedPostReplies = new ArrayList<PostReply>();
-                       if (!soneRescueMode) {
-                               for (PostReply reply : storedSone.get().getReplies()) {
-                                       if (!sone.getReplies().contains(reply)) {
-                                               removedPostReplies.add(reply);
+                       });
+                       soneChangeDetector.onNewPostReplies(new PostReplyProcessor() {
+                               @Override
+                               public void processPostReply(PostReply postReply) {
+                                       if (postReply.getTime() < getSoneFollowingTime(sone)) {
+                                               postReply.setKnown(true);
+                                       } else if (!postReply.isKnown()) {
+                                               eventBus.post(new NewPostReplyFoundEvent(postReply));
                                        }
                                }
-                       }
-                       Set<PostReply> storedReplies = storedSone.get().getReplies();
-                       for (PostReply reply : sone.getReplies()) {
-                               if (storedReplies.contains(reply)) {
-                                       continue;
-                               }
-                               if (reply.getTime() < getSoneFollowingTime(sone)) {
-                                       reply.setKnown(true);
-                               } else if (!reply.isKnown()) {
-                                       newPostReplies.add(reply);
+                       });
+                       soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() {
+                               @Override
+                               public void processPostReply(PostReply postReply) {
+                                       eventBus.post(new PostReplyRemovedEvent(postReply));
                                }
-                       }
+                       });
+                       soneChangeDetector.detectChanges(sone);
+                       /* store posts. */
+                       database.storePosts(sone, sone.getPosts());
                        database.storePostReplies(sone, sone.getReplies());
                        for (Album album : storedSone.get().getRootAlbum().getAlbums()) {
                                database.removeAlbum(album);
@@ -1002,18 +999,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                        database.removeImage(image);
                                }
                        }
-                       for (Post removedPost : removedPosts) {
-                               eventBus.post(new PostRemovedEvent(removedPost));
-                       }
-                       for (Post newPost : newPosts) {
-                               eventBus.post(new NewPostFoundEvent(newPost));
-                       }
-                       for (PostReply removedPostReply : removedPostReplies) {
-                               eventBus.post(new PostReplyRemovedEvent(removedPostReply));
-                       }
-                       for (PostReply newPostReply : newPostReplies) {
-                               eventBus.post(new NewPostReplyFoundEvent(newPostReply));
-                       }
                        for (Album album : toAllAlbums.apply(sone)) {
                                database.storeAlbum(album);
                                for (Image image : album.getImages()) {