Move retrieval of post likes from Core to Post.
[Sone.git] / src / main / java / net / pterodactylus / sone / fcp / AbstractSoneCommand.java
index 87a76e2..baa3129 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - FcpInterface.java - Copyright © 2011 David Roden
+ * Sone - AbstractSoneCommand.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
 
 package net.pterodactylus.sone.fcp;
 
+import static com.google.common.collect.FluentIterable.from;
+import static net.pterodactylus.sone.data.Reply.FUTURE_REPLY_FILTER;
+
 import java.util.Collection;
 import java.util.List;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.PostReply;
+import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
 import net.pterodactylus.sone.freenet.fcp.Command;
 import net.pterodactylus.sone.freenet.fcp.FcpException;
 import net.pterodactylus.sone.template.SoneAccessor;
-import net.pterodactylus.util.filter.Filters;
+
 import freenet.node.FSParseException;
 import freenet.support.SimpleFieldSet;
 
+import com.google.common.base.Optional;
+
 /**
  * Abstract base implementation of a {@link Command} with Sone-related helper
  * methods.
@@ -44,6 +51,9 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
        /** The Sone core. */
        private final Core core;
 
+       /** Whether this command needs write access. */
+       private final boolean writeAccess;
+
        /**
         * Creates a new abstract Sone FCP command.
         *
@@ -51,7 +61,21 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
         *            The Sone core
         */
        protected AbstractSoneCommand(Core core) {
+               this(core, false);
+       }
+
+       /**
+        * Creates a new abstract Sone FCP command.
+        *
+        * @param core
+        *            The Sone core
+        * @param writeAccess
+        *            {@code true} if this command requires write access,
+        *            {@code false} otherwise
+        */
+       protected AbstractSoneCommand(Core core, boolean writeAccess) {
                this.core = core;
+               this.writeAccess = writeAccess;
        }
 
        //
@@ -67,37 +91,66 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
                return core;
        }
 
+       /**
+        * Returns whether this command requires write access.
+        *
+        * @return {@code true} if this command require write access, {@code false}
+        *         otherwise
+        */
+       public boolean requiresWriteAccess() {
+               return writeAccess;
+       }
+
        //
        // PROTECTED METHODS
        //
 
        /**
-        * Returns a Sone whose ID is a parameter in the given simple field set.
+        * Encodes text in a way that makes it possible for the text to be stored in
+        * a {@link SimpleFieldSet}. Backslashes, CR, and LF are prepended with a
+        * backslash.
         *
-        * @param simpleFieldSet
-        *            The simple field set containing the ID of the Sone
-        * @param parameterName
-        *            The name under which the Sone ID is stored in the simple field
-        *            set
-        * @param localOnly
-        *            {@code true} to only return local Sones, {@code false} to
-        *            return any Sones
-        * @return The Sone
-        * @throws FcpException
-        *             if there is no Sone ID stored under the given parameter name,
-        *             or if the Sone ID is invalid
+        * @param text
+        *            The text to encode
+        * @return The encoded text
         */
-       protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException {
-               try {
-                       String soneId = simpleFieldSet.getString(parameterName);
-                       Sone sone = localOnly ? core.getLocalSone(soneId, false) : core.getSone(soneId, false);
-                       if (sone == null) {
-                               throw new FcpException("Could not load Sone from “" + soneId + "”.");
-                       }
-                       return sone;
-               } catch (FSParseException fspe1) {
-                       throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
+       protected static String encodeString(String text) {
+               return text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
+       }
+
+       protected Optional<Sone> getOptionalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+               String soneId = getString(simpleFieldSet, parameterName, null);
+               return (soneId == null) ? Optional.<Sone>absent() : core.getSone(soneId);
+       }
+
+       protected Sone getMandatoryLocalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+               Sone sone = getMandatorySone(simpleFieldSet, parameterName);
+               if (!sone.isLocal()) {
+                       throw new FcpException("Could not load Sone from “" + sone.getId() + "”.");
                }
+               return sone;
+       }
+
+       protected Sone getMandatorySone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+               String soneId = getMandatoryParameter(simpleFieldSet, parameterName);
+               Optional<Sone> sone = getMandatorySone(soneId);
+               return sone.get();
+       }
+
+       private Optional<Sone> getMandatorySone(String soneId) throws FcpException {
+               Optional<Sone> sone = core.getSone(soneId);
+               if (!sone.isPresent()) {
+                       throw new FcpException("Could not load Sone from “" + soneId + "”.");
+               }
+               return sone;
+       }
+
+       private String getMandatoryParameter(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+               String soneId = simpleFieldSet.get(parameterName);
+               if (soneId == null) {
+                       throw new FcpException("Could not load Sone ID from “" + parameterName + "”.");
+               }
+               return soneId;
        }
 
        /**
@@ -116,11 +169,11 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
        protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
                try {
                        String postId = simpleFieldSet.getString(parameterName);
-                       Post post = core.getPost(postId, false);
-                       if (post == null) {
+                       Optional<Post> post = core.getDatabase().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);
                }
@@ -139,20 +192,55 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
         *             if there is no reply ID stored under the given parameter
         *             name, or if the reply ID is invalid
         */
-       protected Reply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+       protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
                try {
                        String replyId = simpleFieldSet.getString(parameterName);
-                       Reply reply = core.getReply(replyId, false);
-                       if (reply == null) {
+                       Optional<PostReply> reply = core.getDatabase().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);
                }
        }
 
        /**
+        * Creates a simple field set from the given Sone, including {@link Profile}
+        * information.
+        *
+        * @param sone
+        *            The Sone to encode
+        * @param prefix
+        *            The prefix for the field names (may be empty but not {@code
+        *            null})
+        * @param localSone
+        *            An optional local Sone that is used for Sone-specific data,
+        *            such as if the Sone is followed by the local Sone
+        * @return The simple field set containing the given Sone
+        */
+       protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional<Sone> localSone) {
+               SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
+
+               soneBuilder.put(prefix + "Name", sone.getName());
+               soneBuilder.put(prefix + "NiceName", SoneAccessor.getNiceName(sone));
+               soneBuilder.put(prefix + "LastUpdated", sone.getTime());
+               if (localSone.isPresent()) {
+                       soneBuilder.put(prefix + "Followed", String.valueOf(localSone.get().hasFriend(sone.getId())));
+               }
+               Profile profile = sone.getProfile();
+               soneBuilder.put(prefix + "Field.Count", profile.getFields().size());
+               int fieldIndex = 0;
+               for (Field field : profile.getFields()) {
+                       soneBuilder.put(prefix + "Field." + fieldIndex + ".Name", field.getName());
+                       soneBuilder.put(prefix + "Field." + fieldIndex + ".Value", field.getValue());
+                       ++fieldIndex;
+               }
+
+               return soneBuilder.get();
+       }
+
+       /**
         * Creates a simple field set from the given collection of Sones.
         *
         * @param sones
@@ -162,7 +250,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
         *            {@code null})
         * @return The simple field set containing the given Sones
         */
-       protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
+       protected static SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
                SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
 
                int soneIndex = 0;
@@ -178,63 +266,60 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
                return soneBuilder.get();
        }
 
-       /**
-        * Creates a simple field set from the given post.
-        *
-        * @param post
-        *            The post to encode
-        * @param prefix
-        *            The prefix for the field names (may be empty but not
-        *            {@code null})
-        * @param includeReplies
-        *            {@code true} to include replies, {@code false} to not include
-        *            replies
-        * @return The simple field set containing the post
-        */
-       protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
+       protected SimpleFieldSet encodePost(Post post, String prefix) {
+               return createPostBuilderFromPost(post, prefix).get();
+       }
+
+       protected SimpleFieldSet encodePostWithReplies(Post post, String prefix) {
+               SimpleFieldSetBuilder postBuilder = createPostBuilderFromPost(post, prefix);
+
+               List<PostReply> replies = from(post.getReplies()).filter(FUTURE_REPLY_FILTER).toList();
+               postBuilder.put(encodeReplies(replies, prefix));
+
+               return postBuilder.get();
+       }
+
+       private SimpleFieldSetBuilder createPostBuilderFromPost(Post post, String prefix) {
                SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
 
                postBuilder.put(prefix + "ID", post.getId());
                postBuilder.put(prefix + "Sone", post.getSone().getId());
-               if (post.getRecipient() != null) {
-                       postBuilder.put(prefix + "Recipient", post.getRecipient().getId());
+               if (post.getRecipientId().isPresent()) {
+                       postBuilder.put(prefix + "Recipient", post.getRecipientId().get());
                }
                postBuilder.put(prefix + "Time", post.getTime());
-               postBuilder.put(prefix + "Text", post.getText());
-               postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
+               postBuilder.put(prefix + "Text", encodeString(post.getText()));
+               postBuilder.put(encodeLikes(post.getLikes(), prefix + "Likes."));
 
-               if (includeReplies) {
-                       List<Reply> replies = core.getReplies(post);
-                       postBuilder.put(encodeReplies(replies, prefix));
-               }
+               return postBuilder;
+       }
+
+       protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix) {
+               SimpleFieldSetBuilder postBuilder = createPostBuilderFromPosts(posts, prefix);
 
                return postBuilder.get();
        }
 
-       /**
-        * Creates a simple field set from the given collection of posts.
-        *
-        * @param posts
-        *            The posts to encode
-        * @param prefix
-        *            The prefix for the field names (may be empty but not
-        *            {@code null})
-        * @param includeReplies
-        *            {@code true} to include the replies, {@code false} to not
-        *            include the replies
-        * @return The simple field set containing the posts
-        */
-       protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
+       private SimpleFieldSetBuilder createPostBuilderFromPosts(Collection<? extends Post> posts, String prefix) {
                SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
 
                int postIndex = 0;
                postBuilder.put(prefix + "Count", posts.size());
                for (Post post : posts) {
                        String postPrefix = prefix + postIndex++;
-                       postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
-                       if (includeReplies) {
-                               postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER), postPrefix + "."));
-                       }
+                       postBuilder.put(encodePost(post, postPrefix + "."));
+               }
+
+               return postBuilder;
+       }
+
+       protected SimpleFieldSet encodePostsWithReplies(Collection<? extends Post> posts, String prefix) {
+               SimpleFieldSetBuilder postBuilder = createPostBuilderFromPosts(posts, prefix);
+
+               int postIndex = 0;
+               for (Post post : posts) {
+                       String postPrefix = prefix + postIndex++;
+                       postBuilder.put(encodeReplies(from(post.getReplies()).filter(FUTURE_REPLY_FILTER).toList(), postPrefix + "."));
                }
 
                return postBuilder.get();
@@ -250,17 +335,17 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
         *            {@code null})
         * @return The simple field set containing the replies
         */
-       protected SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
+       protected static SimpleFieldSet encodeReplies(Collection<? extends PostReply> replies, String prefix) {
                SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
 
                int replyIndex = 0;
                replyBuilder.put(prefix + "Replies.Count", replies.size());
-               for (Reply reply : replies) {
+               for (PostReply reply : replies) {
                        String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
                        replyBuilder.put(replyPrefix + "ID", reply.getId());
                        replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
                        replyBuilder.put(replyPrefix + "Time", reply.getTime());
-                       replyBuilder.put(replyPrefix + "Text", reply.getText());
+                       replyBuilder.put(replyPrefix + "Text", encodeString(reply.getText()));
                }
 
                return replyBuilder.get();
@@ -277,7 +362,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
         *            {@code null})
         * @return The simple field set containing the likes
         */
-       protected SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
+       protected static SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
                SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
 
                int likeIndex = 0;
@@ -290,4 +375,13 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
                return likesBuilder.get();
        }
 
+       //
+       // OBJECT METHODS
+       //
+
+       @Override
+       public String toString() {
+               return getClass().getName() + "[writeAccess=" + writeAccess + "]";
+       }
+
 }