From: David ‘Bombe’ Roden Date: Mon, 27 Jan 2020 09:11:50 +0000 (+0100) Subject: ✨ Use recipient Sone as reply sender X-Git-Tag: v81^2~6 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=de6be950b15db8012395f43217af6565fadecbf4;p=Sone.git ✨ Use recipient Sone as reply sender --- 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) }