1 package net.pterodactylus.sone.web.ajax
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
13 * AJAX page that retrieves the number of “likes” a [net.pterodactylus.sone.data.Post]
14 * or [net.pterodactylus.sone.data.PostReply] has.
16 @ToadletPath("getLikes.ajax")
17 class GetLikesAjaxPage @Inject constructor(webInterface: WebInterface) : JsonPage(webInterface) {
19 override val needsFormPassword = false
21 override fun createJsonObject(request: FreenetRequest) =
22 when (request.parameters["type"]) {
23 "post" -> request.parameters["post"]
27 ?: createErrorJsonObject("invalid-post-id")
28 "reply" -> request.parameters["reply"]
29 ?.let(core::getPostReply)
32 ?: createErrorJsonObject("invalid-reply-id")
33 else -> createErrorJsonObject("invalid-type")
36 private fun Set<Sone>.toReply() = createSuccessJsonObject().apply {
38 put("sones", sortedBy { SoneAccessor.getNiceName(it) }
40 jsonObject("id" to it.id, "name" to SoneAccessor.getNiceName(it))
42 .let { jsonArray(*it.toTypedArray()) }