X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=72e40900df05aa44c8ee56d6ee9a7094788191b6;hp=c9c6da3798b29ae6214899e6dc116d1c7dec5206;hb=f4d3720ef1fab42a063b70eff6ddc6c145e5122b;hpb=2402f55b1ae1000a4decbd2833883bebb91952f8 diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index c9c6da3..72e4090 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.primitives.Longs.tryParse; import static java.lang.String.format; import static java.util.logging.Level.WARNING; +import static java.util.logging.Logger.getLogger; import java.util.Collection; import java.util.HashMap; @@ -43,10 +44,8 @@ import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound; -import net.pterodactylus.sone.core.Options.DefaultOption; import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor; import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor; -import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay; import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent; import net.pterodactylus.sone.core.event.MarkPostKnownEvent; import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent; @@ -81,7 +80,6 @@ import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.database.PostReplyProvider; import net.pterodactylus.sone.database.SoneBuilder; import net.pterodactylus.sone.database.SoneProvider; -import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.IdentityManager; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -91,17 +89,13 @@ import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent; import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent; import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent; import net.pterodactylus.sone.main.SonePlugin; -import net.pterodactylus.sone.utils.IntegerRangePredicate; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; -import net.pterodactylus.util.logging.Logging; -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; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; @@ -120,16 +114,13 @@ import com.google.inject.Singleton; public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider { /** The logger. */ - private static final Logger logger = Logging.getLogger(Core.class); + private static final Logger logger = getLogger("Sone.Core"); /** The start time. */ private final long startupTime = System.currentTimeMillis(); - /** The options. */ - private final Options options = new Options(); - /** The preferences. */ - private final Preferences preferences = new Preferences(options); + private final Preferences preferences; /** The event bus. */ private final EventBus eventBus; @@ -161,9 +152,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /** The trust updater. */ private final WebOfTrustUpdater webOfTrustUpdater; - /** The FCP interface. */ - private volatile FcpInterface fcpInterface; - /** The times Sones were followed. */ private final Map soneFollowingTimes = new HashMap(); @@ -185,10 +173,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /** The post database. */ private final Database database; - /** All bookmarked posts. */ - /* synchronize access on itself. */ - private final Set bookmarkedPosts = new HashSet(); - /** Trusted identities, sorted by own identities. */ private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.create()); @@ -229,6 +213,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; this.database = database; + preferences = new Preferences(eventBus); } @VisibleForTesting @@ -243,6 +228,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; this.database = database; + preferences = new Preferences(eventBus); } // @@ -286,16 +272,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * Sets the FCP interface to use. - * - * @param fcpInterface - * The FCP interface to use - */ - public void setFcpInterface(FcpInterface fcpInterface) { - this.fcpInterface = fcpInterface; - } - - /** * Returns the Sone rescuer for the given local Sone. * * @param sone @@ -526,21 +502,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * otherwise */ public boolean isBookmarked(Post post) { - return isPostBookmarked(post.getId()); - } - - /** - * Returns whether the post with the given ID is bookmarked. - * - * @param id - * The ID of the post to check - * @return {@code true} if the post with the given ID is bookmarked, - * {@code false} otherwise - */ - public boolean isPostBookmarked(String id) { - synchronized (bookmarkedPosts) { - return bookmarkedPosts.contains(id); - } + return database.isPostBookmarked(post); } /** @@ -549,16 +511,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @return All bookmarked posts */ public Set getBookmarkedPosts() { - Set posts = new HashSet(); - synchronized (bookmarkedPosts) { - for (String bookmarkedPostId : bookmarkedPosts) { - Optional post = getPost(bookmarkedPostId); - if (post.isPresent()) { - posts.add(post.get()); - } - } - } - return posts; + return database.getBookmarkedPosts(); } public AlbumBuilder albumBuilder() { @@ -681,11 +634,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity)); Sone sone = database.newSoneBuilder().local().from(ownIdentity).build(); - sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), 0L)); + sone.setLatestEdition(fromNullable(tryParse(ownIdentity.getProperty("Sone.LatestEdition"))).or(0L)); sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); sone.setKnown(true); - /* TODO - load posts ’n stuff */ SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId()); + eventBus.register(soneInserter); synchronized (soneInserters) { soneInserters.put(sone, soneInserter); } @@ -973,10 +926,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone)); return; } - // FIXME – implement in database -// sones.remove(sone.getId()); SoneInserter soneInserter = soneInserters.remove(sone); soneInserter.stop(); + database.removeSone(sone); webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone"); webOfTrustUpdater.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition"); try { @@ -1153,24 +1105,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @return The created post */ public Post createPost(Sone sone, Optional recipient, String text) { - return createPost(sone, recipient, System.currentTimeMillis(), text); - } - - /** - * Creates a new post. - * - * @param sone - * The Sone that creates the post - * @param recipient - * The recipient Sone, or {@code null} if this post does not have - * a recipient - * @param time - * The time of the post - * @param text - * The text of the post - * @return The created post - */ - public Post createPost(Sone sone, Optional recipient, long time, String text) { checkNotNull(text, "text must not be null"); checkArgument(text.trim().length() > 0, "text must not be empty"); if (!sone.isLocal()) { @@ -1178,7 +1112,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return null; } PostBuilder postBuilder = database.newPostBuilder(); - postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim()); + postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim()); if (recipient.isPresent()) { postBuilder.to(recipient.get().getId()); } @@ -1224,16 +1158,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } } - /** - * Bookmarks the post with the given ID. - * - * @param id - * The ID of the post to bookmark - */ - public void bookmarkPost(String id) { - synchronized (bookmarkedPosts) { - bookmarkedPosts.add(id); - } + public void bookmarkPost(Post post) { + database.bookmarkPost(post); } /** @@ -1242,20 +1168,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @param post * The post to unbookmark */ - public void unbookmark(Post post) { - unbookmarkPost(post.getId()); - } - - /** - * Removes the post with the given ID from the bookmarks. - * - * @param id - * The ID of the post to unbookmark - */ - public void unbookmarkPost(String id) { - synchronized (bookmarkedPosts) { - bookmarkedPosts.remove(id); - } + public void unbookmarkPost(Post post) { + database.unbookmarkPost(post); } /** @@ -1654,18 +1568,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /* store the options first. */ try { - configuration.getIntValue("Option/ConfigurationVersion").setValue(0); - configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal()); - configuration.getIntValue("Option/PostsPerPage").setValue(options.getIntegerOption("PostsPerPage").getReal()); - configuration.getIntValue("Option/ImagesPerPage").setValue(options.getIntegerOption("ImagesPerPage").getReal()); - configuration.getIntValue("Option/CharactersPerPost").setValue(options.getIntegerOption("CharactersPerPost").getReal()); - configuration.getIntValue("Option/PostCutOffLength").setValue(options.getIntegerOption("PostCutOffLength").getReal()); - configuration.getBooleanValue("Option/RequireFullAccess").setValue(options.getBooleanOption("RequireFullAccess").getReal()); - configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal()); - configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal()); - configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal()); - configuration.getBooleanValue("Option/ActivateFcpInterface").setValue(options.getBooleanOption("ActivateFcpInterface").getReal()); - configuration.getIntValue("Option/FcpFullAccessRequired").setValue(options.getIntegerOption("FcpFullAccessRequired").getReal()); + preferences.saveTo(configuration); /* save known Sones. */ int soneCounter = 0; @@ -1690,15 +1593,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /* save known posts. */ database.save(); - /* save bookmarked posts. */ - int bookmarkedPostCounter = 0; - synchronized (bookmarkedPosts) { - for (String bookmarkedPostId : bookmarkedPosts) { - configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId); - } - } - configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null); - /* now save it. */ configuration.save(); @@ -1717,30 +1611,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * Loads the configuration. */ private void loadConfiguration() { - /* create options. */ - options.addIntegerOption("InsertionDelay", new DefaultOption(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new SetInsertionDelay())); - options.addIntegerOption("PostsPerPage", new DefaultOption(10, new IntegerRangePredicate(1, Integer.MAX_VALUE))); - options.addIntegerOption("ImagesPerPage", new DefaultOption(9, new IntegerRangePredicate(1, Integer.MAX_VALUE))); - options.addIntegerOption("CharactersPerPost", new DefaultOption(400, Predicates. or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1)))); - options.addIntegerOption("PostCutOffLength", new DefaultOption(200, Predicates. or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1)))); - options.addBooleanOption("RequireFullAccess", new DefaultOption(false)); - options.addIntegerOption("PositiveTrust", new DefaultOption(75, new IntegerRangePredicate(0, 100))); - 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, fcpInterface.new SetFullAccessRequired())); - - loadConfigurationValue("InsertionDelay"); - loadConfigurationValue("PostsPerPage"); - loadConfigurationValue("ImagesPerPage"); - loadConfigurationValue("CharactersPerPost"); - loadConfigurationValue("PostCutOffLength"); - options.getBooleanOption("RequireFullAccess").set(configuration.getBooleanValue("Option/RequireFullAccess").getValue(null)); - loadConfigurationValue("PositiveTrust"); - loadConfigurationValue("NegativeTrust"); - options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null)); - options.getBooleanOption("ActivateFcpInterface").set(configuration.getBooleanValue("Option/ActivateFcpInterface").getValue(null)); - options.getIntegerOption("FcpFullAccessRequired").set(configuration.getIntValue("Option/FcpFullAccessRequired").getValue(null)); + new PreferencesLoader(preferences).loadFrom(configuration); /* load known Sones. */ int soneCounter = 0; @@ -1767,34 +1638,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } ++soneCounter; } - - /* load bookmarked posts. */ - int bookmarkedPostCounter = 0; - while (true) { - String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null); - if (bookmarkedPostId == null) { - break; - } - synchronized (bookmarkedPosts) { - bookmarkedPosts.add(bookmarkedPostId); - } - } - - } - - /** - * Loads an {@link Integer} configuration value for the option with the - * given name, logging validation failures. - * - * @param optionName - * The name of the option to load - */ - private void loadConfigurationValue(String optionName) { - try { - options.getIntegerOption(optionName).set(configuration.getIntValue("Option/" + optionName).getValue(null)); - } catch (IllegalArgumentException iae1) { - logger.log(Level.WARNING, String.format("Invalid value for %s in configuration, using default.", optionName)); - } } /** @@ -1852,7 +1695,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (sone.isLocal()) { return; } - sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition())); + sone.setLatestEdition(fromNullable(tryParse(identity.getProperty("Sone.LatestEdition"))).or(sone.getLatestEdition())); soneDownloader.addSone(sone); soneDownloaders.execute(soneDownloader.fetchSoneAction(sone)); } @@ -1881,16 +1724,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /* TODO - we don’t have the Sone anymore. should this happen? */ return; } - database.removePosts(sone.get()); - for (Post post : sone.get().getPosts()) { - eventBus.post(new PostRemovedEvent(post)); - } - database.removePostReplies(sone.get()); - for (PostReply reply : sone.get().getReplies()) { - eventBus.post(new PostReplyRemovedEvent(reply)); - } -// TODO – implement in database -// sones.remove(identity.getId()); + database.removeSone(sone.get()); eventBus.post(new SoneRemovedEvent(sone.get())); }