From 5452196f428c37cdde3943f327f730b4b983128d Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 27 Jan 2023 13:16:45 -0300 Subject: [PATCH] New root search for the thread view --- .../amethyst/service/NostrThreadDataSource.kt | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/NostrThreadDataSource.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/NostrThreadDataSource.kt index 5447b7458..eb144ad7f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/NostrThreadDataSource.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/NostrThreadDataSource.kt @@ -59,6 +59,32 @@ object NostrThreadDataSource: NostrDataSource("SingleThreadFeed") { loadEventsChannel.filter = listOfNotNull(createLoadEventsIfNotLoadedFilter()).ifEmpty { null } } + fun searchRoot(note: Note, testedNotes: MutableSet = mutableSetOf()): Note? { + if (note.replyTo == null || note.replyTo?.isEmpty() == true) return note + + val markedAsRoot = note.event?.tags?.firstOrNull { it[0] == "e" && it.size > 3 && it[3] == "root" }?.getOrNull(1) + if (markedAsRoot != null) return LocalCache.getOrCreateNote(markedAsRoot) + + val hasNoReplyTo = note.replyTo?.firstOrNull { it.replyTo?.isEmpty() == true } + if (hasNoReplyTo != null) return hasNoReplyTo + + testedNotes.add(note) + + // recursive + val roots = note.replyTo?.map { + if (it !in testedNotes) + searchRoot(it, testedNotes) + else + null + }?.filterNotNull() + + if (roots != null && roots.isNotEmpty()) { + return roots[0] + } + + return null + } + fun loadThread(noteId: String) { val note = LocalCache.notes[noteId] @@ -66,7 +92,7 @@ object NostrThreadDataSource: NostrDataSource("SingleThreadFeed") { val thread = mutableListOf() val threadSet = mutableSetOf() - val threadRoot = note.replyTo?.firstOrNull() ?: note + val threadRoot = searchRoot(note) ?: note loadDown(threadRoot, thread, threadSet)