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