Merge branch 'release-0.9.7'
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / NewPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.utils.Pagination
4 import net.pterodactylus.sone.utils.mapPresent
5 import net.pterodactylus.sone.utils.parameters
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10
11 /**
12  * Page that displays all new posts and replies. The posts are filtered using
13  * [PostVisibilityFilter.isPostVisible(Sone, Post)] and sorted by time.
14  */
15 class NewPage(template: Template, webInterface: WebInterface):
16                 SoneTemplatePage("new.html", template, "Page.New.Title", webInterface, false) {
17
18         override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) =
19                         getCurrentSone(freenetRequest.toadletContext).let { currentSone ->
20                                 (webInterface.getNewPosts(currentSone) + webInterface.getNewReplies(currentSone).mapPresent { it.post })
21                                                 .distinct()
22                                                 .sortedByDescending { it.time }
23                                                 .let { posts ->
24                                                         Pagination(posts, webInterface.core.preferences.postsPerPage).apply {
25                                                                 page = freenetRequest.parameters["page"]?.toIntOrNull() ?: 0
26                                                         }.let { pagination ->
27                                                                 templateContext["pagination"] = pagination
28                                                                 templateContext["posts"] = pagination.items
29                                                         }
30                                                 }
31                         }
32
33 }