808d2fafd0b87728c8dc9b80c299d2a3bb2420b6
[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.*
6 import javax.inject.Inject
7
8 /**
9  * AJAX page that lets the user mark a number of [net.pterodactylus.sone.data.Sone]s,
10  * [net.pterodactylus.sone.data.Post]s, or [net.pterodactylus.sone.data.Reply]s as known.
11  */
12 @ToadletPath("markAsKnown.ajax")
13 class MarkAsKnownAjaxPage @Inject constructor(webInterface: WebInterface) : JsonPage(webInterface) {
14
15         override val requiresLogin = false
16
17         override fun createJsonObject(request: FreenetRequest) = when (request.parameters["type"]) {
18                 "sone" -> processIds(request, core::getSone, core::markSoneKnown)
19                 "post" -> processIds(request, core::getPost, core::markPostKnown)
20                 "reply" -> processIds(request, core::getPostReply, core::markReplyKnown)
21                 else -> createErrorJsonObject("invalid-type")
22         }
23
24         private fun <T : Any> processIds(request: FreenetRequest, getter: (String) -> T?, marker: (T) -> Unit) =
25                         request.parameters["id"]
26                                         ?.split(Regex(" +"))
27                                         ?.mapNotNull(getter)
28                                         ?.onEach(marker)
29                                         .let { createSuccessJsonObject() }
30
31 }