🐛 Fix NPE during Sone removal
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / notification / LocalPostHandler.kt
index 1ff4ea2..5951997 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Sone - NewLocalPostHandler.kt - Copyright © 2019 David ‘Bombe’ Roden
+ * Sone - LocalPostHandler.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.web.notification
 
-import com.google.common.eventbus.*
-import net.pterodactylus.sone.core.event.*
-import net.pterodactylus.sone.data.*
-import net.pterodactylus.sone.notify.*
-import net.pterodactylus.util.notify.*
+import com.google.common.eventbus.Subscribe
+import net.pterodactylus.sone.core.event.MarkPostKnownEvent
+import net.pterodactylus.sone.core.event.NewPostFoundEvent
+import net.pterodactylus.sone.core.event.PostRemovedEvent
+import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.notify.ListNotification
+import net.pterodactylus.sone.notify.hasFirstStartNotification
+import net.pterodactylus.util.notify.NotificationManager
+import javax.inject.Inject
+import javax.inject.Named
 
 /**
  * Handler for local posts.
  */
-class LocalPostHandler(private val notificationManager: NotificationManager, private val notification: ListNotification<Post>) {
+class LocalPostHandler @Inject constructor(private val notificationManager: NotificationManager, @Named("localPost") private val notification: ListNotification<Post>) {
 
        @Subscribe
        fun newPostFound(newPostFoundEvent: NewPostFoundEvent) {
                newPostFoundEvent.post.onLocal { post ->
                        notification.add(post)
-               }
-               if (!notificationManager.hasFirstStartNotification()) {
-                       notificationManager.addNotification(notification)
+                       if (!notificationManager.hasFirstStartNotification()) {
+                               notificationManager.addNotification(notification)
+                       }
                }
        }
 
@@ -55,4 +60,4 @@ class LocalPostHandler(private val notificationManager: NotificationManager, pri
 }
 
 private fun Post.onLocal(action: (Post) -> Unit) =
-               if (sone.isLocal) action(this) else Unit
+               if ((sone == null) || sone!!.isLocal) action(this) else Unit