From 9e697ac643d11a2b7644a948732674eea195718a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 9 Sep 2017 12:12:31 +0200 Subject: [PATCH] Add base JSON page that requires a logged-in user --- .../sone/web/ajax/FollowSoneAjaxPage.java | 11 ++++---- .../pterodactylus/sone/web/ajax/LikeAjaxPage.java | 11 ++++---- .../sone/web/ajax/MoveProfileFieldAjaxPage.java | 8 +++--- .../pterodactylus/sone/web/ajax/TrustAjaxPage.java | 11 ++++---- .../sone/web/ajax/UnfollowSoneAjaxPage.java | 11 ++++---- .../sone/web/ajax/UnlikeAjaxPage.java | 11 ++++---- .../sone/web/ajax/UntrustAjaxPage.java | 11 ++++---- .../sone/web/ajax/CreatePostAjaxPage.kt | 30 ++++++++++------------ .../sone/web/ajax/CreateReplyAjaxPage.kt | 7 ++--- .../sone/web/ajax/DeletePostAjaxPage.kt | 5 ++-- .../sone/web/ajax/DeleteProfileFieldAjaxPage.kt | 27 ++++++++++--------- .../sone/web/ajax/DeleteReplyAjaxPage.kt | 5 ++-- .../sone/web/ajax/DistrustAjaxPage.kt | 7 ++--- .../sone/web/ajax/EditProfileFieldAjaxPage.kt | 9 ++++--- .../sone/web/ajax/LoggedInJsonPage.kt | 20 +++++++++++++++ .../sone/web/ajax/CreatePostAjaxPageTest.kt | 7 ----- 16 files changed, 101 insertions(+), 90 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/ajax/LoggedInJsonPage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java index 6a2d8ce..1ef8773 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import javax.annotation.Nonnull; + import com.google.common.base.Optional; import net.pterodactylus.sone.data.Sone; @@ -28,7 +30,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest; * * @author David ‘Bombe’ Roden */ -public class FollowSoneAjaxPage extends JsonPage { +public class FollowSoneAjaxPage extends LoggedInJsonPage { /** * Creates a new “follow Sone” AJAX page. @@ -43,17 +45,14 @@ public class FollowSoneAjaxPage extends JsonPage { /** * {@inheritDoc} */ + @Nonnull @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { + protected JsonReturnObject createJsonObject(@Nonnull Sone currentSone, @Nonnull FreenetRequest request) { String soneId = request.getHttpRequest().getParam("sone"); Optional sone = webInterface.getCore().getSone(soneId); if (!sone.isPresent()) { return createErrorJsonObject("invalid-sone-id"); } - Sone currentSone = getCurrentSone(request.getToadletContext()); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } webInterface.getCore().followSone(currentSone, soneId); webInterface.getCore().markSoneKnown(sone.get()); return createSuccessJsonObject(); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java index f088acb..666b1bf 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import javax.annotation.Nonnull; + import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; @@ -27,7 +29,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest; * * @author David ‘Bombe’ Roden */ -public class LikeAjaxPage extends JsonPage { +public class LikeAjaxPage extends LoggedInJsonPage { /** * Creates a new “like post” AJAX page. @@ -42,17 +44,14 @@ public class LikeAjaxPage extends JsonPage { /** * {@inheritDoc} */ + @Nonnull @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { + protected JsonReturnObject createJsonObject(@Nonnull Sone currentSone, @Nonnull FreenetRequest request) { String type = request.getHttpRequest().getParam("type", null); String id = request.getHttpRequest().getParam(type, null); if ((id == null) || (id.length() == 0)) { return createErrorJsonObject("invalid-" + type + "-id"); } - Sone currentSone = getCurrentSone(request.getToadletContext()); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } if ("post".equals(type)) { currentSone.addLikedPostId(id); webInterface.getCore().touchConfiguration(); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java index e8377f9..d4685e9 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import javax.annotation.Nonnull; + import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Sone; @@ -30,7 +32,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest; * @see Profile#moveFieldDown(Field) * @author David ‘Bombe’ Roden */ -public class MoveProfileFieldAjaxPage extends JsonPage { +public class MoveProfileFieldAjaxPage extends LoggedInJsonPage { /** * Creates a new “move profile field” AJAX page. @@ -49,9 +51,9 @@ public class MoveProfileFieldAjaxPage extends JsonPage { /** * {@inheritDoc} */ + @Nonnull @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext()); + protected JsonReturnObject createJsonObject(@Nonnull Sone currentSone, @Nonnull FreenetRequest request) { Profile profile = currentSone.getProfile(); String fieldId = request.getHttpRequest().getParam("field"); Field field = profile.getFieldById(fieldId); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java index a186d46..d0247cb 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import javax.annotation.Nonnull; + import com.google.common.base.Optional; import net.pterodactylus.sone.core.Core; @@ -30,7 +32,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest; * @see Core#trustSone(Sone, Sone) * @author David ‘Bombe’ Roden */ -public class TrustAjaxPage extends JsonPage { +public class TrustAjaxPage extends LoggedInJsonPage { /** * Creates a new “trust Sone” AJAX handler. @@ -45,12 +47,9 @@ public class TrustAjaxPage extends JsonPage { /** * {@inheritDoc} */ + @Nonnull @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext(), false); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } + protected JsonReturnObject createJsonObject(@Nonnull Sone currentSone, @Nonnull FreenetRequest request) { String soneId = request.getHttpRequest().getParam("sone"); Optional sone = webInterface.getCore().getSone(soneId); if (!sone.isPresent()) { diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java index 58af936..28ffbf2 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import javax.annotation.Nonnull; + import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -26,7 +28,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest; * * @author David ‘Bombe’ Roden */ -public class UnfollowSoneAjaxPage extends JsonPage { +public class UnfollowSoneAjaxPage extends LoggedInJsonPage { /** * Creates a new “unfollow Sone” AJAX page. @@ -41,16 +43,13 @@ public class UnfollowSoneAjaxPage extends JsonPage { /** * {@inheritDoc} */ + @Nonnull @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { + protected JsonReturnObject createJsonObject(@Nonnull Sone currentSone, @Nonnull FreenetRequest request) { String soneId = request.getHttpRequest().getParam("sone"); if (!webInterface.getCore().getSone(soneId).isPresent()) { return createErrorJsonObject("invalid-sone-id"); } - Sone currentSone = getCurrentSone(request.getToadletContext()); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } webInterface.getCore().unfollowSone(currentSone, soneId); return createSuccessJsonObject(); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java index 2eaaa68..a527b94 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import javax.annotation.Nonnull; + import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; @@ -27,7 +29,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest; * * @author David ‘Bombe’ Roden */ -public class UnlikeAjaxPage extends JsonPage { +public class UnlikeAjaxPage extends LoggedInJsonPage { /** * Creates a new “unlike post” AJAX page. @@ -42,17 +44,14 @@ public class UnlikeAjaxPage extends JsonPage { /** * {@inheritDoc} */ + @Nonnull @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { + protected JsonReturnObject createJsonObject(@Nonnull Sone currentSone, @Nonnull FreenetRequest request) { String type = request.getHttpRequest().getParam("type", null); String id = request.getHttpRequest().getParam(type, null); if ((id == null) || (id.length() == 0)) { return createErrorJsonObject("invalid-" + type + "-id"); } - Sone currentSone = getCurrentSone(request.getToadletContext()); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } if ("post".equals(type)) { currentSone.removeLikedPostId(id); webInterface.getCore().touchConfiguration(); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java index 644dfd6..5ba6693 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import javax.annotation.Nonnull; + import com.google.common.base.Optional; import net.pterodactylus.sone.core.Core; @@ -30,7 +32,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest; * @see Core#untrustSone(Sone, Sone) * @author David ‘Bombe’ Roden */ -public class UntrustAjaxPage extends JsonPage { +public class UntrustAjaxPage extends LoggedInJsonPage { /** * Creates a new “untrust Sone” AJAX handler. @@ -45,12 +47,9 @@ public class UntrustAjaxPage extends JsonPage { /** * {@inheritDoc} */ + @Nonnull @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext(), false); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } + protected JsonReturnObject createJsonObject(@Nonnull Sone currentSone, @Nonnull FreenetRequest request) { String soneId = request.getHttpRequest().getParam("sone"); Optional sone = webInterface.getCore().getSone(soneId); if (!sone.isPresent()) { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt index c9bcf48..9e96e69 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt @@ -12,23 +12,21 @@ import net.pterodactylus.sone.web.page.FreenetRequest /** * AJAX handler that creates a new post. */ -class CreatePostAjaxPage(webInterface: WebInterface) : JsonPage("createPost.ajax", webInterface) { +class CreatePostAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("createPost.ajax", webInterface) { - override fun createJsonObject(request: FreenetRequest) = - getCurrentSone(request.toadletContext)?.let { sone -> - request.parameters["text"].emptyToNull - ?.let { TextFilter.filter(request.headers["Host"], it) } - ?.let { text -> - val sender = request.parameters["sender"].emptyToNull?.let(webInterface.core::getSone)?.orNull() ?: sone - val recipient = request.parameters["recipient"].let(webInterface.core::getSone) - webInterface.core.createPost(sender, recipient, text).let { post -> - createSuccessJsonObject().apply { - put("postId", post.id) - put("sone", sender.id) - put("recipient", recipient.let(Sone::getId)) - } + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = + request.parameters["text"].emptyToNull + ?.let { TextFilter.filter(request.headers["Host"], it) } + ?.let { text -> + val sender = request.parameters["sender"].emptyToNull?.let(webInterface.core::getSone)?.orNull() ?: currentSone + val recipient = request.parameters["recipient"].let(webInterface.core::getSone) + webInterface.core.createPost(sender, recipient, text).let { post -> + createSuccessJsonObject().apply { + put("postId", post.id) + put("sone", sender.id) + put("recipient", recipient.let(Sone::getId)) } - } ?: createErrorJsonObject("text-required") - } ?: createErrorJsonObject("auth-required") + } + } ?: createErrorJsonObject("text-required") } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.kt index 7cf2e1e..c8f9317 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.ajax +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.text.TextFilter import net.pterodactylus.sone.utils.emptyToNull import net.pterodactylus.sone.utils.headers @@ -11,14 +12,14 @@ import net.pterodactylus.sone.web.page.FreenetRequest /** * This AJAX page create a reply. */ -class CreateReplyAjaxPage(webInterface: WebInterface) : JsonPage("createReply.ajax", webInterface) { +class CreateReplyAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("createReply.ajax", webInterface) { - override fun createJsonObject(request: FreenetRequest): JsonReturnObject = + override fun createJsonObject(currentSone: Sone, request: FreenetRequest): JsonReturnObject = request.parameters["post"].emptyToNull ?.let(webInterface.core::getPost) ?.let { post -> val text = TextFilter.filter(request.headers["Host"], request.parameters["text"]) - val sender = request.parameters["sender"].let(webInterface.core::getLocalSone) ?: getCurrentSone(request.toadletContext) + val sender = request.parameters["sender"].let(webInterface.core::getLocalSone) ?: currentSone val reply = webInterface.core.createReply(sender, post, text) createSuccessJsonObject().apply { put("reply", reply.id) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.kt index 4d82063..74fd796 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.ajax +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.ifTrue import net.pterodactylus.sone.utils.let import net.pterodactylus.sone.utils.parameters @@ -9,9 +10,9 @@ import net.pterodactylus.sone.web.page.FreenetRequest /** * This AJAX page deletes a post. */ -class DeletePostAjaxPage(webInterface: WebInterface) : JsonPage("deletePost.ajax", webInterface) { +class DeletePostAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("deletePost.ajax", webInterface) { - override fun createJsonObject(request: FreenetRequest) = + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = request.parameters["post"] .let(webInterface.core::getPost) ?.let { post -> diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.kt index 5e7c901..9595a8c 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.ajax +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -7,21 +8,19 @@ import net.pterodactylus.sone.web.page.FreenetRequest /** * AJAX page that lets the user delete a profile field. */ -class DeleteProfileFieldAjaxPage(webInterface: WebInterface) : JsonPage("deleteProfileField.ajax", webInterface) { +class DeleteProfileFieldAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("deleteProfileField.ajax", webInterface) { - override fun createJsonObject(request: FreenetRequest) = - getCurrentSone(request.toadletContext)!!.let { currentSone -> - currentSone.profile.let { profile -> - request.parameters["field"] - ?.let(profile::getFieldById) - ?.let { field -> - createSuccessJsonObject().also { - profile.removeField(field) - currentSone.profile = profile - webInterface.core.touchConfiguration() - } - } ?: createErrorJsonObject("invalid-field-id") - } + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = + currentSone.profile.let { profile -> + request.parameters["field"] + ?.let(profile::getFieldById) + ?.let { field -> + createSuccessJsonObject().also { + profile.removeField(field) + currentSone.profile = profile + webInterface.core.touchConfiguration() + } + } ?: createErrorJsonObject("invalid-field-id") } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.kt index 762bc8e..010e02b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.ajax +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.ifTrue import net.pterodactylus.sone.utils.let import net.pterodactylus.sone.utils.parameters @@ -9,9 +10,9 @@ import net.pterodactylus.sone.web.page.FreenetRequest /** * This AJAX page deletes a reply. */ -class DeleteReplyAjaxPage(webInterface: WebInterface) : JsonPage("deleteReply.ajax", webInterface) { +class DeleteReplyAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("deleteReply.ajax", webInterface) { - override fun createJsonObject(request: FreenetRequest) = + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = request.parameters["reply"] .let(webInterface.core::getPostReply) ?.let { reply -> diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.kt index 0c56341..1c3506b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.ajax import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.let import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -11,16 +12,16 @@ import net.pterodactylus.sone.web.page.FreenetRequest * * @see Core.distrustSone(Sone, Sone) */ -class DistrustAjaxPage(webInterface: WebInterface) : JsonPage("distrustSone.ajax", webInterface) { +class DistrustAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("distrustSone.ajax", webInterface) { - override fun createJsonObject(request: FreenetRequest) = + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = request.parameters["sone"] .let(webInterface.core::getSone) ?.let { sone -> createSuccessJsonObject() .put("trustValue", webInterface.core.preferences.negativeTrust) .also { - webInterface.core.distrustSone(getCurrentSone(request.toadletContext), sone) + webInterface.core.distrustSone(currentSone, sone) } } ?: createErrorJsonObject("invalid-sone-id") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.kt index 72ee005..e48c92d 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.ajax +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.ifFalse import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -8,10 +9,10 @@ import net.pterodactylus.sone.web.page.FreenetRequest /** * AJAX page that lets the user rename a profile field. */ -class EditProfileFieldAjaxPage(webInterface: WebInterface) : JsonPage("editProfileField.ajax", webInterface) { +class EditProfileFieldAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("editProfileField.ajax", webInterface) { - override fun createJsonObject(request: FreenetRequest) = - getCurrentSone(request.toadletContext).profile.let { profile -> + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = + currentSone.profile.let { profile -> request.parameters["field"]!! .let(profile::getFieldById) ?.let { field -> @@ -20,7 +21,7 @@ class EditProfileFieldAjaxPage(webInterface: WebInterface) : JsonPage("editProfi try { field.name = newName createSuccessJsonObject().also { - getCurrentSone(request.toadletContext).profile = profile + currentSone.profile = profile } } catch (_: IllegalArgumentException) { createErrorJsonObject("duplicate-field-name") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/LoggedInJsonPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/LoggedInJsonPage.kt new file mode 100644 index 0000000..acf1d73 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/LoggedInJsonPage.kt @@ -0,0 +1,20 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest + +/** + * Base JSON page for all pages that require the user to be logged in. + */ +open class LoggedInJsonPage(path: String, webInterface: WebInterface) : JsonPage(path, webInterface) { + + final override fun requiresLogin() = true + + final override fun createJsonObject(request: FreenetRequest) = + createJsonObject(getCurrentSone(request.toadletContext)!!, request) + + open protected fun createJsonObject(currentSone: Sone, request: FreenetRequest) = + createErrorJsonObject("not-implemented") + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt index b153093..47fee5b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt @@ -17,13 +17,6 @@ import org.junit.Test class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = ::CreatePostAjaxPage) { @Test - fun `page requires a current sone`() { - unsetCurrentSone() - assertThat(json.isSuccess, equalTo(false)) - assertThat(json.error, equalTo("auth-required")) - } - - @Test fun `missing text parameter returns error`() { assertThat(json.isSuccess, equalTo(false)) assertThat(json.error, equalTo("text-required")) -- 2.7.4