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