X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=8edb14f364d7265735f2ad5cfe1c7151abeaa588;hb=e42eee396d1646bf97316696f1cd7b836aabd8c6;hp=af810549a7f53a0cc9797ce0a6835299091a86eb;hpb=6163031365fd14a7351a9689ac3325e03c9d165d;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 af81054..8edb14f 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,6 @@ 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; @@ -75,6 +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.SoneBuilder.SoneCreated; import net.pterodactylus.sone.database.SoneProvider; import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; @@ -557,14 +559,8 @@ public class Core extends AbstractService implements SoneProvider { 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.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); @@ -615,14 +611,13 @@ public class Core extends AbstractService implements SoneProvider { return null; } synchronized (sones) { - final Sone sone = database.newSoneBuilder().by(identity).build(); - 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)); + 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()).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()); @@ -643,7 +638,7 @@ public class Core extends AbstractService implements SoneProvider { @Override @SuppressWarnings("synthetic-access") public void run() { - soneDownloader.fetchSone(sone, sone.getRequestUri()); + soneDownloader.fetchSone(sone, TO_FREENET_URI.apply(sone)); } }); @@ -678,7 +673,7 @@ public class Core extends AbstractService implements SoneProvider { } for (PostReply reply : followedSone.get().getReplies()) { if (reply.getTime() < now) { - markReplyKnown(reply); + reply.modify().setKnown().update(Optional.>absent()); } } } @@ -953,12 +948,14 @@ public class Core extends AbstractService implements SoneProvider { /* 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) { @@ -1097,7 +1094,7 @@ public class Core extends AbstractService implements SoneProvider { /* 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. */ @@ -1170,7 +1167,7 @@ public class Core extends AbstractService implements SoneProvider { eventBus.post(new MarkPostKnownEvent(post)); touchConfiguration(); for (PostReply reply : post.getReplies()) { - markReplyKnown(reply); + reply.modify().setKnown().update(postReplyUpdated()); } } @@ -1220,28 +1217,12 @@ public class Core extends AbstractService implements SoneProvider { 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.modify().setKnown().update(postReplyUpdated()); - if (!previouslyKnown) { - touchConfiguration(); - } - } - - /** * Creates a new image. * * @param sone @@ -1743,8 +1724,7 @@ public class Core extends AbstractService implements SoneProvider { @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())); + sone.get().modify().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.get().getLatestEdition())).update(); soneDownloader.addSone(sone.get()); soneDownloader.fetchSone(sone.get()); } @@ -1844,7 +1824,7 @@ public class Core extends AbstractService implements SoneProvider { */ @Override public void run() { - markReplyKnown(postReply); + postReplyUpdated().get().replyUpdated(postReply); } }, 10, TimeUnit.SECONDS); }