From 4316b669b28d294dd2b4b9a34e45f080c77037fe Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 26 Nov 2010 14:20:23 +0100 Subject: [PATCH] Add recipients to posts. --- .../java/net/pterodactylus/sone/core/Core.java | 10 ++++++++- .../pterodactylus/sone/core/SoneDownloader.java | 7 ++++++- .../java/net/pterodactylus/sone/data/Post.java | 24 ++++++++++++++++++++++ src/main/resources/templates/insert/sone.xml | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 54908d8..e1dcb9c 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1016,13 +1016,18 @@ public class Core implements IdentityListener { if (postId == null) { break; } + String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null); long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0); String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null); if ((postTime == 0) || (postText == null)) { logger.log(Level.WARNING, "Invalid post found, aborting load!"); return; } - posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText)); + Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText); + if ((postRecipientId != null) && (postRecipientId.length() == 43)) { + post.setRecipient(getSone(postRecipientId)); + } + posts.add(post); } /* load replies. */ @@ -1140,6 +1145,9 @@ public class Core implements IdentityListener { for (Post post : sone.getPosts()) { String postPrefix = sonePrefix + "/Posts/" + postCounter++; configuration.getStringValue(postPrefix + "/ID").setValue(post.getId()); + if (post.getRecipient() != null) { + configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipient().getId()); + } configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime()); configuration.getStringValue(postPrefix + "/Text").setValue(post.getText()); } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 0cedc15..fa0063e 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -295,6 +295,7 @@ public class SoneDownloader extends AbstractService { } else { for (SimpleXML postXml : postsXml.getNodes("post")) { String postId = postXml.getValue("id", null); + String postRecipientId = postXml.getValue("recipient", null); String postTime = postXml.getValue("time", null); String postText = postXml.getValue("text", null); if ((postId == null) || (postTime == null) || (postText == null)) { @@ -303,7 +304,11 @@ public class SoneDownloader extends AbstractService { return null; } try { - posts.add(core.getPost(postId).setSone(sone).setTime(Long.parseLong(postTime)).setText(postText)); + Post post = core.getPost(postId).setSone(sone).setTime(Long.parseLong(postTime)).setText(postText); + if ((postRecipientId != null) && (postRecipientId.length() == 43)) { + post.setRecipient(core.getSone(postRecipientId)); + } + posts.add(post); } catch (NumberFormatException nfe1) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, "Downloaded post for Sone %s with invalid time: %s", new Object[] { sone, postTime }); diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 654b827..21ba42d 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -44,6 +44,9 @@ public class Post { /** The Sone this post belongs to. */ private volatile Sone sone; + /** The Sone of the recipient. */ + private volatile Sone recipient; + /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */ private volatile long time; @@ -140,6 +143,27 @@ public class Post { } /** + * Returns the recipient of this post, if any. + * + * @return The recipient of this post, or {@code null} + */ + public Sone getRecipient() { + return recipient; + } + + /** + * Sets the recipient of this post. + * + * @param recipient + * The recipient of this post, or {@code null} + * @return This post (for method chaining) + */ + public Post setRecipient(Sone recipient) { + this.recipient = recipient; + return this; + } + + /** * Returns the time of the post. * * @return The time of the post (in milliseconds since Jan 1, 1970 UTC) diff --git a/src/main/resources/templates/insert/sone.xml b/src/main/resources/templates/insert/sone.xml index d6dd643..4e8cbae 100644 --- a/src/main/resources/templates/insert/sone.xml +++ b/src/main/resources/templates/insert/sone.xml @@ -21,6 +21,7 @@ <%foreach currentSone.posts post> <% post.id|xml> + <%ifnull !post.recipient><% post.recipient.id|xml><%/if> <% post.text|xml> -- 2.7.4