Fix loading of local Sones without posts and replies.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index 8815abc..561f14f 100644 (file)
@@ -182,45 +182,56 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        private LocalSone loadLocalSone(OwnIdentity ownIdentity) {
-               LocalSone localSone = newSoneBuilder().from(ownIdentity).using(
-                               new Client("Sone", SonePlugin.VERSION.toString())).buildLocal();
+               final SoneBuilder soneBuilder = newSoneBuilder().from(ownIdentity).using(
+                               new Client("Sone", SonePlugin.VERSION.toString()));
+
+               loadElements(soneBuilder, ownIdentity.getId());
+
+               LocalSone localSone = soneBuilder.buildLocal();
+               loadSone(localSone);
+               localSone.setKnown(true);
                localSone.setLatestEdition(
                                Optional.fromNullable(
                                                Longs.tryParse(ownIdentity.getProperty(LATEST_EDITION_PROPERTY)))
                                .or(0L));
-               localSone.setKnown(true);
-
-               loadSone(localSone);
                return localSone;
        }
 
-       private void loadSone(LocalSone sone) {
-               long soneTime = configurationLoader.getLocalSoneTime(sone.getId());
-               if (soneTime == -1) {
-                       return;
-               }
+       private void loadElements(SoneBuilder soneBuilder, String soneId) {
+               ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, soneId);
 
-               /* load profile. */
-               ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
-               Profile profile = configurationSoneParser.parseProfile();
-
-               /* load posts. */
-               Collection<Post> posts;
                try {
-                       posts = configurationSoneParser.parsePosts(this);
+                       Set<Post> posts = configurationSoneParser.parsePosts(this);
+                       soneBuilder.withPosts(posts);
+                       for (Post post : posts) {
+                               post.setKnown(true);
+                       }
                } catch (InvalidPostFound ipf) {
                        logger.log(Level.WARNING, "Invalid post found, aborting load!");
                        return;
                }
 
-               /* load replies. */
-               Collection<PostReply> postReplies;
                try {
-                       postReplies = configurationSoneParser.parsePostReplies(this);
+                       Set<PostReply> postReplies = configurationSoneParser.parsePostReplies(this);
+                       soneBuilder.withPostReplies(postReplies);
+                       for (PostReply reply : postReplies) {
+                               reply.setKnown(true);
+                       }
                } catch (InvalidPostReplyFound iprf) {
                        logger.log(Level.WARNING, "Invalid reply found, aborting load!");
                        return;
                }
+       }
+
+       private void loadSone(LocalSone sone) {
+               long soneTime = configurationLoader.getLocalSoneTime(sone.getId());
+               if (soneTime == -1) {
+                       return;
+               }
+
+               /* load profile. */
+               ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone.getId());
+               Profile profile = configurationSoneParser.parseProfile(sone);
 
                /* load post likes. */
                Set<String> likedPostIds = configurationSoneParser.parseLikedPostIds();
@@ -231,7 +242,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                /* load albums. */
                List<Album> topLevelAlbums;
                try {
-                       topLevelAlbums = configurationSoneParser.parseTopLevelAlbums(this);
+                       topLevelAlbums = configurationSoneParser.parseTopLevelAlbums(this, sone);
                } catch (InvalidAlbumFound iaf) {
                        logger.log(Level.WARNING, "Invalid album found, aborting load!");
                        return;
@@ -243,7 +254,7 @@ public class MemoryDatabase extends AbstractService implements Database {
 
                /* load images. */
                try {
-                       configurationSoneParser.parseImages(this);
+                       configurationSoneParser.parseImages(this, sone);
                } catch (InvalidImageFound iif) {
                        logger.log(WARNING, "Invalid image found, aborting load!");
                        return;
@@ -284,19 +295,13 @@ public class MemoryDatabase extends AbstractService implements Database {
                        lastInsertFingerprints.put(sone.getId(), lastInsertFingerprint);
 
                        allSones.put(sone.getId(), sone);
-                       storePosts(sone.getId(), posts);
-                       storePostReplies(sone.getId(), postReplies);
+                       storePosts(sone.getId(), sone.getPosts());
+                       storePostReplies(sone.getId(), sone.getReplies());
                        storeAlbums(sone.getId(), topLevelAlbums);
                        storeImages(sone.getId(), from(topLevelAlbums).transformAndConcat(Album.FLATTENER).transformAndConcat(Album.IMAGES).toList());
                } finally {
                        lock.writeLock().unlock();
                }
-               for (Post post : posts) {
-                       post.setKnown(true);
-               }
-               for (PostReply reply : postReplies) {
-                       reply.setKnown(true);
-               }
 
                logger.info(String.format("Sone loaded successfully: %s", sone));
        }