7753b4054c72af08e889f900bf91a3f414cb540a
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / IndexPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.notify.PostVisibilityFilter
4 import net.pterodactylus.sone.utils.parameters
5 import net.pterodactylus.sone.web.WebInterface
6 import net.pterodactylus.sone.web.page.FreenetRequest
7 import net.pterodactylus.util.collection.Pagination
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10
11 /**
12  * The index page shows the main page of Sone. This page will contain the posts
13  * of all friends of the current user.
14  */
15 class IndexPage(template: Template, webInterface: WebInterface, private val postVisibilityFilter: PostVisibilityFilter):
16                 SoneTemplatePage("index.html", template, "Page.Index.Title", webInterface, true) {
17
18         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
19                 getCurrentSone(request.toadletContext)!!.let { currentSone ->
20                         (currentSone.posts +
21                                         currentSone.friends
22                                                         .map { webInterface.core.getSone(it) }
23                                                         .filter { it.isPresent }
24                                                         .map { it.get() }
25                                                         .flatMap { it.posts } +
26                                         webInterface.core.getDirectedPosts(currentSone.id)
27                                         ).distinct()
28                                         .filter { postVisibilityFilter.isVisible(currentSone).apply(it) }
29                                         .sortedByDescending { it.time }
30                                         .let { posts ->
31                                                 Pagination(posts, webInterface.core.preferences.postsPerPage)
32                                                                 .setPage(request.parameters["page"]?.toIntOrNull() ?: 0).let { pagination ->
33                                                         templateContext["pagination"] = pagination
34                                                         templateContext["posts"] = pagination.items
35                                                 }
36                                         }
37                 }
38         }
39
40 }