Allow Sone, Post, and Reply creation only through the Core and its caches.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 03c036a..459431c 100644 (file)
@@ -106,7 +106,8 @@ public class Core extends AbstractService {
         */
        public Core freenetInterface(FreenetInterface freenetInterface) {
                this.freenetInterface = freenetInterface;
-               soneDownloader = new SoneDownloader(freenetInterface);
+               soneDownloader = new SoneDownloader(this, freenetInterface);
+               soneDownloader.start();
                return this;
        }
 
@@ -135,6 +136,34 @@ public class Core extends AbstractService {
                return soneCache.get(soneId);
        }
 
+       /**
+        * 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 getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text);
+       }
+
        //
        // ACTIONS
        //
@@ -145,7 +174,7 @@ public class Core extends AbstractService {
         * @param sone
         *            The Sone to add
         */
-       public void addSone(Sone sone) {
+       public void addLocalSone(Sone sone) {
                if (localSones.add(sone)) {
                        soneCache.put(sone.getId(), sone);
                        SoneInserter soneInserter = new SoneInserter(freenetInterface, sone);
@@ -161,7 +190,7 @@ public class Core extends AbstractService {
         * @param sone
         *            The sone to watch
         */
-       public void addRemoteSone(Sone sone) {
+       public void addSone(Sone sone) {
                if (!soneCache.containsKey(sone.getId())) {
                        soneCache.put(sone.getId(), sone);
                }
@@ -219,7 +248,7 @@ public class Core extends AbstractService {
                        sone.setProfile(new Profile());
                        /* set modification counter to 1 so it is inserted immediately. */
                        sone.setModificationCounter(1);
-                       addSone(sone);
+                       addLocalSone(sone);
                } catch (MalformedURLException mue1) {
                        throw new SoneException(Type.INVALID_URI);
                }
@@ -238,6 +267,36 @@ public class Core extends AbstractService {
                localSones.remove(sone);
        }
 
+       /**
+        * Returns the post with the given ID. If no post exists yet with the given
+        * ID, a new post is returned.
+        *
+        * @param postId
+        *            The ID of the post
+        * @return The post
+        */
+       public Post getPost(String postId) {
+               if (!postCache.containsKey(postId)) {
+                       postCache.put(postId, new Post(postId));
+               }
+               return postCache.get(postId);
+       }
+
+       /**
+        * Returns the reply with the given ID. If no reply exists yet with the
+        * given ID, a new reply is returned.
+        *
+        * @param replyId
+        *            The ID of the reply
+        * @return The reply
+        */
+       public Reply getReply(String replyId) {
+               if (!replyCache.containsKey(replyId)) {
+                       replyCache.put(replyId, new Reply(replyId));
+               }
+               return replyCache.get(replyId);
+       }
+
        //
        // SERVICE METHODS
        //
@@ -306,8 +365,7 @@ public class Core extends AbstractService {
                                        }
                                        long time = configuration.getLongValue(postPrefix + "/Time").getValue(null);
                                        String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
-                                       Post post = new Post(id, sone, time, text);
-                                       postCache.put(id, post);
+                                       Post post = getPost(id).setSone(sone).setTime(time).setText(text);
                                        sone.addPost(post);
                                } while (true);
                                int replyCounter = 0;
@@ -324,7 +382,7 @@ public class Core extends AbstractService {
                                        Post replyPost = postCache.get(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
                                        long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
                                        String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
-                                       Reply reply = new Reply(replyId, replySone, replyPost, replyTime, replyText);
+                                       Reply reply = getReply(replyId).setSone(replySone).setPost(replyPost).setTime(replyTime).setText(replyText);
                                        replyCache.put(replyId, reply);
                                } while (true);
 
@@ -340,12 +398,12 @@ public class Core extends AbstractService {
                                        String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
                                        String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
                                        friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
-                                       addRemoteSone(friendSone);
+                                       addSone(friendSone);
                                        sone.addFriend(sone);
                                }
 
                                sone.setModificationCounter(modificationCounter);
-                               addSone(sone);
+                               addLocalSone(sone);
                        } catch (MalformedURLException mue1) {
                                logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
                        }