Replace index page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / IndexPage.kt
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt
new file mode 100644 (file)
index 0000000..7753b40
--- /dev/null
@@ -0,0 +1,40 @@
+package net.pterodactylus.sone.web.pages
+
+import net.pterodactylus.sone.notify.PostVisibilityFilter
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.util.collection.Pagination
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateContext
+
+/**
+ * The index page shows the main page of Sone. This page will contain the posts
+ * of all friends of the current user.
+ */
+class IndexPage(template: Template, webInterface: WebInterface, private val postVisibilityFilter: PostVisibilityFilter):
+               SoneTemplatePage("index.html", template, "Page.Index.Title", webInterface, true) {
+
+       override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
+               getCurrentSone(request.toadletContext)!!.let { currentSone ->
+                       (currentSone.posts +
+                                       currentSone.friends
+                                                       .map { webInterface.core.getSone(it) }
+                                                       .filter { it.isPresent }
+                                                       .map { it.get() }
+                                                       .flatMap { it.posts } +
+                                       webInterface.core.getDirectedPosts(currentSone.id)
+                                       ).distinct()
+                                       .filter { postVisibilityFilter.isVisible(currentSone).apply(it) }
+                                       .sortedByDescending { it.time }
+                                       .let { posts ->
+                                               Pagination(posts, webInterface.core.preferences.postsPerPage)
+                                                               .setPage(request.parameters["page"]?.toIntOrNull() ?: 0).let { pagination ->
+                                                       templateContext["pagination"] = pagination
+                                                       templateContext["posts"] = pagination.items
+                                               }
+                                       }
+               }
+       }
+
+}