From 45a204f29290fafd305288ed0bccaeec5c0fe9c6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 29 May 2014 22:20:49 +0200 Subject: [PATCH] Only send events after the new posts and replies are stored. --- .../java/net/pterodactylus/sone/core/Core.java | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index ecb5857..500995d 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -994,10 +994,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return; } /* find removed posts. */ + Collection removedPosts = new ArrayList(); + Collection newPosts = new ArrayList(); Collection existingPosts = database.getPosts(sone.getId()); for (Post oldPost : existingPosts) { if (!sone.getPosts().contains(oldPost)) { - eventBus.post(new PostRemovedEvent(oldPost)); + removedPosts.add(oldPost); } } /* find new posts. */ @@ -1008,15 +1010,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (newPost.getTime() < getSoneFollowingTime(sone)) { newPost.setKnown(true); } else if (!newPost.isKnown()) { - eventBus.post(new NewPostFoundEvent(newPost)); + newPosts.add(newPost); } } /* store posts. */ database.storePosts(sone, sone.getPosts()); + Collection newPostReplies = new ArrayList(); + Collection removedPostReplies = new ArrayList(); if (!soneRescueMode) { for (PostReply reply : storedSone.get().getReplies()) { if (!sone.getReplies().contains(reply)) { - eventBus.post(new PostReplyRemovedEvent(reply)); + removedPostReplies.add(reply); } } } @@ -1028,7 +1032,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (reply.getTime() < getSoneFollowingTime(sone)) { reply.setKnown(true); } else if (!reply.isKnown()) { - eventBus.post(new NewPostReplyFoundEvent(reply)); + newPostReplies.add(reply); } } database.storePostReplies(sone, sone.getReplies()); @@ -1038,6 +1042,18 @@ 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 : sone.getRootAlbum().getAlbums()) { database.storeAlbum(album); for (Image image : album.getImages()) { -- 2.7.4