Remove methods to manipulate a Sone’s request and insert URI.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 3a02f15..7ea8134 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;
@@ -24,6 +25,7 @@ import static com.google.common.base.Predicates.not;
 import static com.google.common.collect.FluentIterable.from;
 import static net.pterodactylus.sone.data.Identified.GET_ID;
 import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
+import static net.pterodactylus.sone.data.Sone.TO_FREENET_URI;
 
 import java.net.MalformedURLException;
 import java.util.Collection;
@@ -74,6 +76,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;
@@ -556,12 +559,7 @@ public class Core extends AbstractService implements SoneProvider {
                logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
                synchronized (sones) {
                        final Sone sone;
-                       try {
-                               sone = database.newSoneBuilder().by(ownIdentity).local().build().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;
-                       }
+                       sone = database.newSoneBuilder().by(ownIdentity.getId()).local().build(Optional.<SoneCreated>absent());
                        sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
                        sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
                        sone.setKnown(true);
@@ -614,13 +612,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;
-                       sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
+                       boolean newSone = !existingSone.isPresent();
+                       final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).build(Optional.<SoneCreated>absent()) : existingSone.get();
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
                        if (newSone) {
                                synchronized (knownSones) {
@@ -642,7 +639,7 @@ public class Core extends AbstractService implements SoneProvider {
                                @Override
                                @SuppressWarnings("synthetic-access")
                                public void run() {
-                                       soneDownloader.fetchSone(sone, sone.getRequestUri());
+                                       soneDownloader.fetchSone(sone, TO_FREENET_URI.apply(sone));
                                }
 
                        });
@@ -1728,7 +1725,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());