X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=4474e469a844a4aa424739afb8748cd4b7a86720;hb=b50431949441894198b9f310cd07f2bdcf7231de;hp=860effbece1bad5da0cbedceccc3e4e522dd10ea;hpb=88731847148b7fbea5d578abe968d42daf3d1b58;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 860effb..4474e46 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -1,5 +1,5 @@
/*
- * Sone - Core.java - Copyright © 2010â2013 David Roden
+ * Sone - Core.java - Copyright © 2010â2016 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,12 +17,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 net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
+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.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -38,10 +40,16 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
-import net.pterodactylus.sone.core.Options.DefaultOption;
-import net.pterodactylus.sone.core.Options.Option;
-import net.pterodactylus.sone.core.Options.OptionWatcher;
-import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
+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.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;
@@ -63,19 +71,19 @@ import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Profile.Field;
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.SoneOptions.LoadExternalContent;
import net.pterodactylus.sone.data.TemporaryImage;
+import net.pterodactylus.sone.database.AlbumBuilder;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.DatabaseException;
+import net.pterodactylus.sone.database.ImageBuilder;
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.fcp.FcpInterface.FullAccessRequired;
import net.pterodactylus.sone.freenet.wot.Identity;
import net.pterodactylus.sone.freenet.wot.IdentityManager;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
@@ -85,45 +93,39 @@ 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.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;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
-
-import freenet.keys.FreenetURI;
+import com.google.inject.Singleton;
/**
* The Sone core.
*
* @author David âBombeâ Roden
*/
+@Singleton
public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
/** 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;
@@ -155,9 +157,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** The trust updater. */
private final WebOfTrustUpdater webOfTrustUpdater;
- /** The FCP interface. */
- private volatile FcpInterface fcpInterface;
-
/** The times Sones were followed. */
private final Map soneFollowingTimes = new HashMap();
@@ -173,20 +172,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());
@@ -216,17 +207,33 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* The database
*/
@Inject
- public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
+ public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
+ super("Sone Core");
+ this.configuration = configuration;
+ this.freenetInterface = freenetInterface;
+ this.identityManager = identityManager;
+ this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface);
+ this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier());
+ this.updateChecker = updateChecker;
+ this.webOfTrustUpdater = webOfTrustUpdater;
+ this.eventBus = eventBus;
+ this.database = database;
+ preferences = new Preferences(eventBus);
+ }
+
+ @VisibleForTesting
+ protected Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
super("Sone Core");
this.configuration = configuration;
this.freenetInterface = freenetInterface;
this.identityManager = identityManager;
- this.soneDownloader = new SoneDownloader(this, freenetInterface);
- this.imageInserter = new ImageInserter(freenetInterface);
- this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
+ this.soneDownloader = soneDownloader;
+ this.imageInserter = imageInserter;
+ this.updateChecker = updateChecker;
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
this.database = database;
+ preferences = new Preferences(eventBus);
}
//
@@ -270,16 +277,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
/**
- * Sets the FCP interface to use.
- *
- * @param fcpInterface
- * The FCP interface to use
- */
- public void setFcpInterface(FcpInterface fcpInterface) {
- this.fcpInterface = fcpInterface;
- }
-
- /**
* Returns the Sone rescuer for the given local Sone.
*
* @param sone
@@ -289,7 +286,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);
@@ -313,14 +310,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();
}
/**
@@ -334,9 +338,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);
}
/**
@@ -344,9 +346,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();
}
/**
@@ -354,24 +354,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;
}
/**
@@ -379,30 +369,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();
}
/**
@@ -432,22 +411,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
/**
- * Returns whether the target Sone is trusted by the origin Sone.
- *
- * @param origin
- * The origin Sone
- * @param target
- * The target Sone
- * @return {@code true} if the target Sone is trusted by the origin Sone
- */
- public boolean isSoneTrusted(Sone origin, Sone target) {
- checkNotNull(origin, "origin must not be null");
- checkNotNull(target, "target must not be null");
- checkArgument(origin.getIdentity() instanceof OwnIdentity, "originâs identity must be an OwnIdentity");
- return trustedIdentities.containsEntry(origin.getIdentity(), target.getIdentity());
- }
-
- /**
* Returns a post builder.
*
* @return A new post builder
@@ -549,21 +512,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);
}
/**
@@ -572,28 +521,11 @@ 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();
}
- /**
- * Returns the album with the given ID, creating a new album if no album
- * with the given ID can be found.
- *
- * @param albumId
- * The ID of the album
- * @return The album with the given ID
- */
- public Album getAlbum(String albumId) {
- return getAlbum(albumId, true);
+ public AlbumBuilder albumBuilder() {
+ return database.newAlbumBuilder();
}
/**
@@ -602,23 +534,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*
* @param albumId
* The ID of the album
- * @param create
- * {@code true} to create a new album if none exists for the
- * given ID
* @return The album with the given ID, or {@code null} if no album with the
- * given ID exists and {@code create} is {@code false}
+ * given ID exists
*/
- public Album getAlbum(String albumId, boolean create) {
- Optional album = database.getAlbum(albumId);
- if (album.isPresent()) {
- return album.get();
- }
- if (!create) {
- return null;
- }
- Album newAlbum = database.newAlbumBuilder().withId(albumId).build();
- database.storeAlbum(newAlbum);
- return newAlbum;
+ @Nullable
+ public Album getAlbum(@Nonnull String albumId) {
+ return database.getAlbum(albumId).orNull();
+ }
+
+ public ImageBuilder imageBuilder() {
+ return database.newImageBuilder();
}
/**
@@ -719,26 +644,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.getPluginVersion()));
+ 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;
}
/**
@@ -754,12 +674,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return null;
}
Sone sone = addLocalSone(ownIdentity);
- sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption(false));
- sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption(false));
- sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption(true));
- sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption(true));
- sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption(true));
- sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption(ShowCustomAvatars.NEVER));
followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
touchConfiguration();
@@ -778,41 +692,33 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.WARNING, "Given Identity is null!");
return null;
}
- 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(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ 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().getBooleanOption("AutoFollow").get()) {
- 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(new Runnable() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public void run() {
- soneDownloader.fetchSone(sone, sone.getRequestUri());
- }
-
- });
- return sone;
}
+ database.storeSone(sone);
+ soneDownloader.addSone(sone);
+ soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
+ return sone;
}
/**
@@ -826,7 +732,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();
@@ -861,7 +767,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);
@@ -964,91 +870,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);
- }
+ List