✨ Add metrics for Sone insert durations
[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.*
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.template.*
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.template.*
10 import java.net.*
11 import javax.inject.*
12
13 /**
14  * Lets the user browser another Sone.
15  */
16 @TemplatePath("/templates/viewSone.html")
17 @ToadletPath("viewSone.html")
18 class ViewSonePage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
19                 SoneTemplatePage(webInterface, loaders, templateRenderer) {
20
21         override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
22                 templateContext["soneId"] = soneRequest.parameters["sone"]
23                 soneRequest.parameters["sone"]!!.let(soneRequest.core::getSone)?.let { sone ->
24                         templateContext["sone"] = sone
25                         val sonePosts = sone.posts
26                         val directedPosts = soneRequest.core.getDirectedPosts(sone.id)
27                         (sonePosts + directedPosts)
28                                         .sortedByDescending(Post::getTime)
29                                         .paginate(soneRequest.core.preferences.postsPerPage)
30                                         .apply { page = soneRequest.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 { soneRequest.core.getReplies(it.id).first().time }
41                                         .paginate(soneRequest.core.preferences.postsPerPage)
42                                         .apply { page = soneRequest.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         override fun getPageTitle(soneRequest: SoneRequest): String =
53                         soneRequest.parameters["sone"]!!.let(soneRequest.core::getSone)?.let { sone ->
54                                 "${SoneAccessor.getNiceName(sone)} - ${soneRequest.l10n.getString("Page.ViewSone.Title")}"
55                         } ?: soneRequest.l10n.getString("Page.ViewSone.Page.TitleWithoutSone")
56
57 }