X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=c3ac305dc39f3ec513409abbc92125f1feff40ce;hb=f333f58180a7f112394cd768d86c95a3c9edf794;hp=90b6e2ee5745d2953e114843c21bf430a8b7f8cb;hpb=551444095937c85ad3ae7009c0979c5f433036d3;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 90b6e2e..c3ac305 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -143,9 +143,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /** The trust updater. */ private final WebOfTrustUpdater webOfTrustUpdater; - /** The times Sones were followed. */ - private final Map soneFollowingTimes = new HashMap(); - /** Locked local Sones. */ /* synchronize on itself. */ private final Set lockedSones = new HashSet(); @@ -373,9 +370,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * been followed, or {@link Long#MAX_VALUE} */ public long getSoneFollowingTime(Sone sone) { - synchronized (soneFollowingTimes) { - return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE); - } + return database.getSoneFollowingTime(sone.getId()).or(Long.MAX_VALUE); } /** @@ -605,14 +600,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The own identity to create a Sone from * @return The added (or already existing) Sone */ - public Sone addLocalSone(OwnIdentity ownIdentity) { + public LocalSone addLocalSone(OwnIdentity ownIdentity) { if (ownIdentity == null) { logger.log(Level.WARNING, "Given OwnIdentity is null!"); return null; } logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity)); LocalSone sone = database.registerLocalSone(ownIdentity); - SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId()); + SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, database, ownIdentity.getId()); eventBus.register(soneInserter); synchronized (soneInserters) { soneInserters.put(sone, soneInserter); @@ -637,7 +632,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.SEVERE, String.format("Could not add “Sone” context to own identity: %s", ownIdentity)); return null; } - Sone sone = addLocalSone(ownIdentity); + LocalSone sone = addLocalSone(ownIdentity); followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"); touchConfiguration(); @@ -670,7 +665,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.setKnown(!newSone); if (newSone) { eventBus.post(new NewSoneFoundEvent(sone)); - for (Sone localSone : getLocalSones()) { + for (LocalSone localSone : getLocalSones()) { if (localSone.getOptions().isAutoFollow()) { followSone(localSone, sone.getId()); } @@ -691,31 +686,28 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @param soneId * The ID of the Sone to follow */ - public void followSone(Sone sone, String soneId) { + public void followSone(LocalSone sone, String soneId) { checkNotNull(sone, "sone must not be null"); checkNotNull(soneId, "soneId must not be null"); + boolean newFriend = !database.getSoneFollowingTime(soneId).isPresent(); database.addFriend(sone, soneId); - synchronized (soneFollowingTimes) { - if (!soneFollowingTimes.containsKey(soneId)) { - long now = System.currentTimeMillis(); - soneFollowingTimes.put(soneId, now); - Optional followedSone = getSone(soneId); - if (!followedSone.isPresent()) { - return; - } - for (Post post : followedSone.get().getPosts()) { - if (post.getTime() < now) { - markPostKnown(post); - } + if (newFriend) { + long now = System.currentTimeMillis(); + Optional followedSone = getSone(soneId); + if (!followedSone.isPresent()) { + return; + } + for (Post post : followedSone.get().getPosts()) { + if (post.getTime() < now) { + markPostKnown(post); } - for (PostReply reply : followedSone.get().getReplies()) { - if (reply.getTime() < now) { - markReplyKnown(reply); - } + } + for (PostReply reply : followedSone.get().getReplies()) { + if (reply.getTime() < now) { + markReplyKnown(reply); } } } - touchConfiguration(); } /** @@ -726,20 +718,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @param soneId * The ID of the Sone being unfollowed */ - public void unfollowSone(Sone sone, String soneId) { + public void unfollowSone(LocalSone sone, String soneId) { checkNotNull(sone, "sone must not be null"); checkNotNull(soneId, "soneId must not be null"); database.removeFriend(sone, soneId); - boolean unfollowedSoneStillFollowed = false; - for (Sone localSone : getLocalSones()) { - unfollowedSoneStillFollowed |= localSone.hasFriend(soneId); - } - if (!unfollowedSoneStillFollowed) { - synchronized (soneFollowingTimes) { - soneFollowingTimes.remove(soneId); - } - } - touchConfiguration(); } /** @@ -1276,17 +1258,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, try { preferences.saveTo(configuration); - /* save Sone following times. */ - int soneCounter = 0; - synchronized (soneFollowingTimes) { - for (Entry soneFollowingTime : soneFollowingTimes.entrySet()) { - configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey()); - configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue()); - ++soneCounter; - } - configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null); - } - /* save known posts. */ database.save(); @@ -1309,20 +1280,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, */ private void loadConfiguration() { new PreferencesLoader(preferences).loadFrom(configuration); - - /* load Sone following times. */ - int soneCounter = 0; - while (true) { - String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null); - if (soneId == null) { - break; - } - long time = configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").getValue(Long.MAX_VALUE); - synchronized (soneFollowingTimes) { - soneFollowingTimes.put(soneId, time); - } - ++soneCounter; - } } /**