187bb0d00e023b0c84c06b7453164c93aad66207
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / FollowSonePage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.utils.isPOST
5 import net.pterodactylus.sone.web.WebInterface
6 import net.pterodactylus.sone.web.page.FreenetRequest
7 import net.pterodactylus.util.template.Template
8 import net.pterodactylus.util.template.TemplateContext
9 import javax.inject.Inject
10
11 /**
12  * This page lets the user follow another Sone.
13  */
14 class FollowSonePage @Inject constructor(template: Template, webInterface: WebInterface):
15                 LoggedInPage("followSone.html", template, "Page.FollowSone.Title", webInterface) {
16
17         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
18                 if (freenetRequest.isPOST) {
19                         freenetRequest.httpRequest.getPartAsStringFailsafe("sone", 1200).split(Regex("[ ,]+"))
20                                         .map { it to webInterface.core.getSone(it) }
21                                         .filterNot { it.second == null }
22                                         .forEach { sone ->
23                                                 webInterface.core.followSone(currentSone, sone.first)
24                                                 webInterface.core.markSoneKnown(sone.second)
25                                         }
26                         throw RedirectException(freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256))
27                 }
28         }
29
30 }