X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=ccf633c180b3a3a01fe7c5445df3d70c6411fd4f;hp=af69f98a5ba9cca556ed80be2a05c1c5619aa045;hb=4b42574c9ec4490100ac3e87b9c2f3aac965a202;hpb=e666a5187afee8bdb40b5e4c82a0cdddbe713719 diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index af69f98..ccf633c 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -51,6 +51,7 @@ import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor; import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor; import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent; +import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.core.event.MarkPostKnownEvent; import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent; import net.pterodactylus.sone.core.event.MarkSoneKnownEvent; @@ -157,9 +158,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /** The trust updater. */ private final WebOfTrustUpdater webOfTrustUpdater; - /** The times Sones were followed. */ - private final Map soneFollowingTimes = new HashMap(); - /** Locked local Sones. */ /* synchronize on itself. */ private final Set lockedSones = new HashSet(); @@ -207,22 +205,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The database */ @Inject - public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) { - super("Sone Core"); - this.configuration = configuration; - this.freenetInterface = freenetInterface; - this.identityManager = identityManager; - this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface); - this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier()); - this.updateChecker = updateChecker; - this.webOfTrustUpdater = webOfTrustUpdater; - this.eventBus = eventBus; - this.database = database; - preferences = new Preferences(eventBus); - } - - @VisibleForTesting - protected Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) { + public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) { super("Sone Core"); this.configuration = configuration; this.freenetInterface = freenetInterface; @@ -400,20 +383,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * Returns the time when the given was first followed by any local Sone. - * - * @param sone - * The Sone to get the time for - * @return The time (in milliseconds since Jan 1, 1970) the Sone has first - * been followed, or {@link Long#MAX_VALUE} - */ - public long getSoneFollowingTime(Sone sone) { - synchronized (soneFollowingTimes) { - return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE); - } - } - - /** * Returns a post builder. * * @return A new post builder @@ -422,11 +391,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return database.newPostBuilder(); } - /** - * {@inheritDoc} - */ + @Nullable @Override - public Optional getPost(String postId) { + public Post getPost(@Nonnull String postId) { return database.getPost(postId); } @@ -656,6 +623,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.setClient(new Client("Sone", SonePlugin.getPluginVersion())); sone.setKnown(true); SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId()); + soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay())); eventBus.register(soneInserter); synchronized (soneInserters) { soneInserters.put(sone, soneInserter); @@ -723,7 +691,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } database.storeSone(sone); soneDownloader.addSone(sone); - soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone)); + soneDownloaders.execute(soneDownloader.fetchSoneAsUskAction(sone)); return sone; } @@ -739,24 +707,20 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, checkNotNull(sone, "sone must not be null"); checkNotNull(soneId, "soneId must not be null"); database.addFriend(sone, soneId); - synchronized (soneFollowingTimes) { - if (!soneFollowingTimes.containsKey(soneId)) { - long now = System.currentTimeMillis(); - soneFollowingTimes.put(soneId, now); - Sone followedSone = getSone(soneId); - if (followedSone == null) { - return; - } - for (Post post : followedSone.getPosts()) { - if (post.getTime() < now) { - markPostKnown(post); - } - } - for (PostReply reply : followedSone.getReplies()) { - if (reply.getTime() < now) { - markReplyKnown(reply); - } - } + @SuppressWarnings("ConstantConditions") // we just followed, this can’t be null. + long now = database.getFollowingTime(soneId); + Sone followedSone = getSone(soneId); + if (followedSone == null) { + return; + } + for (Post post : followedSone.getPosts()) { + if (post.getTime() < now) { + markPostKnown(post); + } + } + for (PostReply reply : followedSone.getReplies()) { + if (reply.getTime() < now) { + markReplyKnown(reply); } } touchConfiguration(); @@ -774,15 +738,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, checkNotNull(sone, "sone must not be null"); checkNotNull(soneId, "soneId must not be null"); database.removeFriend(sone, soneId); - boolean unfollowedSoneStillFollowed = false; - for (Sone localSone : getLocalSones()) { - unfollowedSoneStillFollowed |= localSone.hasFriend(soneId); - } - if (!unfollowedSoneStillFollowed) { - synchronized (soneFollowingTimes) { - soneFollowingTimes.remove(soneId); - } - } touchConfiguration(); } @@ -906,7 +861,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, soneChangeDetector.onNewPosts(new PostProcessor() { @Override public void processPost(Post post) { - if (post.getTime() < getSoneFollowingTime(newSone)) { + if (post.getTime() < database.getFollowingTime(newSone.getId())) { post.setKnown(true); } else if (!post.isKnown()) { events.add(new NewPostFoundEvent(post)); @@ -922,7 +877,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, soneChangeDetector.onNewPostReplies(new PostReplyProcessor() { @Override public void processPostReply(PostReply postReply) { - if (postReply.getTime() < getSoneFollowingTime(newSone)) { + if (postReply.getTime() < database.getFollowingTime(newSone.getId())) { postReply.setKnown(true); } else if (!postReply.isKnown()) { events.add(new NewPostReplyFoundEvent(postReply)); @@ -1596,17 +1551,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null); } - /* save Sone following times. */ - soneCounter = 0; - synchronized (soneFollowingTimes) { - for (Entry soneFollowingTime : soneFollowingTimes.entrySet()) { - configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey()); - configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue()); - ++soneCounter; - } - configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null); - } - /* save known posts. */ database.save(); @@ -1641,20 +1585,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, knownSones.add(knownSoneId); } } - - /* load Sone following times. */ - soneCounter = 0; - while (true) { - String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null); - if (soneId == null) { - break; - } - long time = configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").getValue(Long.MAX_VALUE); - synchronized (soneFollowingTimes) { - soneFollowingTimes.put(soneId, time); - } - ++soneCounter; - } } /** @@ -1712,9 +1642,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (sone.isLocal()) { return; } - sone.setLatestEdition(fromNullable(tryParse(identity.getProperty("Sone.LatestEdition"))).or(sone.getLatestEdition())); + String newLatestEdition = identity.getProperty("Sone.LatestEdition"); + if (newLatestEdition != null) { + Long parsedNewLatestEdition = tryParse(newLatestEdition); + if (parsedNewLatestEdition != null) { + sone.setLatestEdition(parsedNewLatestEdition); + } + } soneDownloader.addSone(sone); - soneDownloaders.execute(soneDownloader.fetchSoneAction(sone)); + soneDownloaders.execute(soneDownloader.fetchSoneAsSskAction(sone)); } /**