Implement a “Sone rescue mode.”
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 17 Nov 2010 12:47:39 +0000 (13:47 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 17 Nov 2010 12:47:39 +0000 (13:47 +0100)
This mode will when a local Sone is added try to download all previously
known editions of the Sone and merge all posts, replies, and likes it can
find. The SoneInserter is not started until all editions have been tried
(or the rescue mode is turned off).

src/main/java/net/pterodactylus/sone/core/Core.java

index ccf2cd3..5b1b9f6 100644 (file)
@@ -651,17 +651,31 @@ public class Core implements IdentityListener {
                        sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
                        /* TODO - load posts ’n stuff */
                        localSones.put(ownIdentity.getId(), sone);
-                       SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
+                       final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
                        soneInserters.put(sone, soneInserter);
                        setSoneStatus(sone, SoneStatus.idle);
                        loadSone(sone);
-                       soneInserter.start();
+                       if (!isSoneRescueMode()) {
+                               soneInserter.start();
+                       }
                        new Thread(new Runnable() {
 
                                @Override
                                @SuppressWarnings("synthetic-access")
                                public void run() {
-                                       soneDownloader.fetchSone(sone);
+                                       if (!isSoneRescueMode()) {
+                                               soneDownloader.fetchSone(sone);
+                                               return;
+                                       }
+                                       logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
+                                       long edition = sone.getLatestEdition();
+                                       while (!stopped && (edition >= 0) && isSoneRescueMode()) {
+                                               logger.log(Level.FINE, "Downloading edition " + edition + "…");
+                                               soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
+                                               --edition;
+                                       }
+                                       logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
+                                       soneInserter.start();
                                }
 
                        }, "Sone Downloader").start();
@@ -734,14 +748,17 @@ public class Core implements IdentityListener {
         */
        public void updateSone(Sone sone) {
                if (hasSone(sone.getId())) {
+                       boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode();
                        Sone storedSone = getSone(sone.getId());
-                       if (!(sone.getTime() > storedSone.getTime())) {
+                       if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
                                logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
                                return;
                        }
                        synchronized (posts) {
-                               for (Post post : storedSone.getPosts()) {
-                                       posts.remove(post.getId());
+                               if (!soneRescueMode) {
+                                       for (Post post : storedSone.getPosts()) {
+                                               posts.remove(post.getId());
+                                       }
                                }
                                synchronized (newPosts) {
                                        for (Post post : sone.getPosts()) {
@@ -754,8 +771,10 @@ public class Core implements IdentityListener {
                                }
                        }
                        synchronized (replies) {
-                               for (Reply reply : storedSone.getReplies()) {
-                                       replies.remove(reply.getId());
+                               if (!soneRescueMode) {
+                                       for (Reply reply : storedSone.getReplies()) {
+                                               replies.remove(reply.getId());
+                                       }
                                }
                                synchronized (newReplies) {
                                        for (Reply reply : sone.getReplies()) {
@@ -771,10 +790,25 @@ public class Core implements IdentityListener {
                                storedSone.setTime(sone.getTime());
                                storedSone.setClient(sone.getClient());
                                storedSone.setProfile(sone.getProfile());
-                               storedSone.setPosts(sone.getPosts());
-                               storedSone.setReplies(sone.getReplies());
-                               storedSone.setLikePostIds(sone.getLikedPostIds());
-                               storedSone.setLikeReplyIds(sone.getLikedReplyIds());
+                               if (soneRescueMode) {
+                                       for (Post post : sone.getPosts()) {
+                                               storedSone.addPost(post);
+                                       }
+                                       for (Reply reply : sone.getReplies()) {
+                                               storedSone.addReply(reply);
+                                       }
+                                       for (String likedPostId : sone.getLikedPostIds()) {
+                                               storedSone.addLikedPostId(likedPostId);
+                                       }
+                                       for (String likedReplyId : sone.getLikedReplyIds()) {
+                                               storedSone.addLikedReplyId(likedReplyId);
+                                       }
+                               } else {
+                                       storedSone.setPosts(sone.getPosts());
+                                       storedSone.setReplies(sone.getReplies());
+                                       storedSone.setLikePostIds(sone.getLikedPostIds());
+                                       storedSone.setLikeReplyIds(sone.getLikedReplyIds());
+                               }
                                storedSone.setLatestEdition(sone.getRequestUri().getEdition());
                        }
                }