X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloader.java;h=0812f668110836d39287639d47b0e0699c43e0cc;hp=4328f02078ec45a6d29370cf68b52288c8568421;hb=bb2c8cb2d47a77bcef2d299ad4bf8e16f22a6198;hpb=2c68e00705b85174b25133c6544f65d8c0b18621 diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 4328f02..0812f66 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -92,9 +92,10 @@ public class SoneDownloader extends AbstractService { * The Sone to add */ public void addSone(Sone sone) { - if (sones.add(sone)) { - freenetInterface.registerUsk(sone, this); + if (!sones.add(sone)) { + freenetInterface.unregisterUsk(sone); } + freenetInterface.registerUsk(sone, this); } /** @@ -117,13 +118,12 @@ public class SoneDownloader extends AbstractService { * The Sone to fetch */ public void fetchSone(Sone sone) { - fetchSone(sone, sone.getRequestUri()); + fetchSone(sone, sone.getRequestUri().sskForUSK()); } /** * Fetches the updated Sone. This method can be used to fetch a Sone from a - * specific URI (which happens when {@link Core#isSoneRescueMode() „Sone - * rescue mode“} is active). + * specific URI. * * @param sone * The Sone to fetch @@ -131,9 +131,23 @@ public class SoneDownloader extends AbstractService { * The URI to fetch the Sone from */ public void fetchSone(Sone sone, FreenetURI soneUri) { - if (core.getSoneStatus(sone) == SoneStatus.downloading) { - return; - } + fetchSone(sone, soneUri, false); + } + + /** + * Fetches the Sone from the given URI. + * + * @param sone + * The Sone to fetch + * @param soneUri + * The URI of the Sone to fetch + * @param fetchOnly + * {@code true} to only fetch and parse the Sone, {@code false} + * to {@link Core#updateSone(Sone) update} it in the core + * @return The downloaded Sone, or {@code null} if the Sone could not be + * downloaded + */ + public Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) { logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, soneUri }); FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" }); core.setSoneStatus(sone, SoneStatus.downloading); @@ -141,13 +155,17 @@ public class SoneDownloader extends AbstractService { Pair fetchResults = freenetInterface.fetchUri(requestUri); if (fetchResults == null) { /* TODO - mark Sone as bad. */ - return; + return null; } logger.log(Level.FINEST, "Got %d bytes back.", fetchResults.getRight().size()); Sone parsedSone = parseSone(sone, fetchResults.getRight(), fetchResults.getLeft()); if (parsedSone != null) { - core.updateSone(parsedSone); + if (!fetchOnly) { + core.updateSone(parsedSone); + addSone(parsedSone); + } } + return parsedSone; } finally { core.setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); } @@ -285,7 +303,7 @@ public class SoneDownloader extends AbstractService { if ((soneInsertUri != null) && (sone.getInsertUri() == null)) { try { sone.setInsertUri(new FreenetURI(soneInsertUri)); - sone.setLatestEdition(Math.max(sone.getRequestUri().getSuggestedEdition(), sone.getInsertUri().getSuggestedEdition())); + sone.setLatestEdition(Math.max(sone.getRequestUri().getEdition(), sone.getInsertUri().getEdition())); } catch (MalformedURLException mue1) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, "Downloaded Sone " + sone + " has invalid insert URI: " + soneInsertUri, mue1); @@ -310,6 +328,25 @@ public class SoneDownloader extends AbstractService { Profile profile = new Profile().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName); profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear); + /* parse profile fields. */ + SimpleXML profileFieldsXml = profileXml.getNode("fields"); + if (profileFieldsXml != null) { + for (SimpleXML fieldXml : profileFieldsXml.getNodes("field")) { + String fieldName = fieldXml.getValue("field-name", null); + String fieldValue = fieldXml.getValue("field-value", null); + if ((fieldName == null) || (fieldValue == null)) { + logger.log(Level.WARNING, "Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", new Object[] { sone, fieldName, fieldValue }); + return null; + } + try { + profile.addField(fieldName).setValue(fieldValue); + } catch (IllegalArgumentException iae1) { + logger.log(Level.WARNING, "Duplicate field: " + fieldName, iae1); + return null; + } + } + } + /* parse posts. */ SimpleXML postsXml = soneXml.getNode("posts"); Set posts = new HashSet();