X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=140d80ce354353c38e9cfd0078640648ea4a8846;hb=00a434a23c9ea1e57c63d8a3c0fc4b09277af431;hp=73bb95fea97b87fb73c335d0d5b0d573763b3254;hpb=976fdebc0a2a33a5c5af3c724376da56e1319607;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java
index 73bb95f..140d80c 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -27,6 +27,7 @@ import static java.util.logging.Logger.getLogger;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -72,6 +73,7 @@ import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
import net.pterodactylus.sone.data.Sone.SoneStatus;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.AlbumProvider;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.DatabaseException;
import net.pterodactylus.sone.database.ImageBuilder;
@@ -98,6 +100,7 @@ import net.pterodactylus.util.thread.NamedThreadFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
@@ -113,10 +116,10 @@ import com.google.inject.Singleton;
* @author David âBombeâ Roden
*/
@Singleton
-public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
+public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider, AlbumProvider {
/** The logger. */
- private static final Logger logger = getLogger("Sone.Core");
+ private static final Logger logger = getLogger(Core.class.getName());
/** The start time. */
private final long startupTime = System.currentTimeMillis();
@@ -154,6 +157,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** The trust updater. */
private final WebOfTrustUpdater webOfTrustUpdater;
+ private final Set compatibilityModes = EnumSet.noneOf(CompatibilityMode.class);
+
/** The times Sones were followed. */
private final Map soneFollowingTimes = new HashMap();
@@ -273,6 +278,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return updateChecker;
}
+ public boolean isCompatibilityMode(CompatibilityMode compatibilityMode) {
+ return compatibilityModes.contains(compatibilityMode);
+ }
+
+ public void setCompatibilityMode(CompatibilityMode compatibilityMode) {
+ compatibilityModes.add(compatibilityMode);
+ }
+
+ public void clearCompatibilityMod(CompatibilityMode compatibilityMode) {
+ compatibilityModes.remove(compatibilityMode);
+ }
+
/**
* Returns the Sone rescuer for the given local Sone.
*
@@ -420,8 +437,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* {@inheritDoc}
*/
@Override
- public Optional getPost(String postId) {
- return database.getPost(postId);
+ public Optional getPost(final String postId) {
+ Optional post = database.getPost(postId);
+ if (post.isPresent() || !isCompatibilityMode(CompatibilityMode.oldElementIds)) {
+ return post;
+ }
+ return FluentIterable.from(getSones()).transformAndConcat(Sone.toAllPosts).filter(new Predicate() {
+ @Override
+ public boolean apply(Post input) {
+ return (input != null) && input.getInternalId().equals(postId);
+ }
+ }).first();
}
/**
@@ -534,8 +560,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The album with the given ID, or {@code null} if no album with the
* given ID exists
*/
- public Album getAlbum(String albumId) {
- return database.getAlbum(albumId).orNull();
+ public Optional getAlbum(String albumId) {
+ return database.getAlbum(albumId);
}
public ImageBuilder imageBuilder() {
@@ -651,6 +677,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
soneInserters.put(sone, soneInserter);
}
loadSone(sone);
+ database.storeSone(sone);
sone.setStatus(SoneStatus.idle);
soneInserter.start();
return sone;
@@ -687,8 +714,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.WARNING, "Given Identity is null!");
return null;
}
- final Long latestEdition = tryParse(fromNullable(
- identity.getProperty("Sone.LatestEdition")).or("0"));
+ String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
+ long latestEdition = fromNullable(tryParse(property)).or(0L);
Optional existingSone = getSone(identity.getId());
if (existingSone.isPresent() && existingSone.get().isLocal()) {
return existingSone.get();
@@ -1064,11 +1091,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
/* load options. */
- sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
- sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
- sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
- sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
- sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
+ sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
+ sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
+ sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
+ sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
+ sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
/* if weâre still here, Sone was loaded successfully. */
@@ -1085,7 +1112,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
for (Album album : topLevelAlbums) {
sone.getRootAlbum().addAlbum(album);
}
- database.storeSone(sone);
synchronized (soneInserters) {
soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
}
@@ -1476,7 +1502,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
int postCounter = 0;
for (Post post : sone.getPosts()) {
String postPrefix = sonePrefix + "/Posts/" + postCounter++;
- configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
+ configuration.getStringValue(postPrefix + "/ID").setValue(post.getInternalId());
configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
@@ -1487,7 +1513,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
int replyCounter = 0;
for (PostReply reply : sone.getReplies()) {
String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
- configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
+ configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getInternalId());
configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
@@ -1514,7 +1540,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
int albumCounter = 0;
for (Album album : albums) {
String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
- configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
+ configuration.getStringValue(albumPrefix + "/ID").setValue(album.getInternalId());
configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
@@ -1530,8 +1556,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
continue;
}
String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
- configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
- configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
+ configuration.getStringValue(imagePrefix + "/ID").setValue(image.getInternalId());
+ configuration.getStringValue(imagePrefix + "/Album").setValue(album.getInternalId());
configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());
@@ -1596,6 +1622,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
}
+ /* save compatibility modes. */
+ configuration.getBooleanValue("CompatibilityModes/OldElementIds").setValue(compatibilityModes.contains(CompatibilityMode.oldElementIds));
+
/* save known posts. */
database.save();
@@ -1644,6 +1673,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
++soneCounter;
}
+
+ /* load compatibility modes. */
+ if (configuration.getBooleanValue("CompatibilityModes/OldElementIds").getValue(false)) {
+ setCompatibilityMode(CompatibilityMode.oldElementIds);
+ }
}
/**