X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=9269ea61c58c58a4f32300d4da312ffa94c86864;hb=16172b8d60c485ac52907ba45a354b754bce53e5;hp=a6a33169e21b47691c01203b3800d851f41ad95e;hpb=f313a48bfc36f9968d18b76436739c093b562b7a;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 a6a3316..9269ea6 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -17,11 +17,13 @@
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.primitives.Longs.tryParse;
+import static java.lang.String.format;
+import static java.util.logging.Level.WARNING;
-import java.net.MalformedURLException;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -36,9 +38,13 @@ 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.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;
@@ -52,7 +58,6 @@ import net.pterodactylus.sone.core.event.SoneLockedEvent;
import net.pterodactylus.sone.core.event.SoneRemovedEvent;
import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.AlbumImpl;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
@@ -64,15 +69,17 @@ 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.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;
@@ -82,7 +89,6 @@ 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;
@@ -90,25 +96,23 @@ 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.Optional;
-import com.google.common.base.Predicate;
-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. */
@@ -117,17 +121,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** 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;
/** The configuration. */
- private Configuration configuration;
+ private final Configuration configuration;
/** Whether weâre currently saving the configuration. */
private boolean storingConfiguration = false;
@@ -153,9 +154,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();
@@ -171,29 +169,15 @@ 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());
- /** All known albums. */
- private final Map albums = new HashMap();
-
- /** All known images. */
- private final Map images = new HashMap();
-
/** All temporary images. */
private final Map temporaryImages = new HashMap();
@@ -225,12 +209,28 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
this.configuration = configuration;
this.freenetInterface = freenetInterface;
this.identityManager = identityManager;
- this.soneDownloader = new SoneDownloader(this, freenetInterface);
- this.imageInserter = new ImageInserter(freenetInterface);
+ this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface);
+ this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier());
this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
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 = soneDownloader;
+ this.imageInserter = imageInserter;
+ this.updateChecker = updateChecker;
+ this.webOfTrustUpdater = webOfTrustUpdater;
+ this.eventBus = eventBus;
+ this.database = database;
+ preferences = new Preferences(eventBus);
}
//
@@ -247,18 +247,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
/**
- * Sets the configuration to use. This will automatically save the current
- * configuration to the given configuration.
- *
- * @param configuration
- * The new configuration to use
- */
- public void setConfiguration(Configuration configuration) {
- this.configuration = configuration;
- touchConfiguration();
- }
-
- /**
* Returns the options used by the core.
*
* @return The options of the core
@@ -286,16 +274,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
@@ -305,7 +283,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);
@@ -329,14 +307,16 @@ 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();
}
/**
@@ -350,9 +330,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);
}
/**
@@ -360,15 +338,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Override
public Collection getLocalSones() {
- synchronized (sones) {
- return FluentIterable.from(sones.values()).filter(new Predicate() {
-
- @Override
- public boolean apply(Sone sone) {
- return sone.isLocal();
- }
- }).toSet();
- }
+ return database.getLocalSones();
}
/**
@@ -376,24 +346,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 Sone(id, true);
- sones.put(id, sone);
- }
- if ((sone != null) && !sone.isLocal()) {
- sone = new Sone(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;
}
/**
@@ -401,36 +361,19 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Override
public Collection getRemoteSones() {
- synchronized (sones) {
- return FluentIterable.from(sones.values()).filter(new Predicate() {
-
- @Override
- public boolean apply(Sone sone) {
- return !sone.isLocal();
- }
- }).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 Sone(id, false);
- sones.put(id, sone);
- }
- return sone;
- }
+ public Sone getRemoteSone(String id) {
+ return database.getSone(id).orNull();
}
/**
@@ -442,7 +385,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* {@code false} otherwise
*/
public boolean isModifiedSone(Sone sone) {
- return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
+ return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
}
/**
@@ -460,22 +403,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
@@ -577,21 +504,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);
}
/**
@@ -600,28 +513,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();
}
/**
@@ -630,21 +526,15 @@ 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}
- */
- public Album getAlbum(String albumId, boolean create) {
- synchronized (albums) {
- Album album = albums.get(albumId);
- if (create && (album == null)) {
- album = new AlbumImpl(albumId);
- albums.put(albumId, album);
- }
- return album;
- }
+ * given ID exists
+ */
+ public Album getAlbum(String albumId) {
+ return database.getAlbum(albumId).orNull();
+ }
+
+ public ImageBuilder imageBuilder() {
+ return database.newImageBuilder();
}
/**
@@ -671,14 +561,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* none was created
*/
public Image getImage(String imageId, boolean create) {
- synchronized (images) {
- Image image = images.get(imageId);
- if (create && (image == null)) {
- image = new Image(imageId);
- images.put(imageId, image);
- }
- return image;
+ Optional image = database.getImage(imageId);
+ if (image.isPresent()) {
+ return image.get();
}
+ if (!create) {
+ return null;
+ }
+ Image newImage = database.newImageBuilder().withId(imageId).build();
+ database.storeImage(newImage);
+ return newImage;
}
/**
@@ -743,26 +635,19 @@ 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();
+ sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), 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);
+ sone.setStatus(SoneStatus.idle);
+ soneInserter.start();
+ return sone;
}
/**
@@ -778,12 +663,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();
@@ -802,41 +681,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;
+ final Long latestEdition = tryParse(fromNullable(
+ identity.getProperty("Sone.LatestEdition")).or("0"));
+ 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;
}
/**
@@ -988,7 +859,7 @@ 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())) {
@@ -996,69 +867,46 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return;
}
/* find removed posts. */
- Collection existingPosts = database.getPosts(sone.getId());
- for (Post oldPost : existingPosts) {
- if (!sone.getPosts().contains(oldPost)) {
- eventBus.post(new PostRemovedEvent(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()) {
- eventBus.post(new NewPostFoundEvent(newPost));
- }
- }
- /* store posts. */
- database.storePosts(sone, sone.getPosts());
- if (!soneRescueMode) {
- for (PostReply reply : storedSone.get().getReplies()) {
- if (!sone.getReplies().contains(reply)) {
- eventBus.post(new PostReplyRemovedEvent(reply));
+ 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));
}
}
- }
- Set storedReplies = storedSone.get().getReplies();
- for (PostReply reply : sone.getReplies()) {
- if (storedReplies.contains(reply)) {
- continue;
- }
- if (reply.getTime() < getSoneFollowingTime(sone)) {
- reply.setKnown(true);
- } else if (!reply.isKnown()) {
- eventBus.post(new NewPostReplyFoundEvent(reply));
+ });
+ soneChangeDetector.onRemovedPosts(new PostProcessor() {
+ @Override
+ public void processPost(Post post) {
+ eventBus.post(new PostRemovedEvent(post));
}
- }
- database.storePostReplies(sone, sone.getReplies());
- synchronized (albums) {
- synchronized (images) {
- for (Album album : storedSone.get().getRootAlbum().getAlbums()) {
- albums.remove(album.getId());
- for (Image image : album.getImages()) {
- images.remove(image.getId());
- }
- }
- for (Album album : sone.getRootAlbum().getAlbums()) {
- albums.put(album.getId(), album);
- for (Image image : album.getImages()) {
- images.put(image.getId(), image);
- }
+ });
+ 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));
}
}
- }
- synchronized (sones) {
- sone.setOptions(storedSone.get().getOptions());
- sone.setKnown(storedSone.get().isKnown());
- sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
- if (sone.isLocal()) {
- soneInserters.get(storedSone.get()).setSone(sone);
- touchConfiguration();
+ });
+ soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() {
+ @Override
+ public void processPostReply(PostReply postReply) {
+ eventBus.post(new PostReplyRemovedEvent(postReply));
}
- sones.put(sone.getId(), sone);
+ });
+ soneChangeDetector.detectChanges(sone);
+ database.storeSone(sone);
+ sone.setOptions(storedSone.get().getOptions());
+ sone.setKnown(storedSone.get().isKnown());
+ sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
+ if (sone.isLocal()) {
+ touchConfiguration();
}
}
}
@@ -1076,15 +924,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
return;
}
- synchronized (sones) {
- if (!getLocalSones().contains(sone)) {
- logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
- return;
- }
- sones.remove(sone.getId());
- SoneInserter soneInserter = soneInserters.remove(sone);
- soneInserter.stop();
+ if (!getLocalSones().contains(sone)) {
+ logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
+ return;
}
+ // FIXME â implement in database
+// sones.remove(sone.getId());
+ SoneInserter soneInserter = soneInserters.remove(sone);
+ soneInserter.stop();
webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
webOfTrustUpdater.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
try {
@@ -1126,14 +973,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
logger.info(String.format("Loading local Sone: %s", sone));
- /* initialize options. */
- 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));
-
/* load Sone. */
String sonePrefix = "Sone/" + sone.getId();
Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
@@ -1144,170 +983,80 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
/* load profile. */
- Profile profile = new Profile(sone);
- profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
- profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
- profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
- profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
- profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
- profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
-
- /* load profile fields. */
- while (true) {
- String fieldPrefix = sonePrefix + "/Profile/Fields/" + profile.getFields().size();
- String fieldName = configuration.getStringValue(fieldPrefix + "/Name").getValue(null);
- if (fieldName == null) {
- break;
- }
- String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
- profile.addField(fieldName).setValue(fieldValue);
- }
+ ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
+ Profile profile = configurationSoneParser.parseProfile();
/* load posts. */
- Set posts = new HashSet();
- while (true) {
- String postPrefix = sonePrefix + "/Posts/" + posts.size();
- String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
- if (postId == null) {
- break;
- }
- String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
- long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
- String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
- if ((postTime == 0) || (postText == null)) {
- logger.log(Level.WARNING, "Invalid post found, aborting load!");
- return;
- }
- PostBuilder postBuilder = postBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText);
- if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
- postBuilder.to(postRecipientId);
- }
- posts.add(postBuilder.build());
+ Collection posts;
+ try {
+ posts = configurationSoneParser.parsePosts(database);
+ } catch (InvalidPostFound ipf) {
+ logger.log(Level.WARNING, "Invalid post found, aborting load!");
+ return;
}
/* load replies. */
- Set replies = new HashSet();
- while (true) {
- String replyPrefix = sonePrefix + "/Replies/" + replies.size();
- String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
- if (replyId == null) {
- break;
- }
- String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
- long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
- String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
- if ((postId == null) || (replyTime == 0) || (replyText == null)) {
- logger.log(Level.WARNING, "Invalid reply found, aborting load!");
- return;
- }
- PostReplyBuilder postReplyBuilder = postReplyBuilder().withId(replyId).from(sone.getId()).to(postId).withTime(replyTime).withText(replyText);
- replies.add(postReplyBuilder.build());
+ Collection replies;
+ try {
+ replies = configurationSoneParser.parsePostReplies(database);
+ } catch (InvalidPostReplyFound iprf) {
+ logger.log(Level.WARNING, "Invalid reply found, aborting load!");
+ return;
}
/* load post likes. */
- Set likedPostIds = new HashSet();
- while (true) {
- String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
- if (likedPostId == null) {
- break;
- }
- likedPostIds.add(likedPostId);
- }
+ Set likedPostIds =
+ configurationSoneParser.parseLikedPostIds();
/* load reply likes. */
- Set likedReplyIds = new HashSet();
- while (true) {
- String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
- if (likedReplyId == null) {
- break;
- }
- likedReplyIds.add(likedReplyId);
- }
+ Set likedReplyIds =
+ configurationSoneParser.parseLikedPostReplyIds();
/* load friends. */
- Set friends = new HashSet();
- while (true) {
- String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
- if (friendId == null) {
- break;
- }
- friends.add(friendId);
- }
+ Set friends = configurationSoneParser.parseFriends();
/* load albums. */
- List topLevelAlbums = new ArrayList();
- int albumCounter = 0;
- while (true) {
- String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
- String albumId = configuration.getStringValue(albumPrefix + "/ID").getValue(null);
- if (albumId == null) {
- break;
- }
- String albumTitle = configuration.getStringValue(albumPrefix + "/Title").getValue(null);
- String albumDescription = configuration.getStringValue(albumPrefix + "/Description").getValue(null);
- String albumParentId = configuration.getStringValue(albumPrefix + "/Parent").getValue(null);
- String albumImageId = configuration.getStringValue(albumPrefix + "/AlbumImage").getValue(null);
- if ((albumTitle == null) || (albumDescription == null)) {
- logger.log(Level.WARNING, "Invalid album found, aborting load!");
- return;
- }
- Album album = getAlbum(albumId).setSone(sone).setTitle(albumTitle).setDescription(albumDescription).setAlbumImage(albumImageId);
- if (albumParentId != null) {
- Album parentAlbum = getAlbum(albumParentId, false);
- if (parentAlbum == null) {
- logger.log(Level.WARNING, String.format("Invalid parent album ID: %s", albumParentId));
- return;
- }
- parentAlbum.addAlbum(album);
- } else {
- if (!topLevelAlbums.contains(album)) {
- topLevelAlbums.add(album);
- }
- }
+ List topLevelAlbums;
+ try {
+ topLevelAlbums =
+ configurationSoneParser.parseTopLevelAlbums(database);
+ } catch (InvalidAlbumFound iaf) {
+ logger.log(Level.WARNING, "Invalid album found, aborting load!");
+ return;
+ } catch (InvalidParentAlbumFound ipaf) {
+ logger.log(Level.WARNING, format("Invalid parent album ID: %s",
+ ipaf.getAlbumParentId()));
+ return;
}
/* load images. */
- int imageCounter = 0;
- while (true) {
- String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
- String imageId = configuration.getStringValue(imagePrefix + "/ID").getValue(null);
- if (imageId == null) {
- break;
- }
- String albumId = configuration.getStringValue(imagePrefix + "/Album").getValue(null);
- String key = configuration.getStringValue(imagePrefix + "/Key").getValue(null);
- String title = configuration.getStringValue(imagePrefix + "/Title").getValue(null);
- String description = configuration.getStringValue(imagePrefix + "/Description").getValue(null);
- Long creationTime = configuration.getLongValue(imagePrefix + "/CreationTime").getValue(null);
- Integer width = configuration.getIntValue(imagePrefix + "/Width").getValue(null);
- Integer height = configuration.getIntValue(imagePrefix + "/Height").getValue(null);
- if ((albumId == null) || (key == null) || (title == null) || (description == null) || (creationTime == null) || (width == null) || (height == null)) {
- logger.log(Level.WARNING, "Invalid image found, aborting load!");
- return;
- }
- Album album = getAlbum(albumId, false);
- if (album == null) {
- logger.log(Level.WARNING, "Invalid album image encountered, aborting load!");
- return;
- }
- Image image = getImage(imageId).setSone(sone).setCreationTime(creationTime).setKey(key);
- image.setTitle(title).setDescription(description).setWidth(width).setHeight(height);
- album.addImage(image);
+ try {
+ configurationSoneParser.parseImages(database);
+ } catch (InvalidImageFound iif) {
+ logger.log(WARNING, "Invalid image found, aborting load!");
+ return;
+ } catch (InvalidParentAlbumFound ipaf) {
+ logger.log(Level.WARNING,
+ format("Invalid album image (%s) encountered, aborting load!",
+ ipaf.getAlbumParentId()));
+ return;
}
/* load avatar. */
String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
if (avatarId != null) {
- profile.setAvatar(getImage(avatarId, false));
+ final Map images =
+ configurationSoneParser.getImages();
+ profile.setAvatar(images.get(avatarId));
}
/* load options. */
- sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
- sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
- sone.getOptions().getBooleanOption("ShowNotification/NewSones").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
- sone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
- sone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
- sone.getOptions(). getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
+ 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().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
/* if weâre still here, Sone was loaded successfully. */
synchronized (sone) {
@@ -1326,18 +1075,19 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
for (Album album : topLevelAlbums) {
sone.getRootAlbum().addAlbum(album);
}
- soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
+ database.storeSone(sone);
+ synchronized (soneInserters) {
+ soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
+ }
}
synchronized (knownSones) {
for (String friend : friends) {
knownSones.add(friend);
}
}
- database.storePosts(sone, posts);
for (Post post : posts) {
post.setKnown(true);
}
- database.storePostReplies(sone, replies);
for (PostReply reply : replies) {
reply.setKnown(true);
}
@@ -1350,34 +1100,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*
* @param sone
* The Sone that creates the post
- * @param text
- * The text of the post
- * @return The created post
- */
- public Post createPost(Sone sone, String text) {
- return createPost(sone, System.currentTimeMillis(), text);
- }
-
- /**
- * Creates a new post.
- *
- * @param sone
- * The Sone that creates the post
- * @param time
- * The time of the post
- * @param text
- * The text of the post
- * @return The created post
- */
- public Post createPost(Sone sone, long time, String text) {
- return createPost(sone, null, time, text);
- }
-
- /**
- * Creates a new post.
- *
- * @param sone
- * The Sone that creates the post
* @param recipient
* The recipient Sone, or {@code null} if this post does not have
* a recipient
@@ -1420,16 +1142,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
eventBus.post(new NewPostFoundEvent(post));
sone.addPost(post);
touchConfiguration();
- localElementTicker.schedule(new Runnable() {
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void run() {
- markPostKnown(post);
- }
- }, 10, TimeUnit.SECONDS);
+ localElementTicker.schedule(new MarkPostKnown(post), 10, TimeUnit.SECONDS);
return post;
}
@@ -1466,26 +1179,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
}
- /**
- * Bookmarks the given post.
- *
- * @param post
- * The post to bookmark
- */
- public void bookmark(Post post) {
- bookmarkPost(post.getId());
- }
-
- /**
- * Bookmarks the post with the given ID.
- *
- * @param id
- * The ID of the post to bookmark
- */
- public void bookmarkPost(String id) {
- synchronized (bookmarkedPosts) {
- bookmarkedPosts.add(id);
- }
+ public void bookmarkPost(Post post) {
+ database.bookmarkPost(post);
}
/**
@@ -1494,20 +1189,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @param post
* The post to unbookmark
*/
- public void unbookmark(Post post) {
- unbookmarkPost(post.getId());
- }
-
- /**
- * Removes the post with the given ID from the bookmarks.
- *
- * @param id
- * The ID of the post to unbookmark
- */
- public void unbookmarkPost(String id) {
- synchronized (bookmarkedPosts) {
- bookmarkedPosts.remove(id);
- }
+ public void unbookmarkPost(Post post) {
+ database.unbookmarkPost(post);
}
/**
@@ -1535,16 +1218,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
eventBus.post(new NewPostReplyFoundEvent(reply));
sone.addReply(reply);
touchConfiguration();
- localElementTicker.schedule(new Runnable() {
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void run() {
- markReplyKnown(reply);
- }
- }, 10, TimeUnit.SECONDS);
+ localElementTicker.schedule(new MarkReplyKnown(reply), 10, TimeUnit.SECONDS);
return reply;
}
@@ -1583,17 +1257,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
/**
- * Creates a new top-level album for the given Sone.
- *
- * @param sone
- * The Sone to create the album for
- * @return The new album
- */
- public Album createAlbum(Sone sone) {
- return createAlbum(sone, sone.getRootAlbum());
- }
-
- /**
* Creates a new album for the given Sone.
*
* @param sone
@@ -1604,11 +1267,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The new album
*/
public Album createAlbum(Sone sone, Album parent) {
- AlbumImpl album = new AlbumImpl();
- synchronized (albums) {
- albums.put(album.getId(), album);
- }
- album.setSone(sone);
+ Album album = database.newAlbumBuilder().randomId().by(sone).build();
+ database.storeAlbum(album);
parent.addAlbum(album);
return album;
}
@@ -1627,9 +1287,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return;
}
album.getParent().removeAlbum(album);
- synchronized (albums) {
- albums.remove(album.getId());
- }
+ database.removeAlbum(album);
touchConfiguration();
}
@@ -1650,11 +1308,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
checkNotNull(temporaryImage, "temporaryImage must not be null");
checkArgument(sone.isLocal(), "sone must be a local Sone");
checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
- Image image = new Image(temporaryImage.getId()).setSone(sone).setCreationTime(System.currentTimeMillis());
+ Image image = database.newImageBuilder().withId(temporaryImage.getId()).build().modify().setSone(sone).setCreationTime(System.currentTimeMillis()).update();
album.addImage(image);
- synchronized (images) {
- images.put(image.getId(), image);
- }
+ database.storeImage(image);
imageInserter.insertImage(temporaryImage, image);
return image;
}
@@ -1663,7 +1319,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* Deletes the given image. This method will also delete a matching
* temporary image.
*
- * @see #deleteTemporaryImage(TemporaryImage)
+ * @see #deleteTemporaryImage(String)
* @param image
* The image to delete
*/
@@ -1672,9 +1328,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
checkArgument(image.getSone().isLocal(), "image must belong to a local Sone");
deleteTemporaryImage(image.getId());
image.getAlbum().removeImage(image);
- synchronized (images) {
- images.remove(image.getId());
- }
+ database.removeImage(image);
touchConfiguration();
}
@@ -1697,17 +1351,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
/**
- * Deletes the given temporary image.
- *
- * @param temporaryImage
- * The temporary image to delete
- */
- public void deleteTemporaryImage(TemporaryImage temporaryImage) {
- checkNotNull(temporaryImage, "temporaryImage must not be null");
- deleteTemporaryImage(temporaryImage.getId());
- }
-
- /**
* Deletes the temporary image with the given ID.
*
* @param imageId
@@ -1775,7 +1418,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
@Override
public void serviceStop() {
localElementTicker.shutdownNow();
- synchronized (sones) {
+ synchronized (soneInserters) {
for (Entry soneInserter : soneInserters.entrySet()) {
soneInserter.getValue().stop();
saveSone(soneInserter.getKey());
@@ -1915,12 +1558,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
configuration.getStringValue(sonePrefix + "/Images/" + imageCounter + "/ID").setValue(null);
/* save options. */
- configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
- configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewSones").getReal());
- configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewPosts").getReal());
- configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewReplies").getReal());
- configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").getReal());
- configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions(). getEnumOption("ShowCustomAvatars").get().name());
+ configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().isAutoFollow());
+ configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().isSoneInsertNotificationEnabled());
+ configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications());
+ configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
+ configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
+ configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
configuration.save();
@@ -1946,18 +1589,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/* store the options first. */
try {
- configuration.getIntValue("Option/ConfigurationVersion").setValue(0);
- configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
- configuration.getIntValue("Option/PostsPerPage").setValue(options.getIntegerOption("PostsPerPage").getReal());
- configuration.getIntValue("Option/ImagesPerPage").setValue(options.getIntegerOption("ImagesPerPage").getReal());
- configuration.getIntValue("Option/CharactersPerPost").setValue(options.getIntegerOption("CharactersPerPost").getReal());
- configuration.getIntValue("Option/PostCutOffLength").setValue(options.getIntegerOption("PostCutOffLength").getReal());
- configuration.getBooleanValue("Option/RequireFullAccess").setValue(options.getBooleanOption("RequireFullAccess").getReal());
- configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal());
- configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal());
- configuration.getStringValue("Option/TrustComment").setValue(options.getStringOption("TrustComment").getReal());
- configuration.getBooleanValue("Option/ActivateFcpInterface").setValue(options.getBooleanOption("ActivateFcpInterface").getReal());
- configuration.getIntValue("Option/FcpFullAccessRequired").setValue(options.getIntegerOption("FcpFullAccessRequired").getReal());
+ preferences.saveTo(configuration);
/* save known Sones. */
int soneCounter = 0;
@@ -1984,10 +1616,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/* save bookmarked posts. */
int bookmarkedPostCounter = 0;
- synchronized (bookmarkedPosts) {
- for (String bookmarkedPostId : bookmarkedPosts) {
- configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
- }
+ for (Post bookmarkedPost : getBookmarkedPosts()) {
+ configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPost.getId());
}
configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
@@ -2009,52 +1639,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* Loads the configuration.
*/
private void loadConfiguration() {
- /* create options. */
- options.addIntegerOption("InsertionDelay", new DefaultOption(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new OptionWatcher() {
-
- @Override
- public void optionChanged(Option option, Integer oldValue, Integer newValue) {
- SoneInserter.setInsertionDelay(newValue);
- }
-
- }));
- options.addIntegerOption("PostsPerPage", new DefaultOption(10, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
- options.addIntegerOption("ImagesPerPage", new DefaultOption(9, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
- options.addIntegerOption("CharactersPerPost", new DefaultOption(400, Predicates. or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
- options.addIntegerOption("PostCutOffLength", new DefaultOption(200, Predicates. or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
- options.addBooleanOption("RequireFullAccess", new DefaultOption(false));
- options.addIntegerOption("PositiveTrust", new DefaultOption(75, new IntegerRangePredicate(0, 100)));
- options.addIntegerOption("NegativeTrust", new DefaultOption(-25, new IntegerRangePredicate(-100, 100)));
- options.addStringOption("TrustComment", new DefaultOption("Set from Sone Web Interface"));
- options.addBooleanOption("ActivateFcpInterface", new DefaultOption(false, new OptionWatcher() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public void optionChanged(Option option, Boolean oldValue, Boolean newValue) {
- fcpInterface.setActive(newValue);
- }
- }));
- options.addIntegerOption("FcpFullAccessRequired", new DefaultOption(2, new OptionWatcher() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public void optionChanged(Option option, Integer oldValue, Integer newValue) {
- fcpInterface.setFullAccessRequired(FullAccessRequired.values()[newValue]);
- }
-
- }));
-
- loadConfigurationValue("InsertionDelay");
- loadConfigurationValue("PostsPerPage");
- loadConfigurationValue("ImagesPerPage");
- loadConfigurationValue("CharactersPerPost");
- loadConfigurationValue("PostCutOffLength");
- options.getBooleanOption("RequireFullAccess").set(configuration.getBooleanValue("Option/RequireFullAccess").getValue(null));
- loadConfigurationValue("PositiveTrust");
- loadConfigurationValue("NegativeTrust");
- options.getStringOption("TrustComment").set(configuration.getStringValue("Option/TrustComment").getValue(null));
- options.getBooleanOption("ActivateFcpInterface").set(configuration.getBooleanValue("Option/ActivateFcpInterface").getValue(null));
- options.getIntegerOption("FcpFullAccessRequired").set(configuration.getIntValue("Option/FcpFullAccessRequired").getValue(null));
+ new PreferencesLoader(preferences).loadFrom(configuration);
/* load known Sones. */
int soneCounter = 0;
@@ -2089,29 +1674,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
if (bookmarkedPostId == null) {
break;
}
- synchronized (bookmarkedPosts) {
- bookmarkedPosts.add(bookmarkedPostId);
- }
+ database.bookmarkPost(bookmarkedPostId);
}
}
/**
- * Loads an {@link Integer} configuration value for the option with the
- * given name, logging validation failures.
- *
- * @param optionName
- * The name of the option to load
- */
- private void loadConfigurationValue(String optionName) {
- try {
- options.getIntegerOption(optionName).set(configuration.getIntValue("Option/" + optionName).getValue(null));
- } catch (IllegalArgumentException iae1) {
- logger.log(Level.WARNING, String.format("Invalid value for %s in configuration, using default.", optionName));
- }
- }
-
- /**
* Notifies the core that a new {@link OwnIdentity} was added.
*
* @param ownIdentityAddedEvent
@@ -2161,22 +1729,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Subscribe
public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
- final Identity identity = identityUpdatedEvent.identity();
- soneDownloaders.execute(new Runnable() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public void run() {
- Sone sone = getRemoteSone(identity.getId(), false);
- if (sone.isLocal()) {
- return;
- }
- sone.setIdentity(identity);
- sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition()));
- soneDownloader.addSone(sone);
- soneDownloader.fetchSone(sone);
- }
- });
+ Identity identity = identityUpdatedEvent.identity();
+ final Sone sone = getRemoteSone(identity.getId());
+ if (sone.isLocal()) {
+ return;
+ }
+ sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition()));
+ soneDownloader.addSone(sone);
+ soneDownloaders.execute(soneDownloader.fetchSoneAction(sone));
}
/**
@@ -2190,19 +1750,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
OwnIdentity ownIdentity = identityRemovedEvent.ownIdentity();
Identity identity = identityRemovedEvent.identity();
trustedIdentities.remove(ownIdentity, identity);
- boolean foundIdentity = false;
for (Entry> trustedIdentity : trustedIdentities.asMap().entrySet()) {
if (trustedIdentity.getKey().equals(ownIdentity)) {
continue;
}
if (trustedIdentity.getValue().contains(identity)) {
- foundIdentity = true;
+ return;
}
}
- if (foundIdentity) {
- /* some local identity still trusts this identity, donât remove. */
- return;
- }
Optional sone = getSone(identity.getId());
if (!sone.isPresent()) {
/* TODO - we donât have the Sone anymore. should this happen? */
@@ -2216,9 +1771,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
for (PostReply reply : sone.get().getReplies()) {
eventBus.post(new PostReplyRemovedEvent(reply));
}
- synchronized (sones) {
- sones.remove(identity.getId());
- }
+// TODO â implement in database
+// sones.remove(identity.getId());
eventBus.post(new SoneRemovedEvent(sone.get()));
}
@@ -2231,9 +1785,41 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
@Subscribe
public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.image(), imageInsertFinishedEvent.resultingUri()));
- imageInsertFinishedEvent.image().setKey(imageInsertFinishedEvent.resultingUri().toString());
+ imageInsertFinishedEvent.image().modify().setKey(imageInsertFinishedEvent.resultingUri().toString()).update();
deleteTemporaryImage(imageInsertFinishedEvent.image().getId());
touchConfiguration();
}
+ @VisibleForTesting
+ class MarkPostKnown implements Runnable {
+
+ private final Post post;
+
+ public MarkPostKnown(Post post) {
+ this.post = post;
+ }
+
+ @Override
+ public void run() {
+ markPostKnown(post);
+ }
+
+ }
+
+ @VisibleForTesting
+ class MarkReplyKnown implements Runnable {
+
+ private final PostReply postReply;
+
+ public MarkReplyKnown(PostReply postReply) {
+ this.postReply = postReply;
+ }
+
+ @Override
+ public void run() {
+ markReplyKnown(postReply);
+ }
+
+ }
+
}