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.FreenetRequest
11 import net.pterodactylus.util.template.Template
12 import net.pterodactylus.util.template.TemplateContext
15 * This page lets the user edit the options of the Sone plugin.
17 class OptionsPage(template: Template, webInterface: WebInterface):
18 SoneTemplatePage("options.html", webInterface, template, "Page.Options.Title") {
20 override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
21 if (freenetRequest.isPOST) {
22 val fieldsWithErrors = mutableListOf<String>()
23 getCurrentSone(freenetRequest.toadletContext)?.options?.let { options ->
24 val autoFollow = "auto-follow" in freenetRequest.parameters
25 val loadLinkedImages = freenetRequest.parameters["load-linked-images"].emptyToNull
26 val showCustomAvatars = freenetRequest.parameters["show-custom-avatars"].emptyToNull
27 val enableSoneInsertNotification = "enable-sone-insert-notifications" in freenetRequest.parameters
28 val showNewSoneNotification = "show-notification-new-sones" in freenetRequest.parameters
29 val showNewPostNotification = "show-notification-new-posts" in freenetRequest.parameters
30 val showNewReplyNotification = "show-notification-new-replies" in freenetRequest.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 freenetRequest.parameters
41 val fcpInterfaceActive = "fcp-interface-active" in freenetRequest.parameters
43 webInterface.core.preferences.isRequireFullAccess = fullAccessRequired
44 webInterface.core.preferences.isFcpInterfaceActive = fcpInterfaceActive
46 val postsPerPage = freenetRequest.parameters["posts-per-page"]?.toIntOrNull()
47 val charactersPerPost = freenetRequest.parameters["characters-per-post"]?.toIntOrNull()
48 val postCutOffLength = freenetRequest.parameters["post-cut-off-length"]?.toIntOrNull()
49 val imagesPerPage = freenetRequest.parameters["images-per-page"]?.toIntOrNull()
50 val insertionDelay = freenetRequest.parameters["insertion-delay"]?.toIntOrNull()
51 val fcpFullAccessRequired = freenetRequest.parameters["fcp-full-access-required"]?.toIntOrNull()
52 val negativeTrust = freenetRequest.parameters["negative-trust"]?.toIntOrNull()
53 val positiveTrust = freenetRequest.parameters["positive-trust"]?.toIntOrNull()
54 val trustComment = freenetRequest.parameters["trust-comment"]?.emptyToNull
56 if (cantSetOption { it.setPostsPerPage(postsPerPage) }) fieldsWithErrors += "posts-per-page"
57 if (cantSetOption { it.setCharactersPerPost(charactersPerPost) }) fieldsWithErrors += "characters-per-post"
58 if (cantSetOption { it.setPostCutOffLength(postCutOffLength) }) fieldsWithErrors += "post-cut-off-length"
59 if (cantSetOption { it.setImagesPerPage(imagesPerPage) }) fieldsWithErrors += "images-per-page"
60 if (cantSetOption { it.setInsertionDelay(insertionDelay) }) fieldsWithErrors += "insertion-delay"
61 fcpFullAccessRequired?.also { if (cantSetOption { it.fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequired] }) fieldsWithErrors += "fcp-full-access-required" }
62 if (cantSetOption { it.setNegativeTrust(negativeTrust) }) fieldsWithErrors += "negative-trust"
63 if (cantSetOption { it.setPositiveTrust(positiveTrust) }) fieldsWithErrors += "positive-trust"
64 if (cantSetOption { it.trustComment = trustComment }) fieldsWithErrors += "trust-comment"
66 if (fieldsWithErrors.isEmpty()) {
67 webInterface.core.touchConfiguration()
68 throw RedirectException("options.html")
70 templateContext["fieldErrors"] = fieldsWithErrors
72 getCurrentSone(freenetRequest.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 webInterface.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.isFcpInterfaceActive
87 templateContext["require-full-access"] = preferences.isRequireFullAccess
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: (Preferences) -> Unit) =
98 setter(webInterface.core.preferences)
100 } catch (iae: IllegalArgumentException) {