Restore the actual purpose of the code.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index fade421..612398d 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.core;
 
+import static com.google.common.base.Optional.absent;
 import static com.google.common.base.Optional.of;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -62,7 +63,7 @@ import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Profile.Field;
-import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.Reply.Modifier.ReplyUpdated;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
@@ -74,6 +75,7 @@ import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
 import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
+import net.pterodactylus.sone.database.SoneBuilder.SoneCreated;
 import net.pterodactylus.sone.database.SoneProvider;
 import net.pterodactylus.sone.fcp.FcpInterface;
 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
@@ -407,14 +409,6 @@ public class Core extends AbstractService implements SoneProvider {
                }
        }
 
-       public Optional<PostReply> getPostReply(String replyId) {
-               return database.getPostReply(replyId);
-       }
-
-       public List<PostReply> getReplies(final String postId) {
-               return database.getReplies(postId);
-       }
-
        /**
         * Returns all Sones that have liked the given post.
         *
@@ -565,7 +559,7 @@ public class Core extends AbstractService implements SoneProvider {
                synchronized (sones) {
                        final Sone sone;
                        try {
-                               sone = database.newSoneBuilder().by(ownIdentity).local().build().setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
+                               sone = database.newSoneBuilder().by(ownIdentity.getId()).local().build(Optional.<SoneCreated>absent()).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
                        } catch (MalformedURLException mue1) {
                                logger.log(Level.SEVERE, String.format("Could not convert the Identity’s URIs to Freenet URIs: %s, %s", ownIdentity.getInsertUri(), ownIdentity.getRequestUri()), mue1);
                                return null;
@@ -622,12 +616,12 @@ public class Core extends AbstractService implements SoneProvider {
                        return null;
                }
                synchronized (sones) {
-                       final Sone sone = database.newSoneBuilder().by(identity).build();
-                       if (sone.isLocal()) {
-                               return sone;
+                       Optional<Sone> existingSone = database.getSone(identity.getId());
+                       if (existingSone.isPresent() && existingSone.get().isLocal()) {
+                               return existingSone.get();
                        }
-                       sone.setIdentity(identity);
-                       boolean newSone = sone.getRequestUri() == null;
+                       boolean newSone = !existingSone.isPresent();
+                       final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).build(Optional.<SoneCreated>absent()) : existingSone.get();
                        sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
                        if (newSone) {
@@ -685,7 +679,7 @@ public class Core extends AbstractService implements SoneProvider {
                                }
                                for (PostReply reply : followedSone.get().getReplies()) {
                                        if (reply.getTime() < now) {
-                                               markReplyKnown(reply);
+                                               reply.modify().setKnown().update(Optional.<ReplyUpdated<PostReply>>absent());
                                        }
                                }
                        }
@@ -847,7 +841,7 @@ public class Core extends AbstractService implements SoneProvider {
                                        continue;
                                }
                                if (reply.getTime() < getSoneFollowingTime(sone)) {
-                                       reply.setKnown(true);
+                                       reply.modify().setKnown().update(Optional.<ReplyUpdated<PostReply>>absent());
                                } else if (!reply.isKnown()) {
                                        eventBus.post(new NewPostReplyFoundEvent(reply));
                                }
@@ -960,12 +954,14 @@ public class Core extends AbstractService implements SoneProvider {
 
                /* load profile. */
                Profile profile = new Profile(sone);
-               profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
-               profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
-               profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
-               profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
-               profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
-               profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
+               String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
+               String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
+               String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
+               profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
+               Integer birthDay = configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null);
+               Integer birthMonth = configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null);
+               Integer birthYear = configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null);
+               profile.modify().setBirthYear(birthYear).setBirthMonth(birthMonth).setBirthDay(birthDay).update();
 
                /* load profile fields. */
                while (true) {
@@ -1142,7 +1138,7 @@ public class Core extends AbstractService implements SoneProvider {
                }
                database.storePostReplies(sone, replies);
                for (PostReply reply : replies) {
-                       reply.setKnown(true);
+                       reply.modify().setKnown().update(Optional.<ReplyUpdated<PostReply>>absent());
                }
 
                logger.info(String.format("Sone loaded successfully: %s", sone));
@@ -1176,8 +1172,8 @@ public class Core extends AbstractService implements SoneProvider {
                post.setKnown(true);
                eventBus.post(new MarkPostKnownEvent(post));
                touchConfiguration();
-               for (PostReply reply : getReplies(post.getId())) {
-                       markReplyKnown(reply);
+               for (PostReply reply : post.getReplies()) {
+                       reply.modify().setKnown().update(postReplyUpdated());
                }
        }
 
@@ -1227,29 +1223,12 @@ public class Core extends AbstractService implements SoneProvider {
                        logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
                        return;
                }
+               postReplyUpdated().get().replyUpdated(reply);
                database.removePostReply(reply);
-               markReplyKnown(reply);
-               sone.removeReply(reply);
                touchConfiguration();
        }
 
        /**
-        * Marks the given reply as known, if it is currently not a known reply
-        * (according to {@link Reply#isKnown()}).
-        *
-        * @param reply
-        *              The reply to mark as known
-        */
-       public void markReplyKnown(PostReply reply) {
-               boolean previouslyKnown = reply.isKnown();
-               reply.setKnown(true);
-               eventBus.post(new MarkPostReplyKnownEvent(reply));
-               if (!previouslyKnown) {
-                       touchConfiguration();
-               }
-       }
-
-       /**
         * Creates a new image.
         *
         * @param sone
@@ -1751,7 +1730,6 @@ public class Core extends AbstractService implements SoneProvider {
                        @SuppressWarnings("synthetic-access")
                        public void run() {
                                Optional<Sone> sone = getRemoteSone(identity.getId());
-                               sone.get().setIdentity(identity);
                                sone.get().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.get().getLatestEdition()));
                                soneDownloader.addSone(sone.get());
                                soneDownloader.fetchSone(sone.get());
@@ -1852,7 +1830,7 @@ public class Core extends AbstractService implements SoneProvider {
                                                 */
                                                @Override
                                                public void run() {
-                                                       markReplyKnown(postReply);
+                                                       postReplyUpdated().get().replyUpdated(postReply);
                                                }
                                        }, 10, TimeUnit.SECONDS);
                                }
@@ -1860,6 +1838,15 @@ public class Core extends AbstractService implements SoneProvider {
                });
        }
 
+       public Optional<ReplyUpdated<PostReply>> postReplyUpdated() {
+               return Optional.<ReplyUpdated<PostReply>>of(new ReplyUpdated<PostReply>() {
+                       @Override
+                       public void replyUpdated(PostReply reply) {
+                               eventBus.post(new MarkPostReplyKnownEvent(reply));
+                       }
+               });
+       }
+
        public Optional<ImageCreated> imageCreated() {
                return Optional.<ImageCreated>of(new ImageCreated() {
                        @Override