Rename fetch action methods
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 6d90516..ccf633c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - Core.java - Copyright © 2010–2013 David Roden
+ * Sone - Core.java - Copyright © 2010–2016 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,14 +20,11 @@ package net.pterodactylus.sone.core;
 import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Predicates.not;
 import static com.google.common.primitives.Longs.tryParse;
 import static java.lang.String.format;
 import static java.util.logging.Level.WARNING;
-import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
-import static net.pterodactylus.sone.data.Sone.toAllAlbums;
+import static java.util.logging.Logger.getLogger;
 
-import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -43,14 +40,18 @@ import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
-import net.pterodactylus.sone.core.Options.DefaultOption;
-import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor;
 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
+import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
@@ -71,9 +72,8 @@ import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
-import net.pterodactylus.sone.data.SoneImpl;
+import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent;
 import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.database.AlbumBuilder;
 import net.pterodactylus.sone.database.Database;
@@ -83,8 +83,8 @@ import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostProvider;
 import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.database.PostReplyProvider;
+import net.pterodactylus.sone.database.SoneBuilder;
 import net.pterodactylus.sone.database.SoneProvider;
-import net.pterodactylus.sone.fcp.FcpInterface;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
@@ -94,28 +94,22 @@ import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
 import net.pterodactylus.sone.main.SonePlugin;
-import net.pterodactylus.sone.utils.IntegerRangePredicate;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
-import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
-import com.google.common.base.Predicates;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashMultimap;
-import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multimaps;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
-
-import freenet.keys.FreenetURI;
+import kotlin.jvm.functions.Function1;
 
 /**
  * The Sone core.
@@ -126,16 +120,13 @@ import freenet.keys.FreenetURI;
 public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
 
        /** The logger. */
-       private static final Logger logger = Logging.getLogger(Core.class);
+       private static final Logger logger = getLogger(Core.class.getName());
 
        /** The start time. */
        private final long startupTime = System.currentTimeMillis();
 
-       /** The options. */
-       private final Options options = new Options();
-
        /** The preferences. */
-       private final Preferences preferences = new Preferences(options);
+       private final Preferences preferences;
 
        /** The event bus. */
        private final EventBus eventBus;
@@ -167,12 +158,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /** The trust updater. */
        private final WebOfTrustUpdater webOfTrustUpdater;
 
-       /** The FCP interface. */
-       private volatile FcpInterface fcpInterface;
-
-       /** The times Sones were followed. */
-       private final Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
-
        /** Locked local Sones. */
        /* synchronize on itself. */
        private final Set<Sone> lockedSones = new HashSet<Sone>();
@@ -185,20 +170,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /* 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>();
 
        /** The post database. */
        private final Database database;
 
-       /** All bookmarked posts. */
-       /* synchronize access on itself. */
-       private final Set<String> bookmarkedPosts = new HashSet<String>();
-
        /** Trusted identities, sorted by own identities. */
        private final Multimap<OwnIdentity, Identity> trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.<OwnIdentity, Identity>create());
 
@@ -228,21 +205,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            The database
         */
        @Inject
-       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
-               super("Sone Core");
-               this.configuration = configuration;
-               this.freenetInterface = freenetInterface;
-               this.identityManager = identityManager;
-               this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface);
-               this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier());
-               this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
-               this.webOfTrustUpdater = webOfTrustUpdater;
-               this.eventBus = eventBus;
-               this.database = database;
-       }
-
-       @VisibleForTesting
-       protected Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
+       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
                super("Sone Core");
                this.configuration = configuration;
                this.freenetInterface = freenetInterface;
@@ -253,6 +216,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                this.webOfTrustUpdater = webOfTrustUpdater;
                this.eventBus = eventBus;
                this.database = database;
+               preferences = new Preferences(eventBus);
        }
 
        //
@@ -296,16 +260,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Sets the FCP interface to use.
-        *
-        * @param fcpInterface
-        *            The FCP interface to use
-        */
-       public void setFcpInterface(FcpInterface fcpInterface) {
-               this.fcpInterface = fcpInterface;
-       }
-
-       /**
         * Returns the Sone rescuer for the given local Sone.
         *
         * @param sone
@@ -315,7 +269,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        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);
@@ -339,14 +293,23 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
        }
 
+       public SoneBuilder soneBuilder() {
+               return database.newSoneBuilder();
+       }
+
        /**
         * {@inheritDocs}
         */
+       @Nonnull
        @Override
        public Collection<Sone> getSones() {
-               synchronized (sones) {
-                       return ImmutableSet.copyOf(sones.values());
-               }
+               return database.getSones();
+       }
+
+       @Nonnull
+       @Override
+       public Function1<String, Sone> getSoneLoader() {
+               return database.getSoneLoader();
        }
 
        /**
@@ -359,10 +322,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         Sone
         */
        @Override
-       public Optional<Sone> getSone(String id) {
-               synchronized (sones) {
-                       return Optional.fromNullable(sones.get(id));
-               }
+       @Nullable
+       public Sone getSone(@Nonnull String id) {
+               return database.getSone(id);
        }
 
        /**
@@ -370,9 +332,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        @Override
        public Collection<Sone> getLocalSones() {
-               synchronized (sones) {
-                       return FluentIterable.from(sones.values()).filter(LOCAL_SONE_FILTER).toSet();
-               }
+               return database.getLocalSones();
        }
 
        /**
@@ -380,24 +340,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *
         * @param id
         *            The ID of the Sone
-        * @param create
-        *            {@code true} to create a new Sone if none exists,
-        *            {@code false} to return null if none exists
         * @return The Sone with the given ID, or {@code null}
         */
-       public Sone getLocalSone(String id, boolean create) {
-               synchronized (sones) {
-                       Sone sone = sones.get(id);
-                       if ((sone == null) && create) {
-                               sone = new SoneImpl(id, true);
-                               sones.put(id, sone);
-                       }
-                       if ((sone != null) && !sone.isLocal()) {
-                               sone = new SoneImpl(id, true);
-                               sones.put(id, sone);
-                       }
+       public Sone getLocalSone(String id) {
+               Sone sone = database.getSone(id);
+               if ((sone != null) && sone.isLocal()) {
                        return sone;
                }
+               return null;
        }
 
        /**
@@ -405,30 +355,19 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        @Override
        public Collection<Sone> getRemoteSones() {
-               synchronized (sones) {
-                       return FluentIterable.from(sones.values()).filter(not(LOCAL_SONE_FILTER)).toSet();
-               }
+               return database.getRemoteSones();
        }
 
        /**
         * Returns the remote Sone with the given ID.
         *
+        *
         * @param id
         *            The ID of the remote Sone to get
-        * @param create
-        *            {@code true} to always create a Sone, {@code false} to return
-        *            {@code null} if no Sone with the given ID exists
         * @return The Sone with the given ID
         */
-       public Sone getRemoteSone(String id, boolean create) {
-               synchronized (sones) {
-                       Sone sone = sones.get(id);
-                       if ((sone == null) && create && (id != null) && (id.length() == 43)) {
-                               sone = new SoneImpl(id, false);
-                               sones.put(id, sone);
-                       }
-                       return sone;
-               }
+       public Sone getRemoteSone(String id) {
+               return database.getSone(id);
        }
 
        /**
@@ -444,20 +383,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Returns the time when the given was first followed by any local Sone.
-        *
-        * @param sone
-        *            The Sone to get the time for
-        * @return The time (in milliseconds since Jan 1, 1970) the Sone has first
-        *         been followed, or {@link Long#MAX_VALUE}
-        */
-       public long getSoneFollowingTime(Sone sone) {
-               synchronized (soneFollowingTimes) {
-                       return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE);
-               }
-       }
-
-       /**
         * Returns a post builder.
         *
         * @return A new post builder
@@ -466,11 +391,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                return database.newPostBuilder();
        }
 
-       /**
-        * {@inheritDoc}
-        */
+       @Nullable
        @Override
-       public Optional<Post> getPost(String postId) {
+       public Post getPost(@Nonnull String postId) {
                return database.getPost(postId);
        }
 
@@ -503,8 +426,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /**
         * {@inheritDoc}
         */
+       @Nullable
        @Override
-       public Optional<PostReply> getPostReply(String replyId) {
+       public PostReply getPostReply(String replyId) {
                return database.getPostReply(replyId);
        }
 
@@ -559,21 +483,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         otherwise
         */
        public boolean isBookmarked(Post post) {
-               return isPostBookmarked(post.getId());
-       }
-
-       /**
-        * Returns whether the post with the given ID is bookmarked.
-        *
-        * @param id
-        *            The ID of the post to check
-        * @return {@code true} if the post with the given ID is bookmarked,
-        *         {@code false} otherwise
-        */
-       public boolean isPostBookmarked(String id) {
-               synchronized (bookmarkedPosts) {
-                       return bookmarkedPosts.contains(id);
-               }
+               return database.isPostBookmarked(post);
        }
 
        /**
@@ -582,16 +492,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return All bookmarked posts
         */
        public Set<Post> getBookmarkedPosts() {
-               Set<Post> posts = new HashSet<Post>();
-               synchronized (bookmarkedPosts) {
-                       for (String bookmarkedPostId : bookmarkedPosts) {
-                               Optional<Post> post = getPost(bookmarkedPostId);
-                               if (post.isPresent()) {
-                                       posts.add(post.get());
-                               }
-                       }
-               }
-               return posts;
+               return database.getBookmarkedPosts();
        }
 
        public AlbumBuilder albumBuilder() {
@@ -607,8 +508,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return The album with the given ID, or {@code null} if no album with the
         *         given ID exists
         */
-       public Album getAlbum(String albumId) {
-               return database.getAlbum(albumId).orNull();
+       @Nullable
+       public Album getAlbum(@Nonnull String albumId) {
+               return database.getAlbum(albumId);
        }
 
        public ImageBuilder imageBuilder() {
@@ -622,6 +524,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            The ID of the image
         * @return The image with the given ID
         */
+       @Nullable
        public Image getImage(String imageId) {
                return getImage(imageId, true);
        }
@@ -638,10 +541,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return The image with the given ID, or {@code null} if none exists and
         *         none was created
         */
+       @Nullable
        public Image getImage(String imageId, boolean create) {
-               Optional<Image> image = database.getImage(imageId);
-               if (image.isPresent()) {
-                       return image.get();
+               Image image = database.getImage(imageId);
+               if (image != null) {
+                       return image;
                }
                if (!create) {
                        return null;
@@ -713,26 +617,22 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        return null;
                }
                logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
-               synchronized (sones) {
-                       final Sone sone;
-                       try {
-                               sone = getLocalSone(ownIdentity.getId(), true).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
-                       } catch (MalformedURLException mue1) {
-                               logger.log(Level.SEVERE, String.format("Could not convert the Identity’s URIs to Freenet URIs: %s, %s", ownIdentity.getInsertUri(), ownIdentity.getRequestUri()), mue1);
-                               return null;
-                       }
-                       sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
-                       sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
-                       sone.setKnown(true);
-                       /* TODO - load posts ’n stuff */
-                       sones.put(ownIdentity.getId(), sone);
-                       final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone);
+               Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
+               String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0");
+               sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
+               sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
+               sone.setKnown(true);
+               SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
+               soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
+               eventBus.register(soneInserter);
+               synchronized (soneInserters) {
                        soneInserters.put(sone, soneInserter);
-                       sone.setStatus(SoneStatus.idle);
-                       loadSone(sone);
-                       soneInserter.start();
-                       return sone;
                }
+               loadSone(sone);
+               database.storeSone(sone);
+               sone.setStatus(SoneStatus.idle);
+               soneInserter.start();
+               return sone;
        }
 
        /**
@@ -766,35 +666,33 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.WARNING, "Given Identity is null!");
                        return null;
                }
-               final Long latestEdition = fromNullable(tryParse(
-                               identity.getProperty("Sone.LatestEdition"))).or(0L);
-               synchronized (sones) {
-                       final Sone sone = getRemoteSone(identity.getId(), true);
-                       if (sone.isLocal()) {
-                               return sone;
+               String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
+               long latestEdition = fromNullable(tryParse(property)).or(0L);
+               Sone existingSone = getSone(identity.getId());
+               if ((existingSone != null )&& existingSone.isLocal()) {
+                       return existingSone;
+               }
+               boolean newSone = existingSone == null;
+               Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build();
+               sone.setLatestEdition(latestEdition);
+               if (newSone) {
+                       synchronized (knownSones) {
+                               newSone = !knownSones.contains(sone.getId());
                        }
-                       sone.setIdentity(identity);
-                       boolean newSone = sone.getRequestUri() == null;
-                       sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
-                       sone.setLatestEdition(latestEdition);
+                       sone.setKnown(!newSone);
                        if (newSone) {
-                               synchronized (knownSones) {
-                                       newSone = !knownSones.contains(sone.getId());
-                               }
-                               sone.setKnown(!newSone);
-                               if (newSone) {
-                                       eventBus.post(new NewSoneFoundEvent(sone));
-                                       for (Sone localSone : getLocalSones()) {
-                                               if (localSone.getOptions().isAutoFollow()) {
-                                                       followSone(localSone, sone.getId());
-                                               }
+                               eventBus.post(new NewSoneFoundEvent(sone));
+                               for (Sone localSone : getLocalSones()) {
+                                       if (localSone.getOptions().isAutoFollow()) {
+                                               followSone(localSone, sone.getId());
                                        }
                                }
                        }
-                       soneDownloader.addSone(sone);
-                       soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
-                       return sone;
                }
+               database.storeSone(sone);
+               soneDownloader.addSone(sone);
+               soneDownloaders.execute(soneDownloader.fetchSoneAsUskAction(sone));
+               return sone;
        }
 
        /**
@@ -808,25 +706,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void followSone(Sone sone, String soneId) {
                checkNotNull(sone, "sone must not be null");
                checkNotNull(soneId, "soneId must not be null");
-               sone.addFriend(soneId);
-               synchronized (soneFollowingTimes) {
-                       if (!soneFollowingTimes.containsKey(soneId)) {
-                               long now = System.currentTimeMillis();
-                               soneFollowingTimes.put(soneId, now);
-                               Optional<Sone> followedSone = getSone(soneId);
-                               if (!followedSone.isPresent()) {
-                                       return;
-                               }
-                               for (Post post : followedSone.get().getPosts()) {
-                                       if (post.getTime() < now) {
-                                               markPostKnown(post);
-                                       }
-                               }
-                               for (PostReply reply : followedSone.get().getReplies()) {
-                                       if (reply.getTime() < now) {
-                                               markReplyKnown(reply);
-                                       }
-                               }
+               database.addFriend(sone, soneId);
+               @SuppressWarnings("ConstantConditions") // we just followed, this can’t be null.
+               long now = database.getFollowingTime(soneId);
+               Sone followedSone = getSone(soneId);
+               if (followedSone == null) {
+                       return;
+               }
+               for (Post post : followedSone.getPosts()) {
+                       if (post.getTime() < now) {
+                               markPostKnown(post);
+                       }
+               }
+               for (PostReply reply : followedSone.getReplies()) {
+                       if (reply.getTime() < now) {
+                               markReplyKnown(reply);
                        }
                }
                touchConfiguration();
@@ -843,16 +737,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void unfollowSone(Sone sone, String soneId) {
                checkNotNull(sone, "sone must not be null");
                checkNotNull(soneId, "soneId must not be null");
-               sone.removeFriend(soneId);
-               boolean unfollowedSoneStillFollowed = false;
-               for (Sone localSone : getLocalSones()) {
-                       unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
-               }
-               if (!unfollowedSoneStillFollowed) {
-                       synchronized (soneFollowingTimes) {
-                               soneFollowingTimes.remove(soneId);
-                       }
-               }
+               database.removeFriend(sone, soneId);
                touchConfiguration();
        }
 
@@ -946,91 +831,67 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            {@code true} if the stored Sone should be updated regardless
         *            of the age of the given Sone
         */
-       public void updateSone(Sone sone, boolean soneRescueMode) {
-               Optional<Sone> storedSone = getSone(sone.getId());
-               if (storedSone.isPresent()) {
-                       if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) {
+       public void updateSone(final Sone sone, boolean soneRescueMode) {
+               Sone storedSone = getSone(sone.getId());
+               if (storedSone != null) {
+                       if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
                                logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
                                return;
                        }
-                       /* find removed posts. */
-                       Collection<Post> removedPosts = new ArrayList<Post>();
-                       Collection<Post> newPosts = new ArrayList<Post>();
-                       Collection<Post> existingPosts = database.getPosts(sone.getId());
-                       for (Post oldPost : existingPosts) {
-                               if (!sone.getPosts().contains(oldPost)) {
-                                       removedPosts.add(oldPost);
-                               }
-                       }
-                       /* find new posts. */
-                       for (Post newPost : sone.getPosts()) {
-                               if (existingPosts.contains(newPost)) {
-                                       continue;
-                               }
-                               if (newPost.getTime() < getSoneFollowingTime(sone)) {
-                                       newPost.setKnown(true);
-                               } else if (!newPost.isKnown()) {
-                                       newPosts.add(newPost);
-                               }
-                       }
-                       /* store posts. */
-                       database.storePosts(sone, sone.getPosts());
-                       Collection<PostReply> newPostReplies = new ArrayList<PostReply>();
-                       Collection<PostReply> removedPostReplies = new ArrayList<PostReply>();
-                       if (!soneRescueMode) {
-                               for (PostReply reply : storedSone.get().getReplies()) {
-                                       if (!sone.getReplies().contains(reply)) {
-                                               removedPostReplies.add(reply);
-                                       }
-                               }
+                       List<Object> events =
+                                       collectEventsForChangesInSone(storedSone, sone);
+                       database.storeSone(sone);
+                       for (Object event : events) {
+                               eventBus.post(event);
                        }
-                       Set<PostReply> storedReplies = storedSone.get().getReplies();
-                       for (PostReply reply : sone.getReplies()) {
-                               if (storedReplies.contains(reply)) {
-                                       continue;
-                               }
-                               if (reply.getTime() < getSoneFollowingTime(sone)) {
-                                       reply.setKnown(true);
-                               } else if (!reply.isKnown()) {
-                                       newPostReplies.add(reply);
-                               }
+                       sone.setOptions(storedSone.getOptions());
+                       sone.setKnown(storedSone.isKnown());
+                       sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
+                       if (sone.isLocal()) {
+                               touchConfiguration();
                        }
-                       database.storePostReplies(sone, sone.getReplies());
-                       for (Album album : storedSone.get().getRootAlbum().getAlbums()) {
-                               database.removeAlbum(album);
-                               for (Image image : album.getImages()) {
-                                       database.removeImage(image);
+               }
+       }
+
+       private List<Object> collectEventsForChangesInSone(Sone oldSone,
+                       final Sone newSone) {
+               final List<Object> events = new ArrayList<Object>();
+               SoneChangeDetector soneChangeDetector = new SoneChangeDetector(
+                               oldSone);
+               soneChangeDetector.onNewPosts(new PostProcessor() {
+                       @Override
+                       public void processPost(Post post) {
+                               if (post.getTime() < database.getFollowingTime(newSone.getId())) {
+                                       post.setKnown(true);
+                               } else if (!post.isKnown()) {
+                                       events.add(new NewPostFoundEvent(post));
                                }
                        }
-                       for (Post removedPost : removedPosts) {
-                               eventBus.post(new PostRemovedEvent(removedPost));
-                       }
-                       for (Post newPost : newPosts) {
-                               eventBus.post(new NewPostFoundEvent(newPost));
-                       }
-                       for (PostReply removedPostReply : removedPostReplies) {
-                               eventBus.post(new PostReplyRemovedEvent(removedPostReply));
+               });
+               soneChangeDetector.onRemovedPosts(new PostProcessor() {
+                       @Override
+                       public void processPost(Post post) {
+                               events.add(new PostRemovedEvent(post));
                        }
-                       for (PostReply newPostReply : newPostReplies) {
-                               eventBus.post(new NewPostReplyFoundEvent(newPostReply));
-                       }
-                       for (Album album : toAllAlbums.apply(sone)) {
-                               database.storeAlbum(album);
-                               for (Image image : album.getImages()) {
-                                       database.storeImage(image);
+               });
+               soneChangeDetector.onNewPostReplies(new PostReplyProcessor() {
+                       @Override
+                       public void processPostReply(PostReply postReply) {
+                               if (postReply.getTime() < database.getFollowingTime(newSone.getId())) {
+                                       postReply.setKnown(true);
+                               } else if (!postReply.isKnown()) {
+                                       events.add(new NewPostReplyFoundEvent(postReply));
                                }
                        }
-                       synchronized (sones) {
-                               sone.setOptions(storedSone.get().getOptions());
-                               sone.setKnown(storedSone.get().isKnown());
-                               sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
-                               if (sone.isLocal()) {
-                                       soneInserters.get(storedSone.get()).setSone(sone);
-                                       touchConfiguration();
-                               }
-                               sones.put(sone.getId(), sone);
+               });
+               soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() {
+                       @Override
+                       public void processPostReply(PostReply postReply) {
+                               events.add(new PostReplyRemovedEvent(postReply));
                        }
-               }
+               });
+               soneChangeDetector.detectChanges(newSone);
+               return events;
        }
 
        /**
@@ -1046,15 +907,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
                        return;
                }
-               synchronized (sones) {
-                       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();
+               if (!getLocalSones().contains(sone)) {
+                       logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
+                       return;
                }
+               SoneInserter soneInserter = soneInserters.remove(sone);
+               soneInserter.stop();
+               database.removeSone(sone);
                webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
                webOfTrustUpdater.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
                try {
@@ -1135,9 +994,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                Set<String> likedReplyIds =
                                configurationSoneParser.parseLikedPostReplyIds();
 
-               /* load friends. */
-               Set<String> friends = configurationSoneParser.parseFriends();
-
                /* load albums. */
                List<Album> topLevelAlbums;
                try {
@@ -1174,12 +1030,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
 
                /* load options. */
-               sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
-               sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
-               sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
-               sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
-               sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
-               sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
+               sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
+               sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
+               sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
+               sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
+               sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
+               sone.getOptions().setShowCustomAvatars(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(LoadExternalContent.NEVER.name())));
+               sone.getOptions().setLoadLinkedImages(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").getValue(LoadExternalContent.NEVER.name())));
 
                /* if we’re still here, Sone was loaded successfully. */
                synchronized (sone) {
@@ -1189,33 +1046,19 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        sone.setReplies(replies);
                        sone.setLikePostIds(likedPostIds);
                        sone.setLikeReplyIds(likedReplyIds);
-                       for (String friendId : friends) {
-                               followSone(sone, friendId);
-                       }
                        for (Album album : sone.getRootAlbum().getAlbums()) {
                                sone.getRootAlbum().removeAlbum(album);
                        }
                        for (Album album : topLevelAlbums) {
                                sone.getRootAlbum().addAlbum(album);
                        }
-                       soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
-                       for (Album album : toAllAlbums.apply(sone)) {
-                               database.storeAlbum(album);
-                               for (Image image : album.getImages()) {
-                                       database.storeImage(image);
-                               }
+                       synchronized (soneInserters) {
+                               soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                        }
                }
-               synchronized (knownSones) {
-                       for (String friend : friends) {
-                               knownSones.add(friend);
-                       }
-               }
-               database.storePosts(sone, posts);
                for (Post post : posts) {
                        post.setKnown(true);
                }
-               database.storePostReplies(sone, replies);
                for (PostReply reply : replies) {
                        reply.setKnown(true);
                }
@@ -1236,24 +1079,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @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()) {
@@ -1261,7 +1086,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        return null;
                }
                PostBuilder postBuilder = database.newPostBuilder();
-               postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim());
+               postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim());
                if (recipient.isPresent()) {
                        postBuilder.to(recipient.get().getId());
                }
@@ -1307,16 +1132,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
        }
 
-       /**
-        * Bookmarks the post with the given ID.
-        *
-        * @param id
-        *            The ID of the post to bookmark
-        */
-       public void bookmarkPost(String id) {
-               synchronized (bookmarkedPosts) {
-                       bookmarkedPosts.add(id);
-               }
+       public void bookmarkPost(Post post) {
+               database.bookmarkPost(post);
        }
 
        /**
@@ -1325,20 +1142,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @param post
         *            The post to unbookmark
         */
-       public void unbookmark(Post post) {
-               unbookmarkPost(post.getId());
-       }
-
-       /**
-        * Removes the post with the given ID from the bookmarks.
-        *
-        * @param id
-        *            The ID of the post to unbookmark
-        */
-       public void unbookmarkPost(String id) {
-               synchronized (bookmarkedPosts) {
-                       bookmarkedPosts.remove(id);
-               }
+       public void unbookmarkPost(Post post) {
+               database.unbookmarkPost(post);
        }
 
        /**
@@ -1566,10 +1371,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Override
        public void serviceStop() {
                localElementTicker.shutdownNow();
-               synchronized (sones) {
+               synchronized (soneInserters) {
                        for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
                                soneInserter.getValue().stop();
-                               saveSone(getLocalSone(soneInserter.getKey().getId(), false));
+                               saveSone(soneInserter.getKey());
+                       }
+               }
+               synchronized (soneRescuers) {
+                       for (SoneRescuer soneRescuer : soneRescuers.values()) {
+                               soneRescuer.stop();
                        }
                }
                saveConfiguration();
@@ -1664,13 +1474,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        }
                        configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
 
-                       /* save friends. */
-                       int friendCounter = 0;
-                       for (String friendId : sone.getFriends()) {
-                               configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
-                       }
-                       configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
-
                        /* save albums. first, collect in a flat structure, top-level first. */
                        List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
 
@@ -1681,7 +1484,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
                                configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
                                configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
-                               configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage() == null ? null : album.getAlbumImage().getId());
                        }
                        configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
 
@@ -1712,6 +1514,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
                        configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
                        configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
+                       configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").setValue(sone.getOptions().getLoadLinkedImages().name());
 
                        configuration.save();
 
@@ -1737,18 +1540,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
 
                /* store the options first. */
                try {
-                       configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
-                       configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
-                       configuration.getIntValue("Option/PostsPerPage").setValue(options.getIntegerOption("PostsPerPage").getReal());
-                       configuration.getIntValue("Option/ImagesPerPage").setValue(options.getIntegerOption("ImagesPerPage").getReal());
-                       configuration.getIntValue("Option/CharactersPerPost").setValue(options.getIntegerOption("CharactersPerPost").getReal());
-                       configuration.getIntValue("Option/PostCutOffLength").setValue(options.getIntegerOption("PostCutOffLength").getReal());
-                       configuration.getBooleanValue("Option/RequireFullAccess").setValue(options.getBooleanOption("RequireFullAccess").getReal());
-                       configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
-                       configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
-                       configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
-                       configuration.getBooleanValue("Option/ActivateFcpInterface").setValue(options.getBooleanOption("ActivateFcpInterface").getReal());
-                       configuration.getIntValue("Option/FcpFullAccessRequired").setValue(options.getIntegerOption("FcpFullAccessRequired").getReal());
+                       preferences.saveTo(configuration);
 
                        /* save known Sones. */
                        int soneCounter = 0;
@@ -1759,29 +1551,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
                        }
 
-                       /* save Sone following times. */
-                       soneCounter = 0;
-                       synchronized (soneFollowingTimes) {
-                               for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
-                                       configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
-                                       configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
-                                       ++soneCounter;
-                               }
-                               configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
-                       }
-
                        /* save known posts. */
                        database.save();
 
-                       /* save bookmarked posts. */
-                       int bookmarkedPostCounter = 0;
-                       synchronized (bookmarkedPosts) {
-                               for (String bookmarkedPostId : bookmarkedPosts) {
-                                       configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
-                               }
-                       }
-                       configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
-
                        /* now save it. */
                        configuration.save();
 
@@ -1800,30 +1572,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * Loads the configuration.
         */
        private void loadConfiguration() {
-               /* create options. */
-               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new SetInsertionDelay()));
-               options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
-               options.addIntegerOption("ImagesPerPage", new DefaultOption<Integer>(9, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
-               options.addIntegerOption("CharactersPerPost", new DefaultOption<Integer>(400, Predicates.<Integer> or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
-               options.addIntegerOption("PostCutOffLength", new DefaultOption<Integer>(200, Predicates.<Integer> or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
-               options.addBooleanOption("RequireFullAccess", new DefaultOption<Boolean>(false));
-               options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangePredicate(0, 100)));
-               options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangePredicate(-100, 100)));
-               options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
-               options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, fcpInterface.new SetActive()));
-               options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2, fcpInterface.new SetFullAccessRequired()));
-
-               loadConfigurationValue("InsertionDelay");
-               loadConfigurationValue("PostsPerPage");
-               loadConfigurationValue("ImagesPerPage");
-               loadConfigurationValue("CharactersPerPost");
-               loadConfigurationValue("PostCutOffLength");
-               options.getBooleanOption("RequireFullAccess").set(configuration.getBooleanValue("Option/RequireFullAccess").getValue(null));
-               loadConfigurationValue("PositiveTrust");
-               loadConfigurationValue("NegativeTrust");
-               options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
-               options.getBooleanOption("ActivateFcpInterface").set(configuration.getBooleanValue("Option/ActivateFcpInterface").getValue(null));
-               options.getIntegerOption("FcpFullAccessRequired").set(configuration.getIntValue("Option/FcpFullAccessRequired").getValue(null));
+               new PreferencesLoader(preferences).loadFrom(configuration);
 
                /* load known Sones. */
                int soneCounter = 0;
@@ -1836,48 +1585,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                knownSones.add(knownSoneId);
                        }
                }
-
-               /* load Sone following times. */
-               soneCounter = 0;
-               while (true) {
-                       String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
-                       if (soneId == null) {
-                               break;
-                       }
-                       long time = configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").getValue(Long.MAX_VALUE);
-                       synchronized (soneFollowingTimes) {
-                               soneFollowingTimes.put(soneId, time);
-                       }
-                       ++soneCounter;
-               }
-
-               /* load bookmarked posts. */
-               int bookmarkedPostCounter = 0;
-               while (true) {
-                       String bookmarkedPostId = configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").getValue(null);
-                       if (bookmarkedPostId == null) {
-                               break;
-                       }
-                       synchronized (bookmarkedPosts) {
-                               bookmarkedPosts.add(bookmarkedPostId);
-                       }
-               }
-
-       }
-
-       /**
-        * Loads an {@link Integer} configuration value for the option with the
-        * given name, logging validation failures.
-        *
-        * @param optionName
-        *            The name of the option to load
-        */
-       private void loadConfigurationValue(String optionName) {
-               try {
-                       options.getIntegerOption(optionName).set(configuration.getIntValue("Option/" + optionName).getValue(null));
-               } catch (IllegalArgumentException iae1) {
-                       logger.log(Level.WARNING, String.format("Invalid value for %s in configuration, using default.", optionName));
-               }
        }
 
        /**
@@ -1931,14 +1638,19 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Subscribe
        public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
                Identity identity = identityUpdatedEvent.identity();
-               final Sone sone = getRemoteSone(identity.getId(), false);
+               final Sone sone = getRemoteSone(identity.getId());
                if (sone.isLocal()) {
                        return;
                }
-               sone.setIdentity(identity);
-               sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition()));
+               String newLatestEdition = identity.getProperty("Sone.LatestEdition");
+               if (newLatestEdition != null) {
+                       Long parsedNewLatestEdition = tryParse(newLatestEdition);
+                       if (parsedNewLatestEdition != null) {
+                               sone.setLatestEdition(parsedNewLatestEdition);
+                       }
+               }
                soneDownloader.addSone(sone);
-               soneDownloaders.execute(soneDownloader.fetchSoneAction(sone));
+               soneDownloaders.execute(soneDownloader.fetchSoneAsSskAction(sone));
        }
 
        /**
@@ -1952,36 +1664,27 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                OwnIdentity ownIdentity = identityRemovedEvent.ownIdentity();
                Identity identity = identityRemovedEvent.identity();
                trustedIdentities.remove(ownIdentity, identity);
-               boolean foundIdentity = false;
                for (Entry<OwnIdentity, Collection<Identity>> trustedIdentity : trustedIdentities.asMap().entrySet()) {
                        if (trustedIdentity.getKey().equals(ownIdentity)) {
                                continue;
                        }
                        if (trustedIdentity.getValue().contains(identity)) {
-                               foundIdentity = true;
+                               return;
                        }
                }
-               if (foundIdentity) {
-                       /* some local identity still trusts this identity, don’t remove. */
-                       return;
-               }
-               Optional<Sone> sone = getSone(identity.getId());
-               if (!sone.isPresent()) {
+               Sone sone = getSone(identity.getId());
+               if (sone == null) {
                        /* TODO - we don’t have the Sone anymore. should this happen? */
                        return;
                }
-               database.removePosts(sone.get());
-               for (Post post : sone.get().getPosts()) {
-                       eventBus.post(new PostRemovedEvent(post));
+               for (PostReply postReply : sone.getReplies()) {
+                       eventBus.post(new PostReplyRemovedEvent(postReply));
                }
-               database.removePostReplies(sone.get());
-               for (PostReply reply : sone.get().getReplies()) {
-                       eventBus.post(new PostReplyRemovedEvent(reply));
-               }
-               synchronized (sones) {
-                       sones.remove(identity.getId());
+               for (Post post : sone.getPosts()) {
+                       eventBus.post(new PostRemovedEvent(post));
                }
-               eventBus.post(new SoneRemovedEvent(sone.get()));
+               eventBus.post(new SoneRemovedEvent(sone));
+               database.removeSone(sone);
        }
 
        /**