Store identities in database.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 8edb14f..4bf8a8a 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;
@@ -330,6 +327,11 @@ public class Core extends AbstractService implements SoneProvider {
        }
 
        @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));
@@ -411,40 +413,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
@@ -616,7 +584,7 @@ public class Core extends AbstractService implements SoneProvider {
                                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) {
@@ -965,7 +933,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. */
@@ -1678,6 +1646,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 +1675,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 +1688,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