X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=91825a61ab256e28b3901994c85fe71041714b6e;hb=87d9d82c6f671d69cba305b5ddf3cd81ecedc115;hp=b0dadbea63e533f32d3e38723eb5e1ad01d6252f;hpb=0079039b42e82267b30018162f5be611d80d6b0f;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 b0dadbe..91825a6 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 @@
/*
- * FreenetSone - Core.java - Copyright © 2010 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,75 +17,220 @@
package net.pterodactylus.sone.core;
-import java.net.MalformedURLException;
+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 static java.util.logging.Logger.getLogger;
+
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
-import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
-import net.pterodactylus.sone.core.SoneException.Type;
+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;
+import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
+import net.pterodactylus.sone.core.event.NewPostFoundEvent;
+import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
+import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
+import net.pterodactylus.sone.core.event.PostRemovedEvent;
+import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
+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.Client;
+import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
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.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.freenet.wot.Identity;
+import net.pterodactylus.sone.freenet.wot.IdentityManager;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
+import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
+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.util.config.Configuration;
import net.pterodactylus.util.config.ConfigurationException;
-import net.pterodactylus.util.filter.Filter;
-import net.pterodactylus.util.filter.Filters;
-import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.service.AbstractService;
-import freenet.client.FetchResult;
-import freenet.keys.FreenetURI;
+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.collect.FluentIterable;
+import com.google.common.collect.HashMultimap;
+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 com.google.inject.Singleton;
/**
* The Sone core.
*
* @author David âBombeâ Roden
*/
-public class Core extends AbstractService {
+@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 preferences. */
+ 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;
+
+ /** The identity manager. */
+ private final IdentityManager identityManager;
/** Interface to freenet. */
- private FreenetInterface freenetInterface;
+ private final FreenetInterface freenetInterface;
/** The Sone downloader. */
- private SoneDownloader soneDownloader;
+ private final SoneDownloader soneDownloader;
+
+ /** The image inserter. */
+ private final ImageInserter imageInserter;
- /** The local Sones. */
- private final Set localSones = new HashSet();
+ /** Sone downloader thread-pool. */
+ private final ExecutorService soneDownloaders = Executors.newFixedThreadPool(10, new NamedThreadFactory("Sone Downloader %2$d"));
+
+ /** The update checker. */
+ private final UpdateChecker updateChecker;
+
+ /** The trust updater. */
+ private final WebOfTrustUpdater webOfTrustUpdater;
+
+ /** The times Sones were followed. */
+ private final Map soneFollowingTimes = new HashMap();
+
+ /** Locked local Sones. */
+ /* synchronize on itself. */
+ private final Set lockedSones = new HashSet();
/** Sone inserters. */
+ /* synchronize access on this on sones. */
private final Map soneInserters = new HashMap();
- /* various caches follow here. */
+ /** Sone rescuers. */
+ /* synchronize access on this on sones. */
+ private final Map soneRescuers = new HashMap();
- /** Cache for all known Sones. */
- private final Map soneCache = new HashMap();
+ /** All known Sones. */
+ private final Set knownSones = new HashSet();
- /** Cache for all known posts. */
- private final Map postCache = new HashMap();
+ /** The post database. */
+ private final Database database;
- /** Cache for all known replies. */
- private final Map replyCache = new HashMap();
+ /** Trusted identities, sorted by own identities. */
+ private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.create());
+
+ /** All temporary images. */
+ private final Map temporaryImages = new HashMap();
+
+ /** Ticker for threads that mark own elements as known. */
+ private final ScheduledExecutorService localElementTicker = Executors.newScheduledThreadPool(1);
+
+ /** The time the configuration was last touched. */
+ private volatile long lastConfigurationUpdate;
/**
* Creates a new core.
+ *
+ * @param configuration
+ * The configuration of the core
+ * @param freenetInterface
+ * The freenet interface
+ * @param identityManager
+ * The identity manager
+ * @param webOfTrustUpdater
+ * The WebOfTrust updater
+ * @param eventBus
+ * The event bus
+ * @param database
+ * The database
*/
- public Core() {
+ @Inject
+ 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 = soneDownloader;
+ this.imageInserter = imageInserter;
+ this.updateChecker = updateChecker;
+ this.webOfTrustUpdater = webOfTrustUpdater;
+ this.eventBus = eventBus;
+ this.database = database;
+ preferences = new Preferences(eventBus);
}
//
@@ -93,500 +238,1551 @@ public class Core extends AbstractService {
//
/**
- * Sets the configuration of the core.
+ * Returns the time Sone was started.
*
- * @param configuration
- * The configuration of the core
- * @return This core (for method chaining)
+ * @return The startup time (in milliseconds since Jan 1, 1970 UTC)
*/
- public Core configuration(Configuration configuration) {
- this.configuration = configuration;
- return this;
+ public long getStartupTime() {
+ return startupTime;
}
/**
- * Sets the Freenet interface to use.
+ * Returns the options used by the core.
*
- * @param freenetInterface
- * The Freenet interface to use
- * @return This core (for method chaining)
+ * @return The options of the core
*/
- public Core freenetInterface(FreenetInterface freenetInterface) {
- this.freenetInterface = freenetInterface;
- soneDownloader = new SoneDownloader(this, freenetInterface);
- soneDownloader.start();
- return this;
+ public Preferences getPreferences() {
+ return preferences;
}
/**
- * Returns the local Sones.
+ * Returns the identity manager used by the core.
*
- * @return The local Sones
+ * @return The identity manager
*/
- public Set getSones() {
- return Collections.unmodifiableSet(localSones);
+ public IdentityManager getIdentityManager() {
+ return identityManager;
}
/**
- * Returns the Sone with the given ID, or an empty Sone that has been
- * initialized with the given ID.
+ * Returns the update checker.
*
- * @param soneId
- * The ID of the Sone
- * @return The Sone
+ * @return The update checker
+ */
+ public UpdateChecker getUpdateChecker() {
+ return updateChecker;
+ }
+
+ /**
+ * Returns the Sone rescuer for the given local Sone.
+ *
+ * @param sone
+ * The local Sone to get the rescuer for
+ * @return The Sone rescuer for the given Sone
+ */
+ public SoneRescuer getSoneRescuer(Sone sone) {
+ checkNotNull(sone, "sone must not be null");
+ checkArgument(sone.isLocal(), "sone must be local");
+ synchronized (soneRescuers) {
+ SoneRescuer soneRescuer = soneRescuers.get(sone);
+ if (soneRescuer == null) {
+ soneRescuer = new SoneRescuer(this, soneDownloader, sone);
+ soneRescuers.put(sone, soneRescuer);
+ soneRescuer.start();
+ }
+ return soneRescuer;
+ }
+ }
+
+ /**
+ * Returns whether the given Sone is currently locked.
+ *
+ * @param sone
+ * The sone to check
+ * @return {@code true} if the Sone is locked, {@code false} if it is not
*/
- public Sone getSone(String soneId) {
- if (!soneCache.containsKey(soneId)) {
- Sone sone = new Sone(soneId);
- soneCache.put(soneId, sone);
+ public boolean isLocked(Sone sone) {
+ synchronized (lockedSones) {
+ return lockedSones.contains(sone);
}
- return soneCache.get(soneId);
+ }
+
+ public SoneBuilder soneBuilder() {
+ return database.newSoneBuilder();
+ }
+
+ /**
+ * {@inheritDocs}
+ */
+ @Override
+ public Collection getSones() {
+ return database.getSones();
+ }
+
+ @Override
+ public Function> soneLoader() {
+ return database.soneLoader();
}
/**
- * Returns all known sones.
+ * Returns the Sone with the given ID, regardless whether itâs local or
+ * remote.
*
- * @return All known sones
+ * @param id
+ * The ID of the Sone to get
+ * @return The Sone with the given ID, or {@code null} if there is no such
+ * Sone
+ */
+ @Override
+ public Optional getSone(String id) {
+ return database.getSone(id);
+ }
+
+ /**
+ * {@inheritDocs}
*/
- public Collection getKnownSones() {
- return soneCache.values();
+ @Override
+ public Collection getLocalSones() {
+ return database.getLocalSones();
}
/**
- * Gets all known Sones that are not local Sones.
+ * Returns the local Sone with the given ID, optionally creating a new Sone.
*
- * @return All remote Sones
+ * @param id
+ * The ID of the Sone
+ * @return The Sone with the given ID, or {@code null}
*/
+ public Sone getLocalSone(String id) {
+ Optional sone = database.getSone(id);
+ if (sone.isPresent() && sone.get().isLocal()) {
+ return sone.get();
+ }
+ return null;
+ }
+
+ /**
+ * {@inheritDocs}
+ */
+ @Override
public Collection getRemoteSones() {
- return Filters.filteredCollection(getKnownSones(), new Filter() {
+ return database.getRemoteSones();
+ }
- @Override
- @SuppressWarnings("synthetic-access")
- public boolean filterObject(Sone object) {
- return !localSones.contains(object);
- }
- });
+ /**
+ * Returns the remote Sone with the given ID.
+ *
+ *
+ * @param id
+ * The ID of the remote Sone to get
+ * @return The Sone with the given ID
+ */
+ public Sone getRemoteSone(String id) {
+ return database.getSone(id).orNull();
}
/**
- * Creates a new post and adds it to the given Sone.
+ * Returns whether the given Sone has been modified.
*
* @param sone
- * The sone that creates the post
- * @param text
- * The text of the post
- * @return The created post
+ * The Sone to check for modifications
+ * @return {@code true} if a modification has been detected in the Sone,
+ * {@code false} otherwise
*/
- public Post createPost(Sone sone, String text) {
- return createPost(sone, System.currentTimeMillis(), text);
+ public boolean isModifiedSone(Sone sone) {
+ return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
}
/**
- * Creates a new post and adds it to the given Sone.
+ * Returns the time when the given was first followed by any local Sone.
*
* @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
+ * The Sone to get the time for
+ * @return The time (in milliseconds since Jan 1, 1970) the Sone has first
+ * been followed, or {@link Long#MAX_VALUE}
*/
- public Post createPost(Sone sone, long time, String text) {
- Post post = getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text);
- sone.addPost(post);
- return post;
+ public long getSoneFollowingTime(Sone sone) {
+ synchronized (soneFollowingTimes) {
+ return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE);
+ }
}
/**
- * Creates a reply.
+ * Returns a post builder.
*
- * @param sone
- * The Sone that posts the reply
- * @param post
- * The post the reply refers to
- * @param text
- * The text of the reply
- * @return The created reply
+ * @return A new post builder
+ */
+ public PostBuilder postBuilder() {
+ return database.newPostBuilder();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Optional getPost(String postId) {
+ return database.getPost(postId);
+ }
+
+ /**
+ * {@inheritDocs}
+ */
+ @Override
+ public Collection getPosts(String soneId) {
+ return database.getPosts(soneId);
+ }
+
+ /**
+ * {@inheritDoc}
*/
- public Reply createReply(Sone sone, Post post, String text) {
- return createReply(sone, post, System.currentTimeMillis(), text);
+ @Override
+ public Collection getDirectedPosts(final String recipientId) {
+ checkNotNull(recipientId, "recipient must not be null");
+ return database.getDirectedPosts(recipientId);
}
/**
- * Creates a reply.
+ * Returns a post reply builder.
*
- * @param sone
- * The Sone that posts the reply
- * @param post
- * The post the reply refers to
- * @param time
- * The time of the post
- * @param text
- * The text of the reply
- * @return The created reply
+ * @return A new post reply builder
*/
- public Reply createReply(Sone sone, Post post, long time, String text) {
- Reply reply = getReply(UUID.randomUUID().toString()).setSone(sone).setPost(post).setTime(time).setText(text);
- sone.addReply(reply);
- return reply;
+ public PostReplyBuilder postReplyBuilder() {
+ return database.newPostReplyBuilder();
}
- //
- // ACTIONS
- //
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Optional getPostReply(String replyId) {
+ return database.getPostReply(replyId);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List getReplies(final String postId) {
+ return database.getReplies(postId);
+ }
/**
- * Adds a Sone to watch for updates. The Sone needs to be completely
- * initialized.
+ * Returns all Sones that have liked the given post.
*
- * @param sone
- * The Sone to watch for updates
+ * @param post
+ * The post to get the liking Sones for
+ * @return The Sones that like the given post
*/
- public void addSone(Sone sone) {
- soneCache.put(sone.getId(), sone);
- if (!localSones.contains(sone)) {
- soneDownloader.addSone(sone);
+ public Set getLikes(Post post) {
+ Set sones = new HashSet();
+ for (Sone sone : getSones()) {
+ if (sone.getLikedPostIds().contains(post.getId())) {
+ sones.add(sone);
+ }
}
+ return sones;
}
/**
- * Adds the given Sone.
+ * Returns all Sones that have liked the given reply.
*
- * @param sone
- * The Sone to add
+ * @param reply
+ * The reply to get the liking Sones for
+ * @return The Sones that like the given reply
*/
- public void addLocalSone(Sone sone) {
- if (localSones.add(sone)) {
- SoneInserter soneInserter = new SoneInserter(freenetInterface, sone);
- soneInserter.start();
- soneInserters.put(sone, soneInserter);
+ public Set getLikes(PostReply reply) {
+ Set sones = new HashSet();
+ for (Sone sone : getSones()) {
+ if (sone.getLikedReplyIds().contains(reply.getId())) {
+ sones.add(sone);
+ }
}
+ return sones;
}
/**
- * Creates a new Sone at a random location.
+ * Returns whether the given post is bookmarked.
*
- * @param name
- * The name of the Sone
- * @return The created Sone
- * @throws SoneException
- * if a Sone error occurs
+ * @param post
+ * The post to check
+ * @return {@code true} if the given post is bookmarked, {@code false}
+ * otherwise
*/
- public Sone createSone(String name) throws SoneException {
- return createSone(name, null, null);
+ public boolean isBookmarked(Post post) {
+ return database.isPostBookmarked(post);
}
/**
- * Creates a new Sone at the given location. If one of {@code requestUri} or
- * {@code insertUrI} is {@code null}, the Sone is created at a random
- * location.
+ * Returns all currently known bookmarked posts.
*
- * @param name
- * The name of the Sone
- * @param requestUri
- * The request URI of the Sone, or {@link NullPointerException}
- * to create a Sone at a random location
- * @param insertUri
- * The insert URI of the Sone, or {@code null} to create a Sone
- * at a random location
- * @return The created Sone
- * @throws SoneException
- * if a Sone error occurs
- */
- public Sone createSone(String name, String requestUri, String insertUri) throws SoneException {
- if ((name == null) || (name.trim().length() == 0)) {
- throw new SoneException(Type.INVALID_SONE_NAME);
- }
- String finalRequestUri;
- String finalInsertUri;
- if ((requestUri == null) || (insertUri == null)) {
- String[] keyPair = freenetInterface.generateKeyPair();
- finalRequestUri = keyPair[0];
- finalInsertUri = keyPair[1];
- } else {
- finalRequestUri = requestUri;
- finalInsertUri = insertUri;
- }
- Sone sone;
- try {
- logger.log(Level.FINEST, "Creating new Sone â%sâ at %s (%s)â¦", new Object[] { name, finalRequestUri, finalInsertUri });
- sone = getSone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName("Sone-" + name)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName("Sone-" + name));
- sone.setProfile(new Profile());
- /* set modification counter to 1 so it is inserted immediately. */
- sone.setModificationCounter(1);
- addLocalSone(sone);
- } catch (MalformedURLException mue1) {
- throw new SoneException(Type.INVALID_URI);
- }
- return sone;
+ * @return All bookmarked posts
+ */
+ public Set getBookmarkedPosts() {
+ return database.getBookmarkedPosts();
+ }
+
+ public AlbumBuilder albumBuilder() {
+ return database.newAlbumBuilder();
}
/**
- * Loads the Sone from the given request URI. The fetching of the data is
- * performed in a new thread so this method returns immediately.
+ * Returns the album with the given ID, optionally creating a new album if
+ * an album with the given ID can not be found.
*
- * @param requestUri
- * The request URI to load the Sone from
+ * @param albumId
+ * The ID of the album
+ * @return The album with the given ID, or {@code null} if no album with the
+ * given ID exists
*/
- public void loadSone(final String requestUri) {
- new Thread(new Runnable() {
+ public Album getAlbum(String albumId) {
+ return database.getAlbum(albumId).orNull();
+ }
- @Override
- @SuppressWarnings("synthetic-access")
- public void run() {
- try {
- FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
- FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
- Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
- if (parsedSone != null) {
- addSone(parsedSone);
- }
- } catch (MalformedURLException mue1) {
- logger.log(Level.INFO, "Could not create URI from â" + requestUri + "â.", mue1);
- }
- }
- }, "Sone Downloader").start();
+ public ImageBuilder imageBuilder() {
+ return database.newImageBuilder();
}
/**
- * Deletes the given Sone from this plugin instance.
+ * Returns the image with the given ID, creating it if necessary.
*
- * @param sone
- * The sone to delete
+ * @param imageId
+ * The ID of the image
+ * @return The image with the given ID
*/
- public void deleteSone(Sone sone) {
- SoneInserter soneInserter = soneInserters.remove(sone);
- soneInserter.stop();
- localSones.remove(sone);
+ public Image getImage(String imageId) {
+ return getImage(imageId, true);
}
/**
- * Returns the post with the given ID. If no post exists yet with the given
- * ID, a new post is returned.
+ * Returns the image with the given ID, optionally creating it if it does
+ * not exist.
*
- * @param postId
- * The ID of the post
- * @return The post
+ * @param imageId
+ * The ID of the image
+ * @param create
+ * {@code true} to create an image if none exists with the given
+ * ID
+ * @return The image with the given ID, or {@code null} if none exists and
+ * none was created
*/
- public Post getPost(String postId) {
- if (!postCache.containsKey(postId)) {
- postCache.put(postId, new Post(postId));
+ public Image getImage(String imageId, boolean create) {
+ Optional image = database.getImage(imageId);
+ if (image.isPresent()) {
+ return image.get();
+ }
+ if (!create) {
+ return null;
}
- return postCache.get(postId);
+ Image newImage = database.newImageBuilder().withId(imageId).build();
+ database.storeImage(newImage);
+ return newImage;
}
/**
- * Returns the reply with the given ID. If no reply exists yet with the
- * given ID, a new reply is returned.
+ * Returns the temporary image with the given ID.
*
- * @param replyId
- * The ID of the reply
- * @return The reply
+ * @param imageId
+ * The ID of the temporary image
+ * @return The temporary image, or {@code null} if there is no temporary
+ * image with the given ID
*/
- public Reply getReply(String replyId) {
- if (!replyCache.containsKey(replyId)) {
- replyCache.put(replyId, new Reply(replyId));
+ public TemporaryImage getTemporaryImage(String imageId) {
+ synchronized (temporaryImages) {
+ return temporaryImages.get(imageId);
}
- return replyCache.get(replyId);
}
+ //
+ // ACTIONS
+ //
+
/**
- * Gets all replies to the given post, sorted by date, oldest first.
+ * Locks the given Sone. A locked Sone will not be inserted by
+ * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
+ * again.
*
- * @param post
- * The post the replies refer to
- * @return The sorted list of replies for the post
+ * @param sone
+ * The sone to lock
*/
- public List getReplies(Post post) {
- List replies = new ArrayList();
- for (Reply reply : replyCache.values()) {
- if (reply.getPost().equals(post)) {
- replies.add(reply);
+ public void lockSone(Sone sone) {
+ synchronized (lockedSones) {
+ if (lockedSones.add(sone)) {
+ eventBus.post(new SoneLockedEvent(sone));
}
}
- Collections.sort(replies, new Comparator() {
+ }
- /**
- * {@inheritDoc}
- */
- @Override
- public int compare(Reply leftReply, Reply rightReply) {
- return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
+ /**
+ * Unlocks the given Sone.
+ *
+ * @see #lockSone(Sone)
+ * @param sone
+ * The sone to unlock
+ */
+ public void unlockSone(Sone sone) {
+ synchronized (lockedSones) {
+ if (lockedSones.remove(sone)) {
+ eventBus.post(new SoneUnlockedEvent(sone));
}
- });
- return replies;
+ }
}
- //
- // SERVICE METHODS
- //
-
/**
- * {@inheritDoc}
+ * Adds a local Sone from the given own identity.
+ *
+ * @param ownIdentity
+ * The own identity to create a Sone from
+ * @return The added (or already existing) Sone
*/
- @Override
- protected void serviceStart() {
- loadConfiguration();
+ public Sone addLocalSone(OwnIdentity ownIdentity) {
+ if (ownIdentity == null) {
+ logger.log(Level.WARNING, "Given OwnIdentity is null!");
+ return null;
+ }
+ logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
+ 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);
+ }
+ loadSone(sone);
+ database.storeSone(sone);
+ sone.setStatus(SoneStatus.idle);
+ soneInserter.start();
+ return sone;
}
/**
- * {@inheritDoc}
+ * Creates a new Sone for the given own identity.
+ *
+ * @param ownIdentity
+ * The own identity to create a Sone for
+ * @return The created Sone
*/
- @Override
- protected void serviceStop() {
- soneDownloader.stop();
- /* stop all Sone inserters. */
- for (SoneInserter soneInserter : soneInserters.values()) {
- soneInserter.stop();
+ public Sone createSone(OwnIdentity ownIdentity) {
+ if (!webOfTrustUpdater.addContextWait(ownIdentity, "Sone")) {
+ logger.log(Level.SEVERE, String.format("Could not add âSoneâ context to own identity: %s", ownIdentity));
+ return null;
}
- saveConfiguration();
- }
+ Sone sone = addLocalSone(ownIdentity);
- //
- // PRIVATE METHODS
- //
+ followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
+ touchConfiguration();
+ return sone;
+ }
/**
- * Loads the configuration.
+ * Adds the Sone of the given identity.
+ *
+ * @param identity
+ * The identity whose Sone to add
+ * @return The added or already existing Sone
*/
- private void loadConfiguration() {
- logger.entering(Core.class.getName(), "loadConfiguration()");
-
- /* parse local Sones. */
- logger.log(Level.INFO, "Loading Sonesâ¦");
- int soneId = 0;
- do {
- String sonePrefix = "Sone/Sone." + soneId++;
- String id = configuration.getStringValue(sonePrefix + "/ID").getValue(null);
- if (id == null) {
- break;
+ public Sone addRemoteSone(Identity identity) {
+ if (identity == null) {
+ logger.log(Level.WARNING, "Given Identity is null!");
+ return null;
+ }
+ 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());
}
- String name = configuration.getStringValue(sonePrefix + "/Name").getValue(null);
- String insertUri = configuration.getStringValue(sonePrefix + "/InsertURI").getValue(null);
- String requestUri = configuration.getStringValue(sonePrefix + "/RequestURI").getValue(null);
- long modificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
- String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
- String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
- String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
- try {
- Profile profile = new Profile();
- profile.setFirstName(firstName);
- profile.setMiddleName(middleName);
- profile.setLastName(lastName);
- Sone sone = getSone(id).setName(name).setRequestUri(new FreenetURI(requestUri)).setInsertUri(new FreenetURI(insertUri));
- sone.setProfile(profile);
- int postId = 0;
- do {
- String postPrefix = sonePrefix + "/Post." + postId++;
- id = configuration.getStringValue(postPrefix + "/ID").getValue(null);
- if (id == null) {
- break;
+ sone.setKnown(!newSone);
+ if (newSone) {
+ eventBus.post(new NewSoneFoundEvent(sone));
+ for (Sone localSone : getLocalSones()) {
+ if (localSone.getOptions().isAutoFollow()) {
+ followSone(localSone, sone.getId());
}
- long time = configuration.getLongValue(postPrefix + "/Time").getValue(null);
- String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
- Post post = getPost(id).setSone(sone).setTime(time).setText(text);
- sone.addPost(post);
- } while (true);
- int replyCounter = 0;
- do {
- String replyPrefix = sonePrefix + "/Reply." + replyCounter++;
- String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
- if (replyId == null) {
- break;
- }
- Post replyPost = getPost(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
- long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
- String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
- Reply reply = getReply(replyId).setSone(sone).setPost(replyPost).setTime(replyTime).setText(replyText);
- sone.addReply(reply);
- } while (true);
-
- /* load friends. */
- int friendCounter = 0;
- while (true) {
- String friendPrefix = sonePrefix + "/Friend." + friendCounter++;
- String friendId = configuration.getStringValue(friendPrefix + "/ID").getValue(null);
- if (friendId == null) {
- break;
- }
- Sone friendSone = getSone(friendId);
- String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
- String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
- friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
- loadSone(friendKey);
- sone.addFriend(friendSone);
}
-
- sone.setModificationCounter(modificationCounter);
- addLocalSone(sone);
- } catch (MalformedURLException mue1) {
- logger.log(Level.WARNING, "Could not create Sone from requestUri (â" + requestUri + "â) and insertUri (â" + insertUri + "â)!", mue1);
}
- } while (true);
- logger.log(Level.INFO, "Loaded %d Sones.", getSones().size());
-
- logger.exiting(Core.class.getName(), "loadConfiguration()");
+ }
+ database.storeSone(sone);
+ soneDownloader.addSone(sone);
+ soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
+ return sone;
}
/**
- * Saves the configuraiton.
+ * Lets the given local Sone follow the Sone with the given ID.
+ *
+ * @param sone
+ * The local Sone that should follow another Sone
+ * @param soneId
+ * The ID of the Sone to follow
*/
- private void saveConfiguration() {
- Set sones = getSones();
- logger.log(Level.INFO, "Storing %d Sonesâ¦", sones.size());
- try {
- /* store all Sones. */
- int soneId = 0;
- for (Sone sone : localSones) {
- String sonePrefix = "Sone/Sone." + soneId++;
- configuration.getStringValue(sonePrefix + "/ID").setValue(sone.getId());
- configuration.getStringValue(sonePrefix + "/Name").setValue(sone.getName());
- configuration.getStringValue(sonePrefix + "/RequestURI").setValue(sone.getRequestUri().toString());
- configuration.getStringValue(sonePrefix + "/InsertURI").setValue(sone.getInsertUri().toString());
- configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
- Profile profile = sone.getProfile();
- configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
- configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
- configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
- int postId = 0;
- for (Post post : sone.getPosts()) {
- String postPrefix = sonePrefix + "/Post." + postId++;
- configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
- configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
- configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
+ public void followSone(Sone sone, String soneId) {
+ checkNotNull(sone, "sone must not be null");
+ checkNotNull(soneId, "soneId must not be null");
+ database.addFriend(sone, soneId);
+ synchronized (soneFollowingTimes) {
+ if (!soneFollowingTimes.containsKey(soneId)) {
+ long now = System.currentTimeMillis();
+ soneFollowingTimes.put(soneId, now);
+ Optional followedSone = getSone(soneId);
+ if (!followedSone.isPresent()) {
+ return;
}
- /* write null ID as terminator. */
- configuration.getStringValue(sonePrefix + "/Post." + postId + "/ID").setValue(null);
-
- int replyId = 0;
- for (Reply reply : sone.getReplies()) {
- String replyPrefix = sonePrefix + "/Reply." + replyId++;
- configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
- configuration.getStringValue(replyPrefix + "/Post").setValue(reply.getPost().getId());
- configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
- configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
+ for (Post post : followedSone.get().getPosts()) {
+ if (post.getTime() < now) {
+ markPostKnown(post);
+ }
}
- /* write null ID as terminator. */
- configuration.getStringValue(sonePrefix + "/Reply." + replyId + "/ID").setValue(null);
-
- int friendId = 0;
- for (Sone friend : sone.getFriends()) {
- String friendPrefix = sonePrefix + "/Friend." + friendId++;
- configuration.getStringValue(friendPrefix + "/ID").setValue(friend.getId());
- configuration.getStringValue(friendPrefix + "/Key").setValue(friend.getRequestUri().toString());
- configuration.getStringValue(friendPrefix + "/Name").setValue(friend.getName());
+ for (PostReply reply : followedSone.get().getReplies()) {
+ if (reply.getTime() < now) {
+ markReplyKnown(reply);
+ }
}
- /* write null ID as terminator. */
- configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
-
}
- /* write null ID as terminator. */
- configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
+ }
+ touchConfiguration();
+ }
- } catch (ConfigurationException ce1) {
- logger.log(Level.WARNING, "Could not store configuration!", ce1);
+ /**
+ * Lets the given local Sone unfollow the Sone with the given ID.
+ *
+ * @param sone
+ * The local Sone that should unfollow another Sone
+ * @param soneId
+ * The ID of the Sone being unfollowed
+ */
+ public void unfollowSone(Sone sone, String soneId) {
+ checkNotNull(sone, "sone must not be null");
+ checkNotNull(soneId, "soneId must not be null");
+ database.removeFriend(sone, soneId);
+ boolean unfollowedSoneStillFollowed = false;
+ for (Sone localSone : getLocalSones()) {
+ unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
+ }
+ if (!unfollowedSoneStillFollowed) {
+ synchronized (soneFollowingTimes) {
+ soneFollowingTimes.remove(soneId);
+ }
}
+ touchConfiguration();
+ }
+
+ /**
+ * Sets the trust value of the given origin Sone for the target Sone.
+ *
+ * @param origin
+ * The origin Sone
+ * @param target
+ * The target Sone
+ * @param trustValue
+ * The trust value (from {@code -100} to {@code 100})
+ */
+ public void setTrust(Sone origin, Sone target, int trustValue) {
+ checkNotNull(origin, "origin must not be null");
+ checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
+ checkNotNull(target, "target must not be null");
+ checkArgument((trustValue >= -100) && (trustValue <= 100), "trustValue must be within [-100, 100]");
+ webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), trustValue, preferences.getTrustComment());
+ }
+
+ /**
+ * Removes any trust assignment for the given target Sone.
+ *
+ * @param origin
+ * The trust origin
+ * @param target
+ * The trust target
+ */
+ public void removeTrust(Sone origin, Sone target) {
+ checkNotNull(origin, "origin must not be null");
+ checkNotNull(target, "target must not be null");
+ checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
+ webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), null, null);
+ }
+
+ /**
+ * Assigns the configured positive trust value for the given target.
+ *
+ * @param origin
+ * The trust origin
+ * @param target
+ * The trust target
+ */
+ public void trustSone(Sone origin, Sone target) {
+ setTrust(origin, target, preferences.getPositiveTrust());
+ }
+
+ /**
+ * Assigns the configured negative trust value for the given target.
+ *
+ * @param origin
+ * The trust origin
+ * @param target
+ * The trust target
+ */
+ public void distrustSone(Sone origin, Sone target) {
+ setTrust(origin, target, preferences.getNegativeTrust());
+ }
+
+ /**
+ * Removes the trust assignment for the given target.
+ *
+ * @param origin
+ * The trust origin
+ * @param target
+ * The trust target
+ */
+ public void untrustSone(Sone origin, Sone target) {
+ removeTrust(origin, target);
+ }
+
+ /**
+ * Updates the stored Sone with the given Sone.
+ *
+ * @param sone
+ * The updated Sone
+ */
+ public void updateSone(Sone sone) {
+ updateSone(sone, false);
+ }
+
+ /**
+ * Updates the stored Sone with the given Sone. If {@code soneRescueMode} is
+ * {@code true}, an older Sone than the current Sone can be given to restore
+ * an old state.
+ *
+ * @param sone
+ * The Sone to update
+ * @param soneRescueMode
+ * {@code true} if the stored Sone should be updated regardless
+ * of the age of the given Sone
+ */
+ 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;
+ }
+ List