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;
* @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();
}
/**
* @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
* or if {@code mandatory} is {@code true} and the Sone ID is
* invalid
*/
- protected Optional<Sone> getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException {
+ protected Optional<Sone> 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> 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<LocalSone> 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<LocalSone> 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;
* 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) {
+ protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Optional<LocalSone> localSone) {
SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
soneBuilder.put(prefix + "Name", sone.getName());
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;
/**
* FCP command that creates a new {@link Post}.
*
- * @see Core#createPost(Sone, Sone, String)
+ * @see Core#createPost(Sone, Optional, String)
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
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.");
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;
*/
@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);
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;
*/
@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);
*/
@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<Post> posts = sone.getPosts();
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;
*/
@Override
public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
- Sone sone = getSone(parameters, "Sone", false);
- Optional<Sone> localSone = getSone(parameters, "LocalSone", false, false);
+ Sone sone = getSone(parameters, "Sone");
+ Optional<LocalSone> localSone = getLocalSone(parameters, "LocalSone", false);
return new Response("Sone", encodeSone(sone, "", localSone));
}
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;
@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());
}
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;
@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());
}
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;
@Override
public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
- Optional<Sone> sone = getSone(parameters, "Sone", true, true);
+ Optional<LocalSone> sone = getLocalSone(parameters, "Sone", true);
getCore().lockSone(sone.get());
return new Response("SoneLocked", new SimpleFieldSetBuilder().put("Sone", sone.get().getId()).get());
}
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;
@Override
public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException {
- Optional<Sone> sone = getSone(parameters, "Sone", true, true);
+ Optional<LocalSone> sone = getLocalSone(parameters, "Sone", true);
getCore().unlockSone(sone.get());
return new Response("SoneUnlocked", new SimpleFieldSetBuilder().put("Sone", sone.get().getId()).get());
}
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;
public void testLockingARemoteSone() throws FcpException {
Sone remoteSone = mock(Sone.class);
Core core = mock(Core.class);
+ when(core.getLocalSone(anyString())).thenReturn(Optional.<LocalSone>absent());
when(core.getSone(eq("RemoteSone"))).thenReturn(Optional.of(remoteSone));
SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "RemoteSone").get();
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;
public void testUnlockingARemoteSone() throws FcpException {
Sone removeSone = mock(Sone.class);
Core core = mock(Core.class);
+ when(core.getLocalSone(anyString())).thenReturn(Optional.<LocalSone>absent());
when(core.getSone(eq("RemoteSone"))).thenReturn(Optional.of(removeSone));
SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "RemoteSone").get();