X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=7ea81349dbda12619b97b89aed9487eee737d523;hb=1380d276bf5886e320f89cea232a6db7e4cdb7b8;hp=3e4bf1d96ed2b4a651206307be9e0aec5261267c;hpb=e14e8aff491d7a04e4e04ed9c38bcae630195655;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 3e4bf1d..7ea8134 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.core; +import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.of; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -24,6 +25,7 @@ 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; @@ -62,7 +64,7 @@ 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; @@ -74,7 +76,7 @@ import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostBuilder.PostCreated; import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated; -import net.pterodactylus.sone.database.PostReplyProvider; +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; @@ -114,14 +116,11 @@ import com.google.inject.Inject; * * @author David ‘Bombe’ Roden */ -public class Core extends AbstractService implements SoneProvider, 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(); @@ -237,15 +236,6 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv // /** - * 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. * @@ -421,32 +411,6 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv } /** - * 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()); - } - - @Override - public Optional getPostReply(String replyId) { - return database.getPostReply(replyId); - } - - @Override - public List getReplies(final String postId) { - return database.getReplies(postId); - } - - /** * Returns all Sones that have liked the given post. * * @param post @@ -595,12 +559,7 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity)); synchronized (sones) { final Sone sone; - try { - 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; - } + sone = database.newSoneBuilder().by(ownIdentity.getId()).local().build(Optional.absent()); sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)); sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); sone.setKnown(true); @@ -653,13 +612,12 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv return null; } synchronized (sones) { - final Sone sone = database.newSoneBuilder().by(identity).build(); - if (sone.isLocal()) { - return sone; + Optional existingSone = database.getSone(identity.getId()); + if (existingSone.isPresent() && existingSone.get().isLocal()) { + return existingSone.get(); } - sone.setIdentity(identity); - boolean newSone = sone.getRequestUri() == null; - sone.setRequestUri(SoneUri.create(identity.getRequestUri())); + boolean newSone = !existingSone.isPresent(); + final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).build(Optional.absent()) : existingSone.get(); sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); if (newSone) { synchronized (knownSones) { @@ -681,7 +639,7 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv @Override @SuppressWarnings("synthetic-access") public void run() { - soneDownloader.fetchSone(sone, sone.getRequestUri()); + soneDownloader.fetchSone(sone, TO_FREENET_URI.apply(sone)); } }); @@ -716,7 +674,7 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv } for (PostReply reply : followedSone.get().getReplies()) { if (reply.getTime() < now) { - markReplyKnown(reply); + reply.modify().setKnown().update(Optional.>absent()); } } } @@ -878,7 +836,7 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv continue; } if (reply.getTime() < getSoneFollowingTime(sone)) { - reply.setKnown(true); + reply.modify().setKnown().update(Optional.>absent()); } else if (!reply.isKnown()) { eventBus.post(new NewPostReplyFoundEvent(reply)); } @@ -991,12 +949,14 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv /* 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) { @@ -1173,7 +1133,7 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv } 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)); @@ -1207,22 +1167,12 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv 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 @@ -1268,29 +1218,12 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv 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(); - } - } - - /** * Creates a new image. * * @param sone @@ -1318,7 +1251,7 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv * * @param image * The image to delete - * @see #deleteTemporaryImage(TemporaryImage) + * @see #deleteTemporaryImage(String) */ public void deleteImage(Image image) { checkNotNull(image, "image must not be null"); @@ -1346,17 +1279,6 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv } /** - * 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 @@ -1803,7 +1725,6 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv @SuppressWarnings("synthetic-access") public void run() { 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()); @@ -1904,7 +1825,7 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv */ @Override public void run() { - markReplyKnown(postReply); + postReplyUpdated().get().replyUpdated(postReply); } }, 10, TimeUnit.SECONDS); } @@ -1912,6 +1833,15 @@ public class Core extends AbstractService implements SoneProvider, PostReplyProv }); } + 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