From: David ‘Bombe’ Roden Date: Sun, 9 Apr 2017 08:24:00 +0000 (+0200) Subject: Replace edit profile page with Kotlin version X-Git-Tag: 0.9.7^2~251 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=cc0e665862e8cd6646ce439d5d1f1d44efc17d84 Replace edit profile page with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/web/pages/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/pages/EditProfilePage.java deleted file mode 100644 index b17a22e..0000000 --- a/src/main/java/net/pterodactylus/sone/web/pages/EditProfilePage.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Sone - EditProfilePage.java - Copyright © 2010–2016 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web.pages; - -import static net.pterodactylus.sone.text.TextFilter.filter; -import static net.pterodactylus.sone.utils.NumberParsers.parseInt; - -import java.util.List; - -import net.pterodactylus.sone.data.Profile; -import net.pterodactylus.sone.data.Profile.DuplicateField; -import net.pterodactylus.sone.data.Profile.Field; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; -import net.pterodactylus.util.web.Method; -import freenet.clients.http.ToadletContext; - -/** - * This page lets the user edit her profile. - * - * @author David ‘Bombe’ Roden - */ -public class EditProfilePage extends SoneTemplatePage { - - /** - * Creates a new “edit profile” page. - * - * @param template - * The template to render - * @param webInterface - * The Sone web interface - */ - public EditProfilePage(Template template, WebInterface webInterface) { - super("editProfile.html", template, "Page.EditProfile.Title", webInterface, true); - } - - // - // TEMPLATEPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { - ToadletContext toadletContenxt = request.getToadletContext(); - Sone currentSone = getCurrentSone(toadletContenxt); - Profile profile = currentSone.getProfile(); - String firstName = profile.getFirstName(); - String middleName = profile.getMiddleName(); - String lastName = profile.getLastName(); - Integer birthDay = profile.getBirthDay(); - Integer birthMonth = profile.getBirthMonth(); - Integer birthYear = profile.getBirthYear(); - String avatarId = profile.getAvatar(); - List fields = profile.getFields(); - if (request.getMethod() == Method.POST) { - if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) { - firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim(); - middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim(); - lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim(); - birthDay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim(), null); - birthMonth = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim(), null); - birthYear = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim(), null); - avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36); - profile.setFirstName(firstName.length() > 0 ? firstName : null); - profile.setMiddleName(middleName.length() > 0 ? middleName : null); - profile.setLastName(lastName.length() > 0 ? lastName : null); - profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear); - profile.setAvatar(webInterface.getCore().getImage(avatarId, false)); - for (Field field : fields) { - String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400); - String filteredValue = filter(request.getHttpRequest().getHeader("Host"), value); - field.setValue(filteredValue); - } - currentSone.setProfile(profile); - webInterface.getCore().touchConfiguration(); - throw new RedirectException("editProfile.html"); - } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) { - String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim(); - try { - profile.addField(fieldName); - currentSone.setProfile(profile); - webInterface.getCore().touchConfiguration(); - throw new RedirectException("editProfile.html#profile-fields"); - } catch (DuplicateField df1) { - templateContext.set("fieldName", fieldName); - templateContext.set("duplicateFieldName", true); - } - } else { - String id = getFieldId(request, "delete-field-"); - if (id != null) { - throw new RedirectException("deleteProfileField.html?field=" + id); - } - id = getFieldId(request, "move-up-field-"); - if (id != null) { - Field field = profile.getFieldById(id); - if (field == null) { - throw new RedirectException("invalid.html"); - } - profile.moveFieldUp(field); - currentSone.setProfile(profile); - throw new RedirectException("editProfile.html#profile-fields"); - } - id = getFieldId(request, "move-down-field-"); - if (id != null) { - Field field = profile.getFieldById(id); - if (field == null) { - throw new RedirectException("invalid.html"); - } - profile.moveFieldDown(field); - currentSone.setProfile(profile); - throw new RedirectException("editProfile.html#profile-fields"); - } - id = getFieldId(request, "edit-field-"); - if (id != null) { - throw new RedirectException("editProfileField.html?field=" + id); - } - } - } - templateContext.set("firstName", firstName); - templateContext.set("middleName", middleName); - templateContext.set("lastName", lastName); - templateContext.set("birthDay", birthDay); - templateContext.set("birthMonth", birthMonth); - templateContext.set("birthYear", birthYear); - templateContext.set("avatarId", avatarId); - templateContext.set("fields", fields); - } - - // - // PRIVATE METHODS - // - - /** - * Searches for a part whose names starts with the given {@code String} and - * extracts the ID from the located name. - * - * @param request - * The request to get the parts from - * @param partNameStart - * The start of the name of the requested part - * @return The parsed ID, or {@code null} if there was no part matching the - * given string - */ - private static String getFieldId(FreenetRequest request, String partNameStart) { - for (String partName : request.getHttpRequest().getParts()) { - if (partName.startsWith(partNameStart)) { - return partName.substring(partNameStart.length()); - } - } - return null; - } -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfilePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfilePage.kt new file mode 100644 index 0000000..df47d9a --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfilePage.kt @@ -0,0 +1,73 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.data.Profile.DuplicateField +import net.pterodactylus.sone.text.TextFilter +import net.pterodactylus.sone.utils.isPOST +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContext + +/** + * This page lets the user edit her profile. + */ +class EditProfilePage(template: Template, webInterface: WebInterface): + SoneTemplatePage("editProfile.html", template, "Page.EditProfile.Title", webInterface, true) { + + override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) { + request.currentSone!!.profile.let { profile -> + templateContext["firstName"] = profile.firstName + templateContext["middleName"] = profile.middleName + templateContext["lastName"] = profile.lastName + templateContext["birthDay"] = profile.birthDay + templateContext["birthMonth"] = profile.birthMonth + templateContext["birthYear"] = profile.birthYear + templateContext["avatarId"] = profile.avatar + templateContext["fields"] = profile.fields + if (request.isPOST) { + if (request.httpRequest.getPartAsStringFailsafe("save-profile", 4) == "true") { + profile.firstName = request.httpRequest.getPartAsStringFailsafe("first-name", 256).trim() + profile.middleName = request.httpRequest.getPartAsStringFailsafe("middle-name", 256).trim() + profile.lastName = request.httpRequest.getPartAsStringFailsafe("last-name", 256).trim() + profile.birthDay = request.httpRequest.getPartAsStringFailsafe("birth-day", 256).trim().toIntOrNull() + profile.birthMonth = request.httpRequest.getPartAsStringFailsafe("birth-month", 256).trim().toIntOrNull() + profile.birthYear = request.httpRequest.getPartAsStringFailsafe("birth-year", 256).trim().toIntOrNull() + profile.setAvatar(webInterface.core.getImage(request.httpRequest.getPartAsStringFailsafe("avatarId", 256).trim(), false)) + profile.fields.forEach { field -> + field.value = TextFilter.filter(request.httpRequest.getHeader("Host"), request.httpRequest.getPartAsStringFailsafe("field-${field.id}", 400).trim()) + } + webInterface.core.touchConfiguration() + throw RedirectException("editProfile.html") + } else if (request.httpRequest.getPartAsStringFailsafe("add-field", 4) == "true") { + val fieldName = request.httpRequest.getPartAsStringFailsafe("field-name", 100) + try { + profile.addField(fieldName) + request.currentSone!!.profile = profile + webInterface.core.touchConfiguration() + throw RedirectException("editProfile.html#profile-fields") + } catch (e: DuplicateField) { + templateContext["fieldName"] = fieldName + templateContext["duplicateFieldName"] = true + } + } else profile.fields.forEach { field -> + if (request.httpRequest.getPartAsStringFailsafe("delete-field-${field.id}", 4) == "true") { + throw RedirectException("deleteProfileField.html?field=${field.id}") + } else if (request.httpRequest.getPartAsStringFailsafe("edit-field-${field.id}", 4) == "true") { + throw RedirectException("editProfileField.html?field=${field.id}") + } else if (request.httpRequest.getPartAsStringFailsafe("move-down-field-${field.id}", 4) == "true") { + profile.moveFieldDown(field) + request.currentSone!!.profile = profile + throw RedirectException("editProfile.html#profile-fields") + } else if (request.httpRequest.getPartAsStringFailsafe("move-up-field-${field.id}", 4) == "true") { + profile.moveFieldUp(field) + request.currentSone!!.profile = profile + throw RedirectException("editProfile.html#profile-fields") + } + } + } + } + } + + private val FreenetRequest.currentSone get() = sessionProvider.getCurrentSone(toadletContext) + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt index 46a73cc..282dbc2 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt @@ -64,7 +64,7 @@ class EditProfilePageTest : WebPageTest() { @Test fun `get request stores fields of current sone’s profile in template context`() { request("", GET) - page.handleRequest(freenetRequest, templateContext) + page.processTemplate(freenetRequest, templateContext) assertThat(templateContext["firstName"], equalTo("First")) assertThat(templateContext["middleName"], equalTo("Middle")) assertThat(templateContext["lastName"], equalTo("Last")) @@ -78,7 +78,7 @@ class EditProfilePageTest : WebPageTest() { @Test fun `post request without any command stores fields of current sone’s profile in template context`() { request("", POST) - page.handleRequest(freenetRequest, templateContext) + page.processTemplate(freenetRequest, templateContext) assertThat(templateContext["firstName"], equalTo("First")) assertThat(templateContext["middleName"], equalTo("Middle")) assertThat(templateContext["lastName"], equalTo("Last")) @@ -159,7 +159,7 @@ class EditProfilePageTest : WebPageTest() { profile.addField("new-field") addHttpRequestParameter("add-field", "true") addHttpRequestParameter("field-name", "new-field") - page.handleRequest(freenetRequest, templateContext) + page.processTemplate(freenetRequest, templateContext) assertThat(templateContext["fieldName"], equalTo("new-field")) assertThat(templateContext["duplicateFieldName"], equalTo(true)) verify(core, never()).touchConfiguration() @@ -195,10 +195,10 @@ class EditProfilePageTest : WebPageTest() { } @Test - fun `moving an invalid field up redirects to the invalid page`() { + fun `moving an invalid field up does not redirect`() { request("", POST) addHttpRequestParameter("move-up-field-foo", "true") - verifyRedirect("invalid.html") + page.processTemplate(freenetRequest, templateContext) } @Test @@ -212,10 +212,10 @@ class EditProfilePageTest : WebPageTest() { } @Test - fun `moving an invalid field down redirects to the invalid page`() { + fun `moving an invalid field down does not redirect`() { request("", POST) addHttpRequestParameter("move-down-field-foo", "true") - verifyRedirect("invalid.html") + page.processTemplate(freenetRequest, templateContext) } @Test