Sones may be known before but must not have a request URI to be new.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 52b885e..15a7b0e 100644 (file)
@@ -106,6 +106,9 @@ public class Core implements IdentityListener {
        /* synchronize access on this on itself. */
        private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
 
+       /** All new Sones. */
+       private Set<Sone> newSones = new HashSet<Sone>();
+
        /** All posts. */
        private Map<String, Post> posts = new HashMap<String, Post>();
 
@@ -306,6 +309,35 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Returns whether the Sone with the given ID is a remote Sone.
+        *
+        * @param id
+        *            The ID of the Sone to check
+        * @return {@code true} if the Sone with the given ID is a remote Sone,
+        *         {@code false} otherwise
+        */
+       public boolean isRemoteSone(String id) {
+               synchronized (remoteSones) {
+                       return remoteSones.containsKey(id);
+               }
+       }
+
+       /**
+        * 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) {
+                       return newSones.remove(sone);
+               }
+       }
+
+       /**
         * Returns the post with the given ID.
         *
         * @param postId
@@ -495,10 +527,17 @@ public class Core implements IdentityListener {
                }
                synchronized (remoteSones) {
                        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) {
+                               synchronized (newSones) {
+                                       newSones.add(sone);
+                               }
+                       }
                        remoteSones.put(identity.getId(), sone);
                        soneDownloader.addSone(sone);
+                       setSoneStatus(sone, SoneStatus.unknown);
                        new Thread(new Runnable() {
 
                                @Override
@@ -508,7 +547,6 @@ public class Core implements IdentityListener {
                                }
 
                        }, "Sone Downloader").start();
-                       setSoneStatus(sone, SoneStatus.idle);
                        return sone;
                }
        }