From acdc9746bb0c853bfd36f1c982c95017158166c2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 4 Jan 2020 17:17:45 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Emit=20event=20for=20replies=20as?= =?utf8?q?=20well?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/text/SoneMentionDetector.kt | 11 ++++ .../sone/text/SoneMentionDetectorTest.kt | 58 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt index 7dd8285..32d311d 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt @@ -41,4 +41,15 @@ class SoneMentionDetector @Inject constructor(private val eventBus: EventBus, pr } } + @Subscribe + fun onNewPostReply(event: NewPostReplyFoundEvent) { + event.postReply.let { postReply -> + postReply.sone.isLocal.onFalse { + if (soneTextParser.parse(postReply.text, null).filterIsInstance().any { it.sone.isLocal }) { + postReply.post.let(::LocalSoneMentionedInPostEvent).also(eventBus::post) + } + } + } + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt index c319809..2003988 100644 --- a/src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt @@ -17,6 +17,8 @@ package net.pterodactylus.sone.text +import com.google.common.base.* +import com.google.common.base.Optional.* import com.google.common.eventbus.* import net.pterodactylus.sone.core.event.* import net.pterodactylus.sone.data.* @@ -89,6 +91,51 @@ class SoneMentionDetectorTest { assertThat(capturedEvents, emptyIterable()) } + @Test + fun `detector does not emit event for reply that contains no sones`() { + val reply = emptyPostReply() + eventBus.post(NewPostReplyFoundEvent(reply)) + assertThat(capturedEvents, emptyIterable()) + } + + @Test + fun `detector does not emit event for reply that contains two links to remote sones`() { + val reply = emptyPostReply("text mentions sone://${remoteSone1.id} and sone://${remoteSone2.id}.") + eventBus.post(NewPostReplyFoundEvent(reply)) + assertThat(capturedEvents, emptyIterable()) + } + + @Test + fun `detector emits event on reply that contains links to a remote and a local sone`() { + val post = createPost() + val reply = emptyPostReply("text mentions sone://${remoteSone1.id} and sone://${localSone1.id}.", post) + eventBus.post(NewPostReplyFoundEvent(reply)) + assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post))) + } + + @Test + fun `detector emits one event on reply that contains two links to the same local sone`() { + val post = createPost() + val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone1.id}.", post) + eventBus.post(NewPostReplyFoundEvent(reply)) + assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post))) + } + + @Test + fun `detector emits one event on reply that contains two links to local sones`() { + val post = createPost() + val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post) + eventBus.post(NewPostReplyFoundEvent(reply)) + assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post))) + } + + @Test + fun `detector does not emit event for reply by local sone`() { + val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", sone = localSone1) + eventBus.post(NewPostReplyFoundEvent(reply)) + assertThat(capturedEvents, emptyIterable()) + } + } private val remoteSone1 = createRemoteSone() @@ -122,3 +169,14 @@ private class TestPostProvider : PostProvider { override fun getDirectedPosts(recipientId: String): Collection = emptyList() } + +private fun emptyPostReply(text: String = "", post: Post = createPost(), sone: Sone = remoteSone1) = object : PostReply { + override val id = "reply-id" + override fun getSone() = sone + override fun getPostId() = post.id + override fun getPost(): Optional = of(post) + override fun getTime() = 1L + override fun getText() = text + override fun isKnown() = false + override fun setKnown(known: Boolean): PostReply = this +} -- 2.7.4