package net.pterodactylus.sone.core;
+import static com.google.common.base.Optional.absent;
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 net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.DatabaseException;
import net.pterodactylus.sone.database.PostBuilder;
+import net.pterodactylus.sone.database.PostBuilder.PostCreated;
import net.pterodactylus.sone.database.PostProvider;
import net.pterodactylus.sone.database.PostReplyBuilder;
import net.pterodactylus.sone.database.PostReplyProvider;
if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
postBuilder.to(of(postRecipientId));
}
- posts.add(postBuilder.build());
+ posts.add(postBuilder.build(Optional.<PostCreated>absent()));
}
/* load replies. */
touchConfiguration();
}
+ public PostCreated postCreated() {
+ return new PostCreated() {
+ @Override
+ public void postCreated(final Post post) {
+ if (post.isKnown()) {
+ return;
+ }
+ eventBus.post(new NewPostFoundEvent(post));
+ if (post.getSone().isLocal()) {
+ localElementTicker.schedule(new Runnable() {
+ @Override
+ public void run() {
+ markPostKnown(post);
+ }
+ }, 10, TimeUnit.SECONDS);
+ }
+ }
+ };
+ }
+
}
import net.pterodactylus.sone.data.Sone.SoneStatus;
import net.pterodactylus.sone.data.impl.DefaultSone;
import net.pterodactylus.sone.database.PostBuilder;
+import net.pterodactylus.sone.database.PostBuilder.PostCreated;
import net.pterodactylus.sone.database.PostReplyBuilder;
import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
import freenet.keys.FreenetURI;
import freenet.support.api.Bucket;
+import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import org.w3c.dom.Document;
if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
postBuilder.to(of(postRecipientId));
}
- posts.add(postBuilder.build());
+ posts.add(postBuilder.build(Optional.<PostCreated>absent()));
} catch (NumberFormatException nfe1) {
/* TODO - mark Sone as bad. */
logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime));
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.PostBuilder;
+import com.google.common.base.Optional;
+
/**
* {@link PostBuilder} implementation that creates {@link PostImpl} objects.
*
/** {@inheritDoc} */
@Override
- public Post build() {
+ public Post build(Optional<PostCreated> postCreated) {
validate();
- return new PostImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId.orNull(), currentTime ? System.currentTimeMillis() : time, text);
+ PostImpl post = new PostImpl(database, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId.orNull(), currentTime ? System.currentTimeMillis() : time, text);
+ if (postCreated.isPresent()) {
+ postCreated.get().postCreated(post);
+ }
+ return post;
}
}
import freenet.keys.FreenetURI;
+import com.google.common.base.Optional;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
public PostBuilder newPostBuilder() {
return new DefaultPostBuilder(database, getId()) {
@Override
- public Post build() {
- Post post = super.build();
+ public Post build(Optional<PostCreated> postCreated) {
+ Post post = super.build(postCreated);
database.storePost(post);
return post;
}
* @throws IllegalStateException
* if this builder’s configuration is not valid
*/
- public Post build() throws IllegalStateException;
+ public Post build(Optional<PostCreated> postCreated) throws IllegalStateException;
+
+ interface PostCreated {
+
+ void postCreated(Post post);
+
+ }
}
package net.pterodactylus.sone.fcp;
import static com.google.common.base.Optional.fromNullable;
+import static com.google.common.base.Optional.of;
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;
import net.pterodactylus.sone.freenet.fcp.FcpException;
+
import freenet.support.SimpleFieldSet;
import freenet.support.api.Bucket;
if (sone.equals(recipient)) {
return new ErrorResponse("Sone and Recipient must not be the same.");
}
- Post post = sone.newPostBuilder().randomId().currentTime().to(fromNullable(recipient).transform(GET_ID)).withText(text).build();
+ Post post = sone.newPostBuilder().randomId().currentTime().to(fromNullable(recipient).transform(GET_ID)).withText(text).build(of(getCore().postCreated()));
return new Response("PostCreated", new SimpleFieldSetBuilder().put("Post", post.getId()).get());
}
}
Optional<Sone> recipient = webInterface.getCore().getSone(recipientId);
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(Identified.GET_ID)).withText(text).build();
+ sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(Identified.GET_ID)).withText(text).build(of(webInterface.getCore().postCreated()));
throw new RedirectException(returnPage);
}
templateContext.set("errorTextEmpty", true);
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 = sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(GET_ID)).withText(text).build();
+ Post newPost = sender.get().newPostBuilder().randomId().currentTime().to(recipient.transform(GET_ID)).withText(text).build(of(webInterface.getCore().postCreated()));
return createSuccessJsonObject().put("postId", newPost.getId()).put("sone", sender.get().getId()).put("recipient", newPost.getRecipientId().orNull());
}