Don’t set the request URI anymore, either.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloaderImpl.java
index ebdbecb..d4ed927 100644 (file)
 
 package net.pterodactylus.sone.core;
 
+import static java.lang.String.format;
+import static java.lang.System.currentTimeMillis;
+import static java.util.concurrent.TimeUnit.DAYS;
+
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -38,6 +45,7 @@ import net.pterodactylus.sone.data.Sone.SoneStatus;
 import net.pterodactylus.sone.data.SoneImpl;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.SoneBuilder;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.number.Numbers;
@@ -46,9 +54,15 @@ import net.pterodactylus.util.xml.SimpleXML;
 import net.pterodactylus.util.xml.XML;
 
 import freenet.client.FetchResult;
+import freenet.client.async.ClientContext;
+import freenet.client.async.USKCallback;
 import freenet.keys.FreenetURI;
+import freenet.keys.USK;
+import freenet.node.RequestStarter;
 import freenet.support.api.Bucket;
 
+import com.db4o.ObjectContainer;
+
 import com.google.common.annotations.VisibleForTesting;
 import org.w3c.dom.Document;
 
@@ -99,22 +113,53 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
         *              The Sone to add
         */
        @Override
-       public void addSone(Sone sone) {
+       public void addSone(final Sone sone) {
                if (!sones.add(sone)) {
                        freenetInterface.unregisterUsk(sone);
                }
-               freenetInterface.registerUsk(sone, this);
+               final USKCallback uskCallback = new USKCallback() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void onFoundEdition(long edition, USK key,
+                                       ObjectContainer objectContainer,
+                                       ClientContext clientContext, boolean metadata,
+                                       short codec, byte[] data, boolean newKnownGood,
+                                       boolean newSlotToo) {
+                               logger.log(Level.FINE, format(
+                                               "Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.",
+                                               sone, key, newKnownGood, newSlotToo));
+                               if (edition > sone.getLatestEdition()) {
+                                       sone.setLatestEdition(edition);
+                                       new Thread(fetchSoneAction(sone),
+                                                       "Sone Downloader").start();
+                               }
+                       }
+
+                       @Override
+                       public short getPollingPriorityProgress() {
+                               return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
+                       }
+
+                       @Override
+                       public short getPollingPriorityNormal() {
+                               return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
+                       }
+               };
+               if (soneHasBeenActiveRecently(sone)) {
+                       freenetInterface.registerActiveUsk(sone.getRequestUri(),
+                                       uskCallback);
+               } else {
+                       freenetInterface.registerPassiveUsk(sone.getRequestUri(),
+                                       uskCallback);
+               }
        }
 
-       /**
-        * Fetches the updated Sone. This method is a callback method for
-        * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}.
-        *
-        * @param sone
-        *              The Sone to fetch
-        */
-       @Override
-       public void fetchSone(Sone sone) {
+       private boolean soneHasBeenActiveRecently(Sone sone) {
+               return (currentTimeMillis() - sone.getTime()) < DAYS.toMillis(7);
+       }
+
+       private void fetchSone(Sone sone) {
                fetchSone(sone, sone.getRequestUri().sskForUSK());
        }
 
@@ -191,11 +236,6 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
                        Sone parsedSone = parseSone(originalSone, soneInputStream);
                        if (parsedSone != null) {
                                parsedSone.setLatestEdition(requestUri.getEdition());
-                               if (requestUri.getKeyType().equals("USK")) {
-                                       parsedSone.setRequestUri(requestUri.setMetaString(new String[0]));
-                               } else {
-                                       parsedSone.setRequestUri(requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]));
-                               }
                        }
                        return parsedSone;
                } catch (Exception e1) {
@@ -234,7 +274,11 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
                        return null;
                }
 
-               Sone sone = new SoneImpl(originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity());
+               SoneBuilder soneBuilder = core.soneBuilder().from(originalSone.getIdentity());
+               if (originalSone.isLocal()) {
+                       soneBuilder = soneBuilder.local();
+               }
+               Sone sone = soneBuilder.build();
 
                SimpleXML soneXml;
                try {
@@ -291,21 +335,6 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
                        sone.setClient(new Client(clientName, clientVersion));
                }
 
-               String soneRequestUri = soneXml.getValue("request-uri", null);
-               if (soneRequestUri != null) {
-                       try {
-                               sone.setRequestUri(new FreenetURI(soneRequestUri));
-                       } catch (MalformedURLException mue1) {
-                               /* TODO - mark Sone as bad. */
-                               logger.log(Level.WARNING, String.format("Downloaded Sone %s has invalid request URI: %s", sone, soneRequestUri), mue1);
-                               return null;
-                       }
-               }
-
-               if (originalSone.getInsertUri() != null) {
-                       sone.setInsertUri(originalSone.getInsertUri());
-               }
-
                SimpleXML profileXml = soneXml.getNode("profile");
                if (profileXml == null) {
                        /* TODO - mark Sone as bad. */
@@ -435,6 +464,7 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
 
                /* parse albums. */
                SimpleXML albumsXml = soneXml.getNode("albums");
+               Map<String, Image> allImages = new HashMap<String, Image>();
                List<Album> topLevelAlbums = new ArrayList<Album>();
                if (albumsXml != null) {
                        for (SimpleXML albumXml : albumsXml.getNodes("album")) {
@@ -489,10 +519,11 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
                                                        logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
                                                        return null;
                                                }
-                                               Image image = core.getImage(imageId).modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update();
+                                               Image image = core.imageBuilder().withId(imageId).build().modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update();
                                                image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
                                                image = image.modify().setWidth(imageWidth).setHeight(imageHeight).update();
                                                album.addImage(image);
+                                               allImages.put(imageId, image);
                                        }
                                }
                                album.modify().setAlbumImage(albumImageId).update();
@@ -501,7 +532,7 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
 
                /* process avatar. */
                if (avatarId != null) {
-                       profile.setAvatar(core.getImage(avatarId, false));
+                       profile.setAvatar(allImages.get(avatarId));
                }
 
                /* okay, apparently everything was parsed correctly. Now import. */