import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
import net.pterodactylus.sone.core.event.*;
import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.AlbumsKt;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.SoneKt;
import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.database.AlbumBuilder;
configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
/* save albums. first, collect in a flat structure, top-level first. */
- List<Album> albums = AlbumsKt.getAllAlbums(sone.getRootAlbum());
+ List<Album> albums = SoneKt.getAllAlbums(sone);
int albumCounter = 0;
for (Album album : albums) {
import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.*;
import static java.util.logging.Logger.getLogger;
+import static java.util.stream.Collectors.toList;
import java.io.*;
import java.nio.charset.Charset;
import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
import net.pterodactylus.sone.core.event.SoneInsertedEvent;
import net.pterodactylus.sone.core.event.SoneInsertingEvent;
-import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.AlbumsKt;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.SoneKt;
import net.pterodactylus.sone.main.SonePlugin;
import net.pterodactylus.util.service.AbstractService;
import net.pterodactylus.util.template.HtmlFilter;
import net.pterodactylus.util.template.XmlFilter;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.FluentIterable;
import com.google.common.collect.Ordering;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
soneProperties.put("likedPostIds", new HashSet<>(sone.getLikedPostIds()));
soneProperties.put("likedReplyIds", new HashSet<>(sone.getLikedReplyIds()));
- soneProperties.put("albums", FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).filter(AlbumsKt.notEmpty()::invoke).toList());
+ soneProperties.put("albums", SoneKt.getAllAlbums(sone).stream().filter(AlbumsKt.notEmpty()::invoke).collect(toList()));
manifestCreator = new ManifestCreator(core, soneProperties);
}
package net.pterodactylus.sone.data;
-import static java.util.Arrays.asList;
-import static java.util.Collections.emptyList;
-
-import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import com.google.common.base.Function;
-import com.google.common.base.Predicate;
-import com.google.common.collect.FluentIterable;
-import com.google.common.collect.ImmutableList;
/**
* Container for images that can also contain nested {@link Album}s.
*/
public interface Album extends Identified, Fingerprintable {
- /** Function that flattens the given album and all albums beneath it. */
- Function<Album, List<Album>> FLATTENER = new Function<Album, List<Album>>() {
-
- @Override
- @Nonnull
- public List<Album> apply(Album album) {
- if (album == null) {
- return emptyList();
- }
- List<Album> albums = new ArrayList<>();
- albums.add(album);
- for (Album subAlbum : album.getAlbums()) {
- albums.addAll(FluentIterable.from(ImmutableList.of(subAlbum)).transformAndConcat(FLATTENER).toList());
- }
- return albums;
- }
- };
-
/** Function that transforms an album into the images it contains. */
Function<Album, List<Image>> IMAGES = new Function<Album, List<Image>>() {
package net.pterodactylus.sone.template;
-import static com.google.common.collect.FluentIterable.from;
-import static java.util.Arrays.asList;
import static java.util.logging.Logger.getLogger;
-import static net.pterodactylus.sone.data.Album.FLATTENER;
+import static java.util.stream.Collectors.toList;
import static net.pterodactylus.sone.data.Album.IMAGES;
import java.util.logging.Level;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.SoneKt;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
import net.pterodactylus.sone.freenet.wot.Trust;
import net.pterodactylus.sone.text.TimeTextConverter;
}
return trust;
} else if (member.equals("allImages")) {
- return from(asList(sone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES);
+ return SoneKt.getAllAlbums(sone).stream().flatMap(a -> IMAGES.apply(a).stream()).collect(toList());
} else if (member.equals("albums")) {
return sone.getRootAlbum().getAlbums();
}
val imageCountComparator: Comparator<Sone> =
comparing<Sone, Int> { it.rootAlbum.allImages.size }.reversed()
+
+val Sone.allAlbums: List<Album>
+ get() =
+ rootAlbum.albums.flatMap(Album::allAlbums)
for (postReply in sone.replies) {
allPostReplies[postReply.id] = postReply
}
- sone.rootAlbum.allAlbums.let { albums ->
+ sone.allAlbums.let { albums ->
soneAlbums.putAll(sone.id, albums)
albums.forEach { album -> allAlbums[album.id] = album }
}
} else if (soneRequest.parameters["mode"] == "gallery") {
templateContext["galleryRequested"] = true
soneRequest.core.sones
- .map(Sone::getRootAlbum)
- .flatMap(Album::getAlbums)
- .flatMap { Album.FLATTENER.apply(it)!! }
+ .flatMap(Sone::allAlbums)
.filterNot(Album::isEmpty)
.sortedBy(Album::getTitle)
.also { albums ->
assertThat(imageCountComparator.compare(sone1, sone2), equalTo(0))
}
+ @Test
+ fun `allAlbums returns all albums of a Sone but the root album`() {
+ val sone = object : IdOnlySone("1") {
+ private val rootAlbum = AlbumImpl(this)
+ override fun getRootAlbum() = rootAlbum
+ }
+ val album1 = AlbumImpl(sone).also(sone.rootAlbum::addAlbum)
+ val album11 = AlbumImpl(sone).also(album1::addAlbum)
+ val album2 = AlbumImpl(sone).also(sone.rootAlbum::addAlbum)
+ assertThat(sone.allAlbums, contains<Album>(album1, album11, album2))
+ }
+
}
package net.pterodactylus.sone.web.pages
import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.data.impl.AlbumImpl
+import net.pterodactylus.sone.data.impl.ImageImpl
import net.pterodactylus.sone.test.*
import net.pterodactylus.sone.web.*
import net.pterodactylus.sone.web.page.*
private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
return mock<Sone>().apply {
- val rootAlbum = mock<Album>()
- val firstAlbum = mock<Album>()
- val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
- whenever(firstAlbum.images).thenReturn(listOf(firstImage))
- val secondAlbum = mock<Album>()
- val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
- whenever(secondAlbum.images).thenReturn(listOf(secondImage))
- whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
- whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
- whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
+ val rootAlbum = AlbumImpl(this)
+ val firstAlbum = AlbumImpl(this).modify().setTitle(firstAlbumTitle).update()
+ firstAlbum.addImage(ImageImpl("1").modify().setSone(this).setKey("key").update())
+ val secondAlbum = AlbumImpl(this).modify().setTitle(secondAlbumTitle).update()
+ secondAlbum.addImage(ImageImpl("2").modify().setSone(this).setKey("key").update())
+ rootAlbum.addAlbum(firstAlbum)
+ rootAlbum.addAlbum(secondAlbum)
whenever(this.rootAlbum).thenReturn(rootAlbum)
}
}