From 74cbe983cd38da24847e6247ac595cb70d8e115b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 29 Nov 2017 20:57:22 +0100 Subject: [PATCH] Add page that always requires a logged-in user --- .../pterodactylus/sone/web/pages/CreateAlbumPage.kt | 6 +++--- .../pterodactylus/sone/web/pages/CreatePostPage.kt | 7 ++++--- .../pterodactylus/sone/web/pages/CreateReplyPage.kt | 7 ++++--- .../pterodactylus/sone/web/pages/DeleteAlbumPage.kt | 5 +++-- .../pterodactylus/sone/web/pages/DeleteImagePage.kt | 5 +++-- .../pterodactylus/sone/web/pages/DeletePostPage.kt | 5 +++-- .../sone/web/pages/DeleteProfileFieldPage.kt | 6 +++--- .../pterodactylus/sone/web/pages/DeleteReplyPage.kt | 5 +++-- .../pterodactylus/sone/web/pages/DeleteSonePage.kt | 7 ++++--- .../pterodactylus/sone/web/pages/DistrustPage.kt | 7 ++++--- .../pterodactylus/sone/web/pages/EditAlbumPage.kt | 5 +++-- .../pterodactylus/sone/web/pages/EditImagePage.kt | 5 +++-- .../sone/web/pages/EditProfileFieldPage.kt | 7 +++---- .../pterodactylus/sone/web/pages/EditProfilePage.kt | 17 ++++++++--------- .../pterodactylus/sone/web/pages/FollowSonePage.kt | 9 ++++----- .../sone/web/pages/ImageBrowserPage.kt | 6 +++--- .../net/pterodactylus/sone/web/pages/IndexPage.kt | 7 +++---- .../net/pterodactylus/sone/web/pages/LikePage.kt | 9 ++++----- .../pterodactylus/sone/web/pages/LoggedInPage.kt | 21 +++++++++++++++++++++ .../net/pterodactylus/sone/web/pages/LogoutPage.kt | 5 +++-- .../net/pterodactylus/sone/web/pages/RescuePage.kt | 7 ++++--- .../net/pterodactylus/sone/web/pages/TrustPage.kt | 7 +++---- .../sone/web/pages/UnfollowSonePage.kt | 7 +++---- .../net/pterodactylus/sone/web/pages/UnlikePage.kt | 9 +++++---- .../net/pterodactylus/sone/web/pages/UntrustPage.kt | 7 +++---- .../pterodactylus/sone/web/pages/UploadImagePage.kt | 9 +++++---- 26 files changed, 112 insertions(+), 85 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/pages/LoggedInPage.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateAlbumPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateAlbumPage.kt index ee3936e..b0eea08 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateAlbumPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateAlbumPage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.text.TextFilter import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface @@ -12,9 +13,9 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user create a new album. */ class CreateAlbumPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("createAlbum.html", template, "Page.CreateAlbum.Title", webInterface, true) { + LoggedInPage("createAlbum.html", template, "Page.CreateAlbum.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val name = freenetRequest.httpRequest.getPartAsStringFailsafe("name", 64).trim() if (name.isEmpty()) { @@ -22,7 +23,6 @@ class CreateAlbumPage(template: Template, webInterface: WebInterface): return } val description = freenetRequest.httpRequest.getPartAsStringFailsafe("description", 256).trim() - val currentSone = webInterface.getCurrentSoneCreatingSession(freenetRequest.toadletContext) val parentId = freenetRequest.httpRequest.getPartAsStringFailsafe("parent", 36) val parent = if (parentId == "") currentSone.rootAlbum else webInterface.core.getAlbum(parentId) val album = webInterface.core.createAlbum(currentSone, parent) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt index 6d0c634..31896f7 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.text.TextFilter import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.utils.isPOST @@ -12,9 +13,9 @@ import net.pterodactylus.util.template.TemplateContext * This page lets the user create a new [Post]. */ class CreatePostPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("createPost.html", template, "Page.CreatePost.Title", webInterface, true) { + LoggedInPage("createPost.html", template, "Page.CreatePost.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { val returnPage = freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256) templateContext["returnPage"] = returnPage if (freenetRequest.isPOST) { @@ -23,7 +24,7 @@ class CreatePostPage(template: Template, webInterface: WebInterface): templateContext["errorTextEmpty"] = true return } - val sender = webInterface.core.getLocalSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: getCurrentSone(freenetRequest.toadletContext) + val sender = webInterface.core.getLocalSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone val recipient = webInterface.core.getSone(freenetRequest.httpRequest.getPartAsStringFailsafe("recipient", 43)) webInterface.core.createPost(sender, recipient.asOptional(), TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), text)) throw RedirectException(returnPage) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateReplyPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateReplyPage.kt index a3fc1f3..bc6faa4 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateReplyPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreateReplyPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.text.TextFilter import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface @@ -11,9 +12,9 @@ import net.pterodactylus.util.template.TemplateContext * This page lets the user post a reply to a post. */ class CreateReplyPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("createReply.html", template, "Page.CreateReply.Title", webInterface, true) { + LoggedInPage("createReply.html", template, "Page.CreateReply.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { val postId = freenetRequest.httpRequest.getPartAsStringFailsafe("post", 36).apply { templateContext["postId"] = this } val text = freenetRequest.httpRequest.getPartAsStringFailsafe("text", 65536).trim().apply { templateContext["text"] = this } val returnPage = freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256).apply { templateContext["returnPage"] = this } @@ -23,7 +24,7 @@ class CreateReplyPage(template: Template, webInterface: WebInterface): return } val post = webInterface.core.getPost(postId) ?: throw RedirectException("noPermission.html") - val sender = webInterface.core.getLocalSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: getCurrentSone(freenetRequest.toadletContext) + val sender = webInterface.core.getLocalSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone webInterface.core.createReply(sender, post, TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), text)) throw RedirectException(returnPage) } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPage.kt index 98eea86..eff79b9 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -10,9 +11,9 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user delete an {@link Album}. */ class DeleteAlbumPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("deleteAlbum.html", template, "Page.DeleteAlbum.Title", webInterface, true) { + LoggedInPage("deleteAlbum.html", template, "Page.DeleteAlbum.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val album = webInterface.core.getAlbum(freenetRequest.httpRequest.getPartAsStringFailsafe("album", 36)) ?: throw RedirectException("invalid.html") if (!album.sone.isLocal) { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePage.kt index 61db304..fb77707 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -10,9 +11,9 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user delete an {@link Image}. */ class DeleteImagePage(template: Template, webInterface: WebInterface): - SoneTemplatePage("deleteImage.html", template, "Page.DeleteImage.Title", webInterface, true) { + LoggedInPage("deleteImage.html", template, "Page.DeleteImage.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val image = webInterface.core.getImage(freenetRequest.httpRequest.getPartAsStringFailsafe("image", 36)) ?: throw RedirectException("invalid.html") if (!image.sone.isLocal) { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeletePostPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeletePostPage.kt index 181144e..8eccfb4 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeletePostPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeletePostPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -10,9 +11,9 @@ import net.pterodactylus.util.template.TemplateContext * Lets the user delete a post they made. */ class DeletePostPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("deletePost.html", template, "Page.DeletePost.Title", webInterface, true) { + LoggedInPage("deletePost.html", template, "Page.DeletePost.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val post = webInterface.core.getPost(freenetRequest.httpRequest.getPartAsStringFailsafe("post", 36)) ?: throw RedirectException("noPermission.html") val returnPage = freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt index cf5ac6e..f25299f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -10,10 +11,9 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user confirm the deletion of a profile field. */ class DeleteProfileFieldPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("deleteProfileField.html", template, "Page.DeleteProfileField.Title", webInterface, true) { + LoggedInPage("deleteProfileField.html", template, "Page.DeleteProfileField.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { - val currentSone = getCurrentSone(freenetRequest.toadletContext)!! + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val field = currentSone.profile.getFieldById(freenetRequest.httpRequest.getPartAsStringFailsafe("field", 36)) ?: throw RedirectException("invalid.html") if (freenetRequest.httpRequest.getPartAsStringFailsafe("confirm", 4) == "true") { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteReplyPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteReplyPage.kt index ddaaf5e..29d8c81 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteReplyPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteReplyPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -10,9 +11,9 @@ import net.pterodactylus.util.template.TemplateContext * This page lets the user delete a reply. */ class DeleteReplyPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("deleteReply.html", template, "Page.DeleteReply.Title", webInterface, true) { + LoggedInPage("deleteReply.html", template, "Page.DeleteReply.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val replyId = freenetRequest.httpRequest.getPartAsStringFailsafe("reply", 36) val reply = webInterface.core.getPostReply(replyId) ?: throw RedirectException("noPermission.html") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePage.kt index 0cce5ba..b2a6d7b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteSonePage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -12,12 +13,12 @@ import net.pterodactylus.util.template.TemplateContext * installation. */ class DeleteSonePage(template: Template, webInterface: WebInterface): - SoneTemplatePage("deleteSone.html", template, "Page.DeleteSone.Title", webInterface, true) { + LoggedInPage("deleteSone.html", template, "Page.DeleteSone.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { if (freenetRequest.httpRequest.isPartSet("deleteSone")) { - webInterface.core.deleteSone(getCurrentSone(freenetRequest.toadletContext)) + webInterface.core.deleteSone(currentSone) } throw RedirectException("index.html") } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DistrustPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DistrustPage.kt index 82f6da1..70755f5 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DistrustPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DistrustPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -13,12 +14,12 @@ import net.pterodactylus.util.template.TemplateContext * @see net.pterodactylus.sone.core.Core#distrustSone(Sone, Sone) */ class DistrustPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("distrust.html", template, "Page.Distrust.Title", webInterface, true) { + LoggedInPage("distrust.html", template, "Page.Distrust.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { webInterface.core.getSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sone", 44)) - ?.run { webInterface.core.distrustSone(getCurrentSone(freenetRequest.toadletContext), this) } + ?.run { webInterface.core.distrustSone(currentSone, this) } throw RedirectException(freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPage.kt index bd612c3..26d7c3a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -11,9 +12,9 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user edit the name and description of an album. */ class EditAlbumPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("editAlbum.html", template, "Page.EditAlbum.Title", webInterface, true) { + LoggedInPage("editAlbum.html", template, "Page.EditAlbum.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val album = webInterface.core.getAlbum(freenetRequest.httpRequest.getPartAsStringFailsafe("album", 36)) ?: throw RedirectException("invalid.html") album.takeUnless { it.sone.isLocal }?.run { throw RedirectException("noPermission.html") } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditImagePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditImagePage.kt index b674cb9..350dec7 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditImagePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditImagePage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.Image.Modifier.ImageTitleMustNotBeEmpty +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.text.TextFilter import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface @@ -12,9 +13,9 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user edit title and description of an {@link Image}. */ class EditImagePage(template: Template, webInterface: WebInterface): - SoneTemplatePage("editImage.html", template, "Page.EditImage.Title", webInterface, true) { + LoggedInPage("editImage.html", template, "Page.EditImage.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val image = webInterface.core.getImage(freenetRequest.httpRequest.getPartAsStringFailsafe("image", 36)) ?: throw RedirectException("invalid.html") if (!image.sone.isLocal) { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPage.kt index 5e3d3c6..b63301b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfileFieldPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -10,10 +11,9 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user edit the name of a profile field. */ class EditProfileFieldPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("editProfileField.html", template, "Page.EditProfileField.Title", webInterface, true) { + LoggedInPage("editProfileField.html", template, "Page.EditProfileField.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { - sessionProvider.getCurrentSone(freenetRequest.toadletContext)!!.let { currentSone -> + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { currentSone.profile.let { profile -> if (freenetRequest.isPOST) { if (freenetRequest.httpRequest.getPartAsStringFailsafe("cancel", 4) == "true") { @@ -35,7 +35,6 @@ class EditProfileFieldPage(template: Template, webInterface: WebInterface): } templateContext["field"] = profile.getFieldById(freenetRequest.httpRequest.getParam("field")) ?: throw RedirectException("invalid.html") } - } } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfilePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfilePage.kt index 5ed0c2f..1166208 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfilePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/EditProfilePage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.Profile.DuplicateField +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.text.TextFilter import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface @@ -11,11 +12,11 @@ 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) { +class EditProfilePage(template: Template, webInterface: WebInterface) : + LoggedInPage("editProfile.html", template, "Page.EditProfile.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { - freenetRequest.currentSone!!.profile.let { profile -> + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { + currentSone.profile.let { profile -> templateContext["firstName"] = profile.firstName templateContext["middleName"] = profile.middleName templateContext["lastName"] = profile.lastName @@ -42,7 +43,7 @@ class EditProfilePage(template: Template, webInterface: WebInterface): val fieldName = freenetRequest.httpRequest.getPartAsStringFailsafe("field-name", 100) try { profile.addField(fieldName) - freenetRequest.currentSone!!.profile = profile + currentSone.profile = profile webInterface.core.touchConfiguration() throw RedirectException("editProfile.html#profile-fields") } catch (e: DuplicateField) { @@ -56,11 +57,11 @@ class EditProfilePage(template: Template, webInterface: WebInterface): throw RedirectException("editProfileField.html?field=${field.id}") } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("move-down-field-${field.id}", 4) == "true") { profile.moveFieldDown(field) - freenetRequest.currentSone!!.profile = profile + currentSone.profile = profile throw RedirectException("editProfile.html#profile-fields") } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("move-up-field-${field.id}", 4) == "true") { profile.moveFieldUp(field) - freenetRequest.currentSone!!.profile = profile + currentSone.profile = profile throw RedirectException("editProfile.html#profile-fields") } } @@ -68,6 +69,4 @@ class EditProfilePage(template: Template, webInterface: WebInterface): } } - private val FreenetRequest.currentSone get() = sessionProvider.getCurrentSone(toadletContext) - } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/FollowSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/FollowSonePage.kt index 5069d16..cf5bcee 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/FollowSonePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/FollowSonePage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -10,21 +11,19 @@ import net.pterodactylus.util.template.TemplateContext * This page lets the user follow another Sone. */ class FollowSonePage(template: Template, webInterface: WebInterface): - SoneTemplatePage("followSone.html", template, "Page.FollowSone.Title", webInterface, true) { + LoggedInPage("followSone.html", template, "Page.FollowSone.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { freenetRequest.httpRequest.getPartAsStringFailsafe("sone", 1200).split(Regex("[ ,]+")) .map { it to webInterface.core.getSone(it) } .filterNot { it.second == null } .forEach { sone -> - webInterface.core.followSone(freenetRequest.currentSone, sone.first) + webInterface.core.followSone(currentSone, sone.first) webInterface.core.markSoneKnown(sone.second) } throw RedirectException(freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)) } } - private val FreenetRequest.currentSone get() = sessionProvider.getCurrentSone(toadletContext) - } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt index 60a2bd5..e9911e6 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt @@ -14,9 +14,9 @@ import java.net.URI * The image browser page is the entry page for the image management. */ class ImageBrowserPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface, true) { + LoggedInPage("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if ("album" in freenetRequest.parameters) { templateContext["albumRequested"] = true templateContext["album"] = webInterface.core.getAlbum(freenetRequest.parameters["album"]!!) @@ -40,7 +40,7 @@ class ImageBrowserPage(template: Template, webInterface: WebInterface): } } else { templateContext["soneRequested"] = true - templateContext["sone"] = webInterface.core.getSone(freenetRequest.httpRequest.getParam("sone")) ?: getCurrentSone(freenetRequest.toadletContext) + templateContext["sone"] = webInterface.core.getSone(freenetRequest.httpRequest.getParam("sone")) ?: currentSone } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt index e5685ef..0257cb1 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.notify.PostVisibilityFilter import net.pterodactylus.sone.utils.Pagination import net.pterodactylus.sone.utils.parameters @@ -13,10 +14,9 @@ import net.pterodactylus.util.template.TemplateContext * of all friends of the current user. */ class IndexPage(template: Template, webInterface: WebInterface, private val postVisibilityFilter: PostVisibilityFilter): - SoneTemplatePage("index.html", template, "Page.Index.Title", webInterface, true) { + LoggedInPage("index.html", template, "Page.Index.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { - getCurrentSone(freenetRequest.toadletContext)!!.let { currentSone -> + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { (currentSone.posts + currentSone.friends .mapNotNull(webInterface.core::getSone) @@ -33,7 +33,6 @@ class IndexPage(template: Template, webInterface: WebInterface, private val post templateContext["posts"] = pagination.items } } - } } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/LikePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/LikePage.kt index 43753a3..7d6af18 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/LikePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/LikePage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -10,12 +11,11 @@ import net.pterodactylus.util.template.TemplateContext /** * Page that lets the user like [net.pterodactylus.sone.data.Post]s and [net.pterodactylus.sone.data.Reply]s. */ -class LikePage(template: Template, webInterface: WebInterface) - : SoneTemplatePage("like.html", template, "Page.Like.Title", webInterface, true) { +class LikePage(template: Template, webInterface: WebInterface) : + LoggedInPage("like.html", template, "Page.Like.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { - getCurrentSone(freenetRequest.toadletContext)!!.let { currentSone -> freenetRequest.parameters["type", 16]?.also { type -> when(type) { "post" -> currentSone.addLikedPostId(freenetRequest.parameters["post", 36]!!) @@ -23,7 +23,6 @@ class LikePage(template: Template, webInterface: WebInterface) } } throw RedirectException(freenetRequest.parameters["returnPage", 256]!!) - } } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoggedInPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoggedInPage.kt new file mode 100644 index 0000000..c88499e --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoggedInPage.kt @@ -0,0 +1,21 @@ +package net.pterodactylus.sone.web.pages + +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 + +/** + * Base class for [SoneTemplatePage] implementations that require a logged in user. + */ +abstract class LoggedInPage(path: String, template: Template, pageTitleKey: String, webInterface: WebInterface) : + SoneTemplatePage(path, template, pageTitleKey, webInterface, true) { + + final override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + handleRequest(freenetRequest, getCurrentSone(freenetRequest.toadletContext, false)!!, templateContext) + } + + protected abstract fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) + +} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/LogoutPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/LogoutPage.kt index 3a2a023..7e608ff 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/LogoutPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/LogoutPage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.pages import freenet.clients.http.ToadletContext +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 @@ -10,9 +11,9 @@ import net.pterodactylus.util.template.TemplateContext * Logs a user out. */ class LogoutPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("logout.html", template, "Page.Logout.Title", webInterface, true) { + LoggedInPage("logout.html", template, "Page.Logout.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { setCurrentSone(freenetRequest.toadletContext, null) throw RedirectException("index.html") } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.kt index dbcf59f..c3fabdb 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -11,10 +12,10 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user control the rescue mode for a Sone. */ class RescuePage(template: Template, webInterface: WebInterface): - SoneTemplatePage("rescue.html", template, "Page.Rescue.Title", webInterface, true) { + LoggedInPage("rescue.html", template, "Page.Rescue.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { - val soneRescuer = webInterface.core.getSoneRescuer(getCurrentSone(freenetRequest.toadletContext)!!) + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { + val soneRescuer = webInterface.core.getSoneRescuer(currentSone) templateContext["soneRescuer"] = soneRescuer if (freenetRequest.isPOST) { freenetRequest.parameters["edition", 9]?.toIntOrNull()?.also { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/TrustPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/TrustPage.kt index db53ac3..b6fb038 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/TrustPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/TrustPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -12,15 +13,13 @@ import net.pterodactylus.util.template.TemplateContext * amount of trust to an identity. */ class TrustPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("trust.html", template, "Page.Trust.Title", webInterface, true) { + LoggedInPage("trust.html", template, "Page.Trust.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { - getCurrentSone(freenetRequest.toadletContext)?.also { currentSone -> webInterface.core.getSone(freenetRequest.parameters["sone"]!!)?.let { sone -> webInterface.core.trustSone(currentSone, sone) } - } throw RedirectException(freenetRequest.parameters["returnPage", 256]) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/UnfollowSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/UnfollowSonePage.kt index cc411e2..ef4eab9 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/UnfollowSonePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/UnfollowSonePage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -11,14 +12,12 @@ import net.pterodactylus.util.template.TemplateContext * This page lets the user unfollow another Sone. */ class UnfollowSonePage(template: Template, webInterface: WebInterface): - SoneTemplatePage("unfollowSone.html", template, "Page.UnfollowSone.Title", webInterface, true) { + LoggedInPage("unfollowSone.html", template, "Page.UnfollowSone.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { - getCurrentSone(freenetRequest.toadletContext)!!.also { currentSone -> freenetRequest.parameters["sone"]!!.split(Regex("[ ,]+")) .forEach { webInterface.core.unfollowSone(currentSone, it) } - } throw RedirectException(freenetRequest.parameters["returnPage", 256]) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/UnlikePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/UnlikePage.kt index 42e552e..6bfabe2 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/UnlikePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/UnlikePage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -11,13 +12,13 @@ import net.pterodactylus.util.template.TemplateContext * Page that lets the user unlike a [net.pterodactylus.sone.data.Post] or [net.pterodactylus.sone.data.Reply]. */ class UnlikePage(template: Template, webInterface: WebInterface): - SoneTemplatePage("unlike.html", template, "Page.Unlike.Title", webInterface, true) { + LoggedInPage("unlike.html", template, "Page.Unlike.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { when (freenetRequest.parameters["type"]) { - "post" -> getCurrentSone(freenetRequest.toadletContext)!!.removeLikedPostId(freenetRequest.parameters["post"]!!) - "reply" -> getCurrentSone(freenetRequest.toadletContext)!!.removeLikedReplyId(freenetRequest.parameters["reply"]!!) + "post" -> currentSone.removeLikedPostId(freenetRequest.parameters["post"]!!) + "reply" -> currentSone.removeLikedReplyId(freenetRequest.parameters["reply"]!!) } throw RedirectException(freenetRequest.parameters["returnPage", 256]) } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/UntrustPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/UntrustPage.kt index 77fcdd5..ad34432 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/UntrustPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/UntrustPage.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.pages +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -12,15 +13,13 @@ import net.pterodactylus.util.template.TemplateContext * assignments for an identity. */ class UntrustPage(template: Template, webInterface: WebInterface): - SoneTemplatePage("untrust.html", template, "Page.Untrust.Title", webInterface, true) { + LoggedInPage("untrust.html", template, "Page.Untrust.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { - getCurrentSone(freenetRequest.toadletContext)!!.also { currentSone -> freenetRequest.parameters["sone", 44]!! .let(webInterface.core::getSone) ?.also { webInterface.core.untrustSone(currentSone, it) } - } throw RedirectException(freenetRequest.parameters["returnPage", 256]) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/UploadImagePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/UploadImagePage.kt index 782c90d..4aa35ac 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/UploadImagePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/UploadImagePage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.pages import freenet.support.api.Bucket +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.text.TextFilter import net.pterodactylus.sone.utils.emptyToNull import net.pterodactylus.sone.utils.headers @@ -20,12 +21,12 @@ import javax.imageio.ImageIO * Page implementation that lets the user upload an image. */ class UploadImagePage(template: Template, webInterface: WebInterface): - SoneTemplatePage("uploadImage.html", template, "Page.UploadImage.Title", webInterface, true) { + LoggedInPage("uploadImage.html", template, "Page.UploadImage.Title", webInterface) { - override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) { if (freenetRequest.isPOST) { val parentAlbum = freenetRequest.parameters["parent"]!!.let(webInterface.core::getAlbum) ?: throw RedirectException("noPermission.html") - if (parentAlbum.sone != getCurrentSone(freenetRequest.toadletContext)) { + if (parentAlbum.sone != currentSone) { throw RedirectException("noPermission.html") } val title = freenetRequest.parameters["title", 200].emptyToNull ?: throw RedirectException("emptyImageTitle.html") @@ -39,7 +40,7 @@ class UploadImagePage(template: Template, webInterface: WebInterface): } val temporaryImage = webInterface.core.createTemporaryImage(bytes.mimeType, bytes) - webInterface.core.createImage(getCurrentSone(freenetRequest.toadletContext), parentAlbum, temporaryImage).modify().apply { + webInterface.core.createImage(currentSone, parentAlbum, temporaryImage).modify().apply { setWidth(bufferedImage.width) setHeight(bufferedImage.height) setTitle(title) -- 2.7.4