1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.SoneOptions.*
4 import net.pterodactylus.sone.fcp.FcpInterface.*
5 import net.pterodactylus.sone.main.*
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.template.*
13 * This page lets the user edit the options of the Sone plugin.
16 @TemplatePath("/templates/options.html")
17 class OptionsPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
18 SoneTemplatePage("options.html", webInterface, loaders, templateRenderer, pageTitleKey = "Page.Options.Title") {
20 override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
21 if (soneRequest.isPOST) {
22 val fieldsWithErrors = mutableListOf<String>()
23 getCurrentSone(soneRequest.toadletContext)?.options?.let { options ->
24 val autoFollow = "auto-follow" in soneRequest.parameters
25 val loadLinkedImages = soneRequest.parameters["load-linked-images"].emptyToNull
26 val showCustomAvatars = soneRequest.parameters["show-custom-avatars"].emptyToNull
27 val enableSoneInsertNotification = "enable-sone-insert-notifications" in soneRequest.parameters
28 val showNewSoneNotification = "show-notification-new-sones" in soneRequest.parameters
29 val showNewPostNotification = "show-notification-new-posts" in soneRequest.parameters
30 val showNewReplyNotification = "show-notification-new-replies" in soneRequest.parameters
32 options.isAutoFollow = autoFollow
33 options.isSoneInsertNotificationEnabled = enableSoneInsertNotification
34 options.isShowNewSoneNotifications = showNewSoneNotification
35 options.isShowNewPostNotifications = showNewPostNotification
36 options.isShowNewReplyNotifications = showNewReplyNotification
37 loadLinkedImages?.also { if (cantSetOption { options.loadLinkedImages = LoadExternalContent.valueOf(loadLinkedImages) }) fieldsWithErrors += "load-linked-images" }
38 showCustomAvatars?.also { if (cantSetOption { options.showCustomAvatars = LoadExternalContent.valueOf(showCustomAvatars) }) fieldsWithErrors += "show-custom-avatars" }
40 val fullAccessRequired = "require-full-access" in soneRequest.parameters
41 val fcpInterfaceActive = "fcp-interface-active" in soneRequest.parameters
43 soneRequest.core.preferences.newRequireFullAccess = fullAccessRequired
44 soneRequest.core.preferences.newFcpInterfaceActive = fcpInterfaceActive
46 val postsPerPage = soneRequest.parameters["posts-per-page"]?.toIntOrNull()
47 val charactersPerPost = soneRequest.parameters["characters-per-post"]?.toIntOrNull()
48 val postCutOffLength = soneRequest.parameters["post-cut-off-length"]?.toIntOrNull()
49 val imagesPerPage = soneRequest.parameters["images-per-page"]?.toIntOrNull()
50 val insertionDelay = soneRequest.parameters["insertion-delay"]?.toIntOrNull()
51 val fcpFullAccessRequired = soneRequest.parameters["fcp-full-access-required"]?.toIntOrNull()
52 val negativeTrust = soneRequest.parameters["negative-trust"]?.toIntOrNull()
53 val positiveTrust = soneRequest.parameters["positive-trust"]?.toIntOrNull()
54 val trustComment = soneRequest.parameters["trust-comment"]?.emptyToNull
56 if (cantSetOption { soneRequest.core.preferences.newPostsPerPage = postsPerPage }) fieldsWithErrors += "posts-per-page"
57 if (cantSetOption { soneRequest.core.preferences.newCharactersPerPost = charactersPerPost }) fieldsWithErrors += "characters-per-post"
58 if (cantSetOption { soneRequest.core.preferences.newPostCutOffLength = postCutOffLength }) fieldsWithErrors += "post-cut-off-length"
59 if (cantSetOption { soneRequest.core.preferences.newImagesPerPage = imagesPerPage }) fieldsWithErrors += "images-per-page"
60 if (cantSetOption { soneRequest.core.preferences.newInsertionDelay = insertionDelay }) fieldsWithErrors += "insertion-delay"
61 fcpFullAccessRequired?.also { if (cantSetOption { soneRequest.core.preferences.newFcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequired] }) fieldsWithErrors += "fcp-full-access-required" }
62 if (cantSetOption { soneRequest.core.preferences.newNegativeTrust = negativeTrust }) fieldsWithErrors += "negative-trust"
63 if (cantSetOption { soneRequest.core.preferences.newPositiveTrust = positiveTrust }) fieldsWithErrors += "positive-trust"
64 if (cantSetOption { soneRequest.core.preferences.newTrustComment = trustComment }) fieldsWithErrors += "trust-comment"
66 if (fieldsWithErrors.isEmpty()) {
67 soneRequest.core.touchConfiguration()
68 throw RedirectException("options.html")
70 templateContext["fieldErrors"] = fieldsWithErrors
72 getCurrentSone(soneRequest.toadletContext)?.options?.let { options ->
73 templateContext["auto-follow"] = options.isAutoFollow
74 templateContext["show-notification-new-sones"] = options.isShowNewSoneNotifications
75 templateContext["show-notification-new-posts"] = options.isShowNewPostNotifications
76 templateContext["show-notification-new-replies"] = options.isShowNewReplyNotifications
77 templateContext["enable-sone-insert-notifications"] = options.isSoneInsertNotificationEnabled
78 templateContext["load-linked-images"] = options.loadLinkedImages.toString()
79 templateContext["show-custom-avatars"] = options.showCustomAvatars.toString()
81 soneRequest.core.preferences.let { preferences ->
82 templateContext["insertion-delay"] = preferences.insertionDelay
83 templateContext["characters-per-post"] = preferences.charactersPerPost
84 templateContext["fcp-full-access-required"] = preferences.fcpFullAccessRequired.ordinal
85 templateContext["images-per-page"] = preferences.imagesPerPage
86 templateContext["fcp-interface-active"] = preferences.fcpInterfaceActive
87 templateContext["require-full-access"] = preferences.requireFullAccess
88 templateContext["negative-trust"] = preferences.negativeTrust
89 templateContext["positive-trust"] = preferences.positiveTrust
90 templateContext["post-cut-off-length"] = preferences.postCutOffLength
91 templateContext["posts-per-page"] = preferences.postsPerPage
92 templateContext["trust-comment"] = preferences.trustComment
96 private fun cantSetOption(setter: () -> Unit) =
100 } catch (iae: IllegalArgumentException) {