Set known flag correctly when finding new posts and replies.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 14e9f89..4761959 100644 (file)
@@ -77,6 +77,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        /** The logger. */
        private static final Logger logger = Logging.getLogger(Core.class);
 
+       /** The start time. */
+       private final long startupTime = System.currentTimeMillis();
+
        /** The options. */
        private final Options options = new Options();
 
@@ -228,6 +231,15 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        //
 
        /**
+        * Returns the time Sone was started.
+        *
+        * @return The startup time (in milliseconds since Jan 1, 1970 UTC)
+        */
+       public long getStartupTime() {
+               return startupTime;
+       }
+
+       /**
         * Sets the configuration to use. This will automatically save the current
         * configuration to the given configuration.
         *
@@ -1148,8 +1160,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                                if (!storedPosts.contains(post)) {
                                                        if (post.getTime() < getSoneFollowingTime(sone)) {
                                                                knownPosts.add(post.getId());
+                                                               post.setKnown(true);
                                                        } else if (!knownPosts.contains(post.getId())) {
-                                                               sone.setKnown(false);
                                                                coreListenerManager.fireNewPostFound(post);
                                                        }
                                                }
@@ -1173,8 +1185,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                                if (!storedReplies.contains(reply)) {
                                                        if (reply.getTime() < getSoneFollowingTime(sone)) {
                                                                knownReplies.add(reply.getId());
+                                                               reply.setKnown(true);
                                                        } else if (!knownReplies.contains(reply.getId())) {
-                                                               reply.setKnown(false);
                                                                coreListenerManager.fireNewReplyFound(reply);
                                                        }
                                                }
@@ -1966,11 +1978,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        @Override
        public void serviceStop() {
                synchronized (localSones) {
-                       for (SoneInserter soneInserter : soneInserters.values()) {
-                               soneInserter.removeSoneInsertListener(this);
-                               soneInserter.stop();
+                       for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
+                               soneInserter.getValue().removeSoneInsertListener(this);
+                               soneInserter.getValue().stop();
+                               saveSone(soneInserter.getKey());
                        }
                }
+               saveConfiguration();
                webOfTrustUpdater.stop();
                updateChecker.stop();
                updateChecker.removeUpdateListener(this);
@@ -2348,12 +2362,12 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The URI to derive the Sone URI from
         * @return The derived URI
         */
-       private FreenetURI getSoneUri(String uriString) {
+       private static FreenetURI getSoneUri(String uriString) {
                try {
                        FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
                        return uri;
                } catch (MalformedURLException mue1) {
-                       logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uriString, mue1));
+                       logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uriString), mue1);
                        return null;
                }
        }