✨ Use @TemplatePath annotations on most pages
[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 @TemplatePath("/templates/new.html")
19 class NewPage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer):
20                 SoneTemplatePage("new.html", webInterface, loaders, template, templateRenderer, pageTitleKey = "Page.New.Title") {
21
22         override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) =
23                         getCurrentSone(soneRequest.toadletContext).let { currentSone ->
24                                 (soneRequest.webInterface.getNewPosts(currentSone) + soneRequest.webInterface.getNewReplies(currentSone).mapPresent { it.post })
25                                                 .distinct()
26                                                 .sortedByDescending { it.time }
27                                                 .let { posts ->
28                                                         posts.paginate(soneRequest.core.preferences.postsPerPage)
29                                                                         .turnTo(soneRequest.parameters["page"]?.toIntOrNull() ?: 0)
30                                                                         .let { pagination ->
31                                                                                 templateContext["pagination"] = pagination
32                                                                                 templateContext["posts"] = pagination.items
33                                                                         }
34                                                 }
35                         }
36
37 }