Refactors CreatedAt Comparator

This commit is contained in:
Vitor Pamplona 2024-05-21 15:56:33 -04:00
parent 7acdf56e68
commit a9437d9001
3 changed files with 46 additions and 31 deletions

View File

@ -0,0 +1,43 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.model.observables
import com.vitorpamplona.amethyst.model.Note
object CreatedAtComparator : Comparator<Note> {
override fun compare(
first: Note?,
second: Note?,
): Int {
val firstEvent = first?.event
val secondEvent = second?.event
return if (firstEvent == null && secondEvent == null) {
0
} else if (firstEvent == null) {
1
} else if (secondEvent == null) {
-1
} else {
firstEvent.createdAt().compareTo(secondEvent.createdAt())
}
}
}

View File

@ -50,21 +50,7 @@ class LatestByKindWithETag<T : Event>(private val kind: Int, private val eTag: S
it.kind() == kind && it.isTaggedEvent(eTag)
} == true
},
comparator = { first: Note?, second: Note? ->
println("Comparator $first $second")
val firstEvent = first?.event
val secondEvent = second?.event
if (firstEvent == null && secondEvent == null) {
0
} else if (firstEvent == null) {
1
} else if (secondEvent == null) {
-1
} else {
firstEvent.createdAt().compareTo(secondEvent.createdAt())
}
},
comparator = CreatedAtComparator,
)?.event as? T
_latest.tryEmit(latestNote)

View File

@ -23,6 +23,7 @@ package com.vitorpamplona.amethyst.ui.dal
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.observables.CreatedAtComparator
import com.vitorpamplona.quartz.events.MuteListEvent
import com.vitorpamplona.quartz.events.NIP90ContentDiscoveryResponseEvent
import com.vitorpamplona.quartz.events.PeopleListEvent
@ -52,21 +53,6 @@ open class NIP90ContentDiscoveryResponseFilter(
return noteEvent is NIP90ContentDiscoveryResponseEvent && noteEvent.isTaggedEvent(request)
}
val createAtComparator = { first: Note?, second: Note? ->
val firstEvent = first?.event
val secondEvent = second?.event
if (firstEvent == null && secondEvent == null) {
0
} else if (firstEvent == null) {
1
} else if (secondEvent == null) {
-1
} else {
firstEvent.createdAt().compareTo(secondEvent.createdAt())
}
}
override fun feed(): List<Note> {
val params = buildFilterParams(account)
@ -75,7 +61,7 @@ open class NIP90ContentDiscoveryResponseFilter(
filter = { idHex: String, note: Note ->
acceptableEvent(note)
},
comparator = createAtComparator,
comparator = CreatedAtComparator,
)
val noteEvent = latestNote?.event as? NIP90ContentDiscoveryResponseEvent ?: return listOf()