1 package net.pterodactylus.sone.web.ajax
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.*
8 import javax.inject.Inject
11 * AJAX page that lets the user unlike a [net.pterodactylus.sone.data.Post].
13 @ToadletPath("unlike.ajax")
14 class UnlikeAjaxPage @Inject constructor(webInterface: WebInterface) : LoggedInJsonPage(webInterface) {
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")
22 private fun FreenetRequest.processEntity(entity: String, likeRemover: (String) -> Unit) =
23 parameters[entity].emptyToNull
25 ?.also { core.touchConfiguration() }
26 ?.let { createSuccessJsonObject() }
27 ?: createErrorJsonObject("invalid-$entity-id")