🚧 Change rescheduling method
[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.data.SoneOptions.*
4 import net.pterodactylus.sone.fcp.FcpInterface.*
5 import net.pterodactylus.sone.main.*
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.template.*
10 import javax.inject.*
11
12 /**
13  * This page lets the user edit the options of the Sone plugin.
14  */
15 @MenuName("Options")
16 @TemplatePath("/templates/options.html")
17 @ToadletPath("options.html")
18 class OptionsPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
19                 SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = "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
54                         if (cantSetOption { soneRequest.core.preferences.newPostsPerPage = postsPerPage }) fieldsWithErrors += "posts-per-page"
55                         if (cantSetOption { soneRequest.core.preferences.newCharactersPerPost = charactersPerPost }) fieldsWithErrors += "characters-per-post"
56                         if (cantSetOption { soneRequest.core.preferences.newPostCutOffLength = postCutOffLength }) fieldsWithErrors += "post-cut-off-length"
57                         if (cantSetOption { soneRequest.core.preferences.newImagesPerPage = imagesPerPage }) fieldsWithErrors += "images-per-page"
58                         if (cantSetOption { soneRequest.core.preferences.newInsertionDelay = insertionDelay }) fieldsWithErrors += "insertion-delay"
59                         fcpFullAccessRequired?.also { if (cantSetOption { soneRequest.core.preferences.newFcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequired] }) fieldsWithErrors += "fcp-full-access-required" }
60
61                         if (fieldsWithErrors.isEmpty()) {
62                                 soneRequest.core.touchConfiguration()
63                                 redirectTo("options.html")
64                         }
65                         templateContext["fieldErrors"] = fieldsWithErrors
66                 }
67                 getCurrentSone(soneRequest.toadletContext)?.options?.let { options ->
68                         templateContext["auto-follow"] = options.isAutoFollow
69                         templateContext["show-notification-new-sones"] = options.isShowNewSoneNotifications
70                         templateContext["show-notification-new-posts"] = options.isShowNewPostNotifications
71                         templateContext["show-notification-new-replies"] = options.isShowNewReplyNotifications
72                         templateContext["enable-sone-insert-notifications"] = options.isSoneInsertNotificationEnabled
73                         templateContext["load-linked-images"] = options.loadLinkedImages.toString()
74                         templateContext["show-custom-avatars"] = options.showCustomAvatars.toString()
75                 }
76                 soneRequest.core.preferences.let { preferences ->
77                         templateContext["insertion-delay"] = preferences.insertionDelay
78                         templateContext["characters-per-post"] = preferences.charactersPerPost
79                         templateContext["fcp-full-access-required"] = preferences.fcpFullAccessRequired.ordinal
80                         templateContext["images-per-page"] = preferences.imagesPerPage
81                         templateContext["fcp-interface-active"] = preferences.fcpInterfaceActive
82                         templateContext["require-full-access"] = preferences.requireFullAccess
83                         templateContext["post-cut-off-length"] = preferences.postCutOffLength
84                         templateContext["posts-per-page"] = preferences.postsPerPage
85                 }
86         }
87
88         private fun cantSetOption(setter: () -> Unit) =
89                         try {
90                                 setter()
91                                 false
92                         } catch (iae: IllegalArgumentException) {
93                                 true
94                         }
95
96 }