X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserter.java;h=35910ebebcef0305b331b779ad4481829b9de910;hp=4bb4868074a7cdecdb39bc803cade9b5abfc1334;hb=e675a2814d586e899ff070f8045c59f295304fc3;hpb=92f9a141ed1d027d68a49029fc7cc5e249ad98dd diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 4bb4868..35910eb 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -21,11 +21,9 @@ import java.io.InputStreamReader; import java.io.StringWriter; import java.nio.charset.Charset; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; -import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -34,8 +32,6 @@ import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.StringBucket; -import net.pterodactylus.util.filter.Filter; -import net.pterodactylus.util.filter.Filters; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; @@ -57,6 +53,9 @@ public class SoneInserter extends AbstractService { /** The logger. */ private static final Logger logger = Logging.getLogger(SoneInserter.class); + /** The insertion delay (in seconds). */ + private static volatile int insertionDelay = 60; + /** The template factory used to create the templates. */ private static final DefaultTemplateFactory templateFactory = new DefaultTemplateFactory(); @@ -95,6 +94,21 @@ public class SoneInserter extends AbstractService { } // + // ACCESSORS + // + + /** + * Changes the insertion delay, i.e. the time the Sone inserter waits after + * it has noticed a Sone modification before it starts the insert. + * + * @param insertionDelay + * The insertion delay (in seconds) + */ + public static void setInsertionDelay(int insertionDelay) { + SoneInserter.insertionDelay = insertionDelay; + } + + // // SERVICE METHODS // @@ -115,10 +129,10 @@ public class SoneInserter extends AbstractService { modificationCounter = sone.getModificationCounter(); lastModificationTime = System.currentTimeMillis(); sone.setTime(lastModificationTime); - logger.log(Level.FINE, "Sone %s has been modified, waiting 60 seconds before inserting.", new Object[] { sone.getName() }); - if ((System.currentTimeMillis() - lastModificationTime) > (60 * 1000)) { - insertInformation = new InsertInformation(sone); - } + logger.log(Level.FINE, "Sone %s has been modified, waiting %d seconds before inserting.", new Object[] { sone.getName(), insertionDelay }); + } + if ((lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) { + insertInformation = new InsertInformation(sone); } } @@ -128,8 +142,8 @@ public class SoneInserter extends AbstractService { boolean success = false; try { core.setSoneStatus(sone, SoneStatus.inserting); - FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setDocName("Sone-" + sone.getName()).setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html"); - sone.updateUris(finalUri); + FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html"); + sone.setLatestEdition(finalUri.getEdition()); success = true; logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri }); } catch (SoneException se1) { @@ -147,7 +161,9 @@ public class SoneInserter extends AbstractService { if (sone.getModificationCounter() == modificationCounter) { logger.log(Level.FINE, "Sone “%s” was not modified further, resetting counter…", new Object[] { sone }); sone.setModificationCounter(0); + core.saveSone(sone); modificationCounter = 0; + lastModificationTime = 0; } } } @@ -182,7 +198,6 @@ public class SoneInserter extends AbstractService { soneProperties.put("profile", sone.getProfile()); soneProperties.put("posts", new ArrayList(sone.getPosts())); soneProperties.put("replies", new HashSet(sone.getReplies())); - soneProperties.put("blockedSoneIds", new HashSet(sone.getBlockedSoneIds())); soneProperties.put("likedPostIds", new HashSet(sone.getLikedPostIds())); soneProperties.put("likeReplyIds", new HashSet(sone.getLikedReplyIds())); } @@ -248,21 +263,8 @@ public class SoneInserter extends AbstractService { } finally { Closer.close(templateInputStreamReader); } - @SuppressWarnings("unchecked") - final Set blockedSoneIds = (Set) soneProperties.get("blockedSoneIds"); - Collection knownSones = Filters.filteredCollection(core.getKnownSones(), new Filter() { - - /** - * {@inheritDoc} - */ - @Override - public boolean filterObject(Sone object) { - return !blockedSoneIds.contains(object.getId()) && !object.getId().equals(soneProperties.get("id")); - } - }); template.set("currentSone", soneProperties); - template.set("knownSones", knownSones); StringWriter writer = new StringWriter(); StringBucket bucket = null; try {