Small optimization when an identity is removed.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 8c1aeed..7d38ed9 100644 (file)
@@ -25,6 +25,7 @@ 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 java.net.MalformedURLException;
 import java.util.ArrayList;
@@ -77,6 +78,7 @@ import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.database.AlbumBuilder;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.DatabaseException;
+import net.pterodactylus.sone.database.ImageBuilder;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostProvider;
 import net.pterodactylus.sone.database.PostReplyBuilder;
@@ -609,6 +611,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                return database.getAlbum(albumId).orNull();
        }
 
+       public ImageBuilder imageBuilder() {
+               return database.newImageBuilder();
+       }
+
        /**
         * Returns the image with the given ID, creating it if necessary.
         *
@@ -1008,7 +1014,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        for (PostReply newPostReply : newPostReplies) {
                                eventBus.post(new NewPostReplyFoundEvent(newPostReply));
                        }
-                       for (Album album : sone.getRootAlbum().getAlbums()) {
+                       for (Album album : toAllAlbums.apply(sone)) {
                                database.storeAlbum(album);
                                for (Image image : album.getImages()) {
                                        database.storeImage(image);
@@ -1193,6 +1199,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                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 (knownSones) {
                        for (String friend : friends) {
@@ -1940,19 +1952,14 @@ 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()) {
                        /* TODO - we don’t have the Sone anymore. should this happen? */