Add filter for linked images
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 11 Nov 2016 07:21:28 +0000 (08:21 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 11 Nov 2016 17:10:22 +0000 (18:10 +0100)
src/main/kotlin/net/pterodactylus/sone/template/LinkedImagesFilter.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/template/LinkedImagesFilterTest.kt [new file with mode: 0644]

diff --git a/src/main/kotlin/net/pterodactylus/sone/template/LinkedImagesFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/LinkedImagesFilter.kt
new file mode 100644 (file)
index 0000000..584bec3
--- /dev/null
@@ -0,0 +1,23 @@
+package net.pterodactylus.sone.template
+
+import net.pterodactylus.sone.core.ImageLoader
+import net.pterodactylus.sone.core.LoadedImage
+import net.pterodactylus.sone.text.FreenetLinkPart
+import net.pterodactylus.sone.text.Part
+import net.pterodactylus.util.template.Filter
+import net.pterodactylus.util.template.TemplateContext
+
+/**
+ * Filter that takes a number of pre-rendered [Part]s and replaces all identified links to freenet images
+ * with [LoadedImage]s.
+ */
+class LinkedImagesFilter(private val imageLoader: ImageLoader) : Filter {
+
+       @Suppress("UNCHECKED_CAST")
+       override fun format(templateContext: TemplateContext?, data: Any?, parameters: MutableMap<String, Any?>?) =
+                       (data as? Iterable<Part>)
+                                       ?.filterIsInstance<FreenetLinkPart>()
+                                       ?.mapNotNull { imageLoader.toLoadedImage(it.link) }
+                                       ?: listOf<LoadedImage>()
+
+}
diff --git a/src/test/kotlin/net/pterodactylus/sone/template/LinkedImagesFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/LinkedImagesFilterTest.kt
new file mode 100644 (file)
index 0000000..8e86bd4
--- /dev/null
@@ -0,0 +1,37 @@
+package net.pterodactylus.sone.template
+
+import net.pterodactylus.sone.core.ImageLoader
+import net.pterodactylus.sone.core.LoadedImage
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.text.FreenetLinkPart
+import net.pterodactylus.sone.text.LinkPart
+import net.pterodactylus.sone.text.PlainTextPart
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers
+import org.junit.Test
+import org.mockito.Mockito.`when`
+
+/**
+ * Unit test for [LinkedImagesFilter].
+ */
+class LinkedImagesFilterTest {
+
+       private val imageLoader = mock<ImageLoader>()
+       private val filter = LinkedImagesFilter(imageLoader)
+
+       @Test
+       fun `filter finds all loaded freenet images`() {
+               val parts = listOf(
+                               PlainTextPart("text"),
+                               LinkPart("http://link", "link"),
+                               FreenetLinkPart("KSK@link", "link", false),
+                               FreenetLinkPart("KSK@link.png", "link", false)
+               )
+               `when`(imageLoader.toLoadedImage("KSK@link.png")).thenReturn(LoadedImage("KSK@link.png", "image/png", 1440, 900))
+               val loadedImages = filter.format(null, parts, null)
+               assertThat(loadedImages, Matchers.contains(
+                               LoadedImage("KSK@link.png", "image/png", 1440, 900)
+               ))
+       }
+
+}