Fixing ThreadAssembler with two roots

This commit is contained in:
Vitor Pamplona
2023-08-17 15:15:13 -04:00
parent 4e89b1494d
commit eef534617c
3 changed files with 158 additions and 4 deletions
File diff suppressed because one or more lines are too long
@@ -120,17 +120,24 @@ open class Note(val idHex: String) {
/** /**
* This method caches signatures during each execution to avoid recalculation in longer threads * This method caches signatures during each execution to avoid recalculation in longer threads
*/ */
fun replyLevelSignature(cachedSignatures: MutableMap<Note, String> = mutableMapOf()): String { fun replyLevelSignature(
eventsToConsider: Set<Note>,
cachedSignatures: MutableMap<Note, String>
): String {
val replyTo = replyTo val replyTo = replyTo
if (event is RepostEvent || event is GenericRepostEvent || replyTo == null || replyTo.isEmpty()) { if (event is RepostEvent || event is GenericRepostEvent || replyTo == null || replyTo.isEmpty()) {
return "/" + formattedDateTime(createdAt() ?: 0) + ";" return "/" + formattedDateTime(createdAt() ?: 0) + ";"
} }
return replyTo val mySignature = replyTo
.filter { it in eventsToConsider } // This forces the signature to be based on a branch, avoiding two roots
.map { .map {
cachedSignatures[it] ?: it.replyLevelSignature(cachedSignatures).apply { cachedSignatures.put(it, this) } cachedSignatures[it] ?: it.replyLevelSignature(eventsToConsider, cachedSignatures).apply { cachedSignatures.put(it, this) }
} }
.maxBy { it.length }.removeSuffix(";") + "/" + formattedDateTime(createdAt() ?: 0) + ";" .maxBy { it.length }.removeSuffix(";") + "/" + formattedDateTime(createdAt() ?: 0) + ";"
cachedSignatures[this] = mySignature
return mySignature
} }
fun replyLevel(cachedLevels: MutableMap<Note, Int> = mutableMapOf()): Int { fun replyLevel(cachedLevels: MutableMap<Note, Int> = mutableMapOf()): Int {
@@ -14,8 +14,9 @@ class ThreadFeedFilter(val noteId: String) : FeedFilter<Note>() {
override fun feed(): List<Note> { override fun feed(): List<Note> {
val cachedSignatures: MutableMap<Note, String> = mutableMapOf() val cachedSignatures: MutableMap<Note, String> = mutableMapOf()
val eventsToWatch = ThreadAssembler().findThreadFor(noteId) ?: emptySet() val eventsToWatch = ThreadAssembler().findThreadFor(noteId) ?: emptySet()
// Currently orders by date of each event, descending, at each level of the reply stack // Currently orders by date of each event, descending, at each level of the reply stack
val order = compareByDescending<Note> { it.replyLevelSignature(cachedSignatures) } val order = compareByDescending<Note> { it.replyLevelSignature(eventsToWatch, cachedSignatures) }
return eventsToWatch.sortedWith(order) return eventsToWatch.sortedWith(order)
} }