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