X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=d60bbda40ee59a9fdacd15102a7886f5b046fb74;hb=76f80f2904bc44149bda93e7c93e90568f2e4a80;hp=f056c0ecfee15588965056e76d7ff28b93be8aaa;hpb=3b751d0c053450961bb66d2507d6187ce56f8785;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 f056c0e..ce5daa2 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -1,5 +1,5 @@
/*
- * Sone - Core.java - Copyright © 2010â2013 David Roden
+ * Sone - Core.java - Copyright © 2010â2020 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,13 +17,17 @@
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 static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.data.AlbumsKt.getAllImages;
-import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -34,42 +38,42 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.*;
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.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 javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.codahale.metrics.*;
+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.event.*;
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.PostBuilder;
-import net.pterodactylus.sone.data.PostBuilderFactory;
import net.pterodactylus.sone.data.PostReply;
-import net.pterodactylus.sone.data.PostReplyBuilder;
-import net.pterodactylus.sone.data.PostReplyBuilderFactory;
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.SoneOptions.LoadExternalContent;
import net.pterodactylus.sone.data.TemporaryImage;
-import net.pterodactylus.sone.fcp.FcpInterface;
-import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
+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;
@@ -79,50 +83,45 @@ import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
import net.pterodactylus.sone.main.SonePlugin;
-import net.pterodactylus.sone.utils.IntegerRangePredicate;
import net.pterodactylus.util.config.Configuration;
import net.pterodactylus.util.config.ConfigurationException;
-import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.service.AbstractService;
import net.pterodactylus.util.thread.NamedThreadFactory;
-import com.google.common.base.Function;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.google.common.collect.Collections2;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Stopwatch;
import com.google.common.collect.FluentIterable;
-import com.google.common.collect.Ordering;
+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 freenet.keys.FreenetURI;
+import com.google.inject.Singleton;
+import kotlin.jvm.functions.Function1;
/**
* The Sone core.
- *
- * @author David âBombeâ Roden
*/
+@Singleton
public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
/** The logger. */
- private static final Logger logger = Logging.getLogger(Core.class);
+ private static final Logger logger = getLogger(Core.class.getName());
/** The start time. */
private final long startupTime = System.currentTimeMillis();
- /** The options. */
- private final Options options = new Options();
+ private final AtomicBoolean debug = new AtomicBoolean(false);
/** 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;
@@ -148,64 +147,29 @@ 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();
-
/** Locked local Sones. */
/* synchronize on itself. */
- private final Set lockedSones = new HashSet();
+ private final Set lockedSones = new HashSet<>();
/** Sone inserters. */
/* synchronize access on this on sones. */
- private final Map soneInserters = new HashMap();
+ private final Map soneInserters = new HashMap<>();
/** Sone rescuers. */
/* 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();
+ private final Map soneRescuers = new HashMap<>();
/** All known Sones. */
- private final Set knownSones = new HashSet();
-
- /** The post builder. */
- private final PostBuilderFactory postBuilderFactory;
-
- /** All posts. */
- private final Map posts = new HashMap();
+ private final Set knownSones = new HashSet<>();
- /** All known posts. */
- private final Set knownPosts = new HashSet();
-
- /** The post reply builder factory. */
- private final PostReplyBuilderFactory postReplyBuilderFactory;
-
- /** All replies. */
- private final Map replies = new HashMap();
-
- /** All known replies. */
- private final Set knownReplies = new HashSet();
-
- /** All bookmarked posts. */
- /* synchronize access on itself. */
- private final Set bookmarkedPosts = new HashSet();
+ /** The post database. */
+ private final Database database;
/** Trusted identities, sorted by own identities. */
- private final Map> trustedIdentities = Collections.synchronizedMap(new HashMap>());
-
- /** All known albums. */
- private final Map albums = new HashMap();
-
- /** All known images. */
- private final Map images = new HashMap();
+ private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.create());
/** All temporary images. */
- private final Map temporaryImages = new HashMap();
+ private final Map temporaryImages = new HashMap<>();
/** Ticker for threads that mark own elements as known. */
private final ScheduledExecutorService localElementTicker = Executors.newScheduledThreadPool(1);
@@ -213,37 +177,27 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** 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 postBuilderFactory
- * The post builder
- * @param postReplyBuilderFactory
- * The post reply builder factory
- */
+ private final MetricRegistry metricRegistry;
+ private final Histogram configurationSaveTimeHistogram;
+
+ private final SoneUriCreator soneUriCreator;
+
@Inject
- public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, PostBuilderFactory postBuilderFactory, PostReplyBuilderFactory postReplyBuilderFactory) {
+ public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database, MetricRegistry metricRegistry, SoneUriCreator soneUriCreator) {
super("Sone Core");
this.configuration = configuration;
this.freenetInterface = freenetInterface;
this.identityManager = identityManager;
- this.soneDownloader = new SoneDownloader(this, freenetInterface);
- this.imageInserter = new ImageInserter(freenetInterface);
- this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
+ this.soneDownloader = soneDownloader;
+ this.imageInserter = imageInserter;
+ this.updateChecker = updateChecker;
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
- this.postBuilderFactory = postBuilderFactory;
- this.postReplyBuilderFactory = postReplyBuilderFactory;
+ this.database = database;
+ this.metricRegistry = metricRegistry;
+ this.soneUriCreator = soneUriCreator;
+ preferences = new Preferences(eventBus);
+ this.configurationSaveTimeHistogram = metricRegistry.histogram("configuration.save.duration", () -> new Histogram(new ExponentiallyDecayingReservoir(3000, 0)));
}
//
@@ -259,16 +213,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return startupTime;
}
- /**
- * 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();
+ @Nonnull
+ public boolean getDebug() {
+ return debug.get();
+ }
+
+ public void setDebug() {
+ debug.set(true);
+ eventBus.post(new DebugActivatedEvent());
}
/**
@@ -299,16 +251,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
@@ -318,7 +260,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);
@@ -342,26 +284,23 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
}
- /**
- * Returns all Sones, remote and local.
- *
- * @return All Sones
- */
- public Set getSones() {
- return new HashSet(sones.values());
+ public SoneBuilder soneBuilder() {
+ return database.newSoneBuilder();
}
/**
- * Returns the Sone with the given ID, regardless whether itâs local or
- * remote.
- *
- * @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
+ * {@inheritDocs}
*/
- public Sone getSone(String id) {
- return getSone(id, true);
+ @Nonnull
+ @Override
+ public Collection getSones() {
+ return database.getSones();
+ }
+
+ @Nonnull
+ @Override
+ public Function1 getSoneLoader() {
+ return database.getSoneLoader();
}
/**
@@ -370,53 +309,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*
* @param id
* The ID of the Sone to get
- * @param create
- * {@code true} to create a new Sone if none exists,
- * {@code false} to return {@code null} if a Sone with the given
- * ID does not exist
* @return The Sone with the given ID, or {@code null} if there is no such
* Sone
*/
@Override
- public Sone getSone(String id, boolean create) {
- synchronized (sones) {
- if (!sones.containsKey(id) && create) {
- Sone sone = new Sone(id, false);
- sones.put(id, sone);
- }
- return sones.get(id);
- }
- }
-
- /**
- * Checks whether the core knows a Sone with the given ID.
- *
- * @param id
- * The ID of the Sone
- * @return {@code true} if there is a Sone with the given ID, {@code false}
- * otherwise
- */
- public boolean hasSone(String id) {
- synchronized (sones) {
- return sones.containsKey(id);
- }
+ @Nullable
+ public Sone getSone(@Nonnull String id) {
+ return database.getSone(id);
}
/**
- * Returns all local Sones.
- *
- * @return All local Sones
+ * {@inheritDocs}
*/
+ @Override
public Collection getLocalSones() {
- synchronized (sones) {
- return Collections2.filter(sones.values(), new Predicate() {
-
- @Override
- public boolean apply(Sone sone) {
- return sone.isLocal();
- }
- });
- }
+ return database.getLocalSones();
}
/**
@@ -424,62 +331,34 @@ 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);
- }
+ public Sone getLocalSone(String id) {
+ Sone sone = database.getSone(id);
+ if ((sone != null) && sone.isLocal()) {
return sone;
}
+ return null;
}
/**
- * Returns all remote Sones.
- *
- * @return All remote Sones
+ * {@inheritDocs}
*/
+ @Override
public Collection getRemoteSones() {
- synchronized (sones) {
- return Collections2.filter(sones.values(), new Predicate() {
-
- @Override
- public boolean apply(Sone sone) {
- return !sone.isLocal();
- }
- });
- }
+ 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);
}
/**
@@ -491,40 +370,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;
- }
-
- /**
- * Returns the time when the given was first followed by any local Sone.
- *
- * @param sone
- * 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 long getSoneFollowingTime(Sone sone) {
- synchronized (soneFollowingTimes) {
- if (soneFollowingTimes.containsKey(sone)) {
- return soneFollowingTimes.get(sone);
- }
- return Long.MAX_VALUE;
- }
- }
-
- /**
- * 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.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity());
+ return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
}
/**
@@ -533,38 +379,30 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return A new post builder
*/
public PostBuilder postBuilder() {
- return postBuilderFactory.newPostBuilder();
+ return database.newPostBuilder();
+ }
+
+ @Nullable
+ @Override
+ public Post getPost(@Nonnull String postId) {
+ return database.getPost(postId);
}
/**
- * {@inheritDoc}
+ * {@inheritDocs}
*/
@Override
- public Post getPost(String postId) {
- synchronized (posts) {
- return posts.get(postId);
- }
+ public Collection getPosts(String soneId) {
+ return database.getPosts(soneId);
}
/**
- * Returns all posts that have the given Sone as recipient.
- *
- * @see Post#getRecipient()
- * @param recipient
- * The recipient of the posts
- * @return All posts that have the given Sone as recipient
- */
- public Set getDirectedPosts(Sone recipient) {
- checkNotNull(recipient, "recipient must not be null");
- Set directedPosts = new HashSet();
- synchronized (posts) {
- for (Post post : posts.values()) {
- if (recipient.equals(post.getRecipient())) {
- directedPosts.add(post);
- }
- }
- }
- return directedPosts;
+ * {@inheritDoc}
+ */
+ @Override
+ public Collection getDirectedPosts(final String recipientId) {
+ checkNotNull(recipientId, "recipient must not be null");
+ return database.getDirectedPosts(recipientId);
}
/**
@@ -573,37 +411,24 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return A new post reply builder
*/
public PostReplyBuilder postReplyBuilder() {
- return postReplyBuilderFactory.newPostReplyBuilder();
+ return database.newPostReplyBuilder();
}
/**
* {@inheritDoc}
*/
+ @Nullable
@Override
public PostReply getPostReply(String replyId) {
- synchronized (replies) {
- return replies.get(replyId);
- }
+ return database.getPostReply(replyId);
}
/**
* {@inheritDoc}
*/
@Override
- public List getReplies(final Post post) {
- return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(FluentIterable.from(getSones()).transformAndConcat(new Function>() {
-
- @Override
- public Iterable apply(Sone sone) {
- return sone.getReplies();
- }
- }).filter(new Predicate() {
-
- @Override
- public boolean apply(PostReply reply) {
- return post.equals(reply.getPost());
- }
- }));
+ public List getReplies(final String postId) {
+ return database.getReplies(postId);
}
/**
@@ -614,7 +439,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The Sones that like the given post
*/
public Set getLikes(Post post) {
- Set sones = new HashSet();
+ Set sones = new HashSet<>();
for (Sone sone : getSones()) {
if (sone.getLikedPostIds().contains(post.getId())) {
sones.add(sone);
@@ -631,7 +456,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The Sones that like the given reply
*/
public Set getLikes(PostReply reply) {
- Set sones = new HashSet();
+ Set sones = new HashSet<>();
for (Sone sone : getSones()) {
if (sone.getLikedReplyIds().contains(reply.getId())) {
sones.add(sone);
@@ -649,21 +474,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);
}
/**
@@ -672,28 +483,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) {
- Post post = getPost(bookmarkedPostId);
- if (post != null) {
- posts.add(post);
- }
- }
- }
- 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();
}
/**
@@ -702,21 +496,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*
* @param albumId
* The ID of the album
- * @param create
- * {@code true} to create a new album if none exists for the
- * given ID
* @return The album with the given ID, or {@code null} if no album with the
- * given ID exists and {@code create} is {@code false}
- */
- public Album getAlbum(String albumId, boolean create) {
- synchronized (albums) {
- Album album = albums.get(albumId);
- if (create && (album == null)) {
- album = new Album(albumId);
- albums.put(albumId, album);
- }
- return album;
- }
+ * given ID exists
+ */
+ @Nullable
+ public Album getAlbum(@Nonnull String albumId) {
+ return database.getAlbum(albumId);
+ }
+
+ public ImageBuilder imageBuilder() {
+ return database.newImageBuilder();
}
/**
@@ -726,6 +515,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* The ID of the image
* @return The image with the given ID
*/
+ @Nullable
public Image getImage(String imageId) {
return getImage(imageId, true);
}
@@ -742,15 +532,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The image with the given ID, or {@code null} if none exists and
* none was created
*/
+ @Nullable
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);
- }
+ Image image = database.getImage(imageId);
+ if (image != null) {
return image;
}
+ if (!create) {
+ return null;
+ }
+ Image newImage = database.newImageBuilder().withId(imageId).build();
+ database.storeImage(newImage);
+ return newImage;
}
/**
@@ -814,26 +607,28 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.WARNING, "Given OwnIdentity is null!");
return null;
}
- 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);
+ 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, metricRegistry, soneUriCreator, ownIdentity.getId());
+ soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
+ eventBus.register(soneInserter);
+ synchronized (soneInserters) {
soneInserters.put(sone, soneInserter);
- sone.setStatus(SoneStatus.idle);
- loadSone(sone);
- soneInserter.start();
- return sone;
}
+ loadSone(sone);
+ database.storeSone(sone);
+ sone.setStatus(SoneStatus.idle);
+ if (sone.getPosts().isEmpty() && sone.getReplies().isEmpty() && getAllImages(sone.getRootAlbum()).isEmpty()) {
+ // dirty hack
+ lockSone(sone);
+ eventBus.post(new SoneLockedOnStartup(sone));
+ }
+ soneInserter.start();
+ return sone;
}
/**
@@ -849,14 +644,8 @@ 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, getSone("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"));
+
+ followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
touchConfiguration();
return sone;
}
@@ -873,37 +662,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).setIdentity(identity);
- boolean newSone = sone.getRequestUri() == null;
- sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
- sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
+ long latestEdition = fromNullable(tryParse(property)).or(0L);
+ Sone existingSone = getSone(identity.getId());
+ if ((existingSone != null )&& existingSone.isLocal()) {
+ return existingSone;
+ }
+ boolean newSone = existingSone == null;
+ Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build();
+ sone.setLatestEdition(latestEdition);
+ if (newSone) {
+ synchronized (knownSones) {
+ newSone = !knownSones.contains(sone.getId());
+ }
+ 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);
- }
+ 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.fetchSoneAsUskAction(sone));
+ return sone;
}
/**
@@ -917,43 +702,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
public void followSone(Sone sone, String soneId) {
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
- Sone followedSone = getSone(soneId, true);
+ database.addFriend(sone, soneId);
+ @SuppressWarnings("ConstantConditions") // we just followed, this canât be null.
+ long now = database.getFollowingTime(soneId);
+ Sone followedSone = getSone(soneId);
if (followedSone == null) {
- logger.log(Level.INFO, String.format("Ignored Sone with invalid ID: %s", soneId));
return;
}
- followSone(sone, getSone(soneId));
- }
-
- /**
- * Lets the given local Sone follow the other given Sone. If the given Sone
- * was not followed by any local Sone before, this will mark all elements of
- * the followed Sone as read that have been created before the current
- * moment.
- *
- * @param sone
- * The local Sone that should follow the other Sone
- * @param followedSone
- * The Sone that should be followed
- */
- public void followSone(Sone sone, Sone followedSone) {
- checkNotNull(sone, "sone must not be null");
- checkNotNull(followedSone, "followedSone must not be null");
- sone.addFriend(followedSone.getId());
- synchronized (soneFollowingTimes) {
- if (!soneFollowingTimes.containsKey(followedSone)) {
- long now = System.currentTimeMillis();
- soneFollowingTimes.put(followedSone, now);
- for (Post post : followedSone.getPosts()) {
- if (post.getTime() < now) {
- markPostKnown(post);
- }
- }
- for (PostReply reply : followedSone.getReplies()) {
- if (reply.getTime() < now) {
- markReplyKnown(reply);
- }
- }
+ for (Post post : followedSone.getPosts()) {
+ if (post.getTime() < now) {
+ markPostKnown(post);
+ }
+ }
+ for (PostReply reply : followedSone.getReplies()) {
+ if (reply.getTime() < now) {
+ markReplyKnown(reply);
}
}
touchConfiguration();
@@ -970,105 +733,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
public void unfollowSone(Sone sone, String soneId) {
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
- unfollowSone(sone, getSone(soneId, false));
- }
-
- /**
- * Lets the given local Sone unfollow the other given Sone. If the given
- * local Sone is the last local Sone that followed the given Sone, its
- * following time will be removed.
- *
- * @param sone
- * The local Sone that should unfollow another Sone
- * @param unfollowedSone
- * The Sone being unfollowed
- */
- public void unfollowSone(Sone sone, Sone unfollowedSone) {
- checkNotNull(sone, "sone must not be null");
- checkNotNull(unfollowedSone, "unfollowedSone must not be null");
- sone.removeFriend(unfollowedSone.getId());
- boolean unfollowedSoneStillFollowed = false;
- for (Sone localSone : getLocalSones()) {
- unfollowedSoneStillFollowed |= localSone.hasFriend(unfollowedSone.getId());
- }
- if (!unfollowedSoneStillFollowed) {
- synchronized (soneFollowingTimes) {
- soneFollowingTimes.remove(unfollowedSone);
- }
- }
+ database.removeFriend(sone, 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
@@ -1089,113 +758,56 @@ 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) {
- if (hasSone(sone.getId())) {
- Sone storedSone = getSone(sone.getId());
+ public void updateSone(final Sone sone, boolean soneRescueMode) {
+ Sone storedSone = getSone(sone.getId());
+ if (storedSone != null) {
if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
return;
}
- synchronized (posts) {
- if (!soneRescueMode) {
- for (Post post : storedSone.getPosts()) {
- posts.remove(post.getId());
- if (!sone.getPosts().contains(post)) {
- eventBus.post(new PostRemovedEvent(post));
- }
- }
- }
- List storedPosts = storedSone.getPosts();
- synchronized (knownPosts) {
- for (Post post : sone.getPosts()) {
- PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
- postBuilder.copyPost(post).from(storedSone);
- Post newPost = postBuilder.build().setKnown(knownPosts.contains(post.getId()));
- if (!storedPosts.contains(newPost)) {
- if (newPost.getTime() < getSoneFollowingTime(sone)) {
- knownPosts.add(newPost.getId());
- newPost.setKnown(true);
- } else if (!knownPosts.contains(newPost.getId())) {
- eventBus.post(new NewPostFoundEvent(newPost));
- }
- }
- posts.put(newPost.getId(), newPost);
- }
- }
+ List