From: David ‘Bombe’ Roden Date: Tue, 2 Dec 2014 21:32:21 +0000 (+0100) Subject: Distinguish between local and “normal” Sones in FCP handler. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=30ef483c663ded175b66b50b652a0497e04bd493;p=Sone.git Distinguish between local and “normal” Sones in FCP handler. --- diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index d71ffc4..6e2253d 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -21,6 +21,7 @@ import java.util.Collection; import java.util.List; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; @@ -125,16 +126,30 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * @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 */ - protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException { - return getSone(simpleFieldSet, parameterName, localOnly, true).get(); + protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { + return getSone(simpleFieldSet, parameterName, true).get(); + } + + /** + * Returns a Sone whose ID is a parameter in the given simple field set. + * + * @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 + * @return The Sone + * @throws FcpException + * if there is no Sone ID stored under the given parameter name, + * or if the Sone ID is invalid + */ + protected LocalSone getLocalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { + return getLocalSone(simpleFieldSet, parameterName, true).get(); } /** @@ -145,9 +160,6 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * @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 * @param mandatory * {@code true} if a valid Sone ID is required, {@code false} * otherwise @@ -158,13 +170,43 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * or if {@code mandatory} is {@code true} and the Sone ID is * invalid */ - protected Optional getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException { + protected Optional getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean mandatory) throws FcpException { String soneId = simpleFieldSet.get(parameterName); if (mandatory && (soneId == null)) { throw new FcpException("Could not load Sone ID from “" + parameterName + "”."); } Optional sone = core.getSone(soneId); - if ((mandatory && !sone.isPresent()) || (mandatory && sone.isPresent() && (localOnly && !sone.get().isLocal()))) { + if ((mandatory && !sone.isPresent()) || (mandatory && sone.isPresent())) { + throw new FcpException("Could not load Sone from “" + soneId + "”."); + } + return sone; + } + + /** + * Returns a Sone whose ID is a parameter in the given simple field set. + * + * @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 mandatory + * {@code true} if a valid Sone ID is required, {@code false} + * otherwise + * @return The Sone, or {@code null} if {@code mandatory} is {@code false} + * and the Sone ID is invalid + * @throws FcpException + * if there is no Sone ID stored under the given parameter name, + * or if {@code mandatory} is {@code true} and the Sone ID is + * invalid + */ + protected Optional getLocalSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean mandatory) throws FcpException { + String soneId = simpleFieldSet.get(parameterName); + if (mandatory && (soneId == null)) { + throw new FcpException("Could not load Sone ID from “" + parameterName + "”."); + } + Optional sone = core.getLocalSone(soneId); + if ((mandatory && !sone.isPresent()) || (mandatory && sone.isPresent() && !sone.get().isLocal())) { throw new FcpException("Could not load Sone from “" + soneId + "”."); } return sone; @@ -236,7 +278,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand { * 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) { + protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional localSone) { SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder(); soneBuilder.put(prefix + "Name", sone.getName()); diff --git a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java index f6c5dd9..97a2d45 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java @@ -20,6 +20,7 @@ package net.pterodactylus.sone.fcp; import com.google.common.base.Optional; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; @@ -30,7 +31,7 @@ import freenet.support.api.Bucket; /** * FCP command that creates a new {@link Post}. * - * @see Core#createPost(Sone, Sone, String) + * @see Core#createPost(Sone, Optional, String) * @author David ‘Bombe’ Roden */ public class CreatePostCommand extends AbstractSoneCommand { @@ -50,11 +51,11 @@ public class CreatePostCommand extends AbstractSoneCommand { */ @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", true); + LocalSone sone = getLocalSone(parameters, "Sone", true).get(); String text = getString(parameters, "Text"); Sone recipient = null; if (parameters.get("Recipient") != null) { - recipient = getSone(parameters, "Recipient", false); + recipient = getSone(parameters, "Recipient"); } if (sone.equals(recipient)) { return new ErrorResponse("Sone and Recipient must not be the same."); diff --git a/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java b/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java index fe4bee8..39fc346 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/CreateReplyCommand.java @@ -18,6 +18,7 @@ package net.pterodactylus.sone.fcp; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Reply; @@ -50,7 +51,7 @@ public class CreateReplyCommand extends AbstractSoneCommand { */ @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", true); + LocalSone sone = getLocalSone(parameters, "Sone", true).get(); Post post = getPost(parameters, "Post"); String text = getString(parameters, "Text"); PostReply reply = getCore().createReply(sone, post, text); diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java index b22d356..dbaf189 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java @@ -24,6 +24,7 @@ import java.util.HashSet; import java.util.List; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.fcp.FcpException; @@ -57,7 +58,7 @@ public class GetPostFeedCommand extends AbstractSoneCommand { */ @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", true); + LocalSone sone = getLocalSone(parameters, "Sone", true).get(); int startPost = getInt(parameters, "StartPost", 0); int maxPosts = getInt(parameters, "MaxPosts", -1); diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java index 1947718..8d7a54f 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostsCommand.java @@ -50,7 +50,7 @@ public class GetPostsCommand extends AbstractSoneCommand { */ @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", false); + Sone sone = getSone(parameters, "Sone"); int startPost = getInt(parameters, "StartPost", 0); int maxPosts = getInt(parameters, "MaxPosts", -1); List posts = sone.getPosts(); diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetSoneCommand.java index e3ec1b1..33f7bad 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetSoneCommand.java @@ -18,6 +18,7 @@ package net.pterodactylus.sone.fcp; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.fcp.FcpException; @@ -50,8 +51,8 @@ public class GetSoneCommand extends AbstractSoneCommand { */ @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Sone sone = getSone(parameters, "Sone", false); - Optional localSone = getSone(parameters, "LocalSone", false, false); + Sone sone = getSone(parameters, "Sone"); + Optional localSone = getLocalSone(parameters, "LocalSone", false); return new Response("Sone", encodeSone(sone, "", localSone)); } diff --git a/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java index 20d251c..37a081d 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java @@ -18,8 +18,8 @@ package net.pterodactylus.sone.fcp; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; import net.pterodactylus.sone.freenet.fcp.FcpException; import freenet.support.SimpleFieldSet; @@ -48,7 +48,7 @@ public class LikePostCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { Post post = getPost(parameters, "Post"); - Sone sone = getSone(parameters, "Sone", true); + LocalSone sone = getLocalSone(parameters, "Sone"); sone.addLikedPostId(post.getId()); return new Response("PostLiked", new SimpleFieldSetBuilder().put("LikeCount", getCore().getLikes(post).size()).get()); } diff --git a/src/main/java/net/pterodactylus/sone/fcp/LikeReplyCommand.java b/src/main/java/net/pterodactylus/sone/fcp/LikeReplyCommand.java index a462f08..498cfe3 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/LikeReplyCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/LikeReplyCommand.java @@ -18,8 +18,8 @@ package net.pterodactylus.sone.fcp; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; import net.pterodactylus.sone.freenet.fcp.FcpException; import freenet.support.SimpleFieldSet; @@ -48,7 +48,7 @@ public class LikeReplyCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { PostReply reply = getReply(parameters, "Reply"); - Sone sone = getSone(parameters, "Sone", true); + LocalSone sone = getLocalSone(parameters, "Sone"); sone.addLikedReplyId(reply.getId()); return new Response("ReplyLiked", new SimpleFieldSetBuilder().put("LikeCount", getCore().getLikes(reply).size()).get()); } diff --git a/src/main/java/net/pterodactylus/sone/fcp/LockSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/LockSoneCommand.java index 5cad8ca..e877a74 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/LockSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/LockSoneCommand.java @@ -18,7 +18,7 @@ package net.pterodactylus.sone.fcp; import net.pterodactylus.sone.core.Core; -import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; import net.pterodactylus.sone.freenet.fcp.FcpException; @@ -52,7 +52,7 @@ public class LockSoneCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Optional sone = getSone(parameters, "Sone", true, true); + Optional sone = getLocalSone(parameters, "Sone", true); getCore().lockSone(sone.get()); return new Response("SoneLocked", new SimpleFieldSetBuilder().put("Sone", sone.get().getId()).get()); } diff --git a/src/main/java/net/pterodactylus/sone/fcp/UnlockSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/UnlockSoneCommand.java index 7ecfada..dc6058d 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/UnlockSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/UnlockSoneCommand.java @@ -18,7 +18,7 @@ package net.pterodactylus.sone.fcp; import net.pterodactylus.sone.core.Core; -import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; import net.pterodactylus.sone.freenet.fcp.FcpException; @@ -52,7 +52,7 @@ public class UnlockSoneCommand extends AbstractSoneCommand { @Override public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { - Optional sone = getSone(parameters, "Sone", true, true); + Optional sone = getLocalSone(parameters, "Sone", true); getCore().unlockSone(sone.get()); return new Response("SoneUnlocked", new SimpleFieldSetBuilder().put("Sone", sone.get().getId()).get()); } diff --git a/src/test/java/net/pterodactylus/sone/fcp/LockSoneCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/LockSoneCommandTest.java index 5932c31..7132637 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/LockSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/LockSoneCommandTest.java @@ -20,6 +20,7 @@ package net.pterodactylus.sone.fcp; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -68,6 +69,7 @@ public class LockSoneCommandTest { public void testLockingARemoteSone() throws FcpException { Sone remoteSone = mock(Sone.class); Core core = mock(Core.class); + when(core.getLocalSone(anyString())).thenReturn(Optional.absent()); when(core.getSone(eq("RemoteSone"))).thenReturn(Optional.of(remoteSone)); SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "RemoteSone").get(); diff --git a/src/test/java/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.java index 48f5aa1..f825dbe 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.java @@ -20,6 +20,7 @@ package net.pterodactylus.sone.fcp; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -68,6 +69,7 @@ public class UnlockSoneCommandTest { public void testUnlockingARemoteSone() throws FcpException { Sone removeSone = mock(Sone.class); Core core = mock(Core.class); + when(core.getLocalSone(anyString())).thenReturn(Optional.absent()); when(core.getSone(eq("RemoteSone"))).thenReturn(Optional.of(removeSone)); SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "RemoteSone").get();