✨ Annotate ImageBrowserPage with MenuName
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / OptionsPage.kt
1 package net.pterodactylus.sone.web.pages
2
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
14
15 /**
16  * This page lets the user edit the options of the Sone plugin.
17  */
18 class OptionsPage @Inject constructor(template: Template, webInterface: WebInterface):
19                 SoneTemplatePage("options.html", webInterface, template, "Page.Options.Title") {
20
21         override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
22                 if (soneRequest.isPOST) {
23                         val fieldsWithErrors = mutableListOf<String>()
24                         getCurrentSone(soneRequest.toadletContext)?.options?.let { options ->
25                                 val autoFollow = "auto-follow" in soneRequest.parameters
26                                 val loadLinkedImages = soneRequest.parameters["load-linked-images"].emptyToNull
27                                 val showCustomAvatars = soneRequest.parameters["show-custom-avatars"].emptyToNull
28                                 val enableSoneInsertNotification = "enable-sone-insert-notifications" in soneRequest.parameters
29                                 val showNewSoneNotification = "show-notification-new-sones" in soneRequest.parameters
30                                 val showNewPostNotification = "show-notification-new-posts" in soneRequest.parameters
31                                 val showNewReplyNotification = "show-notification-new-replies" in soneRequest.parameters
32
33                                 options.isAutoFollow = autoFollow
34                                 options.isSoneInsertNotificationEnabled = enableSoneInsertNotification
35                                 options.isShowNewSoneNotifications = showNewSoneNotification
36                                 options.isShowNewPostNotifications = showNewPostNotification
37                                 options.isShowNewReplyNotifications = showNewReplyNotification
38                                 loadLinkedImages?.also { if (cantSetOption { options.loadLinkedImages = LoadExternalContent.valueOf(loadLinkedImages) }) fieldsWithErrors += "load-linked-images" }
39                                 showCustomAvatars?.also { if (cantSetOption { options.showCustomAvatars = LoadExternalContent.valueOf(showCustomAvatars) }) fieldsWithErrors += "show-custom-avatars" }
40                         }
41                         val fullAccessRequired = "require-full-access" in soneRequest.parameters
42                         val fcpInterfaceActive = "fcp-interface-active" in soneRequest.parameters
43
44                         soneRequest.core.preferences.newRequireFullAccess = fullAccessRequired
45                         soneRequest.core.preferences.newFcpInterfaceActive = fcpInterfaceActive
46
47                         val postsPerPage = soneRequest.parameters["posts-per-page"]?.toIntOrNull()
48                         val charactersPerPost = soneRequest.parameters["characters-per-post"]?.toIntOrNull()
49                         val postCutOffLength = soneRequest.parameters["post-cut-off-length"]?.toIntOrNull()
50                         val imagesPerPage = soneRequest.parameters["images-per-page"]?.toIntOrNull()
51                         val insertionDelay = soneRequest.parameters["insertion-delay"]?.toIntOrNull()
52                         val fcpFullAccessRequired = soneRequest.parameters["fcp-full-access-required"]?.toIntOrNull()
53                         val negativeTrust = soneRequest.parameters["negative-trust"]?.toIntOrNull()
54                         val positiveTrust = soneRequest.parameters["positive-trust"]?.toIntOrNull()
55                         val trustComment = soneRequest.parameters["trust-comment"]?.emptyToNull
56
57                         if (cantSetOption { soneRequest.core.preferences.newPostsPerPage = postsPerPage }) fieldsWithErrors += "posts-per-page"
58                         if (cantSetOption { soneRequest.core.preferences.newCharactersPerPost = charactersPerPost }) fieldsWithErrors += "characters-per-post"
59                         if (cantSetOption { soneRequest.core.preferences.newPostCutOffLength = postCutOffLength }) fieldsWithErrors += "post-cut-off-length"
60                         if (cantSetOption { soneRequest.core.preferences.newImagesPerPage = imagesPerPage }) fieldsWithErrors += "images-per-page"
61                         if (cantSetOption { soneRequest.core.preferences.newInsertionDelay = insertionDelay }) fieldsWithErrors += "insertion-delay"
62                         fcpFullAccessRequired?.also { if (cantSetOption { soneRequest.core.preferences.newFcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequired] }) fieldsWithErrors += "fcp-full-access-required" }
63                         if (cantSetOption { soneRequest.core.preferences.newNegativeTrust = negativeTrust }) fieldsWithErrors += "negative-trust"
64                         if (cantSetOption { soneRequest.core.preferences.newPositiveTrust = positiveTrust }) fieldsWithErrors += "positive-trust"
65                         if (cantSetOption { soneRequest.core.preferences.newTrustComment = trustComment }) fieldsWithErrors += "trust-comment"
66
67                         if (fieldsWithErrors.isEmpty()) {
68                                 soneRequest.core.touchConfiguration()
69                                 throw RedirectException("options.html")
70                         }
71                         templateContext["fieldErrors"] = fieldsWithErrors
72                 }
73                 getCurrentSone(soneRequest.toadletContext)?.options?.let { options ->
74                         templateContext["auto-follow"] = options.isAutoFollow
75                         templateContext["show-notification-new-sones"] = options.isShowNewSoneNotifications
76                         templateContext["show-notification-new-posts"] = options.isShowNewPostNotifications
77                         templateContext["show-notification-new-replies"] = options.isShowNewReplyNotifications
78                         templateContext["enable-sone-insert-notifications"] = options.isSoneInsertNotificationEnabled
79                         templateContext["load-linked-images"] = options.loadLinkedImages.toString()
80                         templateContext["show-custom-avatars"] = options.showCustomAvatars.toString()
81                 }
82                 soneRequest.core.preferences.let { preferences ->
83                         templateContext["insertion-delay"] = preferences.insertionDelay
84                         templateContext["characters-per-post"] = preferences.charactersPerPost
85                         templateContext["fcp-full-access-required"] = preferences.fcpFullAccessRequired.ordinal
86                         templateContext["images-per-page"] = preferences.imagesPerPage
87                         templateContext["fcp-interface-active"] = preferences.fcpInterfaceActive
88                         templateContext["require-full-access"] = preferences.requireFullAccess
89                         templateContext["negative-trust"] = preferences.negativeTrust
90                         templateContext["positive-trust"] = preferences.positiveTrust
91                         templateContext["post-cut-off-length"] = preferences.postCutOffLength
92                         templateContext["posts-per-page"] = preferences.postsPerPage
93                         templateContext["trust-comment"] = preferences.trustComment
94                 }
95         }
96
97         private fun cantSetOption(setter: () -> Unit) =
98                         try {
99                                 setter()
100                                 false
101                         } catch (iae: IllegalArgumentException) {
102                                 true
103                         }
104
105 }