From de6be950b15db8012395f43217af6565fadecbf4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 27 Jan 2020 10:11:50 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Use=20recipient=20Sone=20as=20reply?= =?utf8?q?=20sender?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../kotlin/net/pterodactylus/sone/template/PostAccessor.kt | 6 +++--- .../net/pterodactylus/sone/template/PostAccessorTest.kt | 12 +++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt b/src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt index 5222b94..843e6e3 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt @@ -18,6 +18,7 @@ package net.pterodactylus.sone.template import net.pterodactylus.sone.core.* import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.utils.* import net.pterodactylus.util.template.* /** @@ -39,9 +40,8 @@ class PostAccessor(private val core: Core) : ReflectionAccessor() { "liked" -> templateContext.currentSone?.isLikedPostId(post.id) ?: false "new" -> !post.isKnown "bookmarked" -> core.isBookmarked(post) - "replySone" -> core.getReplies(post) - .lastOrNull { it.sone.isLocal } - ?.sone + "replySone" -> core.getReplies(post).lastOrNull { it.sone.isLocal }?.sone + ?: post.recipient.let { it.takeIf { it.isLocal } } ?: post.sone.takeIf { it.isLocal } ?: templateContext.currentSone else -> super.get(templateContext, `object`, member) diff --git a/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt index 72e1343..bb92810 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt @@ -3,6 +3,7 @@ package net.pterodactylus.sone.template import net.pterodactylus.sone.core.* import net.pterodactylus.sone.data.* import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.utils.* import net.pterodactylus.util.template.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* @@ -165,6 +166,14 @@ class PostAccessorTest { } @Test + fun `reply sone for post directed at local sone is local sone`() { + val localSone = mockLocalSone() + val post = mockPostFrom(remoteSone, localSone) + assertThat(accessor[templateContext, post, "replySone"], equalTo(localSone)) + } + + + @Test fun `accessor returns other properties`() { assertThat(accessor[null, post, "hashCode"], equalTo(post.hashCode())) } @@ -179,9 +188,10 @@ private val templateContext = TemplateContext().apply { this["currentSone"] = currentSone } -private fun mockPostFrom(sone: Sone) = mock().apply { +private fun mockPostFrom(sone: Sone, recipient: Sone? = null) = mock().apply { whenever(id).thenReturn("post-id") whenever(this.sone).thenReturn(sone) + whenever(this.recipient).thenReturn(recipient.asOptional()) } private fun mockReplyFrom(sone: Sone) = mock().apply { whenever(this.sone).thenReturn(sone) } -- 2.7.4