From: David ‘Bombe’ Roden Date: Sat, 26 Jan 2013 11:35:09 +0000 (+0100) Subject: Merge branch 'partial-rewrite' into less-critical X-Git-Tag: 0.8.5^2~3^2~45 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;ds=sidebyside;h=8f02544d31f323ae9053dd9a11a99eacd8cf5bcd;hp=-c;p=Sone.git Merge branch 'partial-rewrite' into less-critical Conflicts: src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java src/main/java/net/pterodactylus/sone/text/SoneTextParser.java --- 8f02544d31f323ae9053dd9a11a99eacd8cf5bcd diff --combined src/main/java/net/pterodactylus/sone/core/Core.java index e46eceb,fb3c8a9..fa98bbb --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@@ -56,17 -56,20 +56,20 @@@ import net.pterodactylus.sone.data.Albu 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; import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; @@@ -87,13 -90,9 +90,13 @@@ import net.pterodactylus.util.number.Nu 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; @@@ -361,30 -360,9 +364,9 @@@ public class Core extends AbstractServi * @return The Sone with the given ID, or {@code null} if there is no such * Sone */ - public Sone getSone(String id) { - return getSone(id, true); - } - - /** - * Returns the Sone with the given ID, regardless whether it’s local or - * remote. - * - * @param id - * The ID of the Sone to get - * @param create - * {@code true} to create a new Sone if none exists, - * {@code false} to return {@code null} if a Sone with the given - * ID does not exist - * @return The Sone with the given ID, or {@code null} if there is no such - * Sone - */ @Override - public Sone getSone(String id, boolean create) { + public Sone getSone(String id) { synchronized (sones) { - if (!sones.containsKey(id) && create) { - Sone sone = new Sone(id, false); - sones.put(id, sone); - } return sones.get(id); } } @@@ -505,7 -483,10 +487,7 @@@ */ 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); } } @@@ -538,33 -519,28 +520,28 @@@ * {@inheritDoc} */ @Override - public Post getPost(String postId) { + public Optional getPost(String postId) { synchronized (posts) { - return posts.get(postId); + return Optional.fromNullable(posts.get(postId)); } } /** - * Returns all posts that have the given Sone as recipient. - * - * @see Post#getRecipient() - * @param recipient - * The recipient of the posts - * @return All posts that have the given Sone as recipient + * {@inheritDoc} */ - public Set getDirectedPosts(Sone recipient) { - checkNotNull(recipient, "recipient must not be null"); - Set directedPosts = new HashSet(); + @Override + public Collection getDirectedPosts(final String recipientId) { + checkNotNull(recipientId, "recipient must not be null"); synchronized (posts) { - for (Post post : posts.values()) { - if (recipient.equals(post.getRecipient())) { - directedPosts.add(post); + return Collections2.filter(posts.values(), new Predicate() { + + @Override + public boolean apply(Post post) { + return (post.getRecipient() != null) && (post.getRecipient().getId().equals(recipientId)); } - } + }); } - return directedPosts; } - /** * Returns a post reply builder. * @@@ -578,9 -554,9 +555,9 @@@ * {@inheritDoc} */ @Override - public PostReply getPostReply(String replyId) { + public Optional getPostReply(String replyId) { synchronized (replies) { - return replies.get(replyId); + return Optional.fromNullable(replies.get(replyId)); } } @@@ -588,20 -564,18 +565,20 @@@ * {@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()); + } + })); } /** @@@ -673,9 -647,9 +650,9 @@@ 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()); } } } @@@ -1072,16 -1046,18 +1049,16 @@@ List storedPosts = storedSone.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); } } } @@@ -1126,8 -1102,36 +1103,8 @@@ } } } - 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) { + sones.put(sone.getId(), sone); } } } @@@ -1246,7 -1250,7 +1223,7 @@@ 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); } @@@ -1875,7 -1879,6 +1852,7 @@@ webOfTrustUpdater.stop(); updateChecker.stop(); soneDownloader.stop(); + soneDownloaders.shutdown(); identityManager.stop(); } @@@ -1942,7 -1945,7 +1919,7 @@@ 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()); } @@@ -2333,7 -2336,7 +2310,7 @@@ /* some local identity still trusts this identity, don’t remove. */ return; } - Sone sone = getSone(identity.getId(), false); + Sone sone = getSone(identity.getId()); if (sone == null) { /* TODO - we don’t have the Sone anymore. should this happen? */ return; diff --combined src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index abdea0d,aa2aa99..f493fdc --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@@ -31,12 -31,12 +31,12 @@@ import net.pterodactylus.sone.data.Albu 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.PostReply; - import net.pterodactylus.sone.data.PostReplyBuilder; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; + import net.pterodactylus.sone.database.PostBuilder; + import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.number.Numbers; @@@ -166,7 -166,6 +166,7 @@@ public class SoneDownloader extends Abs Sone parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri()); if (parsedSone != null) { if (!fetchOnly) { + parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); core.updateSone(parsedSone); addSone(parsedSone); } diff --combined src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java index cf32640,567708b..4df8897 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilderFactory.java @@@ -17,12 -17,10 +17,12 @@@ package net.pterodactylus.sone.data.impl; - import com.google.inject.Inject; + import net.pterodactylus.sone.database.PostBuilder; + import net.pterodactylus.sone.database.PostBuilderFactory; + import net.pterodactylus.sone.database.SoneProvider; - import net.pterodactylus.sone.core.SoneProvider; - import net.pterodactylus.sone.data.PostBuilder; - import net.pterodactylus.sone.data.PostBuilderFactory; ++import com.google.inject.Inject; + /** * {@link PostBuilderFactory} implementation that creates * {@link PostBuilderImpl}s. @@@ -40,7 -38,6 +40,7 @@@ public class DefaultPostBuilderFactory * @param soneProvider * The Sone provider */ + @Inject public DefaultPostBuilderFactory(SoneProvider soneProvider) { this.soneProvider = soneProvider; } diff --combined src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java index 065ce2f,87c792a..30badf7 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java @@@ -17,13 -17,11 +17,13 @@@ package net.pterodactylus.sone.data.impl; - import net.pterodactylus.sone.core.PostProvider; - import net.pterodactylus.sone.core.SoneProvider; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; + import net.pterodactylus.sone.database.PostProvider; + import net.pterodactylus.sone.database.SoneProvider; +import com.google.common.base.Optional; + /** * Simple {@link PostReply} implementation. * @@@ -66,18 -64,10 +66,18 @@@ public class PostReplyImpl extends Repl // /** + * {@inheritDocs} + */ + @Override + public String getPostId() { + return postId; + } + + /** * {@inheritDoc} */ @Override - public Post getPost() { + public Optional getPost() { return postProvider.getPost(postId); } diff --combined src/main/java/net/pterodactylus/sone/database/PostProvider.java index 0000000,865376b..13845da mode 000000,100644..100644 --- a/src/main/java/net/pterodactylus/sone/database/PostProvider.java +++ b/src/main/java/net/pterodactylus/sone/database/PostProvider.java @@@ -1,0 -1,50 +1,52 @@@ + /* + * Sone - PostProvider.java - Copyright © 2011–2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + package net.pterodactylus.sone.database; + + import java.util.Collection; + + import net.pterodactylus.sone.data.Post; + ++import com.google.common.base.Optional; ++ + /** + * Interface for objects that can provide {@link Post}s by their ID. + * + * @author David ‘Bombe’ Roden + */ + public interface PostProvider { + + /** + * Returns the post with the given ID. + * + * @param postId + * The ID of the post to return + * @return The post with the given ID, or {@code null} + */ - public Post getPost(String postId); ++ public Optional getPost(String postId); + + /** + * Returns all posts that have the given Sone as recipient. + * + * @see Post#getRecipient() + * @param recipientId + * The ID of the recipient of the posts + * @return All posts that have the given Sone as recipient + */ + public Collection getDirectedPosts(String recipientId); + + } diff --combined src/main/java/net/pterodactylus/sone/database/PostReplyProvider.java index 0000000,2ad38a6..8098f1d mode 000000,100644..100644 --- a/src/main/java/net/pterodactylus/sone/database/PostReplyProvider.java +++ b/src/main/java/net/pterodactylus/sone/database/PostReplyProvider.java @@@ -1,0 -1,50 +1,52 @@@ + /* + * Sone - PostReplyProvider.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + package net.pterodactylus.sone.database; + + import java.util.List; + + import net.pterodactylus.sone.data.Post; + import net.pterodactylus.sone.data.PostReply; + ++import com.google.common.base.Optional; ++ + /** + * Interface for objects that can provide {@link PostReply}s. + * + * @author David ‘Bombe’ Roden + */ + public interface PostReplyProvider { + + /** + * Returns the reply with the given ID. + * + * @param id + * The ID of the reply to get + * @return The reply, or {@code null} if there is no such reply + */ - public PostReply getPostReply(String id); ++ public Optional getPostReply(String id); + + /** + * Returns all replies for the given post, order ascending by time. + * + * @param post + * The post to get all replies for + * @return All replies for the given post + */ + public List getReplies(Post post); + + } diff --combined src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 368f49a,036e451..8afe7c4 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@@ -33,7 -33,6 +33,7 @@@ import net.pterodactylus.sone.freenet.f import net.pterodactylus.sone.freenet.fcp.FcpException; import net.pterodactylus.sone.template.SoneAccessor; +import com.google.common.base.Optional; import com.google.common.collect.Collections2; import freenet.node.FSParseException; @@@ -163,7 -162,7 +163,7 @@@ public abstract class AbstractSoneComma if (mandatory && (soneId == null)) { throw new FcpException("Could not load Sone ID from “" + parameterName + "”."); } - Sone sone = localOnly ? core.getLocalSone(soneId, false) : core.getSone(soneId, false); + Sone sone = localOnly ? core.getLocalSone(soneId, false) : core.getSone(soneId); if (mandatory && (sone == null)) { throw new FcpException("Could not load Sone from “" + soneId + "”."); } @@@ -186,11 -185,11 +186,11 @@@ protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { try { String postId = simpleFieldSet.getString(parameterName); - Post post = core.getPost(postId); - if (post == null) { + Optional post = core.getPost(postId); + if (!post.isPresent()) { throw new FcpException("Could not load post from “" + postId + "”."); } - return post; + return post.get(); } catch (FSParseException fspe1) { throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1); } @@@ -212,11 -211,11 +212,11 @@@ protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { try { String replyId = simpleFieldSet.getString(parameterName); - PostReply reply = core.getPostReply(replyId); - if (reply == null) { + Optional reply = core.getPostReply(replyId); + if (!reply.isPresent()) { throw new FcpException("Could not load reply from “" + replyId + "”."); } - return reply; + return reply.get(); } catch (FSParseException fspe1) { throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1); } diff --combined src/main/java/net/pterodactylus/sone/main/SonePlugin.java index 4b0f3eb,10992a6..26ccf09 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@@ -24,13 -24,13 +24,13 @@@ import java.util.logging.Logger import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.core.FreenetInterface; - import net.pterodactylus.sone.core.PostProvider; - import net.pterodactylus.sone.core.SoneProvider; import net.pterodactylus.sone.core.WebOfTrustUpdater; - import net.pterodactylus.sone.data.PostBuilderFactory; - import net.pterodactylus.sone.data.PostReplyBuilderFactory; import net.pterodactylus.sone.data.impl.DefaultPostBuilderFactory; import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilderFactory; + import net.pterodactylus.sone.database.PostBuilderFactory; + import net.pterodactylus.sone.database.PostProvider; + import net.pterodactylus.sone.database.PostReplyBuilderFactory; + import net.pterodactylus.sone.database.SoneProvider; import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend; import net.pterodactylus.sone.freenet.plugin.PluginConnector; @@@ -213,7 -213,6 +213,7 @@@ public class SonePlugin implements Fred @Override protected void configure() { + bind(Core.class).in(Singleton.class); bind(EventBus.class).toInstance(eventBus); bind(Configuration.class).toInstance(startConfiguration); bind(FreenetInterface.class).in(Singleton.class); diff --combined src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index 6e84c70,9aacff6..f2ca0c5 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@@ -26,14 -26,12 +26,15 @@@ import java.util.logging.Logger import java.util.regex.Matcher; import java.util.regex.Pattern; - import com.google.common.base.Optional; - - import net.pterodactylus.sone.core.PostProvider; - import net.pterodactylus.sone.core.SoneProvider; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; + import net.pterodactylus.sone.database.PostProvider; + import net.pterodactylus.sone.database.SoneProvider; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; ++ ++import com.google.common.base.Optional; ++ import freenet.keys.FreenetURI; /** @@@ -241,7 -239,7 +242,7 @@@ public class SoneTextParser implements if (linkType == LinkType.SONE) { if (line.length() >= (7 + 43)) { String soneId = line.substring(7, 50); - Sone sone = soneProvider.getSone(soneId, false); + Sone sone = soneProvider.getSone(soneId); if (sone == null) { /* * don’t use create=true above, we don’t want @@@ -260,9 -258,9 +261,9 @@@ if (linkType == LinkType.POST) { if (line.length() >= (7 + 36)) { String postId = line.substring(7, 43); - Post post = postProvider.getPost(postId); - if ((post != null) && (post.getSone() != null)) { - parts.add(new PostPart(post)); + Optional post = postProvider.getPost(postId); + if (post.isPresent()) { + parts.add(new PostPart(post.get())); } else { parts.add(new PlainTextPart(line.substring(0, 43))); } diff --combined src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java index f183ff4,a923386..88f1026 --- a/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java +++ b/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java @@@ -27,8 -27,6 +27,8 @@@ import net.pterodactylus.sone.web.page. import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; +import com.google.common.base.Optional; + /** * Page that lets the user mark a number of {@link Sone}s, {@link Post}s, or * {@link Reply Replie}s as known. @@@ -67,19 -65,19 +67,19 @@@ public class MarkAsKnownPage extends So for (StringTokenizer idTokenizer = new StringTokenizer(ids); idTokenizer.hasMoreTokens();) { String id = idTokenizer.nextToken(); if (type.equals("post")) { - Post post = webInterface.getCore().getPost(id); - if (post == null) { + Optional post = webInterface.getCore().getPost(id); + if (!post.isPresent()) { continue; } - webInterface.getCore().markPostKnown(post); + webInterface.getCore().markPostKnown(post.get()); } else if (type.equals("reply")) { - PostReply reply = webInterface.getCore().getPostReply(id); - if (reply == null) { + Optional reply = webInterface.getCore().getPostReply(id); + if (!reply.isPresent()) { continue; } - webInterface.getCore().markReplyKnown(reply); + webInterface.getCore().markReplyKnown(reply.get()); } else if (type.equals("sone")) { - Sone sone = webInterface.getCore().getSone(id, false); + Sone sone = webInterface.getCore().getSone(id); if (sone == null) { continue; } diff --combined src/main/java/net/pterodactylus/sone/web/SearchPage.java index e979a49,c037baf..6cbb11f --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@@ -44,7 -44,6 +44,7 @@@ import net.pterodactylus.util.text.Stri import net.pterodactylus.util.text.TextException; import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@@ -310,7 -309,7 +310,7 @@@ public class SearchPage extends SoneTem */ private String getSoneId(String phrase) { String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase; - return (webInterface.getCore().getSone(soneId, false) != null) ? soneId : null; + return (webInterface.getCore().getSone(soneId) != null) ? soneId : null; } /** @@@ -323,7 -322,7 +323,7 @@@ */ private String getPostId(String phrase) { String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase; - return (webInterface.getCore().getPost(postId) != null) ? postId : null; + return (webInterface.getCore().getPost(postId).isPresent()) ? postId : null; } /** @@@ -337,11 -336,7 +337,11 @@@ */ private String getReplyPostId(String phrase) { String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getPostReply(replyId) != null) ? webInterface.getCore().getPostReply(replyId).getPost().getId() : null; + Optional postReply = webInterface.getCore().getPostReply(replyId); + if (!postReply.isPresent()) { + return null; + } + return postReply.get().getPostId(); } /** diff --combined src/main/java/net/pterodactylus/sone/web/ViewSonePage.java index 5572bca,f216431..592404f --- a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java @@@ -36,8 -36,6 +36,8 @@@ import net.pterodactylus.util.number.Nu import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; +import com.google.common.base.Optional; + /** * Lets the user browser another Sone. * @@@ -67,7 -65,7 +67,7 @@@ public class ViewSonePage extends SoneT @Override protected String getPageTitle(FreenetRequest request) { String soneId = request.getHttpRequest().getParam("sone"); - Sone sone = webInterface.getCore().getSone(soneId, false); + Sone sone = webInterface.getCore().getSone(soneId); if ((sone != null) && (sone.getTime() > 0)) { String soneName = SoneAccessor.getNiceName(sone); return soneName + " - " + webInterface.getL10n().getString("Page.ViewSone.Title"); @@@ -82,14 -80,14 +82,14 @@@ protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); String soneId = request.getHttpRequest().getParam("sone"); - Sone sone = webInterface.getCore().getSone(soneId, false); + Sone sone = webInterface.getCore().getSone(soneId); templateContext.set("sone", sone); templateContext.set("soneId", soneId); if (sone == null) { return; } List sonePosts = sone.getPosts(); - sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone)); + sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone.getId())); Collections.sort(sonePosts, Post.TIME_COMPARATOR); Pagination postPagination = new Pagination(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0)); templateContext.set("postPagination", postPagination); @@@ -97,11 -95,11 +97,11 @@@ Set replies = sone.getReplies(); final Map> repliedPosts = new HashMap>(); for (PostReply reply : replies) { - Post post = reply.getPost(); - if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) { + Optional post = reply.getPost(); + if (!post.isPresent() || repliedPosts.containsKey(post.get()) || sone.equals(post.get().getSone()) || (sone.equals(post.get().getRecipient()))) { continue; } - repliedPosts.put(post, webInterface.getCore().getReplies(post)); + repliedPosts.put(post.get(), webInterface.getCore().getReplies(post.get())); } List posts = new ArrayList(repliedPosts.keySet()); Collections.sort(posts, new Comparator() { diff --combined src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java index 6caa2fd,384261c..f107b71 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java @@@ -74,7 -74,7 +74,7 @@@ public class GetStatusAjaxPage extends String[] soneIds = loadSoneIds.split(","); for (String soneId : soneIds) { /* just add it, we skip null further down. */ - sones.add(webInterface.getCore().getSone(soneId, false)); + sones.add(webInterface.getCore().getSone(soneId)); } } JsonArray jsonSones = new JsonArray(); @@@ -122,14 -122,20 +122,14 @@@ }); } /* remove replies to unknown posts. */ - newReplies = Collections2.filter(newReplies, new Predicate() { - - @Override - public boolean apply(PostReply reply) { - return (reply.getPost() != null) && (reply.getPost().getSone() != null); - } - }); + newReplies = Collections2.filter(newReplies, PostReply.HAS_POST_FILTER); JsonArray jsonReplies = new JsonArray(); for (PostReply reply : newReplies) { JsonObject jsonReply = new JsonObject(); jsonReply.put("id", reply.getId()); jsonReply.put("sone", reply.getSone().getId()); - jsonReply.put("post", reply.getPost().getId()); - jsonReply.put("postSone", reply.getPost().getSone().getId()); + jsonReply.put("post", reply.getPostId()); + jsonReply.put("postSone", reply.getPost().get().getSone().getId()); jsonReplies.add(jsonReply); } return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies); diff --combined src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java index 997c9dc,55ae553..4701118 --- a/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java @@@ -26,8 -26,6 +26,8 @@@ import net.pterodactylus.sone.web.WebIn import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.json.JsonObject; +import com.google.common.base.Optional; + /** * AJAX page that lets the user mark a number of {@link Sone}s, {@link Post}s, * or {@link Reply}s as known. @@@ -59,19 -57,19 +59,19 @@@ public class MarkAsKnownAjaxPage extend Core core = webInterface.getCore(); for (String id : ids) { if (type.equals("post")) { - Post post = core.getPost(id); - if (post == null) { + Optional post = core.getPost(id); + if (!post.isPresent()) { continue; } - core.markPostKnown(post); + core.markPostKnown(post.get()); } else if (type.equals("reply")) { - PostReply reply = core.getPostReply(id); - if (reply == null) { + Optional reply = core.getPostReply(id); + if (!reply.isPresent()) { continue; } - core.markReplyKnown(reply); + core.markReplyKnown(reply.get()); } else if (type.equals("sone")) { - Sone sone = core.getSone(id, false); + Sone sone = core.getSone(id); if (sone == null) { continue; }