🎨 Use new elements container in AJAX 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.Loaders
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.NewElements
8 import net.pterodactylus.sone.web.WebInterface
9 import net.pterodactylus.sone.web.page.MenuName
10 import net.pterodactylus.sone.web.page.SoneRequest
11 import net.pterodactylus.sone.web.page.TemplatePath
12 import net.pterodactylus.sone.web.page.TemplateRenderer
13 import net.pterodactylus.sone.web.page.ToadletPath
14 import net.pterodactylus.util.template.TemplateContext
15 import javax.inject.Inject
16
17 /**
18  * Page that displays all new posts and replies. The posts are filtered using
19  * [PostVisibilityFilter.isPostVisible(Sone, Post)] and sorted by time.
20  */
21 @MenuName("New")
22 @TemplatePath("/templates/new.html")
23 @ToadletPath("new.html")
24 class NewPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer, private val newElements: NewElements) :
25                 SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = "Page.New.Title") {
26
27         override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) =
28                         getCurrentSone(soneRequest.toadletContext).let { currentSone ->
29                                 (newElements.newPosts + newElements.newReplies.mapPresent { it.post })
30                                                 .distinct()
31                                                 .sortedByDescending { it.time }
32                                                 .let { posts ->
33                                                         posts.paginate(soneRequest.core.preferences.postsPerPage)
34                                                                         .turnTo(soneRequest.parameters["page"]?.toIntOrNull() ?: 0)
35                                                                         .let { pagination ->
36                                                                                 templateContext["pagination"] = pagination
37                                                                                 templateContext["posts"] = pagination.items
38                                                                         }
39                                                 }
40                         }
41
42 }