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=b16165e67360fa4b78c671452ec168a13b0bbfad;hpb=3993bb581e277bc8b1a2b45122d0c7bbced9405c;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 b16165e..841f560 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -107,11 +107,18 @@ public class Core implements IdentityListener { private Map remoteSones = new HashMap(); /** All new Sones. */ - private Set newSones = new HashSet(); + private Set newSones = new HashSet(); /** All known Sones. */ /* synchronize access on {@link #newSones}. */ - private Set knownSones = new HashSet(); + 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(); @@ -231,6 +238,18 @@ public class Core implements IdentityListener { } /** + * Checks whether the core knows a Sone with the given ID. + * + * @param id + * The ID of the Sone + * @return {@code true} if there is a Sone with the given ID, {@code false} + * otherwise + */ + public boolean hasSone(String id) { + return isLocalSone(id) || isRemoteSone(id); + } + + /** * Returns whether the given Sone is a local Sone. * * @param sone @@ -368,8 +387,8 @@ public class Core implements IdentityListener { */ public boolean isNewSone(Sone sone) { synchronized (newSones) { - boolean isNew = !knownSones.contains(sone) && newSones.remove(sone); - knownSones.add(sone); + boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId()); + knownSones.add(sone.getId()); return isNew; } } @@ -393,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 @@ -569,7 +605,7 @@ public class Core implements IdentityListener { sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); if (newSone) { synchronized (newSones) { - newSones.add(sone); + newSones.add(sone.getId()); } } remoteSones.put(identity.getId(), sone); @@ -595,8 +631,8 @@ public class Core implements IdentityListener { * The updated Sone */ public void updateSone(Sone sone) { - if (isRemoteSone(sone)) { - Sone storedSone = getRemoteSone(sone.getId()); + if (hasSone(sone.getId())) { + Sone storedSone = getSone(sone.getId()); if (!(sone.getTime() > storedSone.getTime())) { logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone }); return; @@ -605,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) { @@ -747,18 +788,13 @@ public class Core implements IdentityListener { } /* load friends. */ - Set friends = new HashSet(); + Set friends = new HashSet(); while (true) { String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null); if (friendId == null) { break; } - Boolean friendLocal = configuration.getBooleanValue(sonePrefix + "/Friends/" + friends.size() + "/Local").getValue(null); - if (friendLocal == null) { - logger.log(Level.WARNING, "Invalid friend found, aborting load!"); - return; - } - friends.add(friendLocal ? getLocalSone(friendId) : getRemoteSone(friendId)); + friends.add(friendId); } /* if we’re still here, Sone was loaded successfully. */ @@ -773,10 +809,15 @@ public class Core implements IdentityListener { sone.setModificationCounter(soneModificationCounter); } synchronized (newSones) { - for (Sone friend : friends) { + for (String friend : friends) { knownSones.add(friend); } } + synchronized (newPosts) { + for (Post post : posts) { + knownPosts.add(post.getId()); + } + } } /** @@ -850,9 +891,8 @@ public class Core implements IdentityListener { /* save friends. */ int friendCounter = 0; - for (Sone friend : sone.getFriends()) { - configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend.getId()); - configuration.getBooleanValue(sonePrefix + "/Friends/" + friendCounter++ + "/Local").setValue(friend.getInsertUri() != null); + for (String friendId : sone.getFriends()) { + configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId); } configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null); @@ -1034,9 +1074,22 @@ public class Core implements IdentityListener { break; } synchronized (newSones) { - knownSones.add(getRemoteSone(knownSoneId)); + 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); } } + } /** @@ -1052,12 +1105,21 @@ public class Core implements IdentityListener { /* save known Sones. */ int soneCounter = 0; synchronized (newSones) { - for (Sone knownSone : knownSones) { - configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSone.getId()); + for (String knownSoneId : knownSones) { + configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId); } 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); }