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=7d38ed9373416acd97f26dcc04732831b94f502b;hpb=f2eaa4ab7f0055a704425f2159efaa5be46b7c5e;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 7d38ed9..140d80c 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -20,16 +20,14 @@ package net.pterodactylus.sone.core;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Predicates.not;
import static com.google.common.primitives.Longs.tryParse;
import static java.lang.String.format;
import static java.util.logging.Level.WARNING;
-import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
-import static net.pterodactylus.sone.data.Sone.toAllAlbums;
+import static java.util.logging.Logger.getLogger;
-import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -48,8 +46,8 @@ import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
-import net.pterodactylus.sone.core.Options.DefaultOption;
-import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor;
import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
@@ -73,9 +71,9 @@ import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
import net.pterodactylus.sone.data.Sone.SoneStatus;
-import net.pterodactylus.sone.data.SoneImpl;
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;
@@ -83,8 +81,8 @@ import net.pterodactylus.sone.database.PostBuilder;
import net.pterodactylus.sone.database.PostProvider;
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;
@@ -94,20 +92,17 @@ import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
import net.pterodactylus.sone.main.SonePlugin;
-import net.pterodactylus.sone.utils.IntegerRangePredicate;
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.Predicates;
+import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap;
-import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.eventbus.EventBus;
@@ -115,27 +110,22 @@ import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;
-import freenet.keys.FreenetURI;
-
/**
* The Sone core.
*
* @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();
- /** The options. */
- private final Options options = new Options();
-
/** The preferences. */
- private final Preferences preferences = new Preferences(options);
+ private final Preferences preferences;
/** The event bus. */
private final EventBus eventBus;
@@ -167,8 +157,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** The trust updater. */
private final WebOfTrustUpdater webOfTrustUpdater;
- /** The FCP interface. */
- private volatile FcpInterface fcpInterface;
+ private final Set compatibilityModes = EnumSet.noneOf(CompatibilityMode.class);
/** The times Sones were followed. */
private final Map soneFollowingTimes = new HashMap();
@@ -185,20 +174,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/* synchronize access on this on sones. */
private final Map soneRescuers = new HashMap();
- /** All Sones. */
- /* synchronize access on this on itself. */
- private final Map sones = new HashMap();
-
/** All known Sones. */
private final Set knownSones = new HashSet();
/** 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());
@@ -239,6 +220,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
this.database = database;
+ preferences = new Preferences(eventBus);
}
@VisibleForTesting
@@ -253,6 +235,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
this.database = database;
+ preferences = new Preferences(eventBus);
}
//
@@ -295,14 +278,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return updateChecker;
}
- /**
- * Sets the FCP interface to use.
- *
- * @param fcpInterface
- * The FCP interface to use
- */
- public void setFcpInterface(FcpInterface fcpInterface) {
- this.fcpInterface = fcpInterface;
+ 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);
}
/**
@@ -315,7 +300,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
public SoneRescuer getSoneRescuer(Sone sone) {
checkNotNull(sone, "sone must not be null");
checkArgument(sone.isLocal(), "sone must be local");
- synchronized (sones) {
+ synchronized (soneRescuers) {
SoneRescuer soneRescuer = soneRescuers.get(sone);
if (soneRescuer == null) {
soneRescuer = new SoneRescuer(this, soneDownloader, sone);
@@ -339,14 +324,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
}
+ public SoneBuilder soneBuilder() {
+ return database.newSoneBuilder();
+ }
+
/**
* {@inheritDocs}
*/
@Override
public Collection getSones() {
- synchronized (sones) {
- return ImmutableSet.copyOf(sones.values());
- }
+ return database.getSones();
+ }
+
+ @Override
+ public Function> soneLoader() {
+ return database.soneLoader();
}
/**
@@ -360,9 +352,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Override
public Optional getSone(String id) {
- synchronized (sones) {
- return Optional.fromNullable(sones.get(id));
- }
+ return database.getSone(id);
}
/**
@@ -370,9 +360,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Override
public Collection getLocalSones() {
- synchronized (sones) {
- return FluentIterable.from(sones.values()).filter(LOCAL_SONE_FILTER).toSet();
- }
+ return database.getLocalSones();
}
/**
@@ -380,24 +368,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*
* @param id
* The ID of the Sone
- * @param create
- * {@code true} to create a new Sone if none exists,
- * {@code false} to return null if none exists
* @return The Sone with the given ID, or {@code null}
*/
- public Sone getLocalSone(String id, boolean create) {
- synchronized (sones) {
- Sone sone = sones.get(id);
- if ((sone == null) && create) {
- sone = new SoneImpl(id, true);
- sones.put(id, sone);
- }
- if ((sone != null) && !sone.isLocal()) {
- sone = new SoneImpl(id, true);
- sones.put(id, sone);
- }
- return sone;
+ public Sone getLocalSone(String id) {
+ Optional sone = database.getSone(id);
+ if (sone.isPresent() && sone.get().isLocal()) {
+ return sone.get();
}
+ return null;
}
/**
@@ -405,30 +383,19 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Override
public Collection getRemoteSones() {
- synchronized (sones) {
- return FluentIterable.from(sones.values()).filter(not(LOCAL_SONE_FILTER)).toSet();
- }
+ return database.getRemoteSones();
}
/**
* Returns the remote Sone with the given ID.
*
+ *
* @param id
* The ID of the remote Sone to get
- * @param create
- * {@code true} to always create a Sone, {@code false} to return
- * {@code null} if no Sone with the given ID exists
* @return The Sone with the given ID
*/
- public Sone getRemoteSone(String id, boolean create) {
- synchronized (sones) {
- Sone sone = sones.get(id);
- if ((sone == null) && create && (id != null) && (id.length() == 43)) {
- sone = new SoneImpl(id, false);
- sones.put(id, sone);
- }
- return sone;
- }
+ public Sone getRemoteSone(String id) {
+ return database.getSone(id).orNull();
}
/**
@@ -470,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();
}
/**
@@ -559,21 +535,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);
}
/**
@@ -582,16 +544,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() {
@@ -607,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() {
@@ -713,26 +666,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return null;
}
logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
- synchronized (sones) {
- final Sone sone;
- try {
- sone = getLocalSone(ownIdentity.getId(), true).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
- } catch (MalformedURLException mue1) {
- logger.log(Level.SEVERE, String.format("Could not convert the Identityâs URIs to Freenet URIs: %s, %s", ownIdentity.getInsertUri(), ownIdentity.getRequestUri()), mue1);
- return null;
- }
- sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
- sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
- sone.setKnown(true);
- /* TODO - load posts ân stuff */
- sones.put(ownIdentity.getId(), sone);
- final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone);
+ Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
+ 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);
+ SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
+ eventBus.register(soneInserter);
+ synchronized (soneInserters) {
soneInserters.put(sone, soneInserter);
- sone.setStatus(SoneStatus.idle);
- loadSone(sone);
- soneInserter.start();
- return sone;
}
+ loadSone(sone);
+ database.storeSone(sone);
+ sone.setStatus(SoneStatus.idle);
+ soneInserter.start();
+ return sone;
}
/**
@@ -766,35 +714,33 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.WARNING, "Given Identity is null!");
return null;
}
- final Long latestEdition = fromNullable(tryParse(
- identity.getProperty("Sone.LatestEdition"))).or(0L);
- synchronized (sones) {
- final Sone sone = getRemoteSone(identity.getId(), true);
- if (sone.isLocal()) {
- return sone;
+ 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();
+ }
+ boolean newSone = !existingSone.isPresent();
+ Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build();
+ sone.setLatestEdition(latestEdition);
+ if (newSone) {
+ synchronized (knownSones) {
+ newSone = !knownSones.contains(sone.getId());
}
- sone.setIdentity(identity);
- boolean newSone = sone.getRequestUri() == null;
- sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
- sone.setLatestEdition(latestEdition);
+ sone.setKnown(!newSone);
if (newSone) {
- synchronized (knownSones) {
- newSone = !knownSones.contains(sone.getId());
- }
- sone.setKnown(!newSone);
- if (newSone) {
- eventBus.post(new NewSoneFoundEvent(sone));
- for (Sone localSone : getLocalSones()) {
- if (localSone.getOptions().isAutoFollow()) {
- followSone(localSone, sone.getId());
- }
+ eventBus.post(new NewSoneFoundEvent(sone));
+ for (Sone localSone : getLocalSones()) {
+ if (localSone.getOptions().isAutoFollow()) {
+ followSone(localSone, sone.getId());
}
}
}
- soneDownloader.addSone(sone);
- soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
- return sone;
}
+ database.storeSone(sone);
+ soneDownloader.addSone(sone);
+ soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
+ return sone;
}
/**
@@ -808,7 +754,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();
@@ -843,7 +789,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);
@@ -946,91 +892,67 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* {@code true} if the stored Sone should be updated regardless
* of the age of the given Sone
*/
- public void updateSone(Sone sone, boolean soneRescueMode) {
+ public void updateSone(final Sone sone, boolean soneRescueMode) {
Optional storedSone = getSone(sone.getId());
if (storedSone.isPresent()) {
if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) {
logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
return;
}
- /* find removed posts. */
- Collection removedPosts = new ArrayList();
- Collection newPosts = new ArrayList();
- Collection existingPosts = database.getPosts(sone.getId());
- for (Post oldPost : existingPosts) {
- if (!sone.getPosts().contains(oldPost)) {
- removedPosts.add(oldPost);
- }
- }
- /* find new posts. */
- for (Post newPost : sone.getPosts()) {
- if (existingPosts.contains(newPost)) {
- continue;
- }
- if (newPost.getTime() < getSoneFollowingTime(sone)) {
- newPost.setKnown(true);
- } else if (!newPost.isKnown()) {
- newPosts.add(newPost);
- }
- }
- /* store posts. */
- database.storePosts(sone, sone.getPosts());
- Collection newPostReplies = new ArrayList();
- Collection removedPostReplies = new ArrayList();
- if (!soneRescueMode) {
- for (PostReply reply : storedSone.get().getReplies()) {
- if (!sone.getReplies().contains(reply)) {
- removedPostReplies.add(reply);
- }
- }
+ List