From: David ‘Bombe’ Roden Date: Tue, 15 Oct 2013 19:51:23 +0000 (+0200) Subject: Add callback interface to post build() method. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=cf032630b167166215f45d8ae2e5147e1934c223;p=Sone.git Add callback interface to post build() method. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 898e314..e9ebfda 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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.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); + } + } + }; + } + } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 640a3a0..ed8ad98 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -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.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)); diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java index 1aa1596..a07f144 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPostBuilder.java @@ -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) { 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; } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java index 800fd0a..d0c5582 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java @@ -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) { + Post post = super.build(postCreated); database.storePost(post); return post; } diff --git a/src/main/java/net/pterodactylus/sone/database/PostBuilder.java b/src/main/java/net/pterodactylus/sone/database/PostBuilder.java index 0114ac5..5564a93 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/PostBuilder.java @@ -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) throws IllegalStateException; + + interface PostCreated { + + void postCreated(Post post); + + } } diff --git a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java index ae45957..81dff2c 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java @@ -18,16 +18,15 @@ 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()); } diff --git a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java index 032271e..647b3ae 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java @@ -72,7 +72,7 @@ public class CreatePostPage extends SoneTemplatePage { } Optional 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); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java index bf67db7..7941a53 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java @@ -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()); }