X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=12294480019d16fb3a87f1b1f41863f5927d0b8b;hb=5bcdc1b924c598b1e59551d44e2302ef29258dad;hp=4ee0258818ce3fc2c98bef39dc38b87849ed4f25;hpb=b343a185e8b89ea5973bf57ec74217652834f5ea;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 4ee0258..1229448 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -39,8 +39,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.core.Options.DefaultOption; -import net.pterodactylus.sone.core.Options.Option; -import net.pterodactylus.sone.core.Options.OptionWatcher; import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay; import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent; import net.pterodactylus.sone.core.event.MarkPostKnownEvent; @@ -75,8 +73,6 @@ import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.database.PostReplyProvider; import net.pterodactylus.sone.database.SoneProvider; import net.pterodactylus.sone.fcp.FcpInterface; -import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; -import net.pterodactylus.sone.fcp.FcpInterface.SetActive; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.IdentityManager; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -94,6 +90,7 @@ import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.thread.NamedThreadFactory; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; import com.google.common.base.Predicates; import com.google.common.collect.FluentIterable; @@ -104,6 +101,7 @@ import com.google.common.collect.Multimaps; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import com.google.inject.Inject; +import com.google.inject.Singleton; import freenet.keys.FreenetURI; @@ -112,6 +110,7 @@ import freenet.keys.FreenetURI; * * @author David ‘Bombe’ Roden */ +@Singleton public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider { /** The logger. */ @@ -222,8 +221,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, this.configuration = configuration; this.freenetInterface = freenetInterface; this.identityManager = identityManager; - this.soneDownloader = new SoneDownloader(this, freenetInterface); - this.imageInserter = new ImageInserter(freenetInterface); + this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface); + this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier()); this.updateChecker = new UpdateChecker(eventBus, freenetInterface); this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; @@ -755,12 +754,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return null; } Sone sone = addLocalSone(ownIdentity); - sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption(false)); - sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption(false)); - sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption(true)); - sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption(true)); - sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption(true)); - sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption(ShowCustomAvatars.NEVER)); followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"); touchConfiguration(); @@ -796,22 +789,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (newSone) { eventBus.post(new NewSoneFoundEvent(sone)); for (Sone localSone : getLocalSones()) { - if (localSone.getOptions().getBooleanOption("AutoFollow").get()) { + if (localSone.getOptions().isAutoFollow()) { followSone(localSone, sone.getId()); } } } } soneDownloader.addSone(sone); - soneDownloaders.execute(new Runnable() { - - @Override - @SuppressWarnings("synthetic-access") - public void run() { - soneDownloader.fetchSone(sone, sone.getRequestUri()); - } - - }); + soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone)); return sone; } } @@ -1115,14 +1100,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } logger.info(String.format("Loading local Sone: %s", sone)); - /* initialize options. */ - sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption(false)); - sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption(false)); - sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption(true)); - sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption(true)); - sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption(true)); - sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption(ShowCustomAvatars.NEVER)); - /* load Sone. */ String sonePrefix = "Sone/" + sone.getId(); Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null); @@ -1290,12 +1267,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /* load options. */ - sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null)); - sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null)); - sone.getOptions().getBooleanOption("ShowNotification/NewSones").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null)); - sone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null)); - sone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null)); - sone.getOptions(). getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name()))); + sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null)); + sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null)); + sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null)); + sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null)); + sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null)); + sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name()))); /* if we’re still here, Sone was loaded successfully. */ synchronized (sone) { @@ -1408,16 +1385,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, eventBus.post(new NewPostFoundEvent(post)); sone.addPost(post); touchConfiguration(); - localElementTicker.schedule(new Runnable() { - - /** - * {@inheritDoc} - */ - @Override - public void run() { - markPostKnown(post); - } - }, 10, TimeUnit.SECONDS); + localElementTicker.schedule(new MarkPostKnown(post), 10, TimeUnit.SECONDS); return post; } @@ -1523,16 +1491,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, eventBus.post(new NewPostReplyFoundEvent(reply)); sone.addReply(reply); touchConfiguration(); - localElementTicker.schedule(new Runnable() { - - /** - * {@inheritDoc} - */ - @Override - public void run() { - markReplyKnown(reply); - } - }, 10, TimeUnit.SECONDS); + localElementTicker.schedule(new MarkReplyKnown(reply), 10, TimeUnit.SECONDS); return reply; } @@ -1895,12 +1854,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, configuration.getStringValue(sonePrefix + "/Images/" + imageCounter + "/ID").setValue(null); /* save options. */ - configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal()); - configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewSones").getReal()); - configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewPosts").getReal()); - configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewReplies").getReal()); - configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").getReal()); - configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions(). getEnumOption("ShowCustomAvatars").get().name()); + configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().isAutoFollow()); + configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().isSoneInsertNotificationEnabled()); + configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications()); + configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications()); + configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications()); + configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name()); configuration.save(); @@ -2000,15 +1959,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, options.addIntegerOption("NegativeTrust", new DefaultOption(-25, new IntegerRangePredicate(-100, 100))); options.addStringOption("TrustComment", new DefaultOption("Set from Sone Web Interface")); options.addBooleanOption("ActivateFcpInterface", new DefaultOption(false, fcpInterface.new SetActive())); - options.addIntegerOption("FcpFullAccessRequired", new DefaultOption(2, new OptionWatcher() { - - @Override - @SuppressWarnings("synthetic-access") - public void optionChanged(Option option, Integer oldValue, Integer newValue) { - fcpInterface.setFullAccessRequired(FullAccessRequired.values()[newValue]); - } - - })); + options.addIntegerOption("FcpFullAccessRequired", new DefaultOption(2, fcpInterface.new SetFullAccessRequired())); loadConfigurationValue("InsertionDelay"); loadConfigurationValue("PostsPerPage"); @@ -2127,22 +2078,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, */ @Subscribe public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) { - final Identity identity = identityUpdatedEvent.identity(); - soneDownloaders.execute(new Runnable() { - - @Override - @SuppressWarnings("synthetic-access") - public void run() { - Sone sone = getRemoteSone(identity.getId(), false); - if (sone.isLocal()) { - return; - } - sone.setIdentity(identity); - sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition())); - soneDownloader.addSone(sone); - soneDownloader.fetchSone(sone); - } - }); + Identity identity = identityUpdatedEvent.identity(); + final Sone sone = getRemoteSone(identity.getId(), false); + if (sone.isLocal()) { + return; + } + sone.setIdentity(identity); + sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition())); + soneDownloader.addSone(sone); + soneDownloaders.execute(soneDownloader.fetchSoneAction(sone)); } /** @@ -2202,4 +2146,36 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, touchConfiguration(); } + @VisibleForTesting + class MarkPostKnown implements Runnable { + + private final Post post; + + public MarkPostKnown(Post post) { + this.post = post; + } + + @Override + public void run() { + markPostKnown(post); + } + + } + + @VisibleForTesting + class MarkReplyKnown implements Runnable { + + private final PostReply postReply; + + public MarkReplyKnown(PostReply postReply) { + this.postReply = postReply; + } + + @Override + public void run() { + markReplyKnown(postReply); + } + + } + }