2 * Sone - NotificationHandlerModuleTest.kt - Copyright © 2019 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web.notification
20 import com.google.inject.*
21 import com.google.inject.binder.*
22 import net.pterodactylus.sone.core.*
23 import net.pterodactylus.sone.data.*
24 import net.pterodactylus.sone.main.*
25 import net.pterodactylus.sone.notify.*
26 import net.pterodactylus.util.notify.*
27 import java.util.concurrent.*
28 import java.util.concurrent.Executors.*
29 import java.util.function.*
31 import javax.inject.Singleton
34 * Guice module for creating all notification handlers.
36 class NotificationHandlerModule : AbstractModule() {
38 override fun configure() {
39 bind(NotificationHandler::class.java).`in`(Singleton::class.java)
40 bind<MarkPostKnownDuringFirstStartHandler>().asSingleton()
41 bind<SoneLockedOnStartupHandler>().asSingleton()
42 bind<NewSoneHandler>().asSingleton()
43 bind<NewRemotePostHandler>().asSingleton()
44 bind<SoneLockedHandler>().asSingleton()
45 bind<LocalPostHandler>().asSingleton()
46 bind<NewVersionHandler>().asSingleton()
47 bind<ImageInsertHandler>().asSingleton()
48 bind<FirstStartHandler>().asSingleton()
49 bind<ConfigNotReadHandler>().asSingleton()
50 bind<StartupHandler>().asSingleton()
54 fun getMarkPostKnownHandler(core: Core): Consumer<Post> = Consumer { core.markPostKnown(it) }
58 @Named("soneLockedOnStartup")
59 fun getSoneLockedOnStartupNotification(loaders: Loaders) =
60 ListNotification<Sone>("sone-locked-on-startup", "sones", loaders.loadTemplate("/templates/notify/soneLockedOnStartupNotification.html"))
64 fun getNewSoneNotification(loaders: Loaders) =
65 ListNotification<Sone>("new-sone-notification", "sones", loaders.loadTemplate("/templates/notify/newSoneNotification.html"), dismissable = false)
69 @Named("newRemotePost")
70 fun getNewPostNotification(loaders: Loaders) =
71 ListNotification<Post>("new-post-notification", "posts", loaders.loadTemplate("/templates/notify/newPostNotification.html"), dismissable = false)
76 fun getSoneLockedNotification(loaders: Loaders) =
77 ListNotification<Sone>("sones-locked-notification", "sones", loaders.loadTemplate("/templates/notify/lockedSonesNotification.html"), dismissable = true)
80 fun getScheduledExecutorService(): ScheduledExecutorService =
81 newScheduledThreadPool(1)
86 fun getLocalPostNotification(loaders: Loaders) =
87 ListNotification<Post>("local-post-notification", "posts", loaders.loadTemplate("/templates/notify/newPostNotification.html"), dismissable = false)
92 fun getNewVersionNotification(loaders: Loaders) =
93 TemplateNotification("new-version-notification", loaders.loadTemplate("/templates/notify/newVersionNotification.html"))
97 @Named("imageInserting")
98 fun getImageInsertingNotification(loaders: Loaders) =
99 ListNotification<Image>("inserting-images-notification", "images", loaders.loadTemplate("/templates/notify/inserting-images-notification.html"), dismissable = true)
103 @Named("imageFailed")
104 fun getImageInsertingFailedNotification(loaders: Loaders) =
105 ListNotification<Image>("image-insert-failed-notification", "images", loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html"), dismissable = true)
109 @Named("imageInserted")
110 fun getImageInsertedNotification(loaders: Loaders) =
111 ListNotification<Image>("inserted-images-notification", "images", loaders.loadTemplate("/templates/notify/inserted-images-notification.html"), dismissable = true)
116 fun getFirstStartNotification(loaders: Loaders) =
117 TemplateNotification("first-start-notification", loaders.loadTemplate("/templates/notify/firstStartNotification.html"))
121 @Named("configNotRead")
122 fun getConfigNotReadNotification(loaders: Loaders) =
123 TemplateNotification("config-not-read-notification", loaders.loadTemplate("/templates/notify/configNotReadNotification.html"))
128 fun getStartupNotification(loaders: Loaders) =
129 TemplateNotification("startup-notification", loaders.loadTemplate("/templates/notify/startupNotification.html"))
131 private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
132 private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)