mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-08-08 23:54:39 +00:00
fix: stable sort in ShortsFeedFilter to avoid TimSort contract crash
A newer AddressableEvent (e.g. VideoVerticalEvent) arriving on a relay thread can swap a Note's event mid-sort, changing the createdAt value the comparator returned moments earlier. TimSort then trips "Comparison method violates its general contract!" and the feed refresh crashes. Snapshot createdAt once per note before sorting via a new sortedByDefaultFeedOrder() helper.
This commit is contained in:
@@ -32,3 +32,12 @@ val DefaultFeedOrderEvent: Comparator<Event> =
|
||||
|
||||
val DefaultFeedOrderCard: Comparator<Card> =
|
||||
compareByDescending<Card> { it.createdAt() }.thenBy { it.id() }
|
||||
|
||||
// Snapshots createdAt once per note so the comparator stays consistent even if
|
||||
// another thread swaps a Note's event mid-sort (e.g. a newer AddressableEvent
|
||||
// arriving from a relay). Avoids TimSort's "Comparison method violates its
|
||||
// general contract!" IllegalArgumentException.
|
||||
fun Iterable<Note>.sortedByDefaultFeedOrder(): List<Note> =
|
||||
map { it to (it.createdAt() ?: 0L) }
|
||||
.sortedWith(compareByDescending<Pair<Note, Long>> { it.second }.thenBy { it.first.idHex })
|
||||
.map { it.first }
|
||||
|
||||
+2
-2
@@ -28,8 +28,8 @@ import com.vitorpamplona.amethyst.model.Note
|
||||
import com.vitorpamplona.amethyst.model.TopFilter
|
||||
import com.vitorpamplona.amethyst.model.filterIntoSet
|
||||
import com.vitorpamplona.amethyst.ui.dal.AdditiveFeedFilter
|
||||
import com.vitorpamplona.amethyst.ui.dal.DefaultFeedOrder
|
||||
import com.vitorpamplona.amethyst.ui.dal.FilterByListParams
|
||||
import com.vitorpamplona.amethyst.ui.dal.sortedByDefaultFeedOrder
|
||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.video.dal.SupportedContent
|
||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.video.datasource.SUPPORTED_VIDEO_FEED_MIME_TYPES_SET
|
||||
import com.vitorpamplona.quartz.nip01Core.core.AddressableEvent
|
||||
@@ -113,5 +113,5 @@ class ShortsFeedFilter(
|
||||
(params.isHiddenList || account.isAcceptable(note))
|
||||
}
|
||||
|
||||
override fun sort(items: Set<Note>): List<Note> = items.sortedWith(DefaultFeedOrder)
|
||||
override fun sort(items: Set<Note>): List<Note> = items.sortedByDefaultFeedOrder()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user