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

View File

@@ -120,17 +120,24 @@ open class Note(val idHex: String) {
/**
* 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
if (event is RepostEvent || event is GenericRepostEvent || replyTo == null || replyTo.isEmpty()) {
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 {
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) + ";"
cachedSignatures[this] = mySignature
return mySignature
}
fun replyLevel(cachedLevels: MutableMap<Note, Int> = mutableMapOf()): Int {

View File

@@ -14,8 +14,9 @@ class ThreadFeedFilter(val noteId: String) : FeedFilter<Note>() {
override fun feed(): List<Note> {
val cachedSignatures: MutableMap<Note, String> = mutableMapOf()
val eventsToWatch = ThreadAssembler().findThreadFor(noteId) ?: emptySet()
// 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)
}