✨ Use recipient Sone as reply sender
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 Jan 2020 09:11:50 +0000 (10:11 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 Jan 2020 09:11:50 +0000 (10:11 +0100)
src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt
src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt

index 5222b94..843e6e3 100644 (file)
@@ -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)
index 72e1343..bb92810 100644 (file)
@@ -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<Any>(localSone))
+       }
+
+
+       @Test
        fun `accessor returns other properties`() {
                assertThat(accessor[null, post, "hashCode"], equalTo<Any>(post.hashCode()))
        }
@@ -179,9 +188,10 @@ private val templateContext = TemplateContext().apply {
        this["currentSone"] = currentSone
 }
 
-private fun mockPostFrom(sone: Sone) = mock<Post>().apply {
+private fun mockPostFrom(sone: Sone, recipient: Sone? = null) = mock<Post>().apply {
        whenever(id).thenReturn("post-id")
        whenever(this.sone).thenReturn(sone)
+       whenever(this.recipient).thenReturn(recipient.asOptional())
 }
 
 private fun mockReplyFrom(sone: Sone) = mock<PostReply>().apply { whenever(this.sone).thenReturn(sone) }