X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=898e314df5a6d2cc7e8d479336be4e5c16199824;hb=2f4dcbcbc66688f159d96ebfb04d8bd5f96e9c28;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..898e314 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -17,8 +17,13 @@ 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 java.net.MalformedURLException; import java.util.Collection; @@ -61,7 +66,6 @@ import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.data.Sone.SoneStatus; -import net.pterodactylus.sone.data.SoneImpl; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.DatabaseException; @@ -94,7 +98,6 @@ import freenet.keys.FreenetURI; 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; @@ -289,6 +292,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. * @@ -351,7 +358,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, @Override public Collection getLocalSones() { synchronized (sones) { - return FluentIterable.from(sones.values()).filter(new Predicate() { + return from(sones.values()).filter(new Predicate() { @Override public boolean apply(Sone sone) { @@ -366,31 +373,17 @@ 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() { + return from(sones.values()).filter(new Predicate() { @Override public boolean apply(Sone sone) { @@ -405,20 +398,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)); } /** @@ -463,15 +446,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, 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) { @@ -662,7 +636,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, synchronized (sones) { final Sone sone; try { - sone = getLocalSone(ownIdentity.getId(), true).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri())); + sone = database.newSoneBuilder().by(ownIdentity).local().build().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; @@ -719,7 +693,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return null; } synchronized (sones) { - final Sone sone = getRemoteSone(identity.getId(), true); + final Sone sone = database.newSoneBuilder().by(identity).build(); if (sone.isLocal()) { return sone; } @@ -1090,9 +1064,9 @@ 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()); } @@ -1224,7 +1198,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); } @@ -1246,94 +1220,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * 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 @@ -1483,24 +1369,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * 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(); - } - - /** * Creates a new image. * * @param sone @@ -1518,7 +1386,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, 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); imageInserter.insertImage(temporaryImage, image); return image; } @@ -1535,8 +1402,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, 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(); } @@ -1735,7 +1601,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 +1610,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); @@ -2017,14 +1883,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, @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().setIdentity(identity); + sone.get().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.get().getLatestEdition())); + soneDownloader.addSone(sone.get()); + soneDownloader.fetchSone(sone.get()); } }); }