X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FAbstractSoneCommand.java;h=23f63a330fd32cf86da62c133c9d2098c1271e9d;hp=1294b4356ab40df16c558b665c2e7449c6644667;hb=62573c314957b1851f4fbe693b8746686caa940a;hpb=689c7065471aedd7f3830e012bb82ca8a0fa697d diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 1294b43..23f63a3 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -1,5 +1,5 @@ /* - * Sone - FcpInterface.java - Copyright © 2011 David Roden + * Sone - AbstractSoneCommand.java - Copyright © 2011–2016 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 @@ -22,22 +22,24 @@ 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. - * - * @author David ‘Bombe’ Roden */ public abstract class AbstractSoneCommand extends AbstractCommand { @@ -107,8 +109,8 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * The text to encode * @return The encoded text */ - protected String encodeString(String text) { - return text.replaceAll("\\\\", "\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r"); + protected static String encodeString(String text) { + return text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r"); } /** @@ -128,7 +130,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * or if the Sone ID is invalid */ protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException { - return getSone(simpleFieldSet, parameterName, localOnly, true); + return getSone(simpleFieldSet, parameterName, localOnly, true).get(); } /** @@ -152,16 +154,16 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * or if {@code mandatory} is {@code true} and the Sone ID is * invalid */ - protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException { + protected Optional getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException { String soneId = simpleFieldSet.get(parameterName); 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); - if (mandatory && (sone == null)) { + Sone sone = core.getSone(soneId); + if ((mandatory && (sone == null)) || ((sone != null) && localOnly && !sone.isLocal())) { throw new FcpException("Could not load Sone from “" + soneId + "”."); } - return sone; + return Optional.fromNullable(sone); } /** @@ -180,7 +182,7 @@ 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); + Post post = core.getPost(postId); if (post == null) { throw new FcpException("Could not load post from “" + postId + "”."); } @@ -203,10 +205,10 @@ 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); + PostReply reply = core.getPostReply(replyId); if (reply == null) { throw new FcpException("Could not load reply from “" + replyId + "”."); } @@ -217,6 +219,42 @@ public abstract class AbstractSoneCommand extends AbstractCommand { } /** + * 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 localSone) { + SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder(); + + soneBuilder.put(prefix + "ID", sone.getId()); + 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 @@ -226,17 +264,14 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * {@code null}) * @return The simple field set containing the given Sones */ - protected SimpleFieldSet encodeSones(Collection sones, String prefix) { + protected static SimpleFieldSet encodeSones(Collection sones, String prefix) { SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder(); int soneIndex = 0; soneBuilder.put(prefix + "Count", sones.size()); for (Sone sone : sones) { String sonePrefix = prefix + soneIndex++ + "."; - soneBuilder.put(sonePrefix + "ID", sone.getId()); - soneBuilder.put(sonePrefix + "Name", sone.getName()); - soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone)); - soneBuilder.put(sonePrefix + "Time", sone.getTime()); + soneBuilder.put(encodeSone(sone, sonePrefix, Optional.absent())); } return soneBuilder.get(); @@ -260,15 +295,15 @@ public abstract class AbstractSoneCommand extends AbstractCommand { 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", encodeString(post.getText())); postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes.")); if (includeReplies) { - List replies = core.getReplies(post); + List replies = core.getReplies(post.getId()); postBuilder.put(encodeReplies(replies, prefix)); } @@ -296,9 +331,6 @@ public abstract class AbstractSoneCommand extends AbstractCommand { 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 + ".")); - } } return postBuilder.get(); @@ -314,17 +346,18 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * {@code null}) * @return The simple field set containing the replies */ - protected SimpleFieldSet encodeReplies(Collection replies, String prefix) { + protected SimpleFieldSet encodeReplies(Collection 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", encodeString(reply.getText())); + replyBuilder.put(encodeLikes(core.getLikes(reply), replyPrefix + "Likes.")); } return replyBuilder.get(); @@ -341,7 +374,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * {@code null}) * @return The simple field set containing the likes */ - protected SimpleFieldSet encodeLikes(Collection likes, String prefix) { + protected static SimpleFieldSet encodeLikes(Collection likes, String prefix) { SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder(); int likeIndex = 0;