X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=a2e54a67b5b20c6a6ad0cc0f56292a0b25f13a03;hb=11a7eb67daf4f2ca120c476123bb676b9b304e73;hp=2628127bbcca666b723ee55d34d7a2ac76a2b255;hpb=7e76314ca011cb79fa7c25fb1c1239eed7d4e8f6;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 2628127..a2e54a6 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -17,10 +17,15 @@
package net.pterodactylus.sone.core;
+import static com.google.common.base.Optional.of;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Predicates.not;
+import static com.google.common.collect.FluentIterable.from;
+import static net.pterodactylus.sone.data.Identified.GET_ID;
+import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
+import static net.pterodactylus.sone.data.Sone.TO_FREENET_URI;
-import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -57,18 +62,19 @@ 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.Reply.Modifier.ReplyUpdated;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
import net.pterodactylus.sone.data.Sone.SoneStatus;
-import net.pterodactylus.sone.data.SoneImpl;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.DatabaseException;
+import net.pterodactylus.sone.database.ImageBuilder.ImageCreated;
import net.pterodactylus.sone.database.PostBuilder;
-import net.pterodactylus.sone.database.PostProvider;
+import net.pterodactylus.sone.database.PostBuilder.PostCreated;
import net.pterodactylus.sone.database.PostReplyBuilder;
-import net.pterodactylus.sone.database.PostReplyProvider;
+import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
+import net.pterodactylus.sone.database.SoneBuilder.SoneCreated;
import net.pterodactylus.sone.database.SoneProvider;
import net.pterodactylus.sone.fcp.FcpInterface;
import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
@@ -89,12 +95,10 @@ import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.service.AbstractService;
import net.pterodactylus.util.thread.NamedThreadFactory;
-import freenet.keys.FreenetURI;
-
+import com.google.common.base.Function;
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.Maps;
@@ -109,14 +113,11 @@ import com.google.inject.Inject;
*
* @author David âBombeâ Roden
*/
-public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
+public class Core extends AbstractService implements SoneProvider {
/** The logger. */
private static final Logger logger = Logging.getLogger(Core.class);
- /** The start time. */
- private final long startupTime = System.currentTimeMillis();
-
/** The options. */
private final Options options = new Options();
@@ -171,10 +172,6 @@ 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();
@@ -232,15 +229,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
//
/**
- * Returns the time Sone was started.
- *
- * @return The startup time (in milliseconds since Jan 1, 1970 UTC)
- */
- public long getStartupTime() {
- return startupTime;
- }
-
- /**
* Sets the configuration to use. This will automatically save the current
* configuration to the given configuration.
*
@@ -289,6 +277,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
this.fcpInterface = fcpInterface;
}
+ public Database getDatabase() {
+ return database;
+ }
+
/**
* Returns the Sone rescuer for the given local Sone.
*
@@ -299,7 +291,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);
@@ -323,42 +315,24 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
}
- /** {@inheritDocs} */
@Override
public Collection getSones() {
- synchronized (sones) {
- return ImmutableSet.copyOf(sones.values());
- }
+ return database.getSones();
+ }
+
+ @Override
+ public Function> getSone() {
+ return database.getSone();
}
- /**
- * 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
- */
@Override
public Optional getSone(String id) {
- synchronized (sones) {
- return Optional.fromNullable(sones.get(id));
- }
+ return database.getSone(id);
}
- /** {@inheritDocs} */
@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();
}
/**
@@ -366,38 +340,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*
* @param id
* The ID of the Sone
- * @param create
- * {@code true} to create a new Sone if none exists, {@code false} to return
- * null if none exists
* @return The Sone with the given ID, or {@code null}
*/
- public Sone getLocalSone(String id, boolean create) {
- synchronized (sones) {
- Sone sone = sones.get(id);
- if ((sone == null) && create) {
- sone = new SoneImpl(id, true);
- sones.put(id, sone);
- }
- if ((sone != null) && !sone.isLocal()) {
- sone = new SoneImpl(id, true);
- sones.put(id, sone);
- }
- return sone;
- }
+ public Optional getLocalSone(String id) {
+ return from(database.getSone(id).asSet()).firstMatch(LOCAL_SONE_FILTER);
}
- /** {@inheritDocs} */
@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();
}
/**
@@ -405,20 +356,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*
* @param id
* The ID of the remote Sone to get
- * @param create
- * {@code true} to always create a Sone, {@code false} to return {@code null}
- * if no Sone with the given ID exists
* @return The Sone with the given ID
*/
- public Sone getRemoteSone(String id, boolean create) {
- synchronized (sones) {
- Sone sone = sones.get(id);
- if ((sone == null) && create && (id != null) && (id.length() == 43)) {
- sone = new SoneImpl(id, false);
- sones.put(id, sone);
- }
- return sone;
- }
+ public Optional getRemoteSone(String id) {
+ return from(database.getSone(id).asSet()).firstMatch(not(LOCAL_SONE_FILTER));
}
/**
@@ -448,105 +389,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
- */
- 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} */
- @Override
- public Collection getDirectedPosts(final String recipientId) {
- checkNotNull(recipientId, "recipient must not be null");
- return database.getDirectedPosts(recipientId);
- }
-
- /**
- * Returns a post reply builder.
- *
- * @return A new post reply builder
- */
- public PostReplyBuilder postReplyBuilder() {
- return database.newPostReplyBuilder();
- }
-
- /** {@inheritDoc} */
- @Override
- public Optional getPostReply(String replyId) {
- return database.getPostReply(replyId);
- }
-
- /** {@inheritDoc} */
- @Override
- public List getReplies(final String postId) {
- return database.getReplies(postId);
- }
-
- /**
- * Returns all Sones that have liked the given post.
- *
- * @param post
- * The post to get the liking Sones for
- * @return The Sones that like the given post
- */
- public Set getLikes(Post post) {
- Set sones = new HashSet();
- for (Sone sone : getSones()) {
- if (sone.getLikedPostIds().contains(post.getId())) {
- sones.add(sone);
- }
- }
- return sones;
- }
-
- /**
- * Returns all Sones that have liked the given reply.
- *
- * @param reply
- * The reply to get the liking Sones for
- * @return The Sones that like the given reply
- */
- public Set getLikes(PostReply reply) {
- Set sones = new HashSet();
- for (Sone sone : getSones()) {
- if (sone.getLikedReplyIds().contains(reply.getId())) {
- sones.add(sone);
- }
- }
- return sones;
- }
-
- /**
* Returns whether the given post is bookmarked.
*
* @param post
@@ -581,7 +423,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
Set posts = new HashSet();
synchronized (bookmarkedPosts) {
for (String bookmarkedPostId : bookmarkedPosts) {
- Optional post = getPost(bookmarkedPostId);
+ Optional post = database.getPost(bookmarkedPostId);
if (post.isPresent()) {
posts.add(post.get());
}
@@ -659,19 +501,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return null;
}
logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
- synchronized (sones) {
+ synchronized (soneRescuers) {
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 = database.newSoneBuilder().by(ownIdentity.getId()).local().using(new Client("Sone", SonePlugin.VERSION.toString())).build(Optional.absent());
+ sone.modify().setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)).update();
sone.setKnown(true);
/* TODO - load posts ân stuff */
- sones.put(ownIdentity.getId(), sone);
+ database.storeSone(sone);
final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone);
soneInserters.put(sone, soneInserter);
sone.setStatus(SoneStatus.idle);
@@ -718,15 +554,14 @@ 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;
- }
- sone.setIdentity(identity);
- boolean newSone = sone.getRequestUri() == null;
- sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
- sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ synchronized (soneRescuers) {
+ Optional existingSone = database.getSone(identity.getId());
+ if (existingSone.isPresent() && existingSone.get().isLocal()) {
+ return existingSone.get();
+ }
+ boolean newSone = !existingSone.isPresent();
+ final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).using(new Client("Sone", SonePlugin.VERSION.toString())).build(Optional.absent()) : existingSone.get();
+ sone.modify().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)).update();
if (newSone) {
synchronized (knownSones) {
newSone = !knownSones.contains(sone.getId());
@@ -747,7 +582,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
@Override
@SuppressWarnings("synthetic-access")
public void run() {
- soneDownloader.fetchSone(sone, sone.getRequestUri());
+ soneDownloader.fetchSone(sone, TO_FREENET_URI.apply(sone));
}
});
@@ -782,7 +617,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
for (PostReply reply : followedSone.get().getReplies()) {
if (reply.getTime() < now) {
- markReplyKnown(reply);
+ reply.modify().setKnown().update(Optional.>absent());
}
}
}
@@ -944,7 +779,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
continue;
}
if (reply.getTime() < getSoneFollowingTime(sone)) {
- reply.setKnown(true);
+ reply.modify().setKnown().update(Optional.>absent());
} else if (!reply.isKnown()) {
eventBus.post(new NewPostReplyFoundEvent(reply));
}
@@ -962,7 +797,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
database.storeImage(image);
}
}
- synchronized (sones) {
+ synchronized (soneRescuers) {
sone.setOptions(storedSone.get().getOptions());
sone.setKnown(storedSone.get().isKnown());
sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
@@ -970,7 +805,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
soneInserters.get(storedSone.get()).setSone(sone);
touchConfiguration();
}
- sones.put(sone.getId(), sone);
+ database.storeSone(sone);
}
}
}
@@ -988,12 +823,11 @@ 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) {
+ synchronized (soneRescuers) {
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();
}
@@ -1057,12 +891,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/* 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));
+ 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);
+ profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
+ Integer birthDay = configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null);
+ Integer birthMonth = configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null);
+ Integer birthYear = configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null);
+ profile.modify().setBirthYear(birthYear).setBirthMonth(birthMonth).setBirthDay(birthDay).update();
/* load profile fields. */
while (true) {
@@ -1072,7 +908,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
break;
}
String fieldValue = configuration.getStringValue(fieldPrefix + "/Value").getValue("");
- profile.addField(fieldName).setValue(fieldValue);
+ profile.setField(profile.addField(fieldName), fieldValue);
}
/* load posts. */
@@ -1090,11 +926,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.WARNING, "Invalid post found, aborting load!");
return;
}
- PostBuilder postBuilder = postBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText);
+ PostBuilder postBuilder = sone.newPostBuilder().withId(postId).withTime(postTime).withText(postText);
if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
- postBuilder.to(postRecipientId);
+ postBuilder.to(of(postRecipientId));
}
- posts.add(postBuilder.build());
+ posts.add(postBuilder.build(Optional.absent()));
}
/* load replies. */
@@ -1112,8 +948,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
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());
+ PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(postId).withId(replyId).withTime(replyTime).withText(replyText);
+ replies.add(postReplyBuilder.build(Optional.absent()));
}
/* load post likes. */
@@ -1195,13 +1031,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.WARNING, "Invalid album image encountered, aborting load!");
return;
}
- album.newImageBuilder().withId(imageId).created(creationTime).at(key).sized(width, height).build().modify().setTitle(title).setDescription(description).update();
+ album.newImageBuilder().withId(imageId).created(creationTime).at(key).sized(width, height).build(Optional.absent()).modify().setTitle(title).setDescription(description).update();
}
/* load avatar. */
String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
if (avatarId != null) {
- profile.setAvatar(getImage(avatarId).orNull());
+ profile.setAvatar(getImage(avatarId).transform(GET_ID));
}
/* load options. */
@@ -1224,7 +1060,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
followSone(sone, friendId);
}
for (Album album : sone.getRootAlbum().getAlbums()) {
- sone.getRootAlbum().removeAlbum(album);
+ album.remove();
}
soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
}
@@ -1239,101 +1075,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
database.storePostReplies(sone, replies);
for (PostReply reply : replies) {
- reply.setKnown(true);
+ reply.modify().setKnown().update(Optional.>absent());
}
logger.info(String.format("Sone loaded successfully: %s", sone));
}
/**
- * Creates a new post.
- *
- * @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
- * @param text
- * The text of the post
- * @return The created post
- */
- public Post createPost(Sone sone, Optional recipient, String text) {
- return createPost(sone, recipient, System.currentTimeMillis(), 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
- * @param time
- * The time of the post
- * @param text
- * The text of the post
- * @return The created post
- */
- public Post createPost(Sone sone, Optional recipient, long time, String text) {
- checkNotNull(text, "text must not be null");
- checkArgument(text.trim().length() > 0, "text must not be empty");
- if (!sone.isLocal()) {
- logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
- return null;
- }
- PostBuilder postBuilder = database.newPostBuilder();
- postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim());
- if (recipient.isPresent()) {
- postBuilder.to(recipient.get().getId());
- }
- final Post post = postBuilder.build();
- database.storePost(post);
- eventBus.post(new NewPostFoundEvent(post));
- sone.addPost(post);
- touchConfiguration();
- localElementTicker.schedule(new Runnable() {
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void run() {
- markPostKnown(post);
- }
- }, 10, TimeUnit.SECONDS);
- return post;
- }
-
- /**
* Deletes the given post.
*
* @param post
@@ -1361,22 +1109,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
post.setKnown(true);
eventBus.post(new MarkPostKnownEvent(post));
touchConfiguration();
- for (PostReply reply : getReplies(post.getId())) {
- markReplyKnown(reply);
+ for (PostReply reply : post.getReplies()) {
+ reply.modify().setKnown().update(postReplyUpdated());
}
}
/**
- * 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
@@ -1411,44 +1149,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
/**
- * Creates a new reply.
- *
- * @param sone
- * The Sone that creates the reply
- * @param post
- * The post that this reply refers to
- * @param text
- * The text of the reply
- * @return The created reply
- */
- public PostReply createReply(Sone sone, Post post, String text) {
- checkNotNull(text, "text must not be null");
- checkArgument(text.trim().length() > 0, "text must not be empty");
- if (!sone.isLocal()) {
- logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone));
- return null;
- }
- PostReplyBuilder postReplyBuilder = postReplyBuilder();
- postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
- final PostReply reply = postReplyBuilder.build();
- database.storePostReply(reply);
- eventBus.post(new NewPostReplyFoundEvent(reply));
- sone.addReply(reply);
- touchConfiguration();
- localElementTicker.schedule(new Runnable() {
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void run() {
- markReplyKnown(reply);
- }
- }, 10, TimeUnit.SECONDS);
- return reply;
- }
-
- /**
* Deletes the given reply.
*
* @param reply
@@ -1460,43 +1160,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
return;
}
+ postReplyUpdated().get().replyUpdated(reply);
database.removePostReply(reply);
- markReplyKnown(reply);
- sone.removeReply(reply);
- touchConfiguration();
- }
-
- /**
- * Marks the given reply as known, if it is currently not a known reply
- * (according to {@link Reply#isKnown()}).
- *
- * @param reply
- * The reply to mark as known
- */
- public void markReplyKnown(PostReply reply) {
- boolean previouslyKnown = reply.isKnown();
- reply.setKnown(true);
- eventBus.post(new MarkPostReplyKnownEvent(reply));
- if (!previouslyKnown) {
- touchConfiguration();
- }
- }
-
- /**
- * Deletes the given album. The owner of the album has to be a local Sone, and
- * the album has to be {@link Album#isEmpty() empty} to be deleted.
- *
- * @param album
- * The album to remove
- */
- public void deleteAlbum(Album album) {
- checkNotNull(album, "album must not be null");
- checkArgument(album.getSone().isLocal(), "albumâs Sone must be a local Sone");
- if (!album.isEmpty()) {
- return;
- }
- album.getParent().removeAlbum(album);
- database.removeAlbum(album);
touchConfiguration();
}
@@ -1517,8 +1182,7 @@ 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 = album.newImageBuilder().withId(temporaryImage.getId()).createdNow().sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build();
- database.storeImage(image);
+ Image image = album.newImageBuilder().withId(temporaryImage.getId()).sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build(imageCreated());
imageInserter.insertImage(temporaryImage, image);
return image;
}
@@ -1529,14 +1193,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*
* @param image
* The image to delete
- * @see #deleteTemporaryImage(TemporaryImage)
+ * @see #deleteTemporaryImage(String)
*/
public void deleteImage(Image image) {
checkNotNull(image, "image must not be null");
checkArgument(image.getSone().isLocal(), "image must belong to a local Sone");
deleteTemporaryImage(image.getId());
- image.getAlbum().removeImage(image);
- database.removeImage(image);
+ image.remove();
touchConfiguration();
}
@@ -1558,17 +1221,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
@@ -1597,7 +1249,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
// SERVICE METHODS
//
- /** Starts the core. */
@Override
public void serviceStart() {
loadConfiguration();
@@ -1608,7 +1259,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
database.start();
}
- /** {@inheritDoc} */
@Override
public void serviceRun() {
long lastSaved = System.currentTimeMillis();
@@ -1625,11 +1275,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
}
- /** Stops the core. */
@Override
public void serviceStop() {
localElementTicker.shutdownNow();
- synchronized (sones) {
+ synchronized (soneRescuers) {
for (Entry soneInserter : soneInserters.entrySet()) {
soneInserter.getValue().stop();
saveSone(soneInserter.getKey());
@@ -1735,7 +1384,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
/* save albums. first, collect in a flat structure, top-level first. */
- List albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
+ List albums = from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
int albumCounter = 0;
for (Album album : albums) {
@@ -1744,7 +1393,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
- configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage() == null ? null : album.getAlbumImage().getId());
+ configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage().transform(GET_ID).orNull());
}
configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
@@ -1972,6 +1621,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
OwnIdentity ownIdentity = ownIdentityAddedEvent.ownIdentity();
logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity));
if (ownIdentity.hasContext("Sone")) {
+ database.storeIdentity(ownIdentity);
addLocalSone(ownIdentity);
}
}
@@ -2000,6 +1650,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
Identity identity = identityAddedEvent.identity();
logger.log(Level.FINEST, String.format("Adding Identity: %s", identity));
trustedIdentities.put(identityAddedEvent.ownIdentity(), identity);
+ database.storeIdentity(identity);
addRemoteSone(identity);
}
@@ -2012,19 +1663,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
@Subscribe
public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
final Identity identity = identityUpdatedEvent.identity();
+ database.storeIdentity(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);
+ Optional sone = getRemoteSone(identity.getId());
+ sone.get().modify().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.get().getLatestEdition())).update();
+ soneDownloader.addSone(sone.get());
+ soneDownloader.fetchSone(sone.get());
}
});
}
@@ -2066,9 +1714,6 @@ 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());
- }
eventBus.post(new SoneRemovedEvent(sone.get()));
}
@@ -2086,4 +1731,66 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
touchConfiguration();
}
+ public Optional postCreated() {
+ return Optional.of(new PostCreated() {
+ @Override
+ public void postCreated(final Post post) {
+ if (post.isKnown()) {
+ return;
+ }
+ eventBus.post(new NewPostFoundEvent(post));
+ if (post.getSone().isLocal()) {
+ localElementTicker.schedule(new Runnable() {
+ @Override
+ public void run() {
+ markPostKnown(post);
+ }
+ }, 10, TimeUnit.SECONDS);
+ }
+ }
+ });
+ }
+
+ public Optional postReplyCreated() {
+ return Optional.of(new PostReplyCreated() {
+ @Override
+ public void postReplyCreated(final PostReply postReply) {
+ if (postReply.isKnown()) {
+ return;
+ }
+ eventBus.post(new NewPostReplyFoundEvent(postReply));
+ if (postReply.getSone().isLocal()) {
+ localElementTicker.schedule(new Runnable() {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void run() {
+ postReplyUpdated().get().replyUpdated(postReply);
+ }
+ }, 10, TimeUnit.SECONDS);
+ }
+ }
+ });
+ }
+
+ public Optional> postReplyUpdated() {
+ return Optional.>of(new ReplyUpdated() {
+ @Override
+ public void replyUpdated(PostReply reply) {
+ eventBus.post(new MarkPostReplyKnownEvent(reply));
+ }
+ });
+ }
+
+ public Optional imageCreated() {
+ return Optional.of(new ImageCreated() {
+ @Override
+ public void imageCreated(Image image) {
+ /* nothing happens here yet. */
+ }
+ });
+ }
+
}