X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=bafe7a80406f5c730ff6876be6db07de5ec461c2;hp=2a1ef8eeb557927c2cb1f8f340d78d93106bb3e2;hb=d535f744751ba449e0f34fe3a42f1020988f14be;hpb=da609f721e54691f27113e877a19637bd332abc3 diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 2a1ef8e..bafe7a8 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -59,6 +59,8 @@ 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; @@ -66,7 +68,6 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.data.Sone.SoneStatus; import net.pterodactylus.sone.data.TemporaryImage; -import net.pterodactylus.sone.data.impl.PostReplyImpl; import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; import net.pterodactylus.sone.freenet.wot.Identity; @@ -100,7 +101,7 @@ import freenet.keys.FreenetURI; * * @author David ‘Bombe’ Roden */ -public class Core extends AbstractService implements SoneProvider, PostProvider { +public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider { /** The logger. */ private static final Logger logger = Logging.getLogger(Core.class); @@ -148,7 +149,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider private volatile FcpInterface fcpInterface; /** The times Sones were followed. */ - private final Map soneFollowingTimes = new HashMap(); + private final Map soneFollowingTimes = new HashMap(); /** Locked local Sones. */ /* synchronize on itself. */ @@ -178,6 +179,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider /** All known posts. */ private final Set knownPosts = new HashSet(); + /** The post reply builder factory. */ + private final PostReplyBuilderFactory postReplyBuilderFactory; + /** All replies. */ private final Map replies = new HashMap(); @@ -221,9 +225,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The event bus * @param postBuilderFactory * The post builder + * @param postReplyBuilderFactory + * The post reply builder factory */ @Inject - public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, PostBuilderFactory postBuilderFactory) { + public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, PostBuilderFactory postBuilderFactory, PostReplyBuilderFactory postReplyBuilderFactory) { super("Sone Core"); this.configuration = configuration; this.freenetInterface = freenetInterface; @@ -234,6 +240,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; this.postBuilderFactory = postBuilderFactory; + this.postReplyBuilderFactory = postReplyBuilderFactory; } // @@ -350,30 +357,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * @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); } } @@ -537,56 +523,44 @@ public class Core extends AbstractService implements SoneProvider, PostProvider } /** - * 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 the reply with the given ID. If there is no reply with the given - * ID yet, a new one is created, unless {@code create} is false in which - * case {@code null} is returned. + * Returns a post reply builder. * - * @param replyId - * The ID of the reply to get - * @param create - * {@code true} to always return a {@link Reply}, {@code false} - * to return {@code null} if no reply can be found - * @return The reply, or {@code null} if there is no such reply + * @return A new post reply builder + */ + public PostReplyBuilder postReplyBuilder() { + return postReplyBuilderFactory.newPostReplyBuilder(); + } + + /** + * {@inheritDoc} */ - public PostReply getPostReply(String replyId, boolean create) { + @Override + public PostReply getPostReply(String replyId) { synchronized (replies) { - PostReply reply = replies.get(replyId); - if (create && (reply == null)) { - reply = new PostReplyImpl(replyId); - replies.put(replyId, reply); - } - return reply; + return replies.get(replyId); } } /** - * 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 + * {@inheritDoc} */ + @Override public List getReplies(Post post) { Set sones = getSones(); List replies = new ArrayList(); @@ -851,7 +825,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption(true)); sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption(ShowCustomAvatars.NEVER)); - followSone(sone, getSone("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI")); + followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"); touchConfiguration(); return sone; } @@ -871,7 +845,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider synchronized (sones) { final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity); boolean newSone = sone.getRequestUri() == null; - sone.setRequestUri(getSoneUri(identity.getRequestUri())); + sone.setRequestUri(SoneUri.create(identity.getRequestUri())); sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); if (newSone) { synchronized (knownSones) { @@ -882,7 +856,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider eventBus.post(new NewSoneFoundEvent(sone)); for (Sone localSone : getLocalSones()) { if (localSone.getOptions().getBooleanOption("AutoFollow").get()) { - followSone(localSone, sone); + followSone(localSone, sone.getId()); } } } @@ -912,33 +886,15 @@ 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 followedSone = getSone(soneId, true); - if (followedSone == null) { - logger.log(Level.INFO, String.format("Ignored Sone with invalid ID: %s", soneId)); - return; - } - followSone(sone, getSone(soneId)); - } - - /** - * Lets the given local Sone follow the other given Sone. If the given Sone - * was not followed by any local Sone before, this will mark all elements of - * the followed Sone as read that have been created before the current - * moment. - * - * @param sone - * The local Sone that should follow the other Sone - * @param followedSone - * The Sone that should be followed - */ - public void followSone(Sone sone, Sone followedSone) { - checkNotNull(sone, "sone must not be null"); - checkNotNull(followedSone, "followedSone must not be null"); - sone.addFriend(followedSone.getId()); + sone.addFriend(soneId); synchronized (soneFollowingTimes) { - if (!soneFollowingTimes.containsKey(followedSone)) { + if (!soneFollowingTimes.containsKey(soneId)) { long now = System.currentTimeMillis(); - soneFollowingTimes.put(followedSone, now); + soneFollowingTimes.put(soneId, now); + Sone followedSone = getSone(soneId); + if (followedSone == null) { + return; + } for (Post post : followedSone.getPosts()) { if (post.getTime() < now) { markPostKnown(post); @@ -965,30 +921,14 @@ 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"); - unfollowSone(sone, getSone(soneId, false)); - } - - /** - * Lets the given local Sone unfollow the other given Sone. If the given - * local Sone is the last local Sone that followed the given Sone, its - * following time will be removed. - * - * @param sone - * The local Sone that should unfollow another Sone - * @param unfollowedSone - * The Sone being unfollowed - */ - public void unfollowSone(Sone sone, Sone unfollowedSone) { - checkNotNull(sone, "sone must not be null"); - checkNotNull(unfollowedSone, "unfollowedSone must not be null"); - sone.removeFriend(unfollowedSone.getId()); + sone.removeFriend(soneId); boolean unfollowedSoneStillFollowed = false; for (Sone localSone : getLocalSones()) { - unfollowedSoneStillFollowed |= localSone.hasFriend(unfollowedSone.getId()); + unfollowedSoneStillFollowed |= localSone.hasFriend(soneId); } if (!unfollowedSoneStillFollowed) { synchronized (soneFollowingTimes) { - soneFollowingTimes.remove(unfollowedSone); + soneFollowingTimes.remove(soneId); } } touchConfiguration(); @@ -1104,7 +1044,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider synchronized (knownPosts) { for (Post post : sone.getPosts()) { PostBuilder postBuilder = postBuilderFactory.newPostBuilder(); - postBuilder.copyPost(post).from(storedSone); + postBuilder.copyPost(post).from(storedSone.getId()); Post newPost = postBuilder.build().setKnown(knownPosts.contains(post.getId())); if (!storedPosts.contains(newPost)) { if (newPost.getTime() < getSoneFollowingTime(sone)) { @@ -1130,7 +1070,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider Set storedReplies = storedSone.getReplies(); synchronized (knownReplies) { for (PostReply reply : sone.getReplies()) { - reply.setSone(storedSone).setKnown(knownReplies.contains(reply.getId())); + reply.setKnown(knownReplies.contains(reply.getId())); if (!storedReplies.contains(reply)) { if (reply.getTime() < getSoneFollowingTime(sone)) { knownReplies.add(reply.getId()); @@ -1307,9 +1247,9 @@ 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).withTime(postTime).withText(postText); + PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { - postBuilder.to(getSone(postRecipientId)); + postBuilder.to(postRecipientId); } posts.add(postBuilder.build()); } @@ -1329,7 +1269,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider logger.log(Level.WARNING, "Invalid reply found, aborting load!"); return; } - replies.add(getPostReply(replyId, true).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText)); + PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder(); + postReplyBuilder.withId(replyId).from(sone.getId()).to(postId).withTime(replyTime).withText(replyText); + replies.add(postReplyBuilder.build()); } /* load post likes. */ @@ -1534,9 +1476,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider return null; } PostBuilder postBuilder = postBuilderFactory.newPostBuilder(); - postBuilder.from(sone).randomId().withTime(time).withText(text.trim()); + postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim()); if (recipient != null) { - postBuilder.to(recipient); + postBuilder.to(recipient.getId()); } final Post post = postBuilder.build(); synchronized (posts) { @@ -1654,30 +1596,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * @return The created reply */ public PostReply createReply(Sone sone, Post post, String text) { - return createReply(sone, post, System.currentTimeMillis(), text); - } - - /** - * Creates a new reply. - * - * @param sone - * The Sone that creates the reply - * @param post - * The post that this reply refers to - * @param time - * The time of the reply - * @param text - * The text of the reply - * @return The created reply - */ - public PostReply createReply(Sone sone, Post post, long time, String text) { checkNotNull(text, "text must not be null"); checkArgument(text.trim().length() > 0, "text must not be empty"); if (!sone.isLocal()) { logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone)); return null; } - final PostReply reply = new PostReplyImpl(sone, post, System.currentTimeMillis(), text.trim()); + PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder(); + postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim()); + final PostReply reply = postReplyBuilder.build(); synchronized (replies) { replies.put(reply.getId(), reply); } @@ -2133,8 +2060,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider /* save Sone following times. */ soneCounter = 0; synchronized (soneFollowingTimes) { - for (Entry soneFollowingTime : soneFollowingTimes.entrySet()) { - configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey().getId()); + for (Entry soneFollowingTime : soneFollowingTimes.entrySet()) { + configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey()); configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue()); ++soneCounter; } @@ -2257,7 +2184,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider logger.log(Level.WARNING, String.format("Ignoring Sone with invalid ID: %s", soneId)); } else { synchronized (soneFollowingTimes) { - soneFollowingTimes.put(getSone(soneId), time); + soneFollowingTimes.put(soneId, time); } } ++soneCounter; @@ -2317,23 +2244,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider } /** - * Generate a Sone URI from the given URI and latest edition. - * - * @param uriString - * The URI to derive the Sone URI from - * @return The derived URI - */ - private static FreenetURI getSoneUri(String uriString) { - try { - FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]); - return uri; - } catch (MalformedURLException mue1) { - logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uriString), mue1); - return null; - } - } - - /** * Notifies the core that a new {@link OwnIdentity} was added. * * @param ownIdentityAddedEvent @@ -2423,7 +2333,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider /* 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;