🎨 Replace PageToadletFactory with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / UnlikeAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.utils.emptyToNull
5 import net.pterodactylus.sone.utils.parameters
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import javax.inject.Inject
9
10 /**
11  * AJAX page that lets the user unlike a [net.pterodactylus.sone.data.Post].
12  */
13 class UnlikeAjaxPage @Inject constructor(webInterface: WebInterface) :
14                 LoggedInJsonPage("unlike.ajax", webInterface) {
15
16         override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = when (request.parameters["type"]) {
17                 "post" -> request.processEntity("post", currentSone::removeLikedPostId)
18                 "reply" -> request.processEntity("reply", currentSone::removeLikedReplyId)
19                 else -> createErrorJsonObject("invalid-type")
20         }
21
22         private fun FreenetRequest.processEntity(entity: String, likeRemover: (String) -> Unit) =
23                         parameters[entity].emptyToNull
24                                         ?.also(likeRemover)
25                                         ?.also { core.touchConfiguration() }
26                                         ?.let { createSuccessJsonObject() }
27                                         ?: createErrorJsonObject("invalid-$entity-id")
28
29 }