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