package net.pterodactylus.sone.core;
+import static com.google.common.base.Optional.of;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.not;
}
PostBuilder postBuilder = sone.newPostBuilder().withId(postId).withTime(postTime).withText(postText);
if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
- postBuilder.to(postRecipientId);
+ postBuilder.to(of(postRecipientId));
}
posts.add(postBuilder.build());
}
}
/**
- * Creates a new post.
- *
- * @param sone
- * The Sone that creates the post
- * @param text
- * The text of the post
- * @return The created post
- */
- public Post createPost(Sone sone, String text) {
- return createPost(sone, System.currentTimeMillis(), text);
- }
-
- /**
- * Creates a new post.
- *
- * @param sone
- * The Sone that creates the post
- * @param time
- * The time of the post
- * @param text
- * The text of the post
- * @return The created post
- */
- public Post createPost(Sone sone, long time, String text) {
- return createPost(sone, null, time, text);
- }
-
- /**
- * Creates a new post.
- *
- * @param sone
- * The Sone that creates the post
- * @param recipient
- * The recipient Sone, or {@code null} if this post does not have a
- * recipient
- * @param text
- * The text of the post
- * @return The created post
- */
- public Post createPost(Sone sone, Optional<Sone> recipient, String text) {
- return createPost(sone, recipient, System.currentTimeMillis(), text);
- }
-
- /**
- * Creates a new post.
- *
- * @param sone
- * The Sone that creates the post
- * @param recipient
- * The recipient Sone, or {@code null} if this post does not have a
- * recipient
- * @param time
- * The time of the post
- * @param text
- * The text of the post
- * @return The created post
- */
- public Post createPost(Sone sone, Optional<Sone> recipient, long time, String text) {
- checkNotNull(text, "text must not be null");
- checkArgument(text.trim().length() > 0, "text must not be empty");
- if (!sone.isLocal()) {
- logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
- return null;
- }
- PostBuilder postBuilder = sone.newPostBuilder();
- postBuilder.randomId().withTime(time).withText(text.trim());
- if (recipient.isPresent()) {
- postBuilder.to(recipient.get().getId());
- }
- final Post post = postBuilder.build();
- database.storePost(post);
- eventBus.post(new NewPostFoundEvent(post));
- sone.addPost(post);
- touchConfiguration();
- localElementTicker.schedule(new Runnable() {
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void run() {
- markPostKnown(post);
- }
- }, 10, TimeUnit.SECONDS);
- return post;
- }
-
- /**
* Deletes the given post.
*
* @param post
package net.pterodactylus.sone.core;
+import static com.google.common.base.Optional.of;
+
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.HashSet;
/* TODO - parse time correctly. */
postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
- postBuilder.to(postRecipientId);
+ postBuilder.to(of(postRecipientId));
}
posts.add(postBuilder.build());
} catch (NumberFormatException nfe1) {
package net.pterodactylus.sone.data.impl;
+import static com.google.common.base.Optional.absent;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.PostBuilder;
+import com.google.common.base.Optional;
import org.apache.commons.lang.StringUtils;
/**
protected String text;
/** The (optional) recipient of the post. */
- protected String recipientId;
+ protected Optional<String> recipientId = absent();
protected AbstractPostBuilder(Database database, String soneId) {
this.database = checkNotNull(database, "database must not be null");
* {@inheritDoc}
*/
@Override
- public PostBuilder to(String recipientId) {
+ public PostBuilder to(Optional<String> recipientId) {
this.recipientId = recipientId;
return this;
}
checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
checkState((currentTime && (time == 0)) || (!currentTime && (time > 0)), "one of current time or custom time must be set");
checkState(!StringUtils.isBlank(text), "text must not be empty");
- checkState(!senderId.equals(recipientId), "sender and recipient must not be the same");
+ checkState(!recipientId.isPresent() || !senderId.equals(recipientId.get()), "sender and recipient must not be the same");
}
}
@Override
public Post build() {
validate();
- return new PostImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
+ return new PostImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId.orNull(), currentTime ? System.currentTimeMillis() : time, text);
}
}
}
public PostBuilder newPostBuilder() {
- return new DefaultPostBuilder(database, getId());
+ return new DefaultPostBuilder(database, getId()) {
+ @Override
+ public Post build() {
+ Post post = super.build();
+ database.storePost(post);
+ return post;
+ }
+ };
}
//
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
+import com.google.common.base.Optional;
+
/**
* Builder for {@link Post} objects.
* <p>
* The ID of the recipient of the post
* @return This post builder
*/
- public PostBuilder to(String recipientId);
+ public PostBuilder to(Optional<String> recipientId);
/**
* Verifies this builder’s configuration and creates a new post.
package net.pterodactylus.sone.fcp;
+import static com.google.common.base.Optional.fromNullable;
+import static net.pterodactylus.sone.data.Identified.GET_ID;
+
import com.google.common.base.Optional;
import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Identified;
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)
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
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 = sone.newPostBuilder().randomId().currentTime().to(fromNullable(recipient).transform(GET_ID)).withText(text).build();
return new Response("PostCreated", new SimpleFieldSetBuilder().put("Post", post.getId()).get());
}
import static com.google.common.base.Optional.of;
+import net.pterodactylus.sone.data.Identified;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.text.TextFilter;
}
Optional<Sone> recipient = webInterface.getCore().getSone(recipientId);
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- webInterface.getCore().createPost(sender.get(), recipient, System.currentTimeMillis(), text);
+ sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(Identified.GET_ID)).withText(text).build();
throw new RedirectException(returnPage);
}
templateContext.set("errorTextEmpty", true);
package net.pterodactylus.sone.web.ajax;
import static com.google.common.base.Optional.of;
+import static net.pterodactylus.sone.data.Identified.GET_ID;
+import net.pterodactylus.sone.data.Identified;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.text.TextFilter;
return createErrorJsonObject("text-required");
}
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- Post newPost = webInterface.getCore().createPost(sender.get(), recipient, text);
+ Post newPost = sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(GET_ID)).withText(text).build();
return createSuccessJsonObject().put("postId", newPost.getId()).put("sone", sender.get().getId()).put("recipient", newPost.getRecipientId().orNull());
}