X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=140d80ce354353c38e9cfd0078640648ea4a8846;hb=00a434a23c9ea1e57c63d8a3c0fc4b09277af431;hp=9269ea61c58c58a4f32300d4da312ffa94c86864;hpb=16172b8d60c485ac52907ba45a354b754bce53e5;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 9269ea6..140d80c 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -23,8 +23,11 @@ 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.ArrayList; import java.util.Collection; +import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -70,6 +73,7 @@ import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.data.Sone.SoneStatus; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.database.AlbumBuilder; +import net.pterodactylus.sone.database.AlbumProvider; import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.DatabaseException; import net.pterodactylus.sone.database.ImageBuilder; @@ -79,7 +83,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,13 +94,13 @@ import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent; import net.pterodactylus.sone.main.SonePlugin; 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.Function; import com.google.common.base.Optional; +import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; @@ -113,10 +116,10 @@ import com.google.inject.Singleton; * @author David ‘Bombe’ Roden */ @Singleton -public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider { +public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider, AlbumProvider { /** The logger. */ - private static final Logger logger = Logging.getLogger(Core.class); + private static final Logger logger = getLogger(Core.class.getName()); /** The start time. */ private final long startupTime = System.currentTimeMillis(); @@ -154,6 +157,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /** The trust updater. */ private final WebOfTrustUpdater webOfTrustUpdater; + private final Set compatibilityModes = EnumSet.noneOf(CompatibilityMode.class); + /** The times Sones were followed. */ private final Map soneFollowingTimes = new HashMap(); @@ -273,6 +278,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return updateChecker; } + public boolean isCompatibilityMode(CompatibilityMode compatibilityMode) { + return compatibilityModes.contains(compatibilityMode); + } + + public void setCompatibilityMode(CompatibilityMode compatibilityMode) { + compatibilityModes.add(compatibilityMode); + } + + public void clearCompatibilityMod(CompatibilityMode compatibilityMode) { + compatibilityModes.remove(compatibilityMode); + } + /** * Returns the Sone rescuer for the given local Sone. * @@ -319,6 +336,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return database.getSones(); } + @Override + public Function> soneLoader() { + return database.soneLoader(); + } + /** * Returns the Sone with the given ID, regardless whether it’s local or * remote. @@ -415,8 +437,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * {@inheritDoc} */ @Override - public Optional getPost(String postId) { - return database.getPost(postId); + public Optional getPost(final String postId) { + Optional post = database.getPost(postId); + if (post.isPresent() || !isCompatibilityMode(CompatibilityMode.oldElementIds)) { + return post; + } + return FluentIterable.from(getSones()).transformAndConcat(Sone.toAllPosts).filter(new Predicate() { + @Override + public boolean apply(Post input) { + return (input != null) && input.getInternalId().equals(postId); + } + }).first(); } /** @@ -529,8 +560,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @return The album with the given ID, or {@code null} if no album with the * given ID exists */ - public Album getAlbum(String albumId) { - return database.getAlbum(albumId).orNull(); + public Optional getAlbum(String albumId) { + return database.getAlbum(albumId); } public ImageBuilder imageBuilder() { @@ -636,7 +667,8 @@ 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)); + String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0"); + sone.setLatestEdition(fromNullable(tryParse(property)).or(0L)); sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); sone.setKnown(true); SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId()); @@ -645,6 +677,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, soneInserters.put(sone, soneInserter); } loadSone(sone); + database.storeSone(sone); sone.setStatus(SoneStatus.idle); soneInserter.start(); return sone; @@ -681,8 +714,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Given Identity is null!"); return null; } - final Long latestEdition = tryParse(fromNullable( - identity.getProperty("Sone.LatestEdition")).or("0")); + String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0"); + long latestEdition = fromNullable(tryParse(property)).or(0L); Optional existingSone = getSone(identity.getId()); if (existingSone.isPresent() && existingSone.get().isLocal()) { return existingSone.get(); @@ -721,7 +754,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, public void followSone(Sone sone, String soneId) { checkNotNull(sone, "sone must not be null"); checkNotNull(soneId, "soneId must not be null"); - sone.addFriend(soneId); + database.addFriend(sone, soneId); synchronized (soneFollowingTimes) { if (!soneFollowingTimes.containsKey(soneId)) { long now = System.currentTimeMillis(); @@ -756,7 +789,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, public void unfollowSone(Sone sone, String soneId) { checkNotNull(sone, "sone must not be null"); checkNotNull(soneId, "soneId must not be null"); - sone.removeFriend(soneId); + database.removeFriend(sone, soneId); boolean unfollowedSoneStillFollowed = false; for (Sone localSone : getLocalSones()) { unfollowedSoneStillFollowed |= localSone.hasFriend(soneId); @@ -866,42 +899,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone)); return; } - /* find removed posts. */ - SoneChangeDetector soneChangeDetector = new SoneChangeDetector(storedSone.get()); - soneChangeDetector.onNewPosts(new PostProcessor() { - @Override - public void processPost(Post post) { - if (post.getTime() < getSoneFollowingTime(sone)) { - post.setKnown(true); - } else if (!post.isKnown()) { - eventBus.post(new NewPostFoundEvent(post)); - } - } - }); - soneChangeDetector.onRemovedPosts(new PostProcessor() { - @Override - public void processPost(Post post) { - eventBus.post(new PostRemovedEvent(post)); - } - }); - soneChangeDetector.onNewPostReplies(new PostReplyProcessor() { - @Override - public void processPostReply(PostReply postReply) { - if (postReply.getTime() < getSoneFollowingTime(sone)) { - postReply.setKnown(true); - } else if (!postReply.isKnown()) { - eventBus.post(new NewPostReplyFoundEvent(postReply)); - } - } - }); - soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() { - @Override - public void processPostReply(PostReply postReply) { - eventBus.post(new PostReplyRemovedEvent(postReply)); - } - }); - soneChangeDetector.detectChanges(sone); + List events = + collectEventsForChangesInSone(storedSone.get(), sone); database.storeSone(sone); + for (Object event : events) { + eventBus.post(event); + } sone.setOptions(storedSone.get().getOptions()); sone.setKnown(storedSone.get().isKnown()); sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); @@ -911,6 +914,47 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } } + private List collectEventsForChangesInSone(Sone oldSone, + final Sone newSone) { + final List events = new ArrayList(); + SoneChangeDetector soneChangeDetector = new SoneChangeDetector( + oldSone); + soneChangeDetector.onNewPosts(new PostProcessor() { + @Override + public void processPost(Post post) { + if (post.getTime() < getSoneFollowingTime(newSone)) { + post.setKnown(true); + } else if (!post.isKnown()) { + events.add(new NewPostFoundEvent(post)); + } + } + }); + soneChangeDetector.onRemovedPosts(new PostProcessor() { + @Override + public void processPost(Post post) { + events.add(new PostRemovedEvent(post)); + } + }); + soneChangeDetector.onNewPostReplies(new PostReplyProcessor() { + @Override + public void processPostReply(PostReply postReply) { + if (postReply.getTime() < getSoneFollowingTime(newSone)) { + postReply.setKnown(true); + } else if (!postReply.isKnown()) { + events.add(new NewPostReplyFoundEvent(postReply)); + } + } + }); + soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() { + @Override + public void processPostReply(PostReply postReply) { + events.add(new PostReplyRemovedEvent(postReply)); + } + }); + soneChangeDetector.detectChanges(newSone); + return events; + } + /** * Deletes the given Sone. This will remove the Sone from the * {@link #getLocalSones() local Sones}, stop its {@link SoneInserter} and @@ -928,10 +972,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 { @@ -1012,9 +1055,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, Set likedReplyIds = configurationSoneParser.parseLikedPostReplyIds(); - /* load friends. */ - Set friends = configurationSoneParser.parseFriends(); - /* load albums. */ List topLevelAlbums; try { @@ -1051,11 +1091,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /* load options. */ - 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().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false)); + sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false)); + sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true)); + sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true)); + sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true)); sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name()))); /* if we’re still here, Sone was loaded successfully. */ @@ -1066,25 +1106,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.setReplies(replies); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); - for (String friendId : friends) { - followSone(sone, friendId); - } for (Album album : sone.getRootAlbum().getAlbums()) { sone.getRootAlbum().removeAlbum(album); } for (Album album : topLevelAlbums) { sone.getRootAlbum().addAlbum(album); } - database.storeSone(sone); synchronized (soneInserters) { soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint); } } - synchronized (knownSones) { - for (String friend : friends) { - knownSones.add(friend); - } - } for (Post post : posts) { post.setKnown(true); } @@ -1108,24 +1139,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()) { @@ -1133,7 +1146,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()); } @@ -1424,6 +1437,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, saveSone(soneInserter.getKey()); } } + synchronized (soneRescuers) { + for (SoneRescuer soneRescuer : soneRescuers.values()) { + soneRescuer.stop(); + } + } saveConfiguration(); database.stop(); webOfTrustUpdater.stop(); @@ -1484,7 +1502,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, int postCounter = 0; for (Post post : sone.getPosts()) { String postPrefix = sonePrefix + "/Posts/" + postCounter++; - configuration.getStringValue(postPrefix + "/ID").setValue(post.getId()); + configuration.getStringValue(postPrefix + "/ID").setValue(post.getInternalId()); configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull()); configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime()); configuration.getStringValue(postPrefix + "/Text").setValue(post.getText()); @@ -1495,7 +1513,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, int replyCounter = 0; for (PostReply reply : sone.getReplies()) { String replyPrefix = sonePrefix + "/Replies/" + replyCounter++; - configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId()); + configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getInternalId()); configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId()); configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime()); configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText()); @@ -1516,20 +1534,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null); - /* save friends. */ - int friendCounter = 0; - for (String friendId : sone.getFriends()) { - configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId); - } - configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null); - /* save albums. first, collect in a flat structure, top-level first. */ List albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList(); int albumCounter = 0; for (Album album : albums) { String albumPrefix = sonePrefix + "/Albums/" + albumCounter++; - configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId()); + configuration.getStringValue(albumPrefix + "/ID").setValue(album.getInternalId()); configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle()); configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription()); configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId()); @@ -1545,8 +1556,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, continue; } String imagePrefix = sonePrefix + "/Images/" + imageCounter++; - configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId()); - configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId()); + configuration.getStringValue(imagePrefix + "/ID").setValue(image.getInternalId()); + configuration.getStringValue(imagePrefix + "/Album").setValue(album.getInternalId()); configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey()); configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle()); configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription()); @@ -1611,16 +1622,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null); } + /* save compatibility modes. */ + configuration.getBooleanValue("CompatibilityModes/OldElementIds").setValue(compatibilityModes.contains(CompatibilityMode.oldElementIds)); + /* save known posts. */ database.save(); - /* save bookmarked posts. */ - int bookmarkedPostCounter = 0; - for (Post bookmarkedPost : getBookmarkedPosts()) { - configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPost.getId()); - } - configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null); - /* now save it. */ configuration.save(); @@ -1667,16 +1674,10 @@ 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; - } - database.bookmarkPost(bookmarkedPostId); + /* load compatibility modes. */ + if (configuration.getBooleanValue("CompatibilityModes/OldElementIds").getValue(false)) { + setCompatibilityMode(CompatibilityMode.oldElementIds); } - } /** @@ -1734,7 +1735,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)); } @@ -1763,16 +1764,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())); }