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;
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);
}
@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());
+ private void loadElements(SoneBuilder soneBuilder, String soneId) {
+ long soneTime = configurationLoader.getLocalSoneTime(soneId);
if (soneTime == -1) {
return;
}
+ soneBuilder.lastUpdated(soneTime);
- /* load profile. */
- ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
- Profile profile = configurationSoneParser.parseProfile();
+ ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, soneId);
- /* 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) {
+ /* load profile. */
+ ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone.getId());
+ Profile profile = configurationSoneParser.parseProfile(sone);
/* load post likes. */
Set<String> likedPostIds = configurationSoneParser.parseLikedPostIds();
/* 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;
/* load images. */
try {
- configurationSoneParser.parseImages(this);
+ configurationSoneParser.parseImages(this, sone);
} catch (InvalidImageFound iif) {
logger.log(WARNING, "Invalid image found, aborting load!");
return;
/* if we’re still here, Sone was loaded successfully. */
lock.writeLock().lock();
try {
- sone.setTime(soneTime);
+ updateSoneTime(sone, sone.getTime());
sone.setProfile(profile);
sone.setLikePostIds(likedPostIds);
sone.setLikeReplyIds(likedReplyIds);
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));
}
/** {@inheritDocs} */
@Override
protected void doStart() {
+ soneDatabase.start();
+ memoryFriendDatabase.start();
postDatabase.start();
memoryBookmarkDatabase.start();
loadKnownPostReplies();
@Override
protected void doStop() {
try {
+ soneDatabase.stop();
+ memoryFriendDatabase.stop();
postDatabase.stop();
memoryBookmarkDatabase.stop();
save();
}
}
+ @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);
}
}
@Override
- public Collection<String> getFriends(Sone localSone) {
+ public Collection<String> getFriends(LocalSone localSone) {
if (!localSone.isLocal()) {
return Collections.emptySet();
}
}
@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;
}
}
@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);
}