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