import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.core.ElementLoader;
import net.pterodactylus.sone.core.event.*;
-import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
/** Notifications for sone inserts. */
private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<>();
- /** The “inserting images” notification. */
- private final ListNotification<Image> insertingImagesNotification;
-
- /** The “inserted images” notification. */
- private final ListNotification<Image> insertedImagesNotification;
-
- /** The “image insert failed” notification. */
- private final ListNotification<Image> imageInsertFailedNotification;
-
/** Scheduled executor for time-based notifications. */
private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
mentionNotification = new ListNotification<>("mention-notification", "posts", mentionNotificationTemplate, false);
-
- Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
- insertingImagesNotification = new ListNotification<>("inserting-images-notification", "images", insertingImagesTemplate);
-
- Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
- insertedImagesNotification = new ListNotification<>("inserted-images-notification", "images", insertedImagesTemplate);
-
- Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
- imageInsertFailedNotification = new ListNotification<>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
}
//
}
}
- /**
- * Notifies the web interface that an image insert was started
- *
- * @param imageInsertStartedEvent
- * The event
- */
- @Subscribe
- public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
- insertingImagesNotification.add(imageInsertStartedEvent.getImage());
- notificationManager.addNotification(insertingImagesNotification);
- }
-
- /**
- * Notifies the web interface that an {@link Image} insert was aborted.
- *
- * @param imageInsertAbortedEvent
- * The event
- */
- @Subscribe
- public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
- insertingImagesNotification.remove(imageInsertAbortedEvent.getImage());
- }
-
- /**
- * Notifies the web interface that an {@link Image} insert is finished.
- *
- * @param imageInsertFinishedEvent
- * The event
- */
- @Subscribe
- public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
- insertingImagesNotification.remove(imageInsertFinishedEvent.getImage());
- insertedImagesNotification.add(imageInsertFinishedEvent.getImage());
- notificationManager.addNotification(insertedImagesNotification);
- }
-
- /**
- * Notifies the web interface that an {@link Image} insert has failed.
- *
- * @param imageInsertFailedEvent
- * The event
- */
- @Subscribe
- public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
- insertingImagesNotification.remove(imageInsertFailedEvent.getImage());
- imageInsertFailedNotification.add(imageInsertFailedEvent.getImage());
- notificationManager.addNotification(imageInsertFailedNotification);
- }
-
@Subscribe
public void debugActivated(@Nonnull DebugActivatedEvent debugActivatedEvent) {
pageToadletRegistry.activateDebugMode();
import net.pterodactylus.sone.data.*
import net.pterodactylus.sone.notify.*
import net.pterodactylus.util.notify.*
+import javax.inject.*
/**
* Notification handler for the various image-insert-related events.
* @see ImageInsertFailedEvent
* @see ImageInsertFinishedEvent
*/
-class ImageInsertHandler(
+class ImageInsertHandler @Inject constructor(
private val notificationManager: NotificationManager,
- private val imageInsertingNotification: ListNotification<Image>,
- private val imageFailedNotification: ListNotification<Image>,
- private val imageInsertedNotification: ListNotification<Image>) {
+ @Named("imageInserting") private val imageInsertingNotification: ListNotification<Image>,
+ @Named("imageFailed") private val imageFailedNotification: ListNotification<Image>,
+ @Named("imageInserted") private val imageInsertedNotification: ListNotification<Image>) {
@Subscribe
fun imageInsertStarted(imageInsertStartedEvent: ImageInsertStartedEvent) {
newRemotePostHandler: NewRemotePostHandler,
soneLockedOnStartupHandler: SoneLockedOnStartupHandler,
soneLockedHandler: SoneLockedHandler,
- newVersionHandler: NewVersionHandler
+ newVersionHandler: NewVersionHandler,
+ imageInsertHandler: ImageInsertHandler
)
bind<SoneLockedHandler>().asSingleton()
bind<LocalPostHandler>().asSingleton()
bind<NewVersionHandler>().asSingleton()
+ bind<ImageInsertHandler>().asSingleton()
}
@Provides
fun getNewVersionNotification(loaders: Loaders) =
TemplateNotification("new-version-notification", loaders.loadTemplate("/templates/notify/newVersionNotification.html"))
+ @Provides
+ @Singleton
+ @Named("imageInserting")
+ fun getImageInsertingNotification(loaders: Loaders) =
+ ListNotification<Image>("inserting-images-notification", "images", loaders.loadTemplate("/templates/notify/inserting-images-notification.html"), dismissable = true)
+
+ @Provides
+ @Singleton
+ @Named("imageFailed")
+ fun getImageInsertingFailedNotification(loaders: Loaders) =
+ ListNotification<Image>("image-insert-failed-notification", "images", loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html"), dismissable = true)
+
+ @Provides
+ @Singleton
+ @Named("imageInserted")
+ fun getImageInsertedNotification(loaders: Loaders) =
+ ListNotification<Image>("inserted-images-notification", "images", loaders.loadTemplate("/templates/notify/inserted-images-notification.html"), dismissable = true)
+
private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
injector.verifySingletonInstance<NewVersionHandler>()
}
+ @Test
+ fun `inserting-image notification is created as singleton`() {
+ injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
+ }
+
+ @Test
+ fun `inserting-image notification has correct ID`() {
+ assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
+ }
+
+ @Test
+ fun `inserting-image notification is dismissable`() {
+ assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
+ }
+
+ @Test
+ fun `inserting-image notification loads correct template`() {
+ loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
+ val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
+ val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
+ assertThat(notification.render(), equalTo(images.toString()))
+ }
+
+ @Test
+ fun `inserting-image-failed notification is created as singleton`() {
+ injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
+ }
+
+ @Test
+ fun `inserting-image-failed notification has correct ID`() {
+ assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
+ }
+
+ @Test
+ fun `inserting-image-failed notification is dismissable`() {
+ assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
+ }
+
+ @Test
+ fun `inserting-image-failed notification loads correct template`() {
+ loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
+ val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
+ val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
+ assertThat(notification.render(), equalTo(images.toString()))
+ }
+
+ @Test
+ fun `inserted-image notification is created as singleton`() {
+ injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
+ }
+
+ @Test
+ fun `inserted-image notification has correct ID`() {
+ assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
+ }
+
+ @Test
+ fun `inserted-image notification is dismissable`() {
+ assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
+ }
+
+ @Test
+ fun `inserted-image notification loads correct template`() {
+ loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
+ val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
+ val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
+ assertThat(notification.render(), equalTo(images.toString()))
+ }
+
+ @Test
+ fun `image insert handler is created as singleton`() {
+ injector.verifySingletonInstance<ImageInsertHandler>()
+ }
+
}