Replace Sone change detector with simple comparison helper
[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.FreenetRequest
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 class IndexPage @Inject constructor(template: Template, webInterface: WebInterface, private val postVisibilityFilter: PostVisibilityFilter) :
18                 LoggedInPage("index.html", template, "Page.Index.Title", webInterface) {
19
20         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
21                 (currentSone.posts +
22                                 currentSone.friends
23                                                 .mapNotNull(webInterface.core::getSone)
24                                                 .flatMap { it.posts } +
25                                 webInterface.core.getDirectedPosts(currentSone.id)
26                                 ).distinct()
27                                 .filter { postVisibilityFilter.isVisible(currentSone).apply(it) }
28                                 .sortedByDescending { it.time }
29                                 .let { posts ->
30                                         posts.paginate(webInterface.core.preferences.postsPerPage)
31                                                         .turnTo(freenetRequest.parameters["page"]?.toIntOrNull() ?: 0)
32                                                         .let { pagination ->
33                                                                 templateContext["pagination"] = pagination
34                                                                 templateContext["posts"] = pagination.items
35                                                         }
36                                 }
37         }
38
39 }