Replace distrust ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / ViewSonePage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.template.SoneAccessor
6 import net.pterodactylus.sone.utils.let
7 import net.pterodactylus.sone.utils.mapPresent
8 import net.pterodactylus.sone.utils.paginate
9 import net.pterodactylus.sone.utils.parameters
10 import net.pterodactylus.sone.web.WebInterface
11 import net.pterodactylus.sone.web.page.FreenetRequest
12 import net.pterodactylus.util.template.Template
13 import net.pterodactylus.util.template.TemplateContext
14 import java.net.URI
15
16 /**
17  * Lets the user browser another Sone.
18  */
19 class ViewSonePage(template: Template, webInterface: WebInterface):
20                 SoneTemplatePage("viewSone.html", template, webInterface, false) {
21
22         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
23                 templateContext["soneId"] = request.parameters["sone"]
24                 request.parameters["sone"].let(webInterface.core::getSone).let { sone ->
25                         templateContext["sone"] = sone
26                         val sonePosts = sone.posts
27                         val directedPosts = webInterface.core.getDirectedPosts(sone.id)
28                         (sonePosts + directedPosts)
29                                         .sortedByDescending(Post::getTime)
30                                         .paginate(webInterface.core.preferences.postsPerPage)
31                                         .apply { page = request.parameters["postPage"]?.toIntOrNull() ?: 0 }
32                                         .also {
33                                                 templateContext["postPagination"] = it
34                                                 templateContext["posts"] = it.items
35                                         }
36                         sone.replies
37                                         .mapPresent(PostReply::getPost)
38                                         .distinct()
39                                         .minus(sonePosts)
40                                         .minus(directedPosts)
41                                         .sortedByDescending { webInterface.core.getReplies(it.id).first().time }
42                                         .paginate(webInterface.core.preferences.postsPerPage)
43                                         .apply { page = request.parameters["repliedPostPage"]?.toIntOrNull() ?: 0 }
44                                         .also {
45                                                 templateContext["repliedPostPagination"] = it
46                                                 templateContext["repliedPosts"] = it.items
47                                         }
48                 }
49         }
50
51         override fun isLinkExcepted(link: URI?) = true
52
53         public override fun getPageTitle(request: FreenetRequest): String =
54                         request.parameters["sone"].let(webInterface.core::getSone).let { sone ->
55                                 "${SoneAccessor.getNiceName(sone)} - ${webInterface.l10n.getString("Page.ViewSone.Title")}"
56                         } ?: webInterface.l10n.getString("Page.ViewSone.Page.TitleWithoutSone")
57
58 }