🔥 Remove unused session manager from page toadlet
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / GetLikesAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.template.SoneAccessor
5 import net.pterodactylus.sone.utils.jsonArray
6 import net.pterodactylus.sone.utils.jsonObject
7 import net.pterodactylus.sone.utils.parameters
8 import net.pterodactylus.sone.web.WebInterface
9 import net.pterodactylus.sone.web.page.*
10 import javax.inject.Inject
11
12 /**
13  * AJAX page that retrieves the number of â€ślikes” a [net.pterodactylus.sone.data.Post]
14  * or [net.pterodactylus.sone.data.PostReply] has.
15  */
16 @ToadletPath("getLikes.ajax")
17 class GetLikesAjaxPage @Inject constructor(webInterface: WebInterface) : JsonPage(webInterface) {
18
19         override val needsFormPassword = false
20
21         override fun createJsonObject(request: FreenetRequest) =
22                         when (request.parameters["type"]) {
23                                 "post" -> request.parameters["post"]
24                                                 ?.let(core::getPost)
25                                                 ?.let(core::getLikes)
26                                                 ?.toReply()
27                                                 ?: createErrorJsonObject("invalid-post-id")
28                                 "reply" -> request.parameters["reply"]
29                                                 ?.let(core::getPostReply)
30                                                 ?.let(core::getLikes)
31                                                 ?.toReply()
32                                                 ?: createErrorJsonObject("invalid-reply-id")
33                                 else -> createErrorJsonObject("invalid-type")
34                         }
35
36         private fun Set<Sone>.toReply() = createSuccessJsonObject().apply {
37                 put("likes", size)
38                 put("sones", sortedBy { SoneAccessor.getNiceName(it) }
39                                 .map {
40                                         jsonObject("id" to it.id, "name" to SoneAccessor.getNiceName(it))
41                                 }
42                                 .let { jsonArray(*it.toTypedArray()) }
43                 )
44         }
45
46 }