🚧 Add Loaders to all template-using pages
[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.main.*
5 import net.pterodactylus.sone.utils.isPOST
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10 import javax.inject.Inject
11
12 /**
13  * This page lets the user follow another Sone.
14  */
15 class FollowSonePage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders):
16                 LoggedInPage("followSone.html", template, "Page.FollowSone.Title", webInterface, loaders) {
17
18         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
19                 if (soneRequest.isPOST) {
20                         soneRequest.httpRequest.getPartAsStringFailsafe("sone", 1200).split(Regex("[ ,]+"))
21                                         .map { it to soneRequest.core.getSone(it) }
22                                         .filterNot { it.second == null }
23                                         .forEach { sone ->
24                                                 soneRequest.core.followSone(currentSone, sone.first)
25                                                 soneRequest.core.markSoneKnown(sone.second)
26                                         }
27                         throw RedirectException(soneRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256))
28                 }
29         }
30
31 }