import net.pterodactylus.util.thread.NamedThreadFactory;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
* The text of the post
* @return The created post
*/
- public Post createPost(Sone sone, Optional<Sone> recipient, String text) {
+ public Post createPost(Sone sone, @Nullable Sone recipient, String text) {
checkNotNull(text, "text must not be null");
checkArgument(text.trim().length() > 0, "text must not be empty");
if (!sone.isLocal()) {
}
PostBuilder postBuilder = database.newPostBuilder();
postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim());
- if (recipient.isPresent()) {
- postBuilder.to(recipient.get().getId());
+ if (recipient != null) {
+ postBuilder.to(recipient.getId());
}
final Post post = postBuilder.build();
database.storePost(post);
/**
* FCP command that creates a new {@link Post}.
*
- * @see Core#createPost(Sone, Optional, String)
+ * @see Core#createPost(Sone, Sone, String)
*/
public class CreatePostCommand extends AbstractSoneCommand {
if (sone.equals(recipient)) {
return new ErrorResponse("Sone and Recipient must not be the same.");
}
- Post post = getCore().createPost(sone, Optional.fromNullable(recipient), text);
+ Post post = getCore().createPost(sone, recipient, text);
return new Response("PostCreated", new SimpleFieldSetBuilder().put("Post", post.getId()).get());
}
?.let { text ->
val sender = request.parameters["sender"].emptyToNull?.let(core::getSone) ?: currentSone
val recipient = request.parameters["recipient"]?.let(core::getSone)
- core.createPost(sender, recipient.asOptional(), text).let { post ->
+ core.createPost(sender, recipient, text).let { post ->
createSuccessJsonObject().apply {
put("postId", post.id)
put("sone", sender.id)
}
val sender = soneRequest.core.getLocalSone(soneRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone
val recipient = soneRequest.core.getSone(soneRequest.httpRequest.getPartAsStringFailsafe("recipient", 43))
- soneRequest.core.createPost(sender, recipient.asOptional(), TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text))
+ soneRequest.core.createPost(sender, recipient, TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text))
throw RedirectException(returnPage)
}
}
parameters += "Text" to "Test"
whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
- whenever(core.createPost(localSone, absent(), "Test")).thenReturn(post)
+ whenever(core.createPost(localSone, null, "Test")).thenReturn(post)
val response = command.execute(parameters)
assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
assertThat(response.replyParameters.get("Post"), equalTo("PostId"))
whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
- whenever(core.createPost(localSone, of(remoteSone), "Test")).thenReturn(post)
+ whenever(core.createPost(localSone, remoteSone, "Test")).thenReturn(post)
val response = command.execute(parameters)
assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
assertThat(response.replyParameters.get("Post"), equalTo("PostId"))
fun `request with valid data creates post`() {
addRequestParameter("text", "test")
val post = createPost()
- whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post)
+ whenever(core.createPost(currentSone, null, "test")).thenReturn(post)
assertThatJsonIsSuccessful()
assertThat(json["postId"]?.asText(), equalTo("id"))
assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
addRequestParameter("text", "test")
addRequestParameter("recipient", "invalid")
val post = createPost()
- whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post)
+ whenever(core.createPost(currentSone, null, "test")).thenReturn(post)
assertThatJsonIsSuccessful()
assertThat(json["postId"]?.asText(), equalTo("id"))
assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
val recipient = mock<Sone>().apply { whenever(id).thenReturn("valid") }
addSone(recipient)
val post = createPost("valid")
- whenever(core.createPost(currentSone, Optional.of(recipient), "test")).thenReturn(post)
+ whenever(core.createPost(currentSone, recipient, "test")).thenReturn(post)
assertThatJsonIsSuccessful()
assertThat(json["postId"]?.asText(), equalTo("id"))
assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
addRequestParameter("text", "Link http://freenet.test:8888/KSK@foo is filtered")
addRequestHeader("Host", "freenet.test:8888")
val post = createPost()
- whenever(core.createPost(currentSone, Optional.absent(), "Link KSK@foo is filtered")).thenReturn(post)
+ whenever(core.createPost(currentSone, null, "Link KSK@foo is filtered")).thenReturn(post)
assertThatJsonIsSuccessful()
assertThat(json["postId"]?.asText(), equalTo("id"))
assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("text", "post text")
verifyRedirect("return.html") {
- verify(core).createPost(currentSone, absent(), "post text")
+ verify(core).createPost(currentSone, null, "post text")
}
}
val sender = mock<Sone>()
addLocalSone("sender-id", sender)
verifyRedirect("return.html") {
- verify(core).createPost(sender, absent(), "post text")
+ verify(core).createPost(sender, null, "post text")
}
}
val recipient = mock<Sone>()
addSone("recipient-id", recipient)
verifyRedirect("return.html") {
- verify(core).createPost(currentSone, recipient.asOptional(), "post text")
+ verify(core).createPost(currentSone, recipient, "post text")
}
}
addHttpRequestPart("text", "post http://localhost:12345/KSK@foo text")
addHttpRequestHeader("Host", "localhost:12345")
verifyRedirect("return.html") {
- verify(core).createPost(currentSone, absent(), "post KSK@foo text")
+ verify(core).createPost(currentSone, null, "post KSK@foo text")
}
}