Rename post and reply implementations; use builder to create replies.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 14667de..7f74e14 100644 (file)
@@ -17,6 +17,7 @@
 
 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;
@@ -69,8 +70,10 @@ 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.PostReplyBuilder.PostReplyCreated;
 import net.pterodactylus.sone.database.PostReplyProvider;
 import net.pterodactylus.sone.database.SoneProvider;
 import net.pterodactylus.sone.fcp.FcpInterface;
@@ -464,15 +467,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                return database.getDirectedPosts(recipientId);
        }
 
-       /**
-        * Returns a post reply builder.
-        *
-        * @return A new post reply builder
-        */
-       public PostReplyBuilder postReplyBuilder() {
-               return database.newPostReplyBuilder();
-       }
-
        /** {@inheritDoc} */
        @Override
        public Optional<PostReply> getPostReply(String replyId) {
@@ -1065,9 +1059,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        }
                        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());
+                       posts.add(postBuilder.build(Optional.<PostCreated>absent()));
                }
 
                /* load replies. */
@@ -1085,8 +1079,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid reply found, aborting load!");
                                return;
                        }
-                       PostReplyBuilder postReplyBuilder = postReplyBuilder().withId(replyId).from(sone.getId()).to(postId).withTime(replyTime).withText(replyText);
-                       replies.add(postReplyBuilder.build());
+                       PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(postId).withId(replyId).withTime(replyTime).withText(replyText);
+                       replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
                }
 
                /* load post likes. */
@@ -1219,94 +1213,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * 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
@@ -1384,44 +1290,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Creates a new reply.
-        *
-        * @param sone
-        *              The Sone that creates the reply
-        * @param post
-        *              The post that this reply refers to
-        * @param text
-        *              The text of the reply
-        * @return The created reply
-        */
-       public PostReply createReply(Sone sone, Post post, 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 reply for non-local Sone: %s", sone));
-                       return null;
-               }
-               PostReplyBuilder postReplyBuilder = postReplyBuilder();
-               postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
-               final PostReply reply = postReplyBuilder.build();
-               database.storePostReply(reply);
-               eventBus.post(new NewPostReplyFoundEvent(reply));
-               sone.addReply(reply);
-               touchConfiguration();
-               localElementTicker.schedule(new Runnable() {
-
-                       /**
-                        * {@inheritDoc}
-                        */
-                       @Override
-                       public void run() {
-                               markReplyKnown(reply);
-                       }
-               }, 10, TimeUnit.SECONDS);
-               return reply;
-       }
-
-       /**
         * Deletes the given reply.
         *
         * @param reply
@@ -1472,7 +1340,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                checkNotNull(temporaryImage, "temporaryImage must not be null");
                checkArgument(sone.isLocal(), "sone must be a local Sone");
                checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
-               Image image = album.newImageBuilder().withId(temporaryImage.getId()).createdNow().sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build();
+               Image image = album.newImageBuilder().withId(temporaryImage.getId()).sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build();
                imageInserter.insertImage(temporaryImage, image);
                return image;
        }
@@ -2036,4 +1904,48 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                touchConfiguration();
        }
 
+       public Optional<PostCreated> postCreated() {
+               return Optional.<PostCreated>of(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);
+                               }
+                       }
+               });
+       }
+
+       public Optional<PostReplyCreated> postReplyCreated() {
+               return Optional.<PostReplyCreated>of(new PostReplyCreated() {
+                       @Override
+                       public void postReplyCreated(final PostReply postReply) {
+                               if (postReply.isKnown()) {
+                                       return;
+                               }
+                               eventBus.post(new NewPostReplyFoundEvent(postReply));
+                               if (postReply.getSone().isLocal()) {
+                                       localElementTicker.schedule(new Runnable() {
+
+                                               /**
+                                                * {@inheritDoc}
+                                                */
+                                               @Override
+                                               public void run() {
+                                                       markReplyKnown(postReply);
+                                               }
+                                       }, 10, TimeUnit.SECONDS);
+                               }
+                       }
+               });
+       }
+
 }