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> { override fun sort(items: Set<Note>): List<Note> {
// precache to avoid breaking the contract
val lastNote = val lastNote =
items.associateWith { note -> items.associateWith { note ->
LocalCache.getPublicChatChannelIfExists(note.idHex)?.lastNote?.createdAt() ?: 0 LocalCache.getPublicChatChannelIfExists(note.idHex)?.lastNote?.createdAt() ?: 0
} }
return items val createdNote =
.sortedWith( items.associateWith { note ->
compareBy( note.createdAt() ?: 0
{ lastNote[it] }, }
{ it.createdAt() },
{ it.idHex }, val comparator: Comparator<Note> =
), compareByDescending<Note> {
).reversed() lastNote[it]
}.thenByDescending {
createdNote[it]
}.thenBy {
it.idHex
}
return items.sortedWith(comparator)
} }
} }

View File

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

View File

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