X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=9ccaa799868f05189150c5162fab46c01dcd9d9f;hp=7f6268592f247435e45b3f6ac9e6489e9936f6f0;hb=10fa1478fef15624a873c0878ed017603742bcfe;hpb=658a702c550e15bd3c868ed399621606eeb4ef20 diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 7f62685..9ccaa79 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -56,18 +56,18 @@ import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostBuilder; -import net.pterodactylus.sone.data.PostBuilderFactory; import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.PostReplyBuilder; -import net.pterodactylus.sone.data.PostReplyBuilderFactory; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.data.Sone.SoneStatus; +import net.pterodactylus.sone.database.PostBuilder; +import net.pterodactylus.sone.database.PostBuilderFactory; import net.pterodactylus.sone.database.PostProvider; +import net.pterodactylus.sone.database.PostReplyBuilder; +import net.pterodactylus.sone.database.PostReplyBuilderFactory; import net.pterodactylus.sone.database.PostReplyProvider; import net.pterodactylus.sone.database.SoneProvider; import net.pterodactylus.sone.data.TemporaryImage; @@ -90,9 +90,13 @@ import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.thread.NamedThreadFactory; +import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.Ordering; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import com.google.inject.Inject; @@ -343,12 +347,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * Returns all Sones, remote and local. - * - * @return All Sones + * {@inheritDocs} */ - public Set getSones() { - return new HashSet(sones.values()); + @Override + public Collection getSones() { + synchronized (sones) { + return Collections.unmodifiableCollection(sones.values()); + } } /** @@ -361,31 +366,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * Sone */ @Override - public Sone getSone(String id) { + public Optional getSone(String id) { synchronized (sones) { - return sones.get(id); + return Optional.fromNullable(sones.get(id)); } } /** - * Checks whether the core knows a Sone with the given ID. - * - * @param id - * The ID of the Sone - * @return {@code true} if there is a Sone with the given ID, {@code false} - * otherwise - */ - public boolean hasSone(String id) { - synchronized (sones) { - return sones.containsKey(id); - } - } - - /** - * Returns all local Sones. - * - * @return All local Sones + * {@inheritDocs} */ + @Override public Collection getLocalSones() { synchronized (sones) { return Collections2.filter(sones.values(), new Predicate() { @@ -424,10 +414,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * Returns all remote Sones. - * - * @return All remote Sones + * {@inheritDocs} */ + @Override public Collection getRemoteSones() { synchronized (sones) { return Collections2.filter(sones.values(), new Predicate() { @@ -483,10 +472,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, */ public long getSoneFollowingTime(Sone sone) { synchronized (soneFollowingTimes) { - if (soneFollowingTimes.containsKey(sone)) { - return soneFollowingTimes.get(sone); - } - return Long.MAX_VALUE; + return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE); } } @@ -519,13 +505,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * {@inheritDoc} */ @Override - public Post getPost(String postId) { + public Optional getPost(String postId) { synchronized (posts) { - return posts.get(postId); + return Optional.fromNullable(posts.get(postId)); } } /** + * {@inheritDocs} + */ + @Override + public Collection getPosts(String soneId) { + return postDatabase.getPosts(soneId); + } + + /** * {@inheritDoc} */ @Override @@ -536,11 +530,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, @Override public boolean apply(Post post) { - return (post.getRecipient() != null) && (post.getRecipient().getId().equals(recipientId)); + return recipientId.equals(post.getRecipientId().orNull()); } }); } } + /** * Returns a post reply builder. * @@ -554,9 +549,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * {@inheritDoc} */ @Override - public PostReply getPostReply(String replyId) { + public Optional getPostReply(String replyId) { synchronized (replies) { - return replies.get(replyId); + return Optional.fromNullable(replies.get(replyId)); } } @@ -564,18 +559,20 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * {@inheritDoc} */ @Override - public List getReplies(Post post) { - Set sones = getSones(); - List replies = new ArrayList(); - for (Sone sone : sones) { - for (PostReply reply : sone.getReplies()) { - if (reply.getPost().equals(post)) { - replies.add(reply); - } + public List getReplies(final Post post) { + return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(FluentIterable.from(getSones()).transformAndConcat(new Function>() { + + @Override + public Iterable apply(Sone sone) { + return sone.getReplies(); } - } - Collections.sort(replies, Reply.TIME_COMPARATOR); - return replies; + }).filter(new Predicate() { + + @Override + public boolean apply(PostReply reply) { + return post.getId().equals(reply.getPostId()); + } + })); } /** @@ -647,9 +644,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, Set posts = new HashSet(); synchronized (bookmarkedPosts) { for (String bookmarkedPostId : bookmarkedPosts) { - Post post = getPost(bookmarkedPostId); - if (post != null) { - posts.add(post); + Optional post = getPost(bookmarkedPostId); + if (!post.isPresent()) { + posts.add(post.get()); } } } @@ -798,6 +795,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); sone.setKnown(true); /* TODO - load posts ’n stuff */ + trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet())); sones.put(ownIdentity.getId(), sone); final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone); soneInserters.put(sone, soneInserter); @@ -894,16 +892,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (!soneFollowingTimes.containsKey(soneId)) { long now = System.currentTimeMillis(); soneFollowingTimes.put(soneId, now); - Sone followedSone = getSone(soneId); - if (followedSone == null) { + Optional followedSone = getSone(soneId); + if (!followedSone.isPresent()) { return; } - for (Post post : followedSone.getPosts()) { + for (Post post : followedSone.get().getPosts()) { if (post.getTime() < now) { markPostKnown(post); } } - for (PostReply reply : followedSone.getReplies()) { + for (PostReply reply : followedSone.get().getReplies()) { if (reply.getTime() < now) { markReplyKnown(reply); } @@ -1028,49 +1026,47 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * of the age of the given Sone */ public void updateSone(Sone sone, boolean soneRescueMode) { - if (hasSone(sone.getId())) { - Sone storedSone = getSone(sone.getId()); - if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) { + Optional storedSone = getSone(sone.getId()); + if (storedSone.isPresent()) { + if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) { logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone)); return; } synchronized (posts) { if (!soneRescueMode) { - for (Post post : storedSone.getPosts()) { + for (Post post : storedSone.get().getPosts()) { posts.remove(post.getId()); if (!sone.getPosts().contains(post)) { eventBus.post(new PostRemovedEvent(post)); } } } - List storedPosts = storedSone.getPosts(); + List storedPosts = storedSone.get().getPosts(); synchronized (knownPosts) { for (Post post : sone.getPosts()) { - PostBuilder postBuilder = postBuilderFactory.newPostBuilder(); - postBuilder.copyPost(post).from(storedSone.getId()); - Post newPost = postBuilder.build().setKnown(knownPosts.contains(post.getId())); - if (!storedPosts.contains(newPost)) { - if (newPost.getTime() < getSoneFollowingTime(sone)) { - knownPosts.add(newPost.getId()); - newPost.setKnown(true); - } else if (!knownPosts.contains(newPost.getId())) { - eventBus.post(new NewPostFoundEvent(newPost)); + post.setKnown(knownPosts.contains(post.getId())); + if (!storedPosts.contains(post)) { + if (post.getTime() < getSoneFollowingTime(sone)) { + knownPosts.add(post.getId()); + post.setKnown(true); + } else if (!knownPosts.contains(post.getId())) { + eventBus.post(new NewPostFoundEvent(post)); } } - posts.put(newPost.getId(), newPost); + posts.put(post.getId(), post); } } } synchronized (replies) { if (!soneRescueMode) { - for (PostReply reply : storedSone.getReplies()) { + for (PostReply reply : storedSone.get().getReplies()) { replies.remove(reply.getId()); if (!sone.getReplies().contains(reply)) { eventBus.post(new PostReplyRemovedEvent(reply)); } } } - Set storedReplies = storedSone.getReplies(); + Set storedReplies = storedSone.get().getReplies(); synchronized (knownReplies) { for (PostReply reply : sone.getReplies()) { reply.setKnown(knownReplies.contains(reply.getId())); @@ -1088,7 +1084,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } synchronized (albums) { synchronized (images) { - for (Album album : storedSone.getAlbums()) { + for (Album album : storedSone.get().getAlbums()) { albums.remove(album.getId()); for (Image image : album.getImages()) { images.remove(image.getId()); @@ -1102,36 +1098,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } } } - synchronized (storedSone) { - if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) { - storedSone.setTime(sone.getTime()); - } - storedSone.setClient(sone.getClient()); - storedSone.setProfile(sone.getProfile()); - if (soneRescueMode) { - for (Post post : sone.getPosts()) { - storedSone.addPost(post); - } - for (PostReply reply : sone.getReplies()) { - storedSone.addReply(reply); - } - for (String likedPostId : sone.getLikedPostIds()) { - storedSone.addLikedPostId(likedPostId); - } - for (String likedReplyId : sone.getLikedReplyIds()) { - storedSone.addLikedReplyId(likedReplyId); - } - for (Album album : sone.getAlbums()) { - storedSone.addAlbum(album); - } - } else { - storedSone.setPosts(sone.getPosts()); - storedSone.setReplies(sone.getReplies()); - storedSone.setLikePostIds(sone.getLikedPostIds()); - storedSone.setLikeReplyIds(sone.getLikedReplyIds()); - storedSone.setAlbums(sone.getAlbums()); - } - storedSone.setLatestEdition(sone.getLatestEdition()); + synchronized (sones) { + sone.setOptions(storedSone.get().getOptions()); + sones.put(sone.getId(), sone); } } } @@ -1250,7 +1219,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Invalid post found, aborting load!"); return; } - PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText); + PostBuilder postBuilder = postBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { postBuilder.to(postRecipientId); } @@ -1401,11 +1370,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, knownSones.add(friend); } } + synchronized (this.posts) { + for (Post post : posts) { + this.posts.put(post.getId(), post); + } + } synchronized (knownPosts) { for (Post post : posts) { knownPosts.add(post.getId()); } } + synchronized (this.replies) { + for (PostReply postReply : replies) { + this.replies.put(postReply.getId(), postReply); + } + } synchronized (knownReplies) { for (PostReply reply : replies) { knownReplies.add(reply.getId()); @@ -1453,7 +1432,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The text of the post * @return The created post */ - public Post createPost(Sone sone, Sone recipient, String text) { + public Post createPost(Sone sone, Optional recipient, String text) { return createPost(sone, recipient, System.currentTimeMillis(), text); } @@ -1471,7 +1450,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The text of the post * @return The created post */ - public Post createPost(Sone sone, Sone recipient, long time, String text) { + 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()) { @@ -1480,8 +1459,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } PostBuilder postBuilder = postBuilderFactory.newPostBuilder(); postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim()); - if (recipient != null) { - postBuilder.to(recipient.getId()); + if (recipient.isPresent()) { + postBuilder.to(recipient.get().getId()); } final Post post = postBuilder.build(); synchronized (posts) { @@ -1879,6 +1858,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, webOfTrustUpdater.stop(); updateChecker.stop(); soneDownloader.stop(); + soneDownloaders.shutdown(); identityManager.stop(); } @@ -1934,7 +1914,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, for (Post post : sone.getPosts()) { String postPrefix = sonePrefix + "/Posts/" + postCounter++; configuration.getStringValue(postPrefix + "/ID").setValue(post.getId()); - configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null); + configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull()); configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime()); configuration.getStringValue(postPrefix + "/Text").setValue(post.getText()); } @@ -1945,7 +1925,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, for (PostReply reply : sone.getReplies()) { String replyPrefix = sonePrefix + "/Replies/" + replyCounter++; configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId()); - configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId()); + configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId()); configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime()); configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText()); } @@ -2113,7 +2093,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /** * Loads the configuration. */ - @SuppressWarnings("unchecked") private void loadConfiguration() { /* create options. */ options.addIntegerOption("InsertionDelay", new DefaultOption(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new OptionWatcher() { @@ -2182,13 +2161,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, break; } long time = configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").getValue(Long.MAX_VALUE); - Sone followedSone = getSone(soneId); - if (followedSone == null) { - logger.log(Level.WARNING, String.format("Ignoring Sone with invalid ID: %s", soneId)); - } else { - synchronized (soneFollowingTimes) { - soneFollowingTimes.put(soneId, time); - } + synchronized (soneFollowingTimes) { + soneFollowingTimes.put(soneId, time); } ++soneCounter; } @@ -2257,7 +2231,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, OwnIdentity ownIdentity = ownIdentityAddedEvent.ownIdentity(); logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity)); if (ownIdentity.hasContext("Sone")) { - trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet())); addLocalSone(ownIdentity); } } @@ -2336,14 +2309,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /* some local identity still trusts this identity, don’t remove. */ return; } - Sone sone = getSone(identity.getId()); - if (sone == null) { + Optional sone = getSone(identity.getId()); + if (!sone.isPresent()) { /* TODO - we don’t have the Sone anymore. should this happen? */ return; } synchronized (posts) { synchronized (knownPosts) { - for (Post post : sone.getPosts()) { + for (Post post : sone.get().getPosts()) { posts.remove(post.getId()); eventBus.post(new PostRemovedEvent(post)); } @@ -2351,7 +2324,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } synchronized (replies) { synchronized (knownReplies) { - for (PostReply reply : sone.getReplies()) { + for (PostReply reply : sone.get().getReplies()) { replies.remove(reply.getId()); eventBus.post(new PostReplyRemovedEvent(reply)); } @@ -2360,7 +2333,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, synchronized (sones) { sones.remove(identity.getId()); } - eventBus.post(new SoneRemovedEvent(sone)); + eventBus.post(new SoneRemovedEvent(sone.get())); } /**