Avoids breaking the contract (changes during sorting)

This commit is contained in:
Vitor Pamplona
2025-09-08 19:38:18 -04:00
parent 4158d3bd45
commit bbec0d93d2
3 changed files with 37 additions and 17 deletions

View File

@@ -103,18 +103,26 @@ open class DiscoverChatFeedFilter(
}
override fun sort(items: Set<Note>): List<Note> {
// precache to avoid breaking the contract
val lastNote =
items.associateWith { note ->
LocalCache.getPublicChatChannelIfExists(note.idHex)?.lastNote?.createdAt() ?: 0
}
return items
.sortedWith(
compareBy(
{ lastNote[it] },
{ it.createdAt() },
{ it.idHex },
),
).reversed()
val createdNote =
items.associateWith { note ->
note.createdAt() ?: 0
}
val comparator: Comparator<Note> =
compareByDescending<Note> {
lastNote[it]
}.thenByDescending {
createdNote[it]
}.thenBy {
it.idHex
}
return items.sortedWith(comparator)
}
}

View File

@@ -125,13 +125,20 @@ open class DiscoverCommunityFeedFilter(
max
}
return items
.sortedWith(
compareBy(
{ lastNotesCreatedAt[it] },
{ it.createdAt() },
{ it.idHex },
),
).reversed()
val createdNote =
items.associateWith { note ->
note.createdAt() ?: 0
}
val comparator: Comparator<Note> =
compareByDescending<Note> {
lastNotesCreatedAt[it]
}.thenByDescending {
createdNote[it]
}.thenBy {
it.idHex
}
return items.sortedWith(comparator)
}
}

View File

@@ -113,11 +113,16 @@ open class DiscoverNIP89FeedFilter(
val participantCounts =
items.associateWith { counter.countFollowsThatParticipateOn(it, followingKeySet) }
val createdNote =
items.associateWith { note ->
((note.event?.createdAt ?: 0) / 86400).toInt()
}
val feedOrder: Comparator<Note> =
compareByDescending<Note> {
participantCounts[it]
}.thenByDescending {
((it.event?.createdAt ?: 0) / 86400).toInt()
createdNote[it]
}.thenBy { it.idHex }
return items.sortedWith(feedOrder)