X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloader.java;h=decd574577beeceb3479c39c450f2d31ee9955fb;hb=3c4e92547343582aca1fde00f981f029027a1bb4;hp=9bc14a8a5254bbd6be9019e52dca4963146539fb;hpb=d5efb086bee8f103cbe90c7a953ffbb7ff27b689;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 9bc14a8..decd574 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -35,18 +35,18 @@ import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; +import net.pterodactylus.sone.data.SoneImpl; +import net.pterodactylus.sone.database.PostBuilder; +import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.xml.SimpleXML; import net.pterodactylus.util.xml.XML; import org.w3c.dom.Document; -import com.google.common.base.Optional; -import com.google.common.primitives.Ints; -import com.google.common.primitives.Longs; - import freenet.client.FetchResult; import freenet.keys.FreenetURI; import freenet.support.api.Bucket; @@ -105,18 +105,6 @@ public class SoneDownloader extends AbstractService { } /** - * Removes the given Sone from the downloader. - * - * @param sone - * The Sone to stop watching - */ - public void removeSone(Sone sone) { - if (sones.remove(sone)) { - freenetInterface.unregisterUsk(sone); - } - } - - /** * Fetches the updated Sone. This method is a callback method for * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}. * @@ -167,6 +155,7 @@ public class SoneDownloader extends AbstractService { Sone parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri()); if (parsedSone != null) { if (!fetchOnly) { + parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); core.updateSone(parsedSone); addSone(parsedSone); } @@ -239,7 +228,7 @@ public class SoneDownloader extends AbstractService { return null; } - Sone sone = new Sone(originalSone.getId(), false).setIdentity(originalSone.getIdentity()); + Sone sone = new SoneImpl(originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity()); SimpleXML soneXml; try { @@ -253,7 +242,7 @@ public class SoneDownloader extends AbstractService { Integer protocolVersion = null; String soneProtocolVersion = soneXml.getValue("protocol-version", null); if (soneProtocolVersion != null) { - protocolVersion = Optional.fromNullable(Ints.tryParse(soneProtocolVersion)).or(0); + protocolVersion = Numbers.safeParseInteger(soneProtocolVersion); } if (protocolVersion == null) { logger.log(Level.INFO, "No protocol version found, assuming 0."); @@ -307,16 +296,8 @@ public class SoneDownloader extends AbstractService { } } - String soneInsertUri = soneXml.getValue("insert-uri", null); - if ((soneInsertUri != null) && (sone.getInsertUri() == null)) { - try { - sone.setInsertUri(new FreenetURI(soneInsertUri)); - sone.setLatestEdition(Math.max(sone.getRequestUri().getEdition(), sone.getInsertUri().getEdition())); - } catch (MalformedURLException mue1) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded Sone %s has invalid insert URI: %s", sone, soneInsertUri), mue1); - return null; - } + if (originalSone.getInsertUri() != null) { + sone.setInsertUri(originalSone.getInsertUri()); } SimpleXML profileXml = soneXml.getNode("profile"); @@ -330,9 +311,9 @@ public class SoneDownloader extends AbstractService { String profileFirstName = profileXml.getValue("first-name", null); String profileMiddleName = profileXml.getValue("middle-name", null); String profileLastName = profileXml.getValue("last-name", null); - Integer profileBirthDay = Ints.tryParse(profileXml.getValue("birth-day", null)); - Integer profileBirthMonth = Ints.tryParse(profileXml.getValue("birth-month", null)); - Integer profileBirthYear = Ints.tryParse(profileXml.getValue("birth-year", null)); + Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null)); + Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null)); + Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null)); Profile profile = new Profile(sone).setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName); profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear); /* avatar is processed after images are loaded. */ @@ -375,11 +356,13 @@ public class SoneDownloader extends AbstractService { return null; } try { - Post post = core.getPost(postId).setSone(sone).setTime(Long.parseLong(postTime)).setText(postText); + PostBuilder postBuilder = core.postBuilder(); + /* TODO - parse time correctly. */ + postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { - post.setRecipient(core.getSone(postRecipientId)); + postBuilder.to(postRecipientId); } - posts.add(post); + posts.add(postBuilder.build()); } catch (NumberFormatException nfe1) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime)); @@ -406,7 +389,10 @@ public class SoneDownloader extends AbstractService { return null; } try { - replies.add(core.getPostReply(replyId, true).setSone(sone).setPost(core.getPost(replyPostId)).setTime(Long.parseLong(replyTime)).setText(replyText)); + PostReplyBuilder postReplyBuilder = core.postReplyBuilder(); + /* TODO - parse time correctly. */ + postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText); + replies.add(postReplyBuilder.build()); } catch (NumberFormatException nfe1) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime)); @@ -463,7 +449,7 @@ public class SoneDownloader extends AbstractService { return null; } } - Album album = core.getAlbum(id).setSone(sone).setTitle(title).setDescription(description); + Album album = core.getAlbum(id).setSone(sone).modify().setTitle(title).setDescription(description).update(); if (parent != null) { parent.addAlbum(album); } else { @@ -483,20 +469,20 @@ public class SoneDownloader extends AbstractService { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone)); return null; } - long creationTime = Optional.fromNullable(Longs.tryParse(imageCreationTimeString)).or(0L); - int imageWidth = Optional.fromNullable(Ints.tryParse(imageWidthString)).or(0); - int imageHeight = Optional.fromNullable(Ints.tryParse(imageHeightString)).or(0); + long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L); + int imageWidth = Numbers.safeParseInteger(imageWidthString, 0); + int imageHeight = Numbers.safeParseInteger(imageHeightString, 0); if ((imageWidth < 1) || (imageHeight < 1)) { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString)); return null; } - Image image = core.getImage(imageId).setSone(sone).setKey(imageKey).setCreationTime(creationTime); - image.setTitle(imageTitle).setDescription(imageDescription); - image.setWidth(imageWidth).setHeight(imageHeight); + Image image = core.getImage(imageId).modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update(); + image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update(); + image = image.modify().setWidth(imageWidth).setHeight(imageHeight).update(); album.addImage(image); } } - album.setAlbumImage(albumImageId); + album.modify().setAlbumImage(albumImageId).update(); } } @@ -513,7 +499,9 @@ public class SoneDownloader extends AbstractService { sone.setReplies(replies); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); - sone.setAlbums(topLevelAlbums); + for (Album album : topLevelAlbums) { + sone.getRootAlbum().addAlbum(album); + } } return sone; @@ -533,4 +521,34 @@ public class SoneDownloader extends AbstractService { } } + public class FetchSoneWithUri implements Runnable { + + private final Sone sone; + + public FetchSoneWithUri(Sone sone) { + this.sone = sone; + } + + @Override + public void run() { + fetchSone(sone, sone.getRequestUri()); + } + + } + + public class FetchSone implements Runnable { + + private final Sone sone; + + public FetchSone(Sone sone) { + this.sone = sone; + } + + @Override + public void run() { + fetchSone(sone); + } + + } + }