X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=7d38ed9373416acd97f26dcc04732831b94f502b;hb=f2eaa4ab7f0055a704425f2159efaa5be46b7c5e;hp=e08acd136f1ec212732c879bd98d7407a9e470e6;hpb=ffb2ea1773cf7e3d1b7fc41ab0e9c3c1eed514e0;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 e08acd1..7d38ed9 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -17,11 +17,15 @@ package net.pterodactylus.sone.core; +import static com.google.common.base.Optional.fromNullable; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Predicates.not; +import static com.google.common.primitives.Longs.tryParse; import static java.lang.String.format; +import static java.util.logging.Level.WARNING; import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER; +import static net.pterodactylus.sone.data.Sone.toAllAlbums; import java.net.MalformedURLException; import java.util.ArrayList; @@ -40,6 +44,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound; +import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound; @@ -73,6 +78,7 @@ import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.database.AlbumBuilder; import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.DatabaseException; +import net.pterodactylus.sone.database.ImageBuilder; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostProvider; import net.pterodactylus.sone.database.PostReplyBuilder; @@ -605,6 +611,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return database.getAlbum(albumId).orNull(); } + public ImageBuilder imageBuilder() { + return database.newImageBuilder(); + } + /** * Returns the image with the given ID, creating it if necessary. * @@ -756,6 +766,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Given Identity is null!"); return null; } + final Long latestEdition = fromNullable(tryParse( + identity.getProperty("Sone.LatestEdition"))).or(0L); synchronized (sones) { final Sone sone = getRemoteSone(identity.getId(), true); if (sone.isLocal()) { @@ -764,7 +776,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.setIdentity(identity); boolean newSone = sone.getRequestUri() == null; sone.setRequestUri(SoneUri.create(identity.getRequestUri())); - sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); + sone.setLatestEdition(latestEdition); if (newSone) { synchronized (knownSones) { newSone = !knownSones.contains(sone.getId()); @@ -1002,7 +1014,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, for (PostReply newPostReply : newPostReplies) { eventBus.post(new NewPostReplyFoundEvent(newPostReply)); } - for (Album album : sone.getRootAlbum().getAlbums()) { + for (Album album : toAllAlbums.apply(sone)) { database.storeAlbum(album); for (Image image : album.getImages()) { database.storeImage(image); @@ -1141,37 +1153,24 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /* load images. */ - int imageCounter = 0; - while (true) { - String imagePrefix = sonePrefix + "/Images/" + imageCounter++; - String imageId = configuration.getStringValue(imagePrefix + "/ID").getValue(null); - if (imageId == null) { - break; - } - String albumId = configuration.getStringValue(imagePrefix + "/Album").getValue(null); - String key = configuration.getStringValue(imagePrefix + "/Key").getValue(null); - String title = configuration.getStringValue(imagePrefix + "/Title").getValue(null); - String description = configuration.getStringValue(imagePrefix + "/Description").getValue(null); - Long creationTime = configuration.getLongValue(imagePrefix + "/CreationTime").getValue(null); - Integer width = configuration.getIntValue(imagePrefix + "/Width").getValue(null); - Integer height = configuration.getIntValue(imagePrefix + "/Height").getValue(null); - if ((albumId == null) || (key == null) || (title == null) || (description == null) || (creationTime == null) || (width == null) || (height == null)) { - logger.log(Level.WARNING, "Invalid image found, aborting load!"); - return; - } - Album album = getAlbum(albumId); - if (album == null) { - logger.log(Level.WARNING, "Invalid album image encountered, aborting load!"); - return; - } - Image image = getImage(imageId).modify().setSone(sone).setCreationTime(creationTime).setKey(key).setTitle(title).setDescription(description).setWidth(width).setHeight(height).update(); - album.addImage(image); + try { + configurationSoneParser.parseImages(database); + } catch (InvalidImageFound iif) { + logger.log(WARNING, "Invalid image found, aborting load!"); + return; + } catch (InvalidParentAlbumFound ipaf) { + logger.log(Level.WARNING, + format("Invalid album image (%s) encountered, aborting load!", + ipaf.getAlbumParentId())); + return; } /* load avatar. */ String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null); if (avatarId != null) { - profile.setAvatar(getImage(avatarId, false)); + final Map images = + configurationSoneParser.getImages(); + profile.setAvatar(images.get(avatarId)); } /* load options. */ @@ -1200,6 +1199,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.getRootAlbum().addAlbum(album); } soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint); + for (Album album : toAllAlbums.apply(sone)) { + database.storeAlbum(album); + for (Image image : album.getImages()) { + database.storeImage(image); + } + } } synchronized (knownSones) { for (String friend : friends) { @@ -1947,19 +1952,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, OwnIdentity ownIdentity = identityRemovedEvent.ownIdentity(); Identity identity = identityRemovedEvent.identity(); trustedIdentities.remove(ownIdentity, identity); - boolean foundIdentity = false; for (Entry> trustedIdentity : trustedIdentities.asMap().entrySet()) { if (trustedIdentity.getKey().equals(ownIdentity)) { continue; } if (trustedIdentity.getValue().contains(identity)) { - foundIdentity = true; + return; } } - if (foundIdentity) { - /* some local identity still trusts this identity, don’t remove. */ - return; - } Optional sone = getSone(identity.getId()); if (!sone.isPresent()) { /* TODO - we don’t have the Sone anymore. should this happen? */