1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.utils.paginate
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
12 * This page shows all known Sones.
14 class KnownSonesPage(template: Template, webInterface: WebInterface):
15 SoneTemplatePage("knownSones.html", webInterface, template, "Page.KnownSones.Title") {
17 override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
18 getCurrentSone(freenetRequest.toadletContext).let { currentSone ->
19 webInterface.core.sones
20 .filterNot { freenetRequest.parameters["filter"] == "followed" && currentSone != null && !currentSone.hasFriend(it.id) }
21 .filterNot { freenetRequest.parameters["filter"] == "not-followed" && currentSone != null && currentSone.hasFriend(it.id) }
22 .filterNot { freenetRequest.parameters["filter"] == "new" && it.isKnown }
23 .filterNot { freenetRequest.parameters["filter"] == "not-new" && !it.isKnown }
24 .filterNot { freenetRequest.parameters["filter"] == "own" && !it.isLocal }
25 .filterNot { freenetRequest.parameters["filter"] == "not-own" && it.isLocal }
27 when (freenetRequest.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
33 when (freenetRequest.parameters["order"]) {
34 "asc" -> comparator.reversed()
39 .turnTo(freenetRequest.parameters["page"]?.toIntOrNull() ?: 0)
41 templateContext["pagination"] = pagination
42 templateContext["knownSones"] = pagination.items
44 templateContext["sort"] = freenetRequest.parameters["sort"].let { sort -> if (sort in listOf("images", "name", "posts")) sort else "activity" }
45 templateContext["order"] = freenetRequest.parameters["order"].let { order -> if (order == "asc") "asc" else "desc" }
46 templateContext["filter"] = freenetRequest.parameters["filter"]