X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=a51a189c9c0fa903d98341008873cf8e522a776c;hp=ad96ab0b7dc25c2b5f3914c7a75c7aa1539abe3b;hb=cb1cac49eee468cf29124601f8822b78e919258b;hpb=b61436dafcb3eaae98c2352d188aaacb063031d0 diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index ad96ab0..a51a189 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -38,9 +38,11 @@ import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.fcp.FcpInterface; @@ -135,6 +137,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /* synchronize access on itself. */ private final Map soneStatuses = new HashMap(); + /** The times Sones were followed. */ + private final Map soneFollowingTimes = new HashMap(); + /** Locked local Sones. */ /* synchronize on itself. */ private final Set lockedSones = new HashSet(); @@ -173,7 +178,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis private Set knownPosts = new HashSet(); /** All replies. */ - private Map replies = new HashMap(); + private Map replies = new HashMap(); /** All new replies. */ private Set newReplies = new HashSet(); @@ -505,17 +510,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * * @param id * The ID of the remote Sone to get - * @return The Sone with the given ID - */ - public Sone getRemoteSone(String id) { - return getRemoteSone(id, true); - } - - /** - * Returns the remote Sone with the given ID. - * - * @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 @@ -524,7 +518,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis public Sone getRemoteSone(String id, boolean create) { synchronized (remoteSones) { Sone sone = remoteSones.get(id); - if ((sone == null) && create) { + if ((sone == null) && create && (id != null) && (id.length() == 43)) { sone = new Sone(id); remoteSones.put(id, sone); setSoneStatus(sone, SoneStatus.unknown); @@ -587,6 +581,23 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /** + * Returns the time when the given was first followed by any local Sone. + * + * @param sone + * The Sone to get the time for + * @return The time (in milliseconds since Jan 1, 1970) the Sone has first + * been followed, or {@link Long#MAX_VALUE} + */ + public long getSoneFollowingTime(Sone sone) { + synchronized (soneFollowingTimes) { + if (soneFollowingTimes.containsKey(sone)) { + return soneFollowingTimes.get(sone); + } + return Long.MAX_VALUE; + } + } + + /** * Returns whether the target Sone is trusted by the origin Sone. * * @param origin @@ -676,7 +687,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * The ID of the reply to get * @return The reply */ - public Reply getReply(String replyId) { + public PostReply getReply(String replyId) { return getReply(replyId, true); } @@ -692,11 +703,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * to return {@code null} if no reply can be found * @return The reply, or {@code null} if there is no such reply */ - public Reply getReply(String replyId, boolean create) { + public PostReply getReply(String replyId, boolean create) { synchronized (replies) { - Reply reply = replies.get(replyId); + PostReply reply = replies.get(replyId); if (create && (reply == null)) { - reply = new Reply(replyId); + reply = new PostReply(replyId); replies.put(replyId, reply); } return reply; @@ -710,11 +721,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * The post to get all replies for * @return All replies for the given post */ - public List getReplies(Post post) { + public List getReplies(Post post) { Set sones = getSones(); - List replies = new ArrayList(); + List replies = new ArrayList(); for (Sone sone : sones) { - for (Reply reply : sone.getReplies()) { + for (PostReply reply : sone.getReplies()) { if (reply.getPost().equals(post)) { replies.add(reply); } @@ -762,7 +773,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * The reply to get the liking Sones for * @return The Sones that like the given reply */ - public Set getLikes(Reply reply) { + public Set getLikes(PostReply reply) { Set sones = new HashSet(); for (Sone sone : getSones()) { if (sone.getLikedReplyIds().contains(reply.getId())) { @@ -935,29 +946,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /** - * Adds a local Sone from the given ID which has to be the ID of an own - * identity. - * - * @param id - * The ID of an own identity to add a Sone for - * @return The added (or already existing) Sone - */ - public Sone addLocalSone(String id) { - synchronized (localSones) { - if (localSones.containsKey(id)) { - logger.log(Level.FINE, "Tried to add known local Sone: %s", id); - return localSones.get(id); - } - OwnIdentity ownIdentity = identityManager.getOwnIdentity(id); - if (ownIdentity == null) { - logger.log(Level.INFO, "Invalid Sone ID: %s", id); - return null; - } - return addLocalSone(ownIdentity); - } - } - - /** * Adds a local Sone from the given own identity. * * @param ownIdentity @@ -1007,7 +995,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } Sone sone = addLocalSone(ownIdentity); sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption(false)); - sone.addFriend("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"); + sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption(false)); + sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption(true)); + sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption(true)); + sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption(true)); + sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption(ShowCustomAvatars.NEVER)); + + followSone(sone, getSone("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI")); touchConfiguration(); return sone; } @@ -1025,7 +1019,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis return null; } synchronized (remoteSones) { - final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity); + final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity); boolean newSone = sone.getRequestUri() == null; sone.setRequestUri(getSoneUri(identity.getRequestUri())); sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); @@ -1040,13 +1034,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis coreListenerManager.fireNewSoneFound(sone); for (Sone localSone : getLocalSones()) { if (localSone.getOptions().getBooleanOption("AutoFollow").get()) { - localSone.addFriend(sone.getId()); - touchConfiguration(); + followSone(localSone, sone); } } } } - remoteSones.put(identity.getId(), sone); soneDownloader.addSone(sone); setSoneStatus(sone, SoneStatus.unknown); soneDownloaders.execute(new Runnable() { @@ -1063,6 +1055,95 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /** + * Lets the given local Sone follow the Sone with the given ID. + * + * @param sone + * The local Sone that should follow another Sone + * @param soneId + * The ID of the Sone to follow + */ + public void followSone(Sone sone, String soneId) { + Validation.begin().isNotNull("Sone", sone).isNotNull("Sone ID", soneId).check(); + Sone followedSone = getSone(soneId, true); + if (followedSone == null) { + logger.log(Level.INFO, String.format("Ignored Sone with invalid ID: %s", soneId)); + return; + } + followSone(sone, getSone(soneId)); + } + + /** + * Lets the given local Sone follow the other given Sone. If the given Sone + * was not followed by any local Sone before, this will mark all elements of + * the followed Sone as read that have been created before the current + * moment. + * + * @param sone + * The local Sone that should follow the other Sone + * @param followedSone + * The Sone that should be followed + */ + public void followSone(Sone sone, Sone followedSone) { + Validation.begin().isNotNull("Sone", sone).isNotNull("Followed Sone", followedSone).check(); + sone.addFriend(followedSone.getId()); + synchronized (soneFollowingTimes) { + if (!soneFollowingTimes.containsKey(followedSone)) { + long now = System.currentTimeMillis(); + soneFollowingTimes.put(followedSone, now); + for (Post post : followedSone.getPosts()) { + if (post.getTime() < now) { + markPostKnown(post); + } + } + for (PostReply reply : followedSone.getReplies()) { + if (reply.getTime() < now) { + markReplyKnown(reply); + } + } + } + } + touchConfiguration(); + } + + /** + * Lets the given local Sone unfollow the Sone with the given ID. + * + * @param sone + * The local Sone that should unfollow another Sone + * @param soneId + * The ID of the Sone being unfollowed + */ + public void unfollowSone(Sone sone, String soneId) { + Validation.begin().isNotNull("Sone", sone).isNotNull("Sone ID", soneId).check(); + unfollowSone(sone, getSone(soneId, false)); + } + + /** + * Lets the given local Sone unfollow the other given Sone. If the given + * local Sone is the last local Sone that followed the given Sone, its + * following time will be removed. + * + * @param sone + * The local Sone that should unfollow another Sone + * @param unfollowedSone + * The Sone being unfollowed + */ + public void unfollowSone(Sone sone, Sone unfollowedSone) { + Validation.begin().isNotNull("Sone", sone).isNotNull("Unfollowed Sone", unfollowedSone).check(); + sone.removeFriend(unfollowedSone.getId()); + boolean unfollowedSoneStillFollowed = false; + for (Sone localSone : getLocalSones()) { + unfollowedSoneStillFollowed |= localSone.hasFriend(unfollowedSone.getId()); + } + if (!unfollowedSoneStillFollowed) { + synchronized (soneFollowingTimes) { + soneFollowingTimes.remove(unfollowedSone); + } + } + touchConfiguration(); + } + + /** * Retrieves the trust relationship from the origin to the target. If the * trust relationship can not be retrieved, {@code null} is returned. * @@ -1194,9 +1275,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (newPosts) { for (Post post : sone.getPosts()) { post.setSone(storedSone); - if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) { - newPosts.add(post.getId()); - coreListenerManager.fireNewPostFound(post); + if (!storedPosts.contains(post)) { + if (post.getTime() < getSoneFollowingTime(sone)) { + knownPosts.add(post.getId()); + } else if (!knownPosts.contains(post.getId())) { + newPosts.add(post.getId()); + coreListenerManager.fireNewPostFound(post); + } } posts.put(post.getId(), post); } @@ -1204,25 +1289,45 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } synchronized (replies) { if (!soneRescueMode) { - for (Reply reply : storedSone.getReplies()) { + for (PostReply reply : storedSone.getReplies()) { replies.remove(reply.getId()); if (!sone.getReplies().contains(reply)) { coreListenerManager.fireReplyRemoved(reply); } } } - Set storedReplies = storedSone.getReplies(); + Set storedReplies = storedSone.getReplies(); synchronized (newReplies) { - for (Reply reply : sone.getReplies()) { + for (PostReply reply : sone.getReplies()) { reply.setSone(storedSone); - if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) { - newReplies.add(reply.getId()); - coreListenerManager.fireNewReplyFound(reply); + if (!storedReplies.contains(reply)) { + if (reply.getTime() < getSoneFollowingTime(sone)) { + knownReplies.add(reply.getId()); + } else if (!knownReplies.contains(reply.getId())) { + newReplies.add(reply.getId()); + coreListenerManager.fireNewReplyFound(reply); + } } replies.put(reply.getId(), reply); } } } + synchronized (albums) { + synchronized (images) { + for (Album album : storedSone.getAlbums()) { + albums.remove(album.getId()); + for (Image image : album.getImages()) { + images.remove(image.getId()); + } + } + for (Album album : sone.getAlbums()) { + albums.put(album.getId(), album); + for (Image image : album.getImages()) { + images.put(image.getId(), image); + } + } + } + } synchronized (storedSone) { if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) { storedSone.setTime(sone.getTime()); @@ -1233,7 +1338,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis for (Post post : sone.getPosts()) { storedSone.addPost(post); } - for (Reply reply : sone.getReplies()) { + for (PostReply reply : sone.getReplies()) { storedSone.addReply(reply); } for (String likedPostId : sone.getLikedPostIds()) { @@ -1242,6 +1347,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis for (String likedReplyId : sone.getLikedReplyIds()) { storedSone.addLikedReplyId(likedReplyId); } + for (Album album : sone.getAlbums()) { + storedSone.addAlbum(album); + } } else { storedSone.setPosts(sone.getPosts()); storedSone.setReplies(sone.getReplies()); @@ -1323,6 +1431,10 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /* initialize options. */ sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption(false)); sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption(false)); + sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption(true)); + sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption(true)); + sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption(true)); + sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption(ShowCustomAvatars.NEVER)); /* load Sone. */ String sonePrefix = "Sone/" + sone.getId(); @@ -1334,7 +1446,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue(""); /* load profile. */ - Profile profile = new 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)); @@ -1376,7 +1488,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /* load replies. */ - Set replies = new HashSet(); + Set replies = new HashSet(); while (true) { String replyPrefix = sonePrefix + "/Replies/" + replies.size(); String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null); @@ -1482,9 +1594,19 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis album.addImage(image); } + /* load avatar. */ + String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null); + if (avatarId != null) { + profile.setAvatar(getImage(avatarId, false)); + } + /* load options. */ sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null)); sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null)); + sone.getOptions().getBooleanOption("ShowNotification/NewSones").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null)); + sone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null)); + sone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null)); + sone.getOptions(). getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name()))); /* if we’re still here, Sone was loaded successfully. */ synchronized (sone) { @@ -1494,7 +1616,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis sone.setReplies(replies); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); - sone.setFriends(friends); + for (String friendId : friends) { + followSone(sone, friendId); + } sone.setAlbums(topLevelAlbums); soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint); } @@ -1509,7 +1633,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } } synchronized (newReplies) { - for (Reply reply : replies) { + for (PostReply reply : replies) { knownReplies.add(reply.getId()); } } @@ -1699,7 +1823,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * The text of the reply * @return The created reply */ - public Reply createReply(Sone sone, Post post, String text) { + public PostReply createReply(Sone sone, Post post, String text) { return createReply(sone, post, System.currentTimeMillis(), text); } @@ -1716,12 +1840,12 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * The text of the reply * @return The created reply */ - public Reply createReply(Sone sone, Post post, long time, String text) { + public PostReply createReply(Sone sone, Post post, long time, String text) { if (!isLocalSone(sone)) { logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone); return null; } - final Reply reply = new Reply(sone, post, System.currentTimeMillis(), text); + final PostReply reply = new PostReply(sone, post, System.currentTimeMillis(), text); synchronized (replies) { replies.put(reply.getId(), reply); } @@ -1750,7 +1874,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * @param reply * The reply to delete */ - public void deleteReply(Reply reply) { + public void deleteReply(PostReply reply) { Sone sone = reply.getSone(); if (!isLocalSone(sone)) { logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply); @@ -1774,7 +1898,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * @param reply * The reply to mark as known */ - public void markReplyKnown(Reply reply) { + public void markReplyKnown(PostReply reply) { synchronized (newReplies) { if (newReplies.remove(reply.getId())) { knownReplies.add(reply.getId()); @@ -2009,8 +2133,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis logger.log(Level.INFO, "Saving Sone: %s", sone); try { - ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition())); - /* save Sone into configuration. */ String sonePrefix = "Sone/" + sone.getId(); configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime()); @@ -2024,6 +2146,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay()); configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth()); configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear()); + configuration.getStringValue(sonePrefix + "/Profile/Avatar").setValue(profile.getAvatar()); /* save profile fields. */ int fieldCounter = 0; @@ -2047,7 +2170,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /* save replies. */ int replyCounter = 0; - for (Reply reply : sone.getReplies()) { + for (PostReply reply : sone.getReplies()) { String replyPrefix = sonePrefix + "/Replies/" + replyCounter++; configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId()); configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId()); @@ -2078,7 +2201,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null); /* save albums. first, collect in a flat structure, top-level first. */ - List albums = Sone.flattenAlbums(sone.getAlbums()); + List albums = sone.getAllAlbums(); int albumCounter = 0; for (Album album : albums) { @@ -2113,9 +2236,16 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /* save options. */ configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal()); + configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewSones").getReal()); + configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewPosts").getReal()); + configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewReplies").getReal()); configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").getReal()); + configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions(). getEnumOption("ShowCustomAvatars").get().name()); configuration.save(); + + ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition())); + logger.log(Level.INFO, "Sone %s saved.", sone); } catch (ConfigurationException ce1) { logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1); @@ -2142,6 +2272,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal()); configuration.getIntValue("Option/PostsPerPage").setValue(options.getIntegerOption("PostsPerPage").getReal()); configuration.getIntValue("Option/CharactersPerPost").setValue(options.getIntegerOption("CharactersPerPost").getReal()); + configuration.getIntValue("Option/PostCutOffLength").setValue(options.getIntegerOption("PostCutOffLength").getReal()); configuration.getBooleanValue("Option/RequireFullAccess").setValue(options.getBooleanOption("RequireFullAccess").getReal()); configuration.getIntValue("Option/PositiveTrust").setValue(options.getIntegerOption("PositiveTrust").getReal()); configuration.getIntValue("Option/NegativeTrust").setValue(options.getIntegerOption("NegativeTrust").getReal()); @@ -2161,6 +2292,17 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null); } + /* save Sone following times. */ + soneCounter = 0; + synchronized (soneFollowingTimes) { + for (Entry soneFollowingTime : soneFollowingTimes.entrySet()) { + configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey().getId()); + configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue()); + ++soneCounter; + } + configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null); + } + /* save known posts. */ int postCounter = 0; synchronized (newPosts) { @@ -2215,7 +2357,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis })); options.addIntegerOption("PostsPerPage", new DefaultOption(10, new IntegerRangeValidator(1, Integer.MAX_VALUE))); - options.addIntegerOption("CharactersPerPost", new DefaultOption(200, new OrValidator(new IntegerRangeValidator(50, Integer.MAX_VALUE), new EqualityValidator(-1)))); + options.addIntegerOption("CharactersPerPost", new DefaultOption(400, new OrValidator(new IntegerRangeValidator(50, Integer.MAX_VALUE), new EqualityValidator(-1)))); + options.addIntegerOption("PostCutOffLength", new DefaultOption(200, new OrValidator(new IntegerRangeValidator(50, Integer.MAX_VALUE), new EqualityValidator(-1)))); options.addBooleanOption("RequireFullAccess", new DefaultOption(false)); options.addIntegerOption("PositiveTrust", new DefaultOption(75, new IntegerRangeValidator(0, 100))); options.addIntegerOption("NegativeTrust", new DefaultOption(-25, new IntegerRangeValidator(-100, 100))); @@ -2255,6 +2398,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis loadConfigurationValue("InsertionDelay"); loadConfigurationValue("PostsPerPage"); loadConfigurationValue("CharactersPerPost"); + loadConfigurationValue("PostCutOffLength"); options.getBooleanOption("RequireFullAccess").set(configuration.getBooleanValue("Option/RequireFullAccess").getValue(null)); loadConfigurationValue("PositiveTrust"); loadConfigurationValue("NegativeTrust"); @@ -2275,6 +2419,25 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } } + /* load Sone following times. */ + 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); + Sone followedSone = getSone(soneId); + if (followedSone == null) { + logger.log(Level.WARNING, String.format("Ignoring Sone with invalid ID: %s", soneId)); + } else { + synchronized (soneFollowingTimes) { + soneFollowingTimes.put(getSone(soneId), time); + } + } + ++soneCounter; + } + /* load known posts. */ int postCounter = 0; while (true) { @@ -2390,7 +2553,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis @Override @SuppressWarnings("synthetic-access") public void run() { - Sone sone = getRemoteSone(identity.getId()); + Sone sone = getRemoteSone(identity.getId(), false); sone.setIdentity(identity); sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition())); soneDownloader.addSone(sone); @@ -2434,7 +2597,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } synchronized (replies) { synchronized (newReplies) { - for (Reply reply : sone.getReplies()) { + for (PostReply reply : sone.getReplies()) { replies.remove(reply.getId()); newReplies.remove(reply.getId()); coreListenerManager.fireReplyRemoved(reply); @@ -2657,6 +2820,39 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /** + * Returns the number of characters the shortened post should have. + * + * @return The number of characters of the snippet + */ + public int getPostCutOffLength() { + return options.getIntegerOption("PostCutOffLength").get(); + } + + /** + * Validates the number of characters after which to cut off the post. + * + * @param postCutOffLength + * The number of characters of the snippet + * @return {@code true} if the number of characters of the snippet is + * valid, {@code false} otherwise + */ + public boolean validatePostCutOffLength(Integer postCutOffLength) { + return options.getIntegerOption("PostCutOffLength").validate(postCutOffLength); + } + + /** + * Sets the number of characters the shortened post should have. + * + * @param postCutOffLength + * The number of characters of the snippet + * @return This preferences + */ + public Preferences setPostCutOffLength(Integer postCutOffLength) { + options.getIntegerOption("PostCutOffLength").set(postCutOffLength); + return this; + } + + /** * Returns whether Sone requires full access to be even visible. * * @return {@code true} if Sone requires full access, {@code false}