Add test for DI constructability of LockSoneAjaxPage
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / MarkAsKnownAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.utils.parameters
4 import net.pterodactylus.sone.web.WebInterface
5 import net.pterodactylus.sone.web.page.FreenetRequest
6
7 /**
8  * AJAX page that lets the user mark a number of [net.pterodactylus.sone.data.Sone]s,
9  * [net.pterodactylus.sone.data.Post]s, or [net.pterodactylus.sone.data.Reply]s as known.
10  */
11 class MarkAsKnownAjaxPage(webInterface: WebInterface) : JsonPage("markAsKnown.ajax", webInterface) {
12
13         override val requiresLogin = false
14
15         override fun createJsonObject(request: FreenetRequest) = when (request.parameters["type"]) {
16                 "sone" -> processIds(request, core::getSone, core::markSoneKnown)
17                 "post" -> processIds(request, core::getPost, core::markPostKnown)
18                 "reply" -> processIds(request, core::getPostReply, core::markReplyKnown)
19                 else -> createErrorJsonObject("invalid-type")
20         }
21
22         private fun <T : Any> processIds(request: FreenetRequest, getter: (String) -> T?, marker: (T) -> Unit) =
23                         request.parameters["id"]
24                                         ?.split(Regex(" +"))
25                                         ?.mapNotNull(getter)
26                                         ?.onEach(marker)
27                                         .let { createSuccessJsonObject() }
28
29 }