From c66657eb61d863fe2b1f1ea534a2bd46e5064b89 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 30 Jul 2020 00:12:13 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20NPE=20during=20Sone=20remov?= =?utf8?q?al?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/web/notification/LocalPostHandler.kt | 18 +++++++++++------- .../sone/web/notification/LocalPostHandlerTest.kt | 10 ++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandler.kt index e7b8f05..5951997 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandler.kt @@ -17,12 +17,16 @@ 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 javax.inject.* +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. @@ -56,4 +60,4 @@ class LocalPostHandler @Inject constructor(private val notificationManager: Noti } private fun Post.onLocal(action: (Post) -> Unit) = - if (sone.isLocal) action(this) else Unit + if ((sone == null) || sone!!.isLocal) action(this) else Unit diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandlerTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandlerTest.kt index 22648b5..70aa709 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandlerTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandlerTest.kt @@ -22,6 +22,7 @@ import net.pterodactylus.sone.core.event.* import net.pterodactylus.sone.data.* import net.pterodactylus.sone.data.impl.* import net.pterodactylus.sone.notify.* +import net.pterodactylus.sone.test.createPost import net.pterodactylus.util.notify.* import net.pterodactylus.util.template.* import org.hamcrest.MatcherAssert.* @@ -89,6 +90,15 @@ class LocalPostHandlerTest { } @Test + // this scenario can happen when sones are removed. + fun `handler removes post from notification if sone is missing`() { + val post = createPost(sone = null) + notification.add(post) + eventBus.post(PostRemovedEvent(post)) + assertThat(notification.elements, emptyIterable()) + } + + @Test fun `handler removes post from notification when post is marked as known`() { notification.add(localPost) notificationManager.addNotification(notification) -- 2.7.4