Add callback interface to post build() method.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 15 Oct 2013 19:51:23 +0000 (21:51 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:26 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java
src/main/java/net/pterodactylus/sone/database/PostBuilder.java
src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java
src/main/java/net/pterodactylus/sone/web/CreatePostPage.java
src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java

index 898e314..e9ebfda 100644 (file)
@@ -17,6 +17,7 @@
 
 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;
@@ -70,6 +71,7 @@ import net.pterodactylus.sone.data.TemporaryImage;
 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;
@@ -1068,7 +1070,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
                                postBuilder.to(of(postRecipientId));
                        }
-                       posts.add(postBuilder.build());
+                       posts.add(postBuilder.build(Optional.<PostCreated>absent()));
                }
 
                /* load replies. */
@@ -1949,4 +1951,24 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                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);
+                               }
+                       }
+               };
+       }
+
 }
index 640a3a0..ed8ad98 100644 (file)
@@ -38,6 +38,7 @@ import net.pterodactylus.sone.data.Sone;
 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;
@@ -50,6 +51,7 @@ import freenet.client.FetchResult;
 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;
 
@@ -376,7 +378,7 @@ public class SoneDownloader extends AbstractService {
                                        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));
index 1aa1596..a07f144 100644 (file)
@@ -23,6 +23,8 @@ import net.pterodactylus.sone.data.Post;
 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.
  *
@@ -41,9 +43,13 @@ public class DefaultPostBuilder extends AbstractPostBuilder {
 
        /** {@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;
        }
 
 }
index 800fd0a..d0c5582 100644 (file)
@@ -44,6 +44,7 @@ import net.pterodactylus.util.logging.Logging;
 
 import freenet.keys.FreenetURI;
 
+import com.google.common.base.Optional;
 import com.google.common.hash.Hasher;
 import com.google.common.hash.Hashing;
 
@@ -680,8 +681,8 @@ public class DefaultSone implements Sone {
        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;
                        }
index 0114ac5..5564a93 100644 (file)
@@ -121,6 +121,12 @@ public interface PostBuilder {
         * @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);
+
+       }
 
 }
index ae45957..81dff2c 100644 (file)
 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;
 
@@ -62,7 +61,7 @@ public class CreatePostCommand extends AbstractSoneCommand {
                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());
        }
 
index 032271e..647b3ae 100644 (file)
@@ -72,7 +72,7 @@ public class CreatePostPage extends SoneTemplatePage {
                                }
                                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);
index bf67db7..7941a53 100644 (file)
@@ -20,7 +20,6 @@ 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;
@@ -67,7 +66,7 @@ public class CreatePostAjaxPage extends JsonPage {
                        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());
        }