Add page that always requires a logged-in user
[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.data.Sone
4 import net.pterodactylus.sone.notify.PostVisibilityFilter
5 import net.pterodactylus.sone.utils.Pagination
6 import net.pterodactylus.sone.utils.parameters
7 import net.pterodactylus.sone.web.WebInterface
8 import net.pterodactylus.sone.web.page.FreenetRequest
9 import net.pterodactylus.util.template.Template
10 import net.pterodactylus.util.template.TemplateContext
11
12 /**
13  * The index page shows the main page of Sone. This page will contain the posts
14  * of all friends of the current user.
15  */
16 class IndexPage(template: Template, webInterface: WebInterface, private val postVisibilityFilter: PostVisibilityFilter):
17                 LoggedInPage("index.html", template, "Page.Index.Title", webInterface) {
18
19         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
20                         (currentSone.posts +
21                                         currentSone.friends
22                                                         .mapNotNull(webInterface.core::getSone)
23                                                         .flatMap { it.posts } +
24                                         webInterface.core.getDirectedPosts(currentSone.id)
25                                         ).distinct()
26                                         .filter { postVisibilityFilter.isVisible(currentSone).apply(it) }
27                                         .sortedByDescending { it.time }
28                                         .let { posts ->
29                                                 Pagination(posts, webInterface.core.preferences.postsPerPage).apply {
30                                                         page = freenetRequest.parameters["page"]?.toIntOrNull() ?: 0
31                                                 }.let { pagination ->
32                                                         templateContext["pagination"] = pagination
33                                                         templateContext["posts"] = pagination.items
34                                                 }
35                                         }
36         }
37
38 }