✨ Use new template renderer
[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.main.*
4 import net.pterodactylus.sone.utils.mapPresent
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  * Page that displays all new posts and replies. The posts are filtered using
15  * [PostVisibilityFilter.isPostVisible(Sone, Post)] and sorted by time.
16  */
17 @MenuName("New")
18 class NewPage @Inject constructor(  template: Template, webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer):
19                 SoneTemplatePage("new.html", webInterface, loaders, template, templateRenderer, pageTitleKey = "Page.New.Title") {
20
21         override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) =
22                         getCurrentSone(soneRequest.toadletContext).let { currentSone ->
23                                 (soneRequest.webInterface.getNewPosts(currentSone) + soneRequest.webInterface.getNewReplies(currentSone).mapPresent { it.post })
24                                                 .distinct()
25                                                 .sortedByDescending { it.time }
26                                                 .let { posts ->
27                                                         posts.paginate(soneRequest.core.preferences.postsPerPage)
28                                                                         .turnTo(soneRequest.parameters["page"]?.toIntOrNull() ?: 0)
29                                                                         .let { pagination ->
30                                                                                 templateContext["pagination"] = pagination
31                                                                                 templateContext["posts"] = pagination.items
32                                                                         }
33                                                 }
34                         }
35
36 }