cfe5db35775cd9a241b7a5423bdcd4ec725a1571
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / notification / NotificationHandlerModule.kt
1 /**
2  * Sone - NotificationHandlerModuleTest.kt - Copyright © 2019 David ‘Bombe’ Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web.notification
19
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.freenet.wot.*
25 import net.pterodactylus.sone.main.*
26 import net.pterodactylus.sone.notify.*
27 import net.pterodactylus.sone.text.*
28 import net.pterodactylus.util.notify.*
29 import java.util.concurrent.*
30 import java.util.concurrent.TimeUnit.*
31 import java.util.function.*
32 import javax.inject.*
33 import javax.inject.Singleton
34
35 /**
36  * Guice module for creating all notification handlers.
37  */
38 class NotificationHandlerModule : AbstractModule() {
39
40         override fun configure() {
41                 bind(NotificationHandler::class.java).`in`(Singleton::class.java)
42                 bind<MarkPostKnownDuringFirstStartHandler>().asSingleton()
43                 bind<SoneLockedOnStartupHandler>().asSingleton()
44                 bind<NewSoneHandler>().asSingleton()
45                 bind<NewRemotePostHandler>().asSingleton()
46                 bind<SoneLockedHandler>().asSingleton()
47                 bind<LocalPostHandler>().asSingleton()
48                 bind<NewVersionHandler>().asSingleton()
49                 bind<ImageInsertHandler>().asSingleton()
50                 bind<FirstStartHandler>().asSingleton()
51                 bind<ConfigNotReadHandler>().asSingleton()
52                 bind<StartupHandler>().asSingleton()
53                 bind<WebOfTrustHandler>().asSingleton()
54                 bind<SoneMentionDetector>().asSingleton()
55                 bind<SoneMentionedHandler>().asSingleton()
56         }
57
58         @Provides
59         fun getMarkPostKnownHandler(core: Core): Consumer<Post> = Consumer { core.markPostKnown(it) }
60
61         @Provides
62         @Singleton
63         @Named("soneLockedOnStartup")
64         fun getSoneLockedOnStartupNotification(loaders: Loaders) =
65                         ListNotification<Sone>("sone-locked-on-startup", "sones", loaders.loadTemplate("/templates/notify/soneLockedOnStartupNotification.html"))
66
67         @Provides
68         @Named("newSone")
69         fun getNewSoneNotification(loaders: Loaders) =
70                         ListNotification<Sone>("new-sone-notification", "sones", loaders.loadTemplate("/templates/notify/newSoneNotification.html"), dismissable = false)
71
72         @Provides
73         @Singleton
74         @Named("newRemotePost")
75         fun getNewPostNotification(loaders: Loaders) =
76                         ListNotification<Post>("new-post-notification", "posts", loaders.loadTemplate("/templates/notify/newPostNotification.html"), dismissable = false)
77
78         @Provides
79         @Singleton
80         @Named("soneLocked")
81         fun getSoneLockedNotification(loaders: Loaders) =
82                         ListNotification<Sone>("sones-locked-notification", "sones", loaders.loadTemplate("/templates/notify/lockedSonesNotification.html"), dismissable = true)
83
84         @Provides
85         @Singleton
86         @Named("localPost")
87         fun getLocalPostNotification(loaders: Loaders) =
88                         ListNotification<Post>("local-post-notification", "posts", loaders.loadTemplate("/templates/notify/newPostNotification.html"), dismissable = false)
89
90         @Provides
91         @Singleton
92         @Named("newVersion")
93         fun getNewVersionNotification(loaders: Loaders) =
94                         TemplateNotification("new-version-notification", loaders.loadTemplate("/templates/notify/newVersionNotification.html"))
95
96         @Provides
97         @Singleton
98         @Named("imageInserting")
99         fun getImageInsertingNotification(loaders: Loaders) =
100                         ListNotification<Image>("inserting-images-notification", "images", loaders.loadTemplate("/templates/notify/inserting-images-notification.html"), dismissable = true)
101
102         @Provides
103         @Singleton
104         @Named("imageFailed")
105         fun getImageInsertingFailedNotification(loaders: Loaders) =
106                         ListNotification<Image>("image-insert-failed-notification", "images", loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html"), dismissable = true)
107
108         @Provides
109         @Singleton
110         @Named("imageInserted")
111         fun getImageInsertedNotification(loaders: Loaders) =
112                         ListNotification<Image>("inserted-images-notification", "images", loaders.loadTemplate("/templates/notify/inserted-images-notification.html"), dismissable = true)
113
114         @Provides
115         @Singleton
116         @Named("firstStart")
117         fun getFirstStartNotification(loaders: Loaders) =
118                         TemplateNotification("first-start-notification", loaders.loadTemplate("/templates/notify/firstStartNotification.html"))
119
120         @Provides
121         @Singleton
122         @Named("configNotRead")
123         fun getConfigNotReadNotification(loaders: Loaders) =
124                         TemplateNotification("config-not-read-notification", loaders.loadTemplate("/templates/notify/configNotReadNotification.html"))
125
126         @Provides
127         @Singleton
128         @Named("startup")
129         fun getStartupNotification(loaders: Loaders) =
130                         TemplateNotification("startup-notification", loaders.loadTemplate("/templates/notify/startupNotification.html"))
131
132         @Provides
133         @Singleton
134         @Named("webOfTrust")
135         fun getWebOfTrustNotification(loaders: Loaders) =
136                         TemplateNotification("wot-missing-notification", loaders.loadTemplate("/templates/notify/wotMissingNotification.html"))
137
138         @Provides
139         @Singleton
140         @Named("webOfTrustReacher")
141         fun getWebOfTrustReacher(webOfTrustConnector: WebOfTrustConnector): Runnable =
142                         Runnable { webOfTrustConnector.ping() }
143
144         @Provides
145         @Singleton
146         @Named("webOfTrustReschedule")
147         fun getWebOfTrustReschedule(@Named("notification") ticker: ScheduledExecutorService) =
148                         Consumer<Runnable> { ticker.schedule(it, 15, SECONDS) }
149
150         @Provides
151         @Singleton
152         @Named("soneMentioned")
153         fun getSoneMentionedNotification(loaders: Loaders) =
154                         ListNotification<Post>("mention-notification", "posts", loaders.loadTemplate("/templates/notify/mentionNotification.html"), dismissable = false)
155
156         private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
157         private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
158
159 }