Fix loading of local Sones without posts and replies.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index 12ef439..561f14f 100644 (file)
@@ -132,6 +132,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        private final Map<String, Image> allImages = new HashMap<String, Image>();
        private final Multimap<String, Image> soneImages = HashMultimap.create();
 
+       private final MemorySoneDatabase soneDatabase;
        private final MemoryPostDatabase postDatabase;
        private final MemoryBookmarkDatabase memoryBookmarkDatabase;
        private final MemoryFriendDatabase memoryFriendDatabase;
@@ -149,6 +150,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                this.soneProvider = soneProvider;
                this.configuration = configuration;
                this.configurationLoader = new ConfigurationLoader(configuration);
+               soneDatabase = new MemorySoneDatabase(configurationLoader);
                postDatabase = new MemoryPostDatabase(this, configurationLoader);
                memoryBookmarkDatabase =
                                new MemoryBookmarkDatabase(this, configurationLoader);
@@ -173,52 +175,63 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
-       public Sone registerLocalSone(OwnIdentity ownIdentity) {
-               final Sone localSone = loadLocalSone(ownIdentity);
+       public LocalSone registerLocalSone(OwnIdentity ownIdentity) {
+               final LocalSone localSone = loadLocalSone(ownIdentity);
                localSones.add(ownIdentity.getId());
                return localSone;
        }
 
-       private Sone loadLocalSone(OwnIdentity ownIdentity) {
-               Sone localSone = newSoneBuilder().local().from(ownIdentity).build();
+       private LocalSone loadLocalSone(OwnIdentity ownIdentity) {
+               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.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
-               localSone.setKnown(true);
-
-               loadSone(localSone);
                return localSone;
        }
 
-       public void loadSone(Sone 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();
@@ -229,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;
@@ -241,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;
@@ -273,7 +286,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                /* if we’re still here, Sone was loaded successfully. */
                lock.writeLock().lock();
                try {
-                       sone.setTime(soneTime);
+                       updateSoneTime(sone, soneTime);
                        sone.setProfile(profile);
                        sone.setLikePostIds(likedPostIds);
                        sone.setLikeReplyIds(likedReplyIds);
@@ -282,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));
        }
@@ -461,6 +468,8 @@ public class MemoryDatabase extends AbstractService implements Database {
        /** {@inheritDocs} */
        @Override
        protected void doStart() {
+               soneDatabase.start();
+               memoryFriendDatabase.start();
                postDatabase.start();
                memoryBookmarkDatabase.start();
                loadKnownPostReplies();
@@ -471,6 +480,8 @@ public class MemoryDatabase extends AbstractService implements Database {
        @Override
        protected void doStop() {
                try {
+                       soneDatabase.stop();
+                       memoryFriendDatabase.stop();
                        postDatabase.stop();
                        memoryBookmarkDatabase.stop();
                        save();
@@ -501,6 +512,21 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       @Override
+       public boolean isSoneKnown(Sone sone) {
+               return soneDatabase.isKnownSone(sone.getId());
+       }
+
+       @Override
+       public void setSoneKnown(Sone sone) {
+               soneDatabase.setSoneKnown(sone.getId());
+       }
+
+       @Override
+       public void updateSoneTime(Sone sone, long soneTime) {
+               soneDatabase.updateSoneTime(sone.getId(), soneTime);
+       }
+
        private void storePosts(String soneId, Collection<Post> posts) {
                postDatabase.storePosts(soneId, posts);
        }
@@ -583,10 +609,16 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
-       public Collection<Sone> getLocalSones() {
+       public Collection<LocalSone> getLocalSones() {
                lock.readLock().lock();
                try {
-                       return from(allSones.values()).filter(LOCAL_SONE_FILTER).toSet();
+                       return from(allSones.values()).filter(LOCAL_SONE_FILTER).transform(new Function<Sone, LocalSone>() {
+                               @Override
+                               public LocalSone apply(Sone sone) {
+                                       // FIXME – Sones will not always implement LocalSone
+                                       return (LocalSone) sone;
+                               }
+                       }).toSet();
                } finally {
                        lock.readLock().unlock();
                }
@@ -604,7 +636,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
-       public Collection<String> getFriends(Sone localSone) {
+       public Collection<String> getFriends(LocalSone localSone) {
                if (!localSone.isLocal()) {
                        return Collections.emptySet();
                }
@@ -612,7 +644,12 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
-       public boolean isFriend(Sone localSone, String friendSoneId) {
+       public Optional<Long> getSoneFollowingTime(String remoteSoneId) {
+               return memoryFriendDatabase.getSoneFollowingTime(remoteSoneId);
+       }
+
+       @Override
+       public boolean isFriend(LocalSone localSone, String friendSoneId) {
                if (!localSone.isLocal()) {
                        return false;
                }
@@ -620,18 +657,12 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
-       public void addFriend(Sone localSone, String friendSoneId) {
-               if (!localSone.isLocal()) {
-                       return;
-               }
+       public void addFriend(LocalSone localSone, String friendSoneId) {
                memoryFriendDatabase.addFriend(localSone.getId(), friendSoneId);
        }
 
        @Override
-       public void removeFriend(Sone localSone, String friendSoneId) {
-               if (!localSone.isLocal()) {
-                       return;
-               }
+       public void removeFriend(LocalSone localSone, String friendSoneId) {
                memoryFriendDatabase.removeFriend(localSone.getId(), friendSoneId);
        }