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