Add page that always requires a logged-in user
[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
10 /**
11  * This page lets the user follow another Sone.
12  */
13 class FollowSonePage(template: Template, webInterface: WebInterface):
14                 LoggedInPage("followSone.html", template, "Page.FollowSone.Title", webInterface) {
15
16         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
17                 if (freenetRequest.isPOST) {
18                         freenetRequest.httpRequest.getPartAsStringFailsafe("sone", 1200).split(Regex("[ ,]+"))
19                                         .map { it to webInterface.core.getSone(it) }
20                                         .filterNot { it.second == null }
21                                         .forEach { sone ->
22                                                 webInterface.core.followSone(currentSone, sone.first)
23                                                 webInterface.core.markSoneKnown(sone.second)
24                                         }
25                         throw RedirectException(freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256))
26                 }
27         }
28
29 }