🚧 Use ticker from Sone module for notifications
[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.*
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.*
9 import javax.inject.*
10
11 /**
12  * This page shows all known Sones.
13  */
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") {
19
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 }
29                                         .sortedWith(
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
35                                                         }.let { comparator ->
36                                                                 when (soneRequest.parameters["order"]) {
37                                                                         "asc" -> comparator.reversed()
38                                                                         else -> comparator
39                                                                 }
40                                                         }
41                                         ).paginate(25)
42                                         .turnTo(soneRequest.parameters["page"]?.toIntOrNull() ?: 0)
43                                         .let { pagination ->
44                                                 templateContext["pagination"] = pagination
45                                                 templateContext["knownSones"] = pagination.items
46                                         }
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"]
50                 }
51         }
52
53 }