X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FConfigurationSoneParser.java;h=7b7be9ff421107bd366dfb4bad3ba88c19cb3385;hb=ffb2ea1773cf7e3d1b7fc41ab0e9c3c1eed514e0;hp=01d32219d18c6e49cd86ab2f65b39f446f3320b5;hpb=fdc519da8a4d5c994c3b2233fc049e3be728bb76;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java index 01d3221..7b7be9f 100644 --- a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java @@ -1,15 +1,20 @@ package net.pterodactylus.sone.core; -import java.util.Collection; +import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.Set; import javax.annotation.Nullable; +import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.AlbumBuilderFactory; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostBuilderFactory; import net.pterodactylus.sone.database.PostReplyBuilder; @@ -72,7 +77,7 @@ public class ConfigurationSoneParser { .getValue(defaultValue); } - public Collection parsePosts(PostBuilderFactory postBuilderFactory) + public Set parsePosts(PostBuilderFactory postBuilderFactory) throws InvalidPostFound { Set posts = new HashSet(); while (true) { @@ -109,7 +114,7 @@ public class ConfigurationSoneParser { return (postRecipientId != null) && (postRecipientId.length() == 43); } - public Collection parsePostReplies( + public Set parsePostReplies( PostReplyBuilderFactory postReplyBuilderFactory) { Set replies = new HashSet(); while (true) { @@ -136,8 +141,107 @@ public class ConfigurationSoneParser { return replies; } + public Set parseLikedPostIds() { + Set likedPostIds = new HashSet(); + while (true) { + String likedPostId = + getString("/Likes/Post/" + likedPostIds.size() + "/ID", + null); + if (likedPostId == null) { + break; + } + likedPostIds.add(likedPostId); + } + return likedPostIds; + } + + public Set parseLikedPostReplyIds() { + Set likedPostReplyIds = new HashSet(); + while (true) { + String likedReplyId = getString( + "/Likes/Reply/" + likedPostReplyIds.size() + "/ID", null); + if (likedReplyId == null) { + break; + } + likedPostReplyIds.add(likedReplyId); + } + return likedPostReplyIds; + } + + public Set parseFriends() { + Set friends = new HashSet(); + while (true) { + String friendId = + getString("/Friends/" + friends.size() + "/ID", null); + if (friendId == null) { + break; + } + friends.add(friendId); + } + return friends; + } + + public List parseTopLevelAlbums( + AlbumBuilderFactory albumBuilderFactory) { + Map albums = new HashMap(); + List topLevelAlbums = new ArrayList(); + int albumCounter = 0; + while (true) { + String albumPrefix = "/Albums/" + albumCounter++; + String albumId = getString(albumPrefix + "/ID", null); + if (albumId == null) { + break; + } + String albumTitle = getString(albumPrefix + "/Title", null); + String albumDescription = + getString(albumPrefix + "/Description", null); + String albumParentId = getString(albumPrefix + "/Parent", null); + String albumImageId = + getString(albumPrefix + "/AlbumImage", null); + if ((albumTitle == null) || (albumDescription == null)) { + throw new InvalidAlbumFound(); + } + Album album = albumBuilderFactory.newAlbumBuilder() + .withId(albumId) + .by(sone) + .build() + .modify() + .setTitle(albumTitle) + .setDescription(albumDescription) + .setAlbumImage(albumImageId) + .update(); + if (albumParentId != null) { + Album parentAlbum = albums.get(albumParentId); + if (parentAlbum == null) { + throw new InvalidParentAlbumFound(albumParentId); + } + parentAlbum.addAlbum(album); + } else { + topLevelAlbums.add(album); + } + albums.put(albumId, album); + } + return topLevelAlbums; + } + public static class InvalidPostFound extends RuntimeException { } public static class InvalidPostReplyFound extends RuntimeException { } + public static class InvalidAlbumFound extends RuntimeException { } + + public static class InvalidParentAlbumFound extends RuntimeException { + + private final String albumParentId; + + public InvalidParentAlbumFound(String albumParentId) { + this.albumParentId = albumParentId; + } + + public String getAlbumParentId() { + return albumParentId; + } + + } + }