Add test for DI constructability of CreateAlbumPage
[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.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
10
11 /**
12  * This page shows all known Sones.
13  */
14 class KnownSonesPage(template: Template, webInterface: WebInterface):
15                 SoneTemplatePage("knownSones.html", webInterface, template, "Page.KnownSones.Title") {
16
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 }
26                                         .sortedWith(
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
32                                                         }.let { comparator ->
33                                                                 when (freenetRequest.parameters["order"]) {
34                                                                         "asc" -> comparator.reversed()
35                                                                         else -> comparator
36                                                                 }
37                                                         }
38                                         ).paginate(25)
39                                         .turnTo(freenetRequest.parameters["page"]?.toIntOrNull() ?: 0)
40                                         .let { pagination ->
41                                                 templateContext["pagination"] = pagination
42                                                 templateContext["knownSones"] = pagination.items
43                                         }
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"]
47                 }
48         }
49
50 }