X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryDatabase.java;h=561f14f5e498d9ed7623cc773fabd9d7f880d4fe;hp=8815abc5c21cd35632f7eb8ce9667a97a22c09d5;hb=851ef7a7f4e25fe3c57c2d9c67349acce58f1ddc;hpb=f8672b1385173a103d7f085d8e9cd43bc5762d71 diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 8815abc..561f14f 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -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 posts; try { - posts = configurationSoneParser.parsePosts(this); + Set 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 postReplies; try { - postReplies = configurationSoneParser.parsePostReplies(this); + Set 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 likedPostIds = configurationSoneParser.parseLikedPostIds(); @@ -231,7 +242,7 @@ public class MemoryDatabase extends AbstractService implements Database { /* load albums. */ List 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)); }