Add second set of known Sones and persist that across restarts.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 132e4a6..93891ab 100644 (file)
@@ -109,6 +109,10 @@ public class Core implements IdentityListener {
        /** All new Sones. */
        private Set<Sone> newSones = new HashSet<Sone>();
 
+       /** All known Sones. */
+       /* synchronize access on {@link #newSones}. */
+       private Set<Sone> knownSones = new HashSet<Sone>();
+
        /** All posts. */
        private Map<String, Post> posts = new HashMap<String, Post>();
 
@@ -323,6 +327,23 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Returns whether the given Sone is a new Sone. After this check, the Sone
+        * is marked as known, i.e. a second call with the same parameters will
+        * always yield {@code false}.
+        *
+        * @param sone
+        *            The sone to check for
+        * @return {@code true} if the given Sone is new, false otherwise
+        */
+       public boolean isNewSone(Sone sone) {
+               synchronized (newSones) {
+                       boolean isNew = !knownSones.contains(sone) && newSones.remove(sone);
+                       knownSones.add(sone);
+                       return isNew;
+               }
+       }
+
+       /**
         * Returns the post with the given ID.
         *
         * @param postId
@@ -511,8 +532,8 @@ public class Core implements IdentityListener {
                        return null;
                }
                synchronized (remoteSones) {
-                       boolean newSone = !isRemoteSone(identity.getId());
                        final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
+                       boolean newSone = sone.getRequestUri() == null;
                        sone.setRequestUri(getSoneUri(identity.getRequestUri()));
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
                        if (newSone) {
@@ -720,6 +741,11 @@ public class Core implements IdentityListener {
                        sone.setFriends(friends);
                        sone.setModificationCounter(soneModificationCounter);
                }
+               synchronized (newSones) {
+                       for (Sone friend : friends) {
+                               knownSones.add(friend);
+                       }
+               }
        }
 
        /**
@@ -969,6 +995,17 @@ public class Core implements IdentityListener {
 
                options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
 
+               /* load known Sones. */
+               int soneCounter = 0;
+               while (true) {
+                       String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
+                       if (knownSoneId == null) {
+                               break;
+                       }
+                       synchronized (newSones) {
+                               knownSones.add(getRemoteSone(knownSoneId));
+                       }
+               }
        }
 
        /**
@@ -980,6 +1017,16 @@ public class Core implements IdentityListener {
                        configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
                        configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
                        configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
+
+                       /* save known Sones. */
+                       int soneCounter = 0;
+                       synchronized (newSones) {
+                               for (Sone knownSone : knownSones) {
+                                       configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSone.getId());
+                               }
+                               configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
+                       }
+
                } catch (ConfigurationException ce1) {
                        logger.log(Level.SEVERE, "Could not store configuration!", ce1);
                }