1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.utils.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.template.*
12 * This page shows all known Sones.
14 @MenuName("KnownSones")
15 @TemplatePath("/templates/knownSones.html")
16 @ToadletPath("knownSones.html")
17 class KnownSonesPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
18 SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = "Page.KnownSones.Title") {
20 override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
21 getCurrentSone(soneRequest.toadletContext).let { currentSone ->
22 soneRequest.core.sones.asSequence()
23 .filterNot { soneRequest.parameters["filter"] == "followed" && currentSone != null && !currentSone.hasFriend(it.id) }
24 .filterNot { soneRequest.parameters["filter"] == "not-followed" && currentSone != null && currentSone.hasFriend(it.id) }
25 .filterNot { soneRequest.parameters["filter"] == "new" && it.isKnown }
26 .filterNot { soneRequest.parameters["filter"] == "not-new" && !it.isKnown }
27 .filterNot { soneRequest.parameters["filter"] == "own" && !it.isLocal }
28 .filterNot { soneRequest.parameters["filter"] == "not-own" && it.isLocal }
30 when (soneRequest.parameters["sort"]) {
31 "images" -> Sone.IMAGE_COUNT_COMPARATOR
32 "name" -> Sone.NICE_NAME_COMPARATOR.reversed()
33 "posts" -> Sone.POST_COUNT_COMPARATOR
34 else -> Sone.LAST_ACTIVITY_COMPARATOR
36 when (soneRequest.parameters["order"]) {
37 "asc" -> comparator.reversed()
42 .turnTo(soneRequest.parameters["page"]?.toIntOrNull() ?: 0)
44 templateContext["pagination"] = pagination
45 templateContext["knownSones"] = pagination.items
47 templateContext["sort"] = soneRequest.parameters["sort"].let { sort -> if (sort in listOf("images", "name", "posts")) sort else "activity" }
48 templateContext["order"] = soneRequest.parameters["order"].let { order -> if (order == "asc") "asc" else "desc" }
49 templateContext["filter"] = soneRequest.parameters["filter"]