X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=e3fd103d73495c230eaa7fa0314617ef673c1310;hb=df37c71a5b53af746f2c0c8b7ce8c1d4ae33486f;hp=3462f886ffd2906a5a30ae008ef6a372b128859c;hpb=b90a657060b67444ca5d8a403b9cca928ea4fbbd;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 3462f88..e3fd103 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -23,8 +23,11 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.primitives.Longs.tryParse;
import static java.lang.String.format;
import static java.util.logging.Level.WARNING;
+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;
@@ -70,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;
@@ -79,7 +83,6 @@ import net.pterodactylus.sone.database.PostReplyBuilder;
import net.pterodactylus.sone.database.PostReplyProvider;
import net.pterodactylus.sone.database.SoneBuilder;
import net.pterodactylus.sone.database.SoneProvider;
-import net.pterodactylus.sone.fcp.FcpInterface;
import net.pterodactylus.sone.freenet.wot.Identity;
import net.pterodactylus.sone.freenet.wot.IdentityManager;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
@@ -91,17 +94,20 @@ import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
import net.pterodactylus.sone.main.SonePlugin;
import net.pterodactylus.util.config.Configuration;
import net.pterodactylus.util.config.ConfigurationException;
-import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.service.AbstractService;
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.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
+import com.google.common.collect.Ordering;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
@@ -113,10 +119,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 = Logging.getLogger(Core.class);
+ private static final Logger logger = getLogger(Core.class.getName());
/** The start time. */
private final long startupTime = System.currentTimeMillis();
@@ -154,6 +160,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();
@@ -175,10 +183,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** The post database. */
private final Database database;
- /** All bookmarked posts. */
- /* synchronize access on itself. */
- private final Set bookmarkedPosts = new HashSet();
-
/** Trusted identities, sorted by own identities. */
private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.create());
@@ -277,6 +281,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.
*
@@ -323,6 +339,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return database.getSones();
}
+ @Override
+ public Function> soneLoader() {
+ return database.soneLoader();
+ }
+
/**
* Returns the Sone with the given ID, regardless whether itâs local or
* remote.
@@ -419,8 +440,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();
}
/**
@@ -462,7 +492,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Override
public List getReplies(final String postId) {
- return database.getReplies(postId);
+ Builder postReplies = ImmutableList.builder().addAll(database.getReplies(postId));
+ if (isCompatibilityMode(CompatibilityMode.oldElementIds)) {
+ Optional post = getPost(postId);
+ if (post.isPresent()) {
+ postReplies.addAll(database.getReplies(post.get().getInternalId()));
+ }
+ }
+ return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(postReplies.build());
}
/**
@@ -478,6 +515,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
if (sone.getLikedPostIds().contains(post.getId())) {
sones.add(sone);
}
+ if (isCompatibilityMode(CompatibilityMode.oldElementIds)) {
+ if (sone.getLikedPostIds().contains(post.getInternalId())) {
+ sones.add(sone);
+ }
+ }
}
return sones;
}
@@ -495,6 +537,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
if (sone.getLikedReplyIds().contains(reply.getId())) {
sones.add(sone);
}
+ if (isCompatibilityMode(CompatibilityMode.oldElementIds)) {
+ if (sone.getLikedReplyIds().contains(reply.getInternalId())) {
+ sones.add(sone);
+ }
+ }
}
return sones;
}
@@ -508,21 +555,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* otherwise
*/
public boolean isBookmarked(Post post) {
- return isPostBookmarked(post.getId());
- }
-
- /**
- * Returns whether the post with the given ID is bookmarked.
- *
- * @param id
- * The ID of the post to check
- * @return {@code true} if the post with the given ID is bookmarked,
- * {@code false} otherwise
- */
- public boolean isPostBookmarked(String id) {
- synchronized (bookmarkedPosts) {
- return bookmarkedPosts.contains(id);
- }
+ return database.isPostBookmarked(post);
}
/**
@@ -531,16 +564,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return All bookmarked posts
*/
public Set getBookmarkedPosts() {
- Set posts = new HashSet();
- synchronized (bookmarkedPosts) {
- for (String bookmarkedPostId : bookmarkedPosts) {
- Optional post = getPost(bookmarkedPostId);
- if (post.isPresent()) {
- posts.add(post.get());
- }
- }
- }
- return posts;
+ return database.getBookmarkedPosts();
}
public AlbumBuilder albumBuilder() {
@@ -556,8 +580,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() {
@@ -663,16 +687,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
- sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), 0L));
+ String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0");
+ sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
sone.setKnown(true);
- /* TODO - load posts ân stuff */
SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
eventBus.register(soneInserter);
synchronized (soneInserters) {
soneInserters.put(sone, soneInserter);
}
loadSone(sone);
+ database.storeSone(sone);
sone.setStatus(SoneStatus.idle);
soneInserter.start();
return sone;
@@ -709,8 +734,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();
@@ -749,7 +774,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
public void followSone(Sone sone, String soneId) {
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
- sone.addFriend(soneId);
+ database.addFriend(sone, soneId);
synchronized (soneFollowingTimes) {
if (!soneFollowingTimes.containsKey(soneId)) {
long now = System.currentTimeMillis();
@@ -784,7 +809,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
public void unfollowSone(Sone sone, String soneId) {
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
- sone.removeFriend(soneId);
+ database.removeFriend(sone, soneId);
boolean unfollowedSoneStillFollowed = false;
for (Sone localSone : getLocalSones()) {
unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
@@ -894,42 +919,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
return;
}
- /* find removed posts. */
- SoneChangeDetector soneChangeDetector = new SoneChangeDetector(storedSone.get());
- soneChangeDetector.onNewPosts(new PostProcessor() {
- @Override
- public void processPost(Post post) {
- if (post.getTime() < getSoneFollowingTime(sone)) {
- post.setKnown(true);
- } else if (!post.isKnown()) {
- eventBus.post(new NewPostFoundEvent(post));
- }
- }
- });
- soneChangeDetector.onRemovedPosts(new PostProcessor() {
- @Override
- public void processPost(Post post) {
- eventBus.post(new PostRemovedEvent(post));
- }
- });
- soneChangeDetector.onNewPostReplies(new PostReplyProcessor() {
- @Override
- public void processPostReply(PostReply postReply) {
- if (postReply.getTime() < getSoneFollowingTime(sone)) {
- postReply.setKnown(true);
- } else if (!postReply.isKnown()) {
- eventBus.post(new NewPostReplyFoundEvent(postReply));
- }
- }
- });
- soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() {
- @Override
- public void processPostReply(PostReply postReply) {
- eventBus.post(new PostReplyRemovedEvent(postReply));
- }
- });
- soneChangeDetector.detectChanges(sone);
+ List