Replace get post ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / KnownSonesPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.utils.Pagination
5 import net.pterodactylus.sone.utils.parameters
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10
11 /**
12  * This page shows all known Sones.
13  */
14 class KnownSonesPage(template: Template, webInterface: WebInterface):
15                 SoneTemplatePage("knownSones.html", template, "Page.KnownSones.Title", webInterface, false) {
16
17         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
18                 getCurrentSone(request.toadletContext).let { currentSone ->
19                         Pagination(webInterface.core.sones
20                                         .filterNot { request.parameters["filter"] == "followed" && currentSone != null && !currentSone.hasFriend(it.id) }
21                                         .filterNot { request.parameters["filter"] == "not-followed" && currentSone != null && currentSone.hasFriend(it.id) }
22                                         .filterNot { request.parameters["filter"] == "new" && it.isKnown }
23                                         .filterNot { request.parameters["filter"] == "not-new" && !it.isKnown }
24                                         .filterNot { request.parameters["filter"] == "own" && !it.isLocal }
25                                         .filterNot { request.parameters["filter"] == "not-own" && it.isLocal }
26                                         .sortedWith(
27                                                         when (request.parameters["sort"]) {
28                                                                 "images" -> Sone.IMAGE_COUNT_COMPARATOR
29                                                                 "name" -> Sone.NICE_NAME_COMPARATOR.reversed()
30                                                                 "posts" -> Sone.POST_COUNT_COMPARATOR
31                                                                 else -> Sone.LAST_ACTIVITY_COMPARATOR
32                                                         }.let { comparator ->
33                                                                 when (request.parameters["order"]) {
34                                                                         "asc" -> comparator.reversed()
35                                                                         else -> comparator
36                                                                 }
37                                                         }
38                                         ), 25).apply { page = request.parameters["page"]?.toIntOrNull() ?: 0 }
39                                         .let { pagination ->
40                                                 templateContext["pagination"] = pagination
41                                                 templateContext["knownSones"] = pagination.items
42                                         }
43                         templateContext["sort"] = request.parameters["sort"].let { sort -> if (sort in listOf("images", "name", "posts")) sort else "activity" }
44                         templateContext["order"] = request.parameters["order"].let { order -> if (order == "asc") "asc" else "desc" }
45                         templateContext["filter"] = request.parameters["filter"]
46                 }
47         }
48
49 }