X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=841f5608b5964a8a38518b94135342e84f7c0e75;hb=fbc4cd017e9453094a63f92a87951caa31d8de71;hp=f8f894864aeaed8b3b544193cff8c254d5e2b722;hpb=3495e4e58c3d5b2c920df98e6e4af559dc2566ae;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index f8f8948..841f560 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -113,6 +113,13 @@ public class Core implements IdentityListener { /* synchronize access on {@link #newSones}. */ private Set knownSones = new HashSet(); + /** All new posts. */ + private Set newPosts = new HashSet(); + + /** All known posts. */ + /* synchronize access on {@link #newPosts}. */ + private Set knownPosts = new HashSet(); + /** All posts. */ private Map posts = new HashMap(); @@ -405,6 +412,23 @@ public class Core implements IdentityListener { } /** + * Returns whether the given post ID is new. After this method returns it is + * marked a known post ID. + * + * @param postId + * The post ID + * @return {@code true} if the post is considered to be new, {@code false} + * otherwise + */ + public boolean isNewPost(String postId) { + synchronized (newPosts) { + boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId); + knownPosts.add(postId); + return isNew; + } + } + + /** * Returns the reply with the given ID. * * @param replyId @@ -617,8 +641,13 @@ public class Core implements IdentityListener { for (Post post : storedSone.getPosts()) { posts.remove(post.getId()); } - for (Post post : sone.getPosts()) { - posts.put(post.getId(), post); + synchronized (newPosts) { + for (Post post : sone.getPosts()) { + if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) { + newPosts.add(post.getId()); + } + posts.put(post.getId(), post); + } } } synchronized (replies) { @@ -784,6 +813,11 @@ public class Core implements IdentityListener { knownSones.add(friend); } } + synchronized (newPosts) { + for (Post post : posts) { + knownPosts.add(post.getId()); + } + } } /** @@ -1043,6 +1077,19 @@ public class Core implements IdentityListener { knownSones.add(knownSoneId); } } + + /* load known posts. */ + int postCounter = 0; + while (true) { + String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null); + if (knownPostId == null) { + break; + } + synchronized (newPosts) { + knownPosts.add(knownPostId); + } + } + } /** @@ -1064,6 +1111,15 @@ public class Core implements IdentityListener { configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null); } + /* save known posts. */ + int postCounter = 0; + synchronized (newPosts) { + for (String knownPostId : knownPosts) { + configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId); + } + configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null); + } + } catch (ConfigurationException ce1) { logger.log(Level.SEVERE, "Could not store configuration!", ce1); }