Add test for DI constructability of ReloadingPage
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / EditProfilePage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Profile.DuplicateField
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.text.TextFilter
6 import net.pterodactylus.sone.utils.isPOST
7 import net.pterodactylus.sone.web.WebInterface
8 import net.pterodactylus.sone.web.page.FreenetRequest
9 import net.pterodactylus.util.template.Template
10 import net.pterodactylus.util.template.TemplateContext
11 import javax.inject.Inject
12
13 /**
14  * This page lets the user edit her profile.
15  */
16 class EditProfilePage @Inject constructor(template: Template, webInterface: WebInterface) :
17                 LoggedInPage("editProfile.html", template, "Page.EditProfile.Title", webInterface) {
18
19         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
20                 currentSone.profile.let { profile ->
21                         templateContext["firstName"] = profile.firstName
22                         templateContext["middleName"] = profile.middleName
23                         templateContext["lastName"] = profile.lastName
24                         templateContext["birthDay"] = profile.birthDay
25                         templateContext["birthMonth"] = profile.birthMonth
26                         templateContext["birthYear"] = profile.birthYear
27                         templateContext["avatarId"] = profile.avatar
28                         templateContext["fields"] = profile.fields
29                         if (freenetRequest.isPOST) {
30                                 if (freenetRequest.httpRequest.getPartAsStringFailsafe("save-profile", 4) == "true") {
31                                         profile.firstName = freenetRequest.httpRequest.getPartAsStringFailsafe("first-name", 256).trim()
32                                         profile.middleName = freenetRequest.httpRequest.getPartAsStringFailsafe("middle-name", 256).trim()
33                                         profile.lastName = freenetRequest.httpRequest.getPartAsStringFailsafe("last-name", 256).trim()
34                                         profile.birthDay = freenetRequest.httpRequest.getPartAsStringFailsafe("birth-day", 256).trim().toIntOrNull()
35                                         profile.birthMonth = freenetRequest.httpRequest.getPartAsStringFailsafe("birth-month", 256).trim().toIntOrNull()
36                                         profile.birthYear = freenetRequest.httpRequest.getPartAsStringFailsafe("birth-year", 256).trim().toIntOrNull()
37                                         profile.setAvatar(webInterface.core.getImage(freenetRequest.httpRequest.getPartAsStringFailsafe("avatarId", 256).trim(), false))
38                                         profile.fields.forEach { field ->
39                                                 field.value = TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), freenetRequest.httpRequest.getPartAsStringFailsafe("field-${field.id}", 400).trim())
40                                         }
41                                         currentSone.profile = profile
42                                         webInterface.core.touchConfiguration()
43                                         throw RedirectException("editProfile.html")
44                                 } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("add-field", 4) == "true") {
45                                         val fieldName = freenetRequest.httpRequest.getPartAsStringFailsafe("field-name", 100)
46                                         try {
47                                                 profile.addField(fieldName)
48                                                 currentSone.profile = profile
49                                                 webInterface.core.touchConfiguration()
50                                                 throw RedirectException("editProfile.html#profile-fields")
51                                         } catch (e: DuplicateField) {
52                                                 templateContext["fieldName"] = fieldName
53                                                 templateContext["duplicateFieldName"] = true
54                                         }
55                                 } else profile.fields.forEach { field ->
56                                         if (freenetRequest.httpRequest.getPartAsStringFailsafe("delete-field-${field.id}", 4) == "true") {
57                                                 throw RedirectException("deleteProfileField.html?field=${field.id}")
58                                         } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("edit-field-${field.id}", 4) == "true") {
59                                                 throw RedirectException("editProfileField.html?field=${field.id}")
60                                         } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("move-down-field-${field.id}", 4) == "true") {
61                                                 profile.moveFieldDown(field)
62                                                 currentSone.profile = profile
63                                                 throw RedirectException("editProfile.html#profile-fields")
64                                         } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("move-up-field-${field.id}", 4) == "true") {
65                                                 profile.moveFieldUp(field)
66                                                 currentSone.profile = profile
67                                                 throw RedirectException("editProfile.html#profile-fields")
68                                         }
69                                 }
70                         }
71                 }
72         }
73
74 }