52004ab761aa68838d36c447863307eb1a89db8b
[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.*
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.notify.*
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.template.*
10 import javax.inject.*
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 @MenuName("Index")
17 @TemplatePath("/templates/index.html")
18 @ToadletPath("index.html")
19 class IndexPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer, private val postVisibilityFilter: PostVisibilityFilter) :
20                 LoggedInPage("index.html", "Page.Index.Title", webInterface, loaders, templateRenderer) {
21
22         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
23                 (currentSone.posts +
24                                 currentSone.friends
25                                                 .mapNotNull(soneRequest.core::getSone)
26                                                 .flatMap { it.posts } +
27                                 soneRequest.core.getDirectedPosts(currentSone.id)
28                                 ).distinct()
29                                 .filter { postVisibilityFilter.isVisible(currentSone).apply(it) }
30                                 .sortedByDescending { it.time }
31                                 .let { posts ->
32                                         posts.paginate(soneRequest.core.preferences.postsPerPage)
33                                                         .turnTo(soneRequest.parameters["page"]?.toIntOrNull() ?: 0)
34                                                         .let { pagination ->
35                                                                 templateContext["pagination"] = pagination
36                                                                 templateContext["posts"] = pagination.items
37                                                         }
38                                 }
39         }
40
41 }