Don’t store Sones in core.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 0f22b81..a2e54a6 100644 (file)
@@ -17,7 +17,6 @@
 
 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;
@@ -27,7 +26,6 @@ import static net.pterodactylus.sone.data.Identified.GET_ID;
 import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
 import static net.pterodactylus.sone.data.Sone.TO_FREENET_URI;
 
-import java.net.MalformedURLException;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -97,8 +95,7 @@ import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 
-import freenet.keys.FreenetURI;
-
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
@@ -175,10 +172,6 @@ public class Core extends AbstractService implements SoneProvider {
        /* synchronize access on this on sones. */
        private final Map<Sone, SoneRescuer> soneRescuers = new HashMap<Sone, SoneRescuer>();
 
-       /** All Sones. */
-       /* synchronize access on this on itself. */
-       private final Map<String, Sone> sones = new HashMap<String, Sone>();
-
        /** All known Sones. */
        private final Set<String> knownSones = new HashSet<String>();
 
@@ -298,7 +291,7 @@ public class Core extends AbstractService implements SoneProvider {
        public SoneRescuer getSoneRescuer(Sone sone) {
                checkNotNull(sone, "sone must not be null");
                checkArgument(sone.isLocal(), "sone must be local");
-               synchronized (sones) {
+               synchronized (soneRescuers) {
                        SoneRescuer soneRescuer = soneRescuers.get(sone);
                        if (soneRescuer == null) {
                                soneRescuer = new SoneRescuer(this, soneDownloader, sone);
@@ -324,29 +317,22 @@ public class Core extends AbstractService implements SoneProvider {
 
        @Override
        public Collection<Sone> getSones() {
-               synchronized (sones) {
-                       return ImmutableSet.copyOf(sones.values());
-               }
+               return database.getSones();
+       }
+
+       @Override
+       public Function<String, Optional<Sone>> getSone() {
+               return database.getSone();
        }
 
        @Override
        public Optional<Sone> getSone(String id) {
-               synchronized (sones) {
-                       return Optional.fromNullable(sones.get(id));
-               }
+               return database.getSone(id);
        }
 
        @Override
        public Collection<Sone> getLocalSones() {
-               synchronized (sones) {
-                       return from(sones.values()).filter(new Predicate<Sone>() {
-
-                               @Override
-                               public boolean apply(Sone sone) {
-                                       return sone.isLocal();
-                               }
-                       }).toSet();
-               }
+               return database.getLocalSones();
        }
 
        /**
@@ -362,15 +348,7 @@ public class Core extends AbstractService implements SoneProvider {
 
        @Override
        public Collection<Sone> getRemoteSones() {
-               synchronized (sones) {
-                       return from(sones.values()).filter(new Predicate<Sone>() {
-
-                               @Override
-                               public boolean apply(Sone sone) {
-                                       return !sone.isLocal();
-                               }
-                       }).toSet();
-               }
+               return database.getRemoteSones();
        }
 
        /**
@@ -411,40 +389,6 @@ public class Core extends AbstractService implements SoneProvider {
        }
 
        /**
-        * Returns all Sones that have liked the given post.
-        *
-        * @param post
-        *              The post to get the liking Sones for
-        * @return The Sones that like the given post
-        */
-       public Set<Sone> getLikes(Post post) {
-               Set<Sone> sones = new HashSet<Sone>();
-               for (Sone sone : getSones()) {
-                       if (sone.getLikedPostIds().contains(post.getId())) {
-                               sones.add(sone);
-                       }
-               }
-               return sones;
-       }
-
-       /**
-        * Returns all Sones that have liked the given reply.
-        *
-        * @param reply
-        *              The reply to get the liking Sones for
-        * @return The Sones that like the given reply
-        */
-       public Set<Sone> getLikes(PostReply reply) {
-               Set<Sone> sones = new HashSet<Sone>();
-               for (Sone sone : getSones()) {
-                       if (sone.getLikedReplyIds().contains(reply.getId())) {
-                               sones.add(sone);
-                       }
-               }
-               return sones;
-       }
-
-       /**
         * Returns whether the given post is bookmarked.
         *
         * @param post
@@ -557,13 +501,13 @@ public class Core extends AbstractService implements SoneProvider {
                        return null;
                }
                logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
-               synchronized (sones) {
+               synchronized (soneRescuers) {
                        final Sone sone;
                        sone = database.newSoneBuilder().by(ownIdentity.getId()).local().using(new Client("Sone", SonePlugin.VERSION.toString())).build(Optional.<SoneCreated>absent());
                        sone.modify().setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)).update();
                        sone.setKnown(true);
                        /* TODO - load posts ’n stuff */
-                       sones.put(ownIdentity.getId(), sone);
+                       database.storeSone(sone);
                        final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone);
                        soneInserters.put(sone, soneInserter);
                        sone.setStatus(SoneStatus.idle);
@@ -610,13 +554,13 @@ public class Core extends AbstractService implements SoneProvider {
                        logger.log(Level.WARNING, "Given Identity is null!");
                        return null;
                }
-               synchronized (sones) {
+               synchronized (soneRescuers) {
                        Optional<Sone> existingSone = database.getSone(identity.getId());
                        if (existingSone.isPresent() && existingSone.get().isLocal()) {
                                return existingSone.get();
                        }
                        boolean newSone = !existingSone.isPresent();
-                       final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).build(Optional.<SoneCreated>absent()) : existingSone.get();
+                       final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).using(new Client("Sone", SonePlugin.VERSION.toString())).build(Optional.<SoneCreated>absent()) : existingSone.get();
                        sone.modify().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)).update();
                        if (newSone) {
                                synchronized (knownSones) {
@@ -853,7 +797,7 @@ public class Core extends AbstractService implements SoneProvider {
                                        database.storeImage(image);
                                }
                        }
-                       synchronized (sones) {
+                       synchronized (soneRescuers) {
                                sone.setOptions(storedSone.get().getOptions());
                                sone.setKnown(storedSone.get().isKnown());
                                sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
@@ -861,7 +805,7 @@ public class Core extends AbstractService implements SoneProvider {
                                        soneInserters.get(storedSone.get()).setSone(sone);
                                        touchConfiguration();
                                }
-                               sones.put(sone.getId(), sone);
+                               database.storeSone(sone);
                        }
                }
        }
@@ -879,12 +823,11 @@ public class Core extends AbstractService implements SoneProvider {
                        logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
                        return;
                }
-               synchronized (sones) {
+               synchronized (soneRescuers) {
                        if (!getLocalSones().contains(sone)) {
                                logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
                                return;
                        }
-                       sones.remove(sone.getId());
                        SoneInserter soneInserter = soneInserters.remove(sone);
                        soneInserter.stop();
                }
@@ -965,7 +908,7 @@ public class Core extends AbstractService implements SoneProvider {
                                break;
                        }
                        String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
-                       profile.addField(fieldName).setValue(fieldValue);
+                       profile.setField(profile.addField(fieldName), fieldValue);
                }
 
                /* load posts. */
@@ -1094,7 +1037,7 @@ public class Core extends AbstractService implements SoneProvider {
                /* load avatar. */
                String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
                if (avatarId != null) {
-                       profile.setAvatar(getImage(avatarId).orNull());
+                       profile.setAvatar(getImage(avatarId).transform(GET_ID));
                }
 
                /* load options. */
@@ -1335,7 +1278,7 @@ public class Core extends AbstractService implements SoneProvider {
        @Override
        public void serviceStop() {
                localElementTicker.shutdownNow();
-               synchronized (sones) {
+               synchronized (soneRescuers) {
                        for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
                                soneInserter.getValue().stop();
                                saveSone(soneInserter.getKey());
@@ -1678,6 +1621,7 @@ public class Core extends AbstractService implements SoneProvider {
                OwnIdentity ownIdentity = ownIdentityAddedEvent.ownIdentity();
                logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity));
                if (ownIdentity.hasContext("Sone")) {
+                       database.storeIdentity(ownIdentity);
                        addLocalSone(ownIdentity);
                }
        }
@@ -1706,6 +1650,7 @@ public class Core extends AbstractService implements SoneProvider {
                Identity identity = identityAddedEvent.identity();
                logger.log(Level.FINEST, String.format("Adding Identity: %s", identity));
                trustedIdentities.put(identityAddedEvent.ownIdentity(), identity);
+               database.storeIdentity(identity);
                addRemoteSone(identity);
        }
 
@@ -1718,6 +1663,7 @@ public class Core extends AbstractService implements SoneProvider {
        @Subscribe
        public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
                final Identity identity = identityUpdatedEvent.identity();
+               database.storeIdentity(identity);
                soneDownloaders.execute(new Runnable() {
 
                        @Override
@@ -1768,9 +1714,6 @@ public class Core extends AbstractService implements SoneProvider {
                for (PostReply reply : sone.get().getReplies()) {
                        eventBus.post(new PostReplyRemovedEvent(reply));
                }
-               synchronized (sones) {
-                       sones.remove(identity.getId());
-               }
                eventBus.post(new SoneRemovedEvent(sone.get()));
        }