X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=ba66784c82a96688f7a2da855340c003acaa9141;hb=414ee1b03f7155ccb5049ede303dd01020ddcd94;hp=2d24690f8435474d103af3aa6819a350f95bf6ec;hpb=a43efdbac39a7462d5b9392ac064ea0e1042abb4;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 2d24690..ba66784 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -21,9 +21,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import java.net.MalformedURLException; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -63,6 +61,7 @@ import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.data.Sone.SoneStatus; +import net.pterodactylus.sone.data.SoneImpl; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.DatabaseException; @@ -93,7 +92,12 @@ import net.pterodactylus.util.thread.NamedThreadFactory; import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -import com.google.common.collect.Collections2; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import com.google.inject.Inject; @@ -182,13 +186,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, private final Set bookmarkedPosts = new HashSet(); /** Trusted identities, sorted by own identities. */ - private final Map> trustedIdentities = Collections.synchronizedMap(new HashMap>()); - - /** All known albums. */ - private final Map albums = new HashMap(); - - /** All known images. */ - private final Map images = new HashMap(); + private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.create()); /** All temporary images. */ private final Map temporaryImages = new HashMap(); @@ -331,7 +329,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, @Override public Collection getSones() { synchronized (sones) { - return Collections.unmodifiableCollection(sones.values()); + return ImmutableSet.copyOf(sones.values()); } } @@ -357,13 +355,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, @Override public Collection getLocalSones() { synchronized (sones) { - return Collections2.filter(sones.values(), new Predicate() { + return FluentIterable.from(sones.values()).filter(new Predicate() { @Override public boolean apply(Sone sone) { return sone.isLocal(); } - }); + }).toSet(); } } @@ -381,11 +379,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, synchronized (sones) { Sone sone = sones.get(id); if ((sone == null) && create) { - sone = new Sone(id, true); + sone = new SoneImpl(id, true); sones.put(id, sone); } if ((sone != null) && !sone.isLocal()) { - sone = new Sone(id, true); + sone = new SoneImpl(id, true); sones.put(id, sone); } return sone; @@ -398,13 +396,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, @Override public Collection getRemoteSones() { synchronized (sones) { - return Collections2.filter(sones.values(), new Predicate() { + return FluentIterable.from(sones.values()).filter(new Predicate() { @Override public boolean apply(Sone sone) { return !sone.isLocal(); } - }); + }).toSet(); } } @@ -422,7 +420,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, synchronized (sones) { Sone sone = sones.get(id); if ((sone == null) && create && (id != null) && (id.length() == 43)) { - sone = new Sone(id, false); + sone = new SoneImpl(id, false); sones.put(id, sone); } return sone; @@ -468,7 +466,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, checkNotNull(origin, "origin must not be null"); checkNotNull(target, "target must not be null"); checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin’s identity must be an OwnIdentity"); - return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity()); + return trustedIdentities.containsEntry(origin.getIdentity(), target.getIdentity()); } /** @@ -600,7 +598,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, synchronized (bookmarkedPosts) { for (String bookmarkedPostId : bookmarkedPosts) { Optional post = getPost(bookmarkedPostId); - if (!post.isPresent()) { + if (post.isPresent()) { posts.add(post.get()); } } @@ -608,73 +606,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return posts; } - /** - * Returns the album with the given ID, creating a new album if no album - * with the given ID can be found. - * - * @param albumId - * The ID of the album - * @return The album with the given ID - */ - public Album getAlbum(String albumId) { - return getAlbum(albumId, true); + public Optional getAlbum(String albumId) { + return database.getAlbum(albumId); } - /** - * Returns the album with the given ID, optionally creating a new album if - * an album with the given ID can not be found. - * - * @param albumId - * The ID of the album - * @param create - * {@code true} to create a new album if none exists for the - * given ID - * @return The album with the given ID, or {@code null} if no album with the - * given ID exists and {@code create} is {@code false} - */ - public Album getAlbum(String albumId, boolean create) { - synchronized (albums) { - Album album = albums.get(albumId); - if (create && (album == null)) { - album = new Album(albumId); - albums.put(albumId, album); - } - return album; - } - } - - /** - * Returns the image with the given ID, creating it if necessary. - * - * @param imageId - * The ID of the image - * @return The image with the given ID - */ - public Image getImage(String imageId) { - return getImage(imageId, true); - } - - /** - * Returns the image with the given ID, optionally creating it if it does - * not exist. - * - * @param imageId - * The ID of the image - * @param create - * {@code true} to create an image if none exists with the given - * ID - * @return The image with the given ID, or {@code null} if none exists and - * none was created - */ - public Image getImage(String imageId, boolean create) { - synchronized (images) { - Image image = images.get(imageId); - if (create && (image == null)) { - image = new Image(imageId); - images.put(imageId, image); - } - return image; - } + public Optional getImage(String imageId) { + return database.getImage(imageId); } /** @@ -738,6 +675,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Given OwnIdentity is null!"); return null; } + logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity)); synchronized (sones) { final Sone sone; try { @@ -750,7 +688,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); sone.setKnown(true); /* TODO - load posts ’n stuff */ - trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet())); sones.put(ownIdentity.getId(), sone); final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone); soneInserters.put(sone, soneInserter); @@ -799,7 +736,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return null; } synchronized (sones) { - final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity); + final Sone sone = getRemoteSone(identity.getId(), true); + if (sone.isLocal()) { + return sone; + } + sone.setIdentity(identity); boolean newSone = sone.getRequestUri() == null; sone.setRequestUri(SoneUri.create(identity.getRequestUri())); sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); @@ -1026,24 +967,26 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } } database.storePostReplies(sone, sone.getReplies()); - synchronized (albums) { - synchronized (images) { - for (Album album : storedSone.get().getAlbums()) { - albums.remove(album.getId()); - for (Image image : album.getImages()) { - images.remove(image.getId()); - } - } - for (Album album : sone.getAlbums()) { - albums.put(album.getId(), album); - for (Image image : album.getImages()) { - images.put(image.getId(), image); - } - } + for (Album album : storedSone.get().getRootAlbum().getAlbums()) { + database.removeAlbum(album); + for (Image image : album.getImages()) { + database.removeImage(image); + } + } + for (Album album : sone.getRootAlbum().getAlbums()) { + database.storeAlbum(album); + for (Image image : album.getImages()) { + database.storeImage(image); } } synchronized (sones) { sone.setOptions(storedSone.get().getOptions()); + sone.setKnown(storedSone.get().isKnown()); + sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); + if (sone.isLocal()) { + soneInserters.get(storedSone.get()).setSone(sone); + touchConfiguration(); + } sones.put(sone.getId(), sone); } } @@ -1110,6 +1053,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.FINE, String.format("Tried to load non-local Sone: %s", sone)); return; } + logger.info(String.format("Loading local Sone: %s", sone)); /* initialize options. */ sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption(false)); @@ -1220,7 +1164,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /* load albums. */ - List topLevelAlbums = new ArrayList(); + Map albums = Maps.newHashMap(); int albumCounter = 0; while (true) { String albumPrefix = sonePrefix + "/Albums/" + albumCounter++; @@ -1236,19 +1180,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Invalid album found, aborting load!"); return; } - Album album = getAlbum(albumId).setSone(sone).setTitle(albumTitle).setDescription(albumDescription).setAlbumImage(albumImageId); + Album parentAlbum = sone.getRootAlbum(); if (albumParentId != null) { - Album parentAlbum = getAlbum(albumParentId, false); - if (parentAlbum == null) { - logger.log(Level.WARNING, String.format("Invalid parent album ID: %s", albumParentId)); - return; - } - parentAlbum.addAlbum(album); - } else { - if (!topLevelAlbums.contains(album)) { - topLevelAlbums.add(album); - } + parentAlbum = albums.get(albumParentId); } + Album album = parentAlbum.newAlbumBuilder().withId(albumId).build().modify().setTitle(albumTitle).setDescription(albumDescription).setAlbumImage(albumImageId).update(); + albums.put(album.getId(), album); } /* load images. */ @@ -1270,20 +1207,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Invalid image found, aborting load!"); return; } - Album album = getAlbum(albumId, false); + Album album = albums.get(albumId); if (album == null) { logger.log(Level.WARNING, "Invalid album image encountered, aborting load!"); return; } - Image image = getImage(imageId).setSone(sone).setCreationTime(creationTime).setKey(key); - image.setTitle(title).setDescription(description).setWidth(width).setHeight(height); - album.addImage(image); + album.newImageBuilder().withId(imageId).created(creationTime).at(key).sized(width, height).build().modify().setTitle(title).setDescription(description).update(); } /* load avatar. */ String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null); if (avatarId != null) { - profile.setAvatar(getImage(avatarId, false)); + profile.setAvatar(getImage(avatarId).orNull()); } /* load options. */ @@ -1305,7 +1240,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, for (String friendId : friends) { followSone(sone, friendId); } - sone.setAlbums(topLevelAlbums); + for (Album album : sone.getRootAlbum().getAlbums()) { + sone.getRootAlbum().removeAlbum(album); + } soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint); } synchronized (knownSones) { @@ -1321,6 +1258,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, for (PostReply reply : replies) { reply.setKnown(true); } + + logger.info(String.format("Sone loaded successfully: %s", sone)); } /** @@ -1561,41 +1500,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * Creates a new top-level album for the given Sone. - * - * @param sone - * The Sone to create the album for - * @return The new album - */ - public Album createAlbum(Sone sone) { - return createAlbum(sone, null); - } - - /** - * Creates a new album for the given Sone. - * - * @param sone - * The Sone to create the album for - * @param parent - * The parent of the album (may be {@code null} to create a - * top-level album) - * @return The new album - */ - public Album createAlbum(Sone sone, Album parent) { - Album album = new Album(); - synchronized (albums) { - albums.put(album.getId(), album); - } - album.setSone(sone); - if (parent != null) { - parent.addAlbum(album); - } else { - sone.addAlbum(album); - } - return album; - } - - /** * Deletes the given album. The owner of the album has to be a local Sone, * and the album has to be {@link Album#isEmpty() empty} to be deleted. * @@ -1608,14 +1512,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (!album.isEmpty()) { return; } - if (album.getParent() == null) { - album.getSone().removeAlbum(album); - } else { - album.getParent().removeAlbum(album); - } - synchronized (albums) { - albums.remove(album.getId()); - } + album.getParent().removeAlbum(album); + database.removeAlbum(album); touchConfiguration(); } @@ -1636,11 +1534,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, checkNotNull(temporaryImage, "temporaryImage must not be null"); checkArgument(sone.isLocal(), "sone must be a local Sone"); checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone"); - Image image = new Image(temporaryImage.getId()).setSone(sone).setCreationTime(System.currentTimeMillis()); - album.addImage(image); - synchronized (images) { - images.put(image.getId(), image); - } + Image image = album.newImageBuilder().withId(temporaryImage.getId()).createdNow().sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build(); + database.storeImage(image); imageInserter.insertImage(temporaryImage, image); return image; } @@ -1658,9 +1553,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, checkArgument(image.getSone().isLocal(), "image must belong to a local Sone"); deleteTemporaryImage(image.getId()); image.getAlbum().removeImage(image); - synchronized (images) { - images.remove(image.getId()); - } + database.removeImage(image); touchConfiguration(); } @@ -1673,9 +1566,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The encoded data of the image * @return The temporary image */ - public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData) { - TemporaryImage temporaryImage = new TemporaryImage(); - temporaryImage.setMimeType(mimeType).setImageData(imageData); + public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData, int width, int height) { + TemporaryImage temporaryImage = new TemporaryImage(mimeType, imageData, width, height); synchronized (temporaryImages) { temporaryImages.put(temporaryImage.getId(), temporaryImage); } @@ -1704,9 +1596,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, synchronized (temporaryImages) { temporaryImages.remove(imageId); } - Image image = getImage(imageId, false); - if (image != null) { - imageInserter.cancelImageInsert(image); + Optional image = getImage(imageId); + if (image.isPresent()) { + imageInserter.cancelImageInsert(image.get()); } } @@ -1867,7 +1759,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null); /* save albums. first, collect in a flat structure, top-level first. */ - List albums = sone.getAllAlbums(); + List albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList(); int albumCounter = 0; for (Album album : albums) { @@ -1875,7 +1767,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId()); configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle()); configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription()); - configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent() == null ? null : album.getParent().getId()); + configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId()); configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage() == null ? null : album.getAlbumImage().getId()); } configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null); @@ -2122,7 +2014,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, public void ownIdentityRemoved(OwnIdentityRemovedEvent ownIdentityRemovedEvent) { OwnIdentity ownIdentity = ownIdentityRemovedEvent.ownIdentity(); logger.log(Level.FINEST, String.format("Removing OwnIdentity: %s", ownIdentity)); - trustedIdentities.remove(ownIdentity); + trustedIdentities.removeAll(ownIdentity); } /** @@ -2135,7 +2027,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, public void identityAdded(IdentityAddedEvent identityAddedEvent) { Identity identity = identityAddedEvent.identity(); logger.log(Level.FINEST, String.format("Adding Identity: %s", identity)); - trustedIdentities.get(identityAddedEvent.ownIdentity()).add(identity); + trustedIdentities.put(identityAddedEvent.ownIdentity(), identity); addRemoteSone(identity); } @@ -2154,6 +2046,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, @SuppressWarnings("synthetic-access") public void run() { Sone sone = getRemoteSone(identity.getId(), false); + if (sone.isLocal()) { + return; + } sone.setIdentity(identity); sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition())); soneDownloader.addSone(sone); @@ -2172,9 +2067,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, public void identityRemoved(IdentityRemovedEvent identityRemovedEvent) { OwnIdentity ownIdentity = identityRemovedEvent.ownIdentity(); Identity identity = identityRemovedEvent.identity(); - trustedIdentities.get(ownIdentity).remove(identity); + trustedIdentities.remove(ownIdentity, identity); boolean foundIdentity = false; - for (Entry> trustedIdentity : trustedIdentities.entrySet()) { + for (Entry> trustedIdentity : trustedIdentities.asMap().entrySet()) { if (trustedIdentity.getKey().equals(ownIdentity)) { continue; } @@ -2214,7 +2109,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, @Subscribe public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) { logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.image(), imageInsertFinishedEvent.resultingUri())); - imageInsertFinishedEvent.image().setKey(imageInsertFinishedEvent.resultingUri().toString()); + imageInsertFinishedEvent.image().modify().setKey(imageInsertFinishedEvent.resultingUri().toString()).update(); deleteTemporaryImage(imageInsertFinishedEvent.image().getId()); touchConfiguration(); }